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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
//
// test suite for bits and bits math functions
//
module bits
fn test_bits() {
mut i := 0
mut i1 := u64(0)
//
// --- LeadingZeros ---
//
// 8 bit
i = 1
for x in 0 .. 8 {
// C.printf("x:%02x lz: %d cmp: %d\n", i << x, leading_zeros_8(i << x), 7-x)
assert leading_zeros_8(byte(i << x)) == 7 - x
}
// 16 bit
i = 1
for x in 0 .. 16 {
// C.printf("x:%04x lz: %d cmp: %d\n", u16(i) << x, leading_zeros_16(u16(i) << x), 15-x)
assert leading_zeros_16(u16(i) << x) == 15 - x
}
// 32 bit
i = 1
for x in 0 .. 32 {
// C.printf("x:%08x lz: %d cmp: %d\n", u32(i) << x, leading_zeros_32(u32(i) << x), 31-x)
assert leading_zeros_32(u32(i) << x) == 31 - x
}
// 64 bit
i = 1
for x in 0 .. 64 {
// C.printf("x:%016llx lz: %llu cmp: %d\n", u64(i) << x, leading_zeros_64(u64(i) << x), 63-x)
assert leading_zeros_64(u64(i) << x) == 63 - x
}
//
// --- ones_count ---
//
// 8 bit
i = 0
for x in 0 .. 9 {
// C.printf("x:%02x lz: %llu cmp: %d\n", byte(i), ones_count_8(byte(i)), x)
assert ones_count_8(byte(i)) == x
i = (i << 1) + 1
}
// 16 bit
i = 0
for x in 0 .. 17 {
// C.printf("x:%04x lz: %llu cmp: %d\n", u16(i), ones_count_16(u16(i)), x)
assert ones_count_16(u16(i)) == x
i = (i << 1) + 1
}
// 32 bit
i = 0
for x in 0 .. 33 {
// C.printf("x:%08x lz: %llu cmp: %d\n", u32(i), ones_count_32(u32(i)), x)
assert ones_count_32(u32(i)) == x
i = (i << 1) + 1
}
// 64 bit
i1 = 0
for x in 0 .. 65 {
// C.printf("x:%016llx lz: %llu cmp: %d\n", u64(i1), ones_count_64(u64(i1)), x)
assert ones_count_64(i1) == x
i1 = (i1 << 1) + 1
}
//
// --- rotate_left/right ---
//
assert rotate_left_8(0x12, 4) == 0x21
assert rotate_left_16(0x1234, 8) == 0x3412
assert rotate_left_32(0x12345678, 16) == 0x56781234
assert rotate_left_64(0x1234567887654321, 32) == 0x8765432112345678
//
// --- reverse ---
//
// 8 bit
i = 0
for _ in 0 .. 9 {
mut rv := byte(0)
mut bc := 0
mut n := i
for bc < 8 {
rv = (rv << 1) | (byte(n) & 0x01)
bc++
n = n >> 1
}
// C.printf("x:%02x lz: %llu cmp: %d\n", byte(i), reverse_8(byte(i)), rv)
assert reverse_8(byte(i)) == rv
i = (i << 1) + 1
}
// 16 bit
i = 0
for _ in 0 .. 17 {
mut rv := u16(0)
mut bc := 0
mut n := i
for bc < 16 {
rv = (rv << 1) | (u16(n) & 0x01)
bc++
n = n >> 1
}
// C.printf("x:%04x lz: %llu cmp: %d\n", u16(i), reverse_16(u16(i)), rv)
assert reverse_16(u16(i)) == rv
i = (i << 1) + 1
}
// 32 bit
i = 0
for _ in 0 .. 33 {
mut rv := u32(0)
mut bc := 0
mut n := i
for bc < 32 {
rv = (rv << 1) | (u32(n) & 0x01)
bc++
n = n >> 1
}
// C.printf("x:%08x lz: %llu cmp: %d\n", u32(i), reverse_32(u32(i)), rv)
assert reverse_32(u32(i)) == rv
i = (i << 1) + 1
}
// 64 bit
i1 = 0
for _ in 0 .. 64 {
mut rv := u64(0)
mut bc := 0
mut n := i1
for bc < 64 {
rv = (rv << 1) | (n & 0x01)
bc++
n = n >> 1
}
// C.printf("x:%016llx lz: %016llx cmp: %016llx\n", u64(i1), reverse_64(u64(i1)), rv)
assert reverse_64(i1) == rv
i1 = (i1 << 1) + 1
}
//
// --- add ---
//
// 32 bit
i = 1
for x in 0 .. 32 {
v := u32(i) << x
sum, carry := add_32(v, v, u32(0))
// C.printf("x:%08x [%llu,%llu] %llu\n", u32(i) << x, sum, carry, u64(v) + u64(v))
assert ((u64(carry) << 32) | u64(sum)) == u64(v) + u64(v)
}
mut sum_32t, mut carry_32t := add_32(0x8000_0000, 0x8000_0000, u32(0))
assert sum_32t == u32(0)
assert carry_32t == u32(1)
sum_32t, carry_32t = add_32(0xFFFF_FFFF, 0xFFFF_FFFF, u32(1))
assert sum_32t == 0xFFFF_FFFF
assert carry_32t == u32(1)
// 64 bit
i = 1
for x in 0 .. 63 {
v := u64(i) << x
sum, carry := add_64(v, v, u64(0))
// C.printf("x:%16x [%llu,%llu] %llu\n", u64(i) << x, sum, carry, u64(v >> 32) + u64(v >> 32))
assert ((carry << 32) | sum) == v + v
}
mut sum_64t, mut carry_64t := add_64(0x8000_0000_0000_0000, 0x8000_0000_0000_0000,
u64(0))
assert sum_64t == u64(0)
assert carry_64t == u64(1)
sum_64t, carry_64t = add_64(0xFFFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF, u64(1))
assert sum_64t == 0xFFFF_FFFF_FFFF_FFFF
assert carry_64t == u64(1)
//
// --- sub ---
//
// 32 bit
i = 1
for x in 1 .. 32 {
v0 := u32(i) << x
v1 := v0 >> 1
mut diff, mut borrow_out := sub_32(v0, v1, u32(0))
// C.printf("x:%08x [%llu,%llu] %08x\n", u32(i) << x, diff, borrow_out, v0 - v1)
assert diff == v1
diff, borrow_out = sub_32(v0, v1, u32(1))
// C.printf("x:%08x [%llu,%llu] %08x\n", u32(i) << x, diff, borrow_out, v0 - v1)
assert diff == (v1 - 1)
assert borrow_out == u32(0)
diff, borrow_out = sub_32(v1, v0, u32(1))
// C.printf("x:%08x [%llu,%llu] %08x\n", u32(i) << x, diff, borrow_out, v1 - v0)
assert borrow_out == u32(1)
}
// 64 bit
i = 1
for x in 1 .. 64 {
v0 := u64(i) << x
v1 := v0 >> 1
mut diff, mut borrow_out := sub_64(v0, v1, u64(0))
// C.printf("x:%08x [%llu,%llu] %08x\n", u64(i) << x, diff, borrow_out, v0 - v1)
assert diff == v1
diff, borrow_out = sub_64(v0, v1, u64(1))
// C.printf("x:%08x [%llu,%llu] %08x\n", u64(i) << x, diff, borrow_out, v0 - v1)
assert diff == (v1 - 1)
assert borrow_out == u64(0)
diff, borrow_out = sub_64(v1, v0, u64(1))
// C.printf("x:%08x [%llu,%llu] %08x\n",u64(i) << x, diff, borrow_out, v1 - v0)
assert borrow_out == u64(1)
}
//
// --- mul ---
//
// 32 bit
i = 1
for x in 0 .. 32 {
v0 := u32(i) << x
v1 := v0 - 1
hi, lo := mul_32(v0, v1)
assert (u64(hi) << 32) | (u64(lo)) == u64(v0) * u64(v1)
}
// 64 bit
i = 1
for x in 0 .. 64 {
v0 := u64(i) << x
v1 := v0 - 1
hi, lo := mul_64(v0, v1)
// C.printf("v0: %llu v1: %llu [%llu,%llu] tt: %llu\n", v0, v1, hi, lo, (v0 >> 32) * (v1 >> 32))
assert (hi & 0xFFFF_FFFF_0000_0000) == (((v0 >> 32) * (v1 >> 32)) & 0xFFFF_FFFF_0000_0000)
assert (lo & 0x0000_0000_FFFF_FFFF) == (((v0 & 0x0000_0000_FFFF_FFFF) * (v1 & 0x0000_0000_FFFF_FFFF)) & 0x0000_0000_FFFF_FFFF)
}
//
// --- div ---
//
// 32 bit
i = 1
for x in 0 .. 31 {
hi := u32(i) << x
lo := hi - 1
y := u32(3) << x
quo, rem := div_32(hi, lo, y)
// C.printf("[%08x_%08x] %08x (%08x,%08x)\n", hi, lo, y, quo, rem)
tst := ((u64(hi) << 32) | u64(lo))
assert quo == (tst / u64(y))
assert rem == (tst % u64(y))
assert rem == rem_32(hi, lo, y)
}
// 64 bit
i = 1
for x in 0 .. 62 {
hi := u64(i) << x
lo := u64(2) // hi - 1
y := u64(0x4000_0000_0000_0000)
quo, rem := div_64(hi, lo, y)
// C.printf("[%016llx_%016llx] %016llx (%016llx,%016llx)\n", hi, lo, y, quo, rem)
assert quo == u64(2) << (x + 1)
_, rem1 := div_64(hi % y, lo, y)
assert rem == rem1
assert rem == rem_64(hi, lo, y)
}
}
|