1 package encoder
2
3 import (
4 "unsafe"
5 )
6
7 var endianness int
8
9 func init() {
10 var b [2]byte
11 *(*uint16)(unsafe.Pointer(&b)) = uint16(0xABCD)
12
13 switch b[0] {
14 case 0xCD:
15 endianness = 0
16 case 0xAB:
17 endianness = 1
18 default:
19 panic("could not determine endianness")
20 }
21 }
22
23
24 var intLELookup = [100]uint16{
25 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930,
26 0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931,
27 0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932,
28 0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933,
29 0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934,
30 0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935,
31 0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936,
32 0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937,
33 0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938,
34 0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939,
35 }
36
37 var intBELookup = [100]uint16{
38 0x3030, 0x3031, 0x3032, 0x3033, 0x3034, 0x3035, 0x3036, 0x3037, 0x3038, 0x3039,
39 0x3130, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, 0x3138, 0x3139,
40 0x3230, 0x3231, 0x3232, 0x3233, 0x3234, 0x3235, 0x3236, 0x3237, 0x3238, 0x3239,
41 0x3330, 0x3331, 0x3332, 0x3333, 0x3334, 0x3335, 0x3336, 0x3337, 0x3338, 0x3339,
42 0x3430, 0x3431, 0x3432, 0x3433, 0x3434, 0x3435, 0x3436, 0x3437, 0x3438, 0x3439,
43 0x3530, 0x3531, 0x3532, 0x3533, 0x3534, 0x3535, 0x3536, 0x3537, 0x3538, 0x3539,
44 0x3630, 0x3631, 0x3632, 0x3633, 0x3634, 0x3635, 0x3636, 0x3637, 0x3638, 0x3639,
45 0x3730, 0x3731, 0x3732, 0x3733, 0x3734, 0x3735, 0x3736, 0x3737, 0x3738, 0x3739,
46 0x3830, 0x3831, 0x3832, 0x3833, 0x3834, 0x3835, 0x3836, 0x3837, 0x3838, 0x3839,
47 0x3930, 0x3931, 0x3932, 0x3933, 0x3934, 0x3935, 0x3936, 0x3937, 0x3938, 0x3939,
48 }
49
50 var intLookup = [2]*[100]uint16{&intLELookup, &intBELookup}
51
52 func numMask(numBitSize uint8) uint64 {
53 return 1<<numBitSize - 1
54 }
55
56 func AppendInt(_ *RuntimeContext, out []byte, p uintptr, code *Opcode) []byte {
57 var u64 uint64
58 switch code.NumBitSize {
59 case 8:
60 u64 = (uint64)(**(**uint8)(unsafe.Pointer(&p)))
61 case 16:
62 u64 = (uint64)(**(**uint16)(unsafe.Pointer(&p)))
63 case 32:
64 u64 = (uint64)(**(**uint32)(unsafe.Pointer(&p)))
65 case 64:
66 u64 = **(**uint64)(unsafe.Pointer(&p))
67 }
68 mask := numMask(code.NumBitSize)
69 n := u64 & mask
70 negative := (u64>>(code.NumBitSize-1))&1 == 1
71 if !negative {
72 if n < 10 {
73 return append(out, byte(n+'0'))
74 } else if n < 100 {
75 u := intLELookup[n]
76 return append(out, byte(u), byte(u>>8))
77 }
78 } else {
79 n = -n & mask
80 }
81
82 lookup := intLookup[endianness]
83
84 var b [22]byte
85 u := (*[11]uint16)(unsafe.Pointer(&b))
86 i := 11
87
88 for n >= 100 {
89 j := n % 100
90 n /= 100
91 i--
92 u[i] = lookup[j]
93 }
94
95 i--
96 u[i] = lookup[n]
97
98 i *= 2
99 if n < 10 {
100 i++
101 }
102 if negative {
103 i--
104 b[i] = '-'
105 }
106
107 return append(out, b[i:]...)
108 }
109
110 func AppendUint(_ *RuntimeContext, out []byte, p uintptr, code *Opcode) []byte {
111 var u64 uint64
112 switch code.NumBitSize {
113 case 8:
114 u64 = (uint64)(**(**uint8)(unsafe.Pointer(&p)))
115 case 16:
116 u64 = (uint64)(**(**uint16)(unsafe.Pointer(&p)))
117 case 32:
118 u64 = (uint64)(**(**uint32)(unsafe.Pointer(&p)))
119 case 64:
120 u64 = **(**uint64)(unsafe.Pointer(&p))
121 }
122 mask := numMask(code.NumBitSize)
123 n := u64 & mask
124 if n < 10 {
125 return append(out, byte(n+'0'))
126 } else if n < 100 {
127 u := intLELookup[n]
128 return append(out, byte(u), byte(u>>8))
129 }
130
131 lookup := intLookup[endianness]
132
133 var b [22]byte
134 u := (*[11]uint16)(unsafe.Pointer(&b))
135 i := 11
136
137 for n >= 100 {
138 j := n % 100
139 n /= 100
140 i--
141 u[i] = lookup[j]
142 }
143
144 i--
145 u[i] = lookup[n]
146
147 i *= 2
148 if n < 10 {
149 i++
150 }
151 return append(out, b[i:]...)
152 }
153
View as plain text