1
2
3
4
5
6
7 package codegen
8
9
10
11
12
13 func lshConst64x64(v int64) int64 {
14
15
16 return v << uint64(33)
17 }
18
19 func rshConst64Ux64(v uint64) uint64 {
20
21
22 return v >> uint64(33)
23 }
24
25 func rshConst64Ux64Overflow32(v uint32) uint64 {
26
27 return uint64(v) >> 32
28 }
29
30 func rshConst64Ux64Overflow16(v uint16) uint64 {
31
32 return uint64(v) >> 16
33 }
34
35 func rshConst64Ux64Overflow8(v uint8) uint64 {
36
37 return uint64(v) >> 8
38 }
39
40 func rshConst64x64(v int64) int64 {
41
42
43 return v >> uint64(33)
44 }
45
46 func rshConst64x64Overflow32(v int32) int64 {
47
48 return int64(v) >> 32
49 }
50
51 func rshConst64x64Overflow16(v int16) int64 {
52
53 return int64(v) >> 16
54 }
55
56 func rshConst64x64Overflow8(v int8) int64 {
57
58 return int64(v) >> 8
59 }
60
61 func lshConst32x64(v int32) int32 {
62
63
64 return v << uint64(29)
65 }
66
67 func rshConst32Ux64(v uint32) uint32 {
68
69
70 return v >> uint64(29)
71 }
72
73 func rshConst32x64(v int32) int32 {
74
75
76 return v >> uint64(29)
77 }
78
79 func lshConst64x32(v int64) int64 {
80
81
82 return v << uint32(33)
83 }
84
85 func rshConst64Ux32(v uint64) uint64 {
86
87
88 return v >> uint32(33)
89 }
90
91 func rshConst64x32(v int64) int64 {
92
93
94 return v >> uint32(33)
95 }
96
97
98
99
100
101 func lshMask64x64(v int64, s uint64) int64 {
102
103
104
105
106 return v << (s & 63)
107 }
108
109 func rshMask64Ux64(v uint64, s uint64) uint64 {
110
111
112
113
114 return v >> (s & 63)
115 }
116
117 func rshMask64x64(v int64, s uint64) int64 {
118
119
120
121
122 return v >> (s & 63)
123 }
124
125 func lshMask32x64(v int32, s uint64) int32 {
126
127
128
129
130 return v << (s & 63)
131 }
132
133 func rshMask32Ux64(v uint32, s uint64) uint32 {
134
135
136
137
138 return v >> (s & 63)
139 }
140
141 func rsh5Mask32Ux64(v uint32, s uint64) uint32 {
142
143 return v >> (s & 31)
144 }
145
146 func rshMask32x64(v int32, s uint64) int32 {
147
148
149
150
151 return v >> (s & 63)
152 }
153
154 func rsh5Mask32x64(v int32, s uint64) int32 {
155
156 return v >> (s & 31)
157 }
158
159 func lshMask64x32(v int64, s uint32) int64 {
160
161
162
163
164 return v << (s & 63)
165 }
166
167 func rshMask64Ux32(v uint64, s uint32) uint64 {
168
169
170
171
172 return v >> (s & 63)
173 }
174
175 func rshMask64x32(v int64, s uint32) int64 {
176
177
178
179
180 return v >> (s & 63)
181 }
182
183 func lshMask64x32Ext(v int64, s int32) int64 {
184
185
186
187 return v << uint(s&63)
188 }
189
190 func rshMask64Ux32Ext(v uint64, s int32) uint64 {
191
192
193
194 return v >> uint(s&63)
195 }
196
197 func rshMask64x32Ext(v int64, s int32) int64 {
198
199
200
201 return v >> uint(s&63)
202 }
203
204
205
206
207
208
209 func lshSigned(v8 int8, v16 int16, v32 int32, v64 int64, x int) {
210
211 _ = x << v8
212
213 _ = x << v16
214
215 _ = x << v32
216
217 _ = x << v64
218 }
219
220
221 func lshSignedMasked(v8 int8, v16 int16, v32 int32, v64 int64, x int) {
222
223 _ = x << (v8 & 7)
224
225 _ = x << (v16 & 15)
226
227 _ = x << (v32 & 31)
228
229 _ = x << (v64 & 63)
230 }
231
232
233
234
235
236 func lshGuarded64(v int64, s uint) int64 {
237 if s < 64 {
238
239
240
241
242 return v << s
243 }
244 panic("shift too large")
245 }
246
247 func rshGuarded64U(v uint64, s uint) uint64 {
248 if s < 64 {
249
250
251
252
253 return v >> s
254 }
255 panic("shift too large")
256 }
257
258 func rshGuarded64(v int64, s uint) int64 {
259 if s < 64 {
260
261
262
263
264 return v >> s
265 }
266 panic("shift too large")
267 }
268
269 func provedUnsignedShiftLeft(val64 uint64, val32 uint32, val16 uint16, val8 uint8, shift int) (r1 uint64, r2 uint32, r3 uint16, r4 uint8) {
270 if shift >= 0 && shift < 64 {
271
272 r1 = val64 << shift
273 }
274 if shift >= 0 && shift < 32 {
275
276 r2 = val32 << shift
277 }
278 if shift >= 0 && shift < 16 {
279
280 r3 = val16 << shift
281 }
282 if shift >= 0 && shift < 8 {
283
284 r4 = val8 << shift
285 }
286 return r1, r2, r3, r4
287 }
288
289 func provedSignedShiftLeft(val64 int64, val32 int32, val16 int16, val8 int8, shift int) (r1 int64, r2 int32, r3 int16, r4 int8) {
290 if shift >= 0 && shift < 64 {
291
292 r1 = val64 << shift
293 }
294 if shift >= 0 && shift < 32 {
295
296 r2 = val32 << shift
297 }
298 if shift >= 0 && shift < 16 {
299
300 r3 = val16 << shift
301 }
302 if shift >= 0 && shift < 8 {
303
304 r4 = val8 << shift
305 }
306 return r1, r2, r3, r4
307 }
308
309 func provedUnsignedShiftRight(val64 uint64, val32 uint32, val16 uint16, val8 uint8, shift int) (r1 uint64, r2 uint32, r3 uint16, r4 uint8) {
310 if shift >= 0 && shift < 64 {
311
312 r1 = val64 >> shift
313 }
314 if shift >= 0 && shift < 32 {
315
316 r2 = val32 >> shift
317 }
318 if shift >= 0 && shift < 16 {
319
320 r3 = val16 >> shift
321 }
322 if shift >= 0 && shift < 8 {
323
324 r4 = val8 >> shift
325 }
326 return r1, r2, r3, r4
327 }
328
329 func provedSignedShiftRight(val64 int64, val32 int32, val16 int16, val8 int8, shift int) (r1 int64, r2 int32, r3 int16, r4 int8) {
330 if shift >= 0 && shift < 64 {
331
332 r1 = val64 >> shift
333 }
334 if shift >= 0 && shift < 32 {
335
336 r2 = val32 >> shift
337 }
338 if shift >= 0 && shift < 16 {
339
340 r3 = val16 >> shift
341 }
342 if shift >= 0 && shift < 8 {
343
344 r4 = val8 >> shift
345 }
346 return r1, r2, r3, r4
347 }
348
349 func checkUnneededTrunc(tab *[100000]uint32, d uint64, v uint32, h uint16, b byte) (uint32, uint64) {
350
351
352 f := tab[byte(v)^b]
353
354 f += tab[byte(v)&b]
355
356 f += tab[byte(v)|b]
357
358 f += tab[uint16(v)&h]
359
360 f += tab[uint16(v)^h]
361
362 f += tab[uint16(v)|h]
363
364 f += tab[v&0xff]
365
366 f += 2 * uint32(uint16(d))
367
368 g := 2 * uint64(uint32(d))
369 return f, g
370 }
371
372 func checkCombinedShifts(v8 uint8, v16 uint16, v32 uint32, x32 int32, v64 uint64) (uint8, uint16, uint32, uint64, int64) {
373
374
375 f := (v8 & 0xF) << 2
376
377 f += byte(v16) << 3
378
379 g := (v16 & 0xFF) << 3
380
381 h := (v32 & 0xFFFFF) << 2
382
383 i := (v64 & 0xFFFFFFFF) << 5
384
385 i += (v64 & 0xFFFFFFF) << 38
386
387 i += (v64 & 0xFFFF00) << 10
388
389 j := int64(x32+32) * 8
390 return f, g, h, i, j
391 }
392
393 func checkWidenAfterShift(v int64, u uint64) (int64, uint64) {
394
395
396 f := int32(v >> 32)
397
398 f += int32(v >> 31)
399
400 g := int16(v >> 48)
401
402 g += int16(v >> 30)
403
404 g += int16(f >> 16)
405
406 h := int8(v >> 56)
407
408 h += int8(v >> 28)
409
410 h += int8(f >> 24)
411
412 h += int8(f >> 16)
413 return int64(h), uint64(g)
414 }
415
416 func checkShiftAndMask32(v []uint32) {
417 i := 0
418
419
420 v[i] = (v[i] & 0xFF00000) >> 8
421 i++
422
423 v[i] = (v[i] & 0xFF00) >> 6
424 i++
425
426 v[i] = (v[i] & 0xFF) >> 8
427 i++
428
429 v[i] = (v[i] & 0xF000000) >> 28
430 i++
431
432 v[i] = (v[i] >> 6) & 0xFF
433 i++
434
435 v[i] = (v[i] >> 6) & 0xFF000
436 i++
437
438 v[i] = (v[i] >> 20) & 0xFF000
439 i++
440
441 v[i] = (v[i] >> 24) & 0xFF00
442 i++
443 }
444
445 func checkMergedShifts32(a [256]uint32, b [256]uint64, u uint32, v uint32) {
446
447 a[0] = a[uint8(v>>24)]
448
449 b[0] = b[uint8(v>>24)]
450
451 b[1] = b[(v>>20)&0xFF]
452
453 b[2] = b[v>>25]
454 }
455
456 func checkMergedShifts64(a [256]uint32, b [256]uint64, c [256]byte, v uint64) {
457
458 a[0] = a[uint8(v>>24)]
459
460 a[1] = a[uint8(v>>25)]
461
462 a[2] = a[v>>25&0x7F]
463
464 a[3] = a[(v>>31)&0x01]
465
466 a[4] = a[(v>>30)&0x07]
467
468 a[5] = a[(v>>32)&0x01]
469
470 a[6] = a[(v>>34)&0x03]
471
472 b[0] = b[uint8(v>>23)]
473
474 b[1] = b[(v>>20)&0xFF]
475
476 b[2] = b[((uint64((uint32(v) >> 21)) & 0x3f) << 4)]
477
478 c[0] = c[((v>>5)&0x3F)<<16]
479
480 c[1] = c[((v>>7)&0x3F)<<7]
481 }
482
483 func checkShiftMask(a uint32, b uint64, z []uint32, y []uint64) {
484 _ = y[128]
485 _ = z[128]
486
487 z[0] = uint32(uint8(a >> 5))
488
489 z[1] = uint32(uint8((a >> 4) & 0x7e))
490
491 z[2] = uint32(uint8(a>>7)) & 0x1c
492
493 y[0] = uint64((a >> 6) & 0x1c)
494
495 y[1] = uint64(uint32(b)<<6) + 1
496
497 y[2] = uint64((uint16(a) >> 9) & 0x1F)
498
499 y[3] = uint64(((uint16(a) & 0xFF0) >> 9) & 0x1F)
500 }
501
502
503
504 func check128bitShifts(x, y uint64, bits uint) (uint64, uint64) {
505 s := bits & 63
506 ŝ := (64 - bits) & 63
507
508
509 shr := x>>s | y<<ŝ
510
511 shl := x<<s | y>>ŝ
512 return shr, shl
513 }
514
515 func checkShiftToMask(u []uint64, s []int64) {
516
517 u[0] = u[0] >> 5 << 5
518
519 s[0] = s[0] >> 5 << 5
520
521 u[1] = u[1] << 5 >> 5
522 }
523
View as plain text