-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor_utils.cpp
192 lines (146 loc) · 3.17 KB
/
processor_utils.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdlib.h>
#include "debug.h"
#include "optimize.h"
#include "processor_utils.h"
int64_t untwos_complement(uint64_t value, uint8_t bits)
{
ASSERT(bits >= 1 && bits <= 64);
if (IS_BIT_OFF0_SET(bits - 1, value))
return -((1ll << bits) - value); // FIXME fails with 64 bit?
return value;
}
uint64_t twos_complement(int64_t value, uint8_t bits)
{
ASSERT(bits >= 1 && bits <= 64);
if (value >= 0)
return value;
uint64_t mask = MASK_N(bits);
return value & mask;
}
uint8_t count_leading_ones(uint8_t n_bits, uint64_t value)
{
ASSERT(n_bits >= 1 && n_bits <= 64);
uint64_t mask = 1ll << (n_bits - 1);
uint8_t nr = 0;
for(nr=0; nr<n_bits; nr++)
{
if (!(value & mask))
break;
value <<= 1;
}
return nr;
}
uint8_t count_leading_zeros(uint8_t n_bits, uint64_t value)
{
ASSERT(n_bits >= 1 && n_bits <= 64);
uint64_t mask = 1ll << (n_bits - 1);
uint8_t nr = 0;
for(nr=0; nr<n_bits; nr++)
{
if (value & mask)
break;
value <<= 1;
}
return nr;
}
uint64_t rotate_right(uint64_t value, uint8_t n, uint8_t width)
{
ASSERT(n >= 0 && n <= 64);
ASSERT(width >= 0 && width <= 64);
uint64_t right_bits = value & MASK_N(n);
value >>= n;
value |= right_bits << (width - n);
return value;
}
uint64_t sign_extend_8b(uint8_t value)
{
if (value & 128)
return uint64_t(value) | ~0xffll;
return value;
}
uint64_t sign_extend_16b(uint16_t value)
{
if (value & 32768)
return uint64_t(value) | ~0xffffll;
return value;
}
uint64_t sign_extend_32b(uint32_t value)
{
if (IS_BIT_OFF0_SET(31, value))
return uint64_t(value) | ~0xffffffffll;
return value;
}
// FIXME need to optimize this
int64_t sign_extend(int64_t value, uint8_t bits)
{
ASSERT(bits >= 1 && bits <= 64);
int mask = 1 << (bits - 1);
if (value & mask)
{
while(bits <= 64)
{
value |= mask;
bits++;
mask <<= 1;
}
}
else
{
while(bits <= 64)
{
value &= ~mask;
bits++;
mask <<= 1;
}
}
return value;
}
bool test_tc_overflow_32b(int32_t val1, int32_t val2)
{
int32_t result = val1 + val2;
if (val1 > 0 && val2 > 0 && result <= 0)
return true;
if (val1 < 0 && val2 < 0 && result >= 0)
return true;
return false;
}
uint32_t make_cmd_I_TYPE(uint8_t rs, uint8_t rt, uint8_t function, int immediate)
{
ASSERT(rs >=0 && rs <= 31);
ASSERT(rt >=0 && rt <= 31);
ASSERT(function >=0 && function <= 63);
ASSERT(immediate >=0 && immediate <= 0xffff);
return (function << 26) |
(rs << 21) |
(rt << 16) |
immediate;
}
uint32_t make_cmd_SPECIAL(uint8_t rt, uint8_t rd, uint8_t sa, uint8_t function, uint8_t extra)
{
ASSERT(rt >=0 && rt <= 31);
ASSERT(rd >=0 && rd <= 31);
ASSERT(sa >=0 && sa <= 31);
ASSERT(function >=0 && function <= 63);
ASSERT(extra >=0 && extra <= 31);
return function |
(sa << 6) |
(rd << 11) |
(rt << 16) |
(extra << 21);
}
uint32_t make_cmd_J_TYPE(uint8_t function, int target)
{
ASSERT(function >=0 && function <= 63);
ASSERT(target >=0 && target <= MASK_26B);
return (function << 26) |
target;
}
uint32_t make_cmd_R_TYPE(uint8_t opcode, uint8_t sa, uint8_t rd, uint8_t rt, uint8_t rs, uint8_t function)
{
return (opcode << 26) |
(rs << 21) |
(rt << 16) |
(rd << 11) |
(sa << 6) |
function;
}