1
2
3
4
5 package abi
6
7
8
9 type BoundsErrorCode uint8
10
11 const (
12 BoundsIndex BoundsErrorCode = iota
13 BoundsSliceAlen
14 BoundsSliceAcap
15 BoundsSliceB
16 BoundsSlice3Alen
17 BoundsSlice3Acap
18 BoundsSlice3B
19 BoundsSlice3C
20 BoundsConvert
21 numBoundsCodes
22 )
23
24 const (
25 BoundsMaxReg = 15
26 BoundsMaxConst = 31
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 func BoundsEncode(code BoundsErrorCode, signed, xIsReg, yIsReg bool, xVal, yVal int) int {
56 c := int(0)
57 if xIsReg {
58 c |= 1 << 0
59 if signed {
60 c |= 1 << 2
61 }
62 if xVal < 0 || xVal > BoundsMaxReg {
63 panic("bad xReg")
64 }
65 c |= xVal << 3
66 } else {
67 if xVal < 0 || xVal > BoundsMaxConst {
68 panic("bad xConst")
69 }
70 c |= xVal << 2
71 }
72 if yIsReg {
73 c |= 1 << 1
74 if yVal < 0 || yVal > BoundsMaxReg {
75 panic("bad yReg")
76 }
77 c |= yVal << 7
78 } else {
79 if yVal < 0 || yVal > BoundsMaxConst {
80 panic("bad yConst")
81 }
82 c |= yVal << 7
83 }
84 return c*int(numBoundsCodes) + int(code)
85 }
86 func BoundsDecode(v int) (code BoundsErrorCode, signed, xIsReg, yIsReg bool, xVal, yVal int) {
87 code = BoundsErrorCode(v % int(numBoundsCodes))
88 c := v / int(numBoundsCodes)
89 xIsReg = c&1 != 0
90 c >>= 1
91 yIsReg = c&1 != 0
92 c >>= 1
93 if xIsReg {
94 signed = c&1 != 0
95 c >>= 1
96 xVal = c & 0xf
97 c >>= 4
98 } else {
99 xVal = c & 0x1f
100 c >>= 5
101 }
102 if yIsReg {
103 yVal = c & 0xf
104 c >>= 4
105 } else {
106 yVal = c & 0x1f
107 c >>= 5
108 }
109 if c != 0 {
110 panic("BoundsDecode decoding error")
111 }
112 return
113 }
114
View as plain text