|
25 | 25 | package encoder
|
26 | 26 |
|
27 | 27 | import (
|
| 28 | + "sync" |
28 | 29 | "unsafe"
|
29 | 30 | )
|
30 | 31 |
|
31 |
| -var endianness int |
| 32 | +var ( |
| 33 | + endianness int |
| 34 | + initIntOnce sync.Once |
| 35 | + intLELookup *[100]uint16 |
| 36 | + intBELookup *[100]uint16 |
| 37 | + intLookup [2]*[100]uint16 |
| 38 | +) |
32 | 39 |
|
33 |
| -func init() { |
34 |
| - var b [2]byte |
35 |
| - *(*uint16)(unsafe.Pointer(&b)) = uint16(0xABCD) |
| 40 | +func initInt() { |
| 41 | + initIntOnce.Do(func() { |
| 42 | + var b [2]byte |
| 43 | + *(*uint16)(unsafe.Pointer(&b)) = uint16(0xABCD) |
| 44 | + |
| 45 | + switch b[0] { |
| 46 | + case 0xCD: |
| 47 | + endianness = 0 // LE |
| 48 | + case 0xAB: |
| 49 | + endianness = 1 // BE |
| 50 | + default: |
| 51 | + panic("could not determine endianness") |
| 52 | + } |
36 | 53 |
|
37 |
| - switch b[0] { |
38 |
| - case 0xCD: |
39 |
| - endianness = 0 // LE |
40 |
| - case 0xAB: |
41 |
| - endianness = 1 // BE |
42 |
| - default: |
43 |
| - panic("could not determine endianness") |
44 |
| - } |
45 |
| -} |
| 54 | + // "00010203...96979899" cast to []uint16 |
| 55 | + intLELookup = &[100]uint16{ |
| 56 | + 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930, |
| 57 | + 0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931, |
| 58 | + 0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932, |
| 59 | + 0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933, |
| 60 | + 0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934, |
| 61 | + 0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935, |
| 62 | + 0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936, |
| 63 | + 0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937, |
| 64 | + 0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938, |
| 65 | + 0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939, |
| 66 | + } |
46 | 67 |
|
47 |
| -// "00010203...96979899" cast to []uint16 |
48 |
| -var intLELookup = [100]uint16{ |
49 |
| - 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930, |
50 |
| - 0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931, |
51 |
| - 0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932, |
52 |
| - 0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933, |
53 |
| - 0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934, |
54 |
| - 0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935, |
55 |
| - 0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936, |
56 |
| - 0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937, |
57 |
| - 0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938, |
58 |
| - 0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939, |
59 |
| -} |
| 68 | + intBELookup = &[100]uint16{ |
| 69 | + 0x3030, 0x3031, 0x3032, 0x3033, 0x3034, 0x3035, 0x3036, 0x3037, 0x3038, 0x3039, |
| 70 | + 0x3130, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, 0x3138, 0x3139, |
| 71 | + 0x3230, 0x3231, 0x3232, 0x3233, 0x3234, 0x3235, 0x3236, 0x3237, 0x3238, 0x3239, |
| 72 | + 0x3330, 0x3331, 0x3332, 0x3333, 0x3334, 0x3335, 0x3336, 0x3337, 0x3338, 0x3339, |
| 73 | + 0x3430, 0x3431, 0x3432, 0x3433, 0x3434, 0x3435, 0x3436, 0x3437, 0x3438, 0x3439, |
| 74 | + 0x3530, 0x3531, 0x3532, 0x3533, 0x3534, 0x3535, 0x3536, 0x3537, 0x3538, 0x3539, |
| 75 | + 0x3630, 0x3631, 0x3632, 0x3633, 0x3634, 0x3635, 0x3636, 0x3637, 0x3638, 0x3639, |
| 76 | + 0x3730, 0x3731, 0x3732, 0x3733, 0x3734, 0x3735, 0x3736, 0x3737, 0x3738, 0x3739, |
| 77 | + 0x3830, 0x3831, 0x3832, 0x3833, 0x3834, 0x3835, 0x3836, 0x3837, 0x3838, 0x3839, |
| 78 | + 0x3930, 0x3931, 0x3932, 0x3933, 0x3934, 0x3935, 0x3936, 0x3937, 0x3938, 0x3939, |
| 79 | + } |
60 | 80 |
|
61 |
| -var intBELookup = [100]uint16{ |
62 |
| - 0x3030, 0x3031, 0x3032, 0x3033, 0x3034, 0x3035, 0x3036, 0x3037, 0x3038, 0x3039, |
63 |
| - 0x3130, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, 0x3138, 0x3139, |
64 |
| - 0x3230, 0x3231, 0x3232, 0x3233, 0x3234, 0x3235, 0x3236, 0x3237, 0x3238, 0x3239, |
65 |
| - 0x3330, 0x3331, 0x3332, 0x3333, 0x3334, 0x3335, 0x3336, 0x3337, 0x3338, 0x3339, |
66 |
| - 0x3430, 0x3431, 0x3432, 0x3433, 0x3434, 0x3435, 0x3436, 0x3437, 0x3438, 0x3439, |
67 |
| - 0x3530, 0x3531, 0x3532, 0x3533, 0x3534, 0x3535, 0x3536, 0x3537, 0x3538, 0x3539, |
68 |
| - 0x3630, 0x3631, 0x3632, 0x3633, 0x3634, 0x3635, 0x3636, 0x3637, 0x3638, 0x3639, |
69 |
| - 0x3730, 0x3731, 0x3732, 0x3733, 0x3734, 0x3735, 0x3736, 0x3737, 0x3738, 0x3739, |
70 |
| - 0x3830, 0x3831, 0x3832, 0x3833, 0x3834, 0x3835, 0x3836, 0x3837, 0x3838, 0x3839, |
71 |
| - 0x3930, 0x3931, 0x3932, 0x3933, 0x3934, 0x3935, 0x3936, 0x3937, 0x3938, 0x3939, |
| 81 | + intLookup = [2]*[100]uint16{intLELookup, intBELookup} |
| 82 | + }) |
72 | 83 | }
|
73 | 84 |
|
74 |
| -var intLookup = [2]*[100]uint16{&intLELookup, &intBELookup} |
75 |
| - |
76 | 85 | func numMask(numBitSize uint8) uint64 {
|
77 | 86 | return 1<<numBitSize - 1
|
78 | 87 | }
|
79 | 88 |
|
80 | 89 | func AppendInt(_ *RuntimeContext, out []byte, p uintptr, code *Opcode) []byte {
|
| 90 | + initInt() // lazy init |
| 91 | + |
81 | 92 | var u64 uint64
|
82 | 93 | switch code.NumBitSize {
|
83 | 94 | case 8:
|
@@ -132,6 +143,8 @@ func AppendInt(_ *RuntimeContext, out []byte, p uintptr, code *Opcode) []byte {
|
132 | 143 | }
|
133 | 144 |
|
134 | 145 | func AppendUint(_ *RuntimeContext, out []byte, p uintptr, code *Opcode) []byte {
|
| 146 | + initInt() // lazy init |
| 147 | + |
135 | 148 | var u64 uint64
|
136 | 149 | switch code.NumBitSize {
|
137 | 150 | case 8:
|
|
0 commit comments