aboutsummaryrefslogtreecommitdiff
path: root/lib/Target/ARM/ARMCommon.cpp
blob: fd3757303bc18e212677ee5b6522e49daa24419a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//===-- ARMCommon.cpp - Define support functions for ARM --------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the "Instituto Nokia de Tecnologia" and
// is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//
//
//===----------------------------------------------------------------------===//
#include "ARMCommon.h"

static inline unsigned rotateL(unsigned x, unsigned n){
  return ((x << n) | (x  >> (32 - n)));
}

static inline unsigned rotateR(unsigned x, unsigned n){
  return ((x >> n) | (x  << (32 - n)));
}

// finds the end position of largest sequence of zeros in binary representation
// of 'immediate'.
static int findLargestZeroSequence(unsigned immediate){
  int max_zero_pos = 0;
  int max_zero_length = 0;
  int zero_pos;
  int zero_length;
  int pos = 0;
  int end_pos;

  while ((immediate & 0x3) == 0) {
    immediate = rotateR(immediate, 2);
    pos+=2;
  }
  end_pos = pos+32;

  while (pos<end_pos){
    while (((immediate & 0x3) != 0)&&(pos<end_pos)) {
      immediate = rotateR(immediate, 2);
      pos+=2;
    }
    zero_pos = pos;
    while (((immediate & 0x3) == 0)&&(pos<end_pos)) {
      immediate = rotateR(immediate, 2);
      pos+=2;
    }
    zero_length = pos - zero_pos;
    if (zero_length > max_zero_length){
      max_zero_length = zero_length;
      max_zero_pos = zero_pos % 32;
    }

  }

  return (max_zero_pos + max_zero_length) % 32;
}

std::vector<unsigned> splitImmediate(unsigned immediate){
  std::vector<unsigned> immediatePieces;

  if (immediate == 0){
    immediatePieces.push_back(0);
  } else {
    int start_pos = findLargestZeroSequence(immediate);
    unsigned immediate_tmp = rotateR(immediate, start_pos);
    int pos = 0;
    while (pos < 32){
      while(((immediate_tmp&0x3) == 0)&&(pos<32)){
        immediate_tmp = rotateR(immediate_tmp,2);
        pos+=2;
      }
      if (pos < 32){
        immediatePieces.push_back(rotateL(immediate_tmp&0xFF,
                                          (start_pos + pos) % 32 ));
        immediate_tmp = rotateR(immediate_tmp,8);
        pos+=8;
      }
    }
  }
  return immediatePieces;
}