int.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // This files's processing codes are inspired by https://github.com/segmentio/encoding.
  2. // The license notation is as follows.
  3. //
  4. // # MIT License
  5. //
  6. // Copyright (c) 2019 Segment.io, Inc.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. // SOFTWARE.
  25. package encoder
  26. import (
  27. "unsafe"
  28. )
  29. var endianness int
  30. func init() {
  31. var b [2]byte
  32. *(*uint16)(unsafe.Pointer(&b)) = uint16(0xABCD)
  33. switch b[0] {
  34. case 0xCD:
  35. endianness = 0 // LE
  36. case 0xAB:
  37. endianness = 1 // BE
  38. default:
  39. panic("could not determine endianness")
  40. }
  41. }
  42. // "00010203...96979899" cast to []uint16
  43. var intLELookup = [100]uint16{
  44. 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930,
  45. 0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931,
  46. 0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932,
  47. 0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933,
  48. 0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934,
  49. 0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935,
  50. 0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936,
  51. 0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937,
  52. 0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938,
  53. 0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939,
  54. }
  55. var intBELookup = [100]uint16{
  56. 0x3030, 0x3031, 0x3032, 0x3033, 0x3034, 0x3035, 0x3036, 0x3037, 0x3038, 0x3039,
  57. 0x3130, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, 0x3138, 0x3139,
  58. 0x3230, 0x3231, 0x3232, 0x3233, 0x3234, 0x3235, 0x3236, 0x3237, 0x3238, 0x3239,
  59. 0x3330, 0x3331, 0x3332, 0x3333, 0x3334, 0x3335, 0x3336, 0x3337, 0x3338, 0x3339,
  60. 0x3430, 0x3431, 0x3432, 0x3433, 0x3434, 0x3435, 0x3436, 0x3437, 0x3438, 0x3439,
  61. 0x3530, 0x3531, 0x3532, 0x3533, 0x3534, 0x3535, 0x3536, 0x3537, 0x3538, 0x3539,
  62. 0x3630, 0x3631, 0x3632, 0x3633, 0x3634, 0x3635, 0x3636, 0x3637, 0x3638, 0x3639,
  63. 0x3730, 0x3731, 0x3732, 0x3733, 0x3734, 0x3735, 0x3736, 0x3737, 0x3738, 0x3739,
  64. 0x3830, 0x3831, 0x3832, 0x3833, 0x3834, 0x3835, 0x3836, 0x3837, 0x3838, 0x3839,
  65. 0x3930, 0x3931, 0x3932, 0x3933, 0x3934, 0x3935, 0x3936, 0x3937, 0x3938, 0x3939,
  66. }
  67. var intLookup = [2]*[100]uint16{&intLELookup, &intBELookup}
  68. func numMask(numBitSize uint8) uint64 {
  69. return 1<<numBitSize - 1
  70. }
  71. func AppendInt(_ *RuntimeContext, out []byte, p uintptr, code *Opcode) []byte {
  72. var u64 uint64
  73. switch code.NumBitSize {
  74. case 8:
  75. u64 = (uint64)(**(**uint8)(unsafe.Pointer(&p)))
  76. case 16:
  77. u64 = (uint64)(**(**uint16)(unsafe.Pointer(&p)))
  78. case 32:
  79. u64 = (uint64)(**(**uint32)(unsafe.Pointer(&p)))
  80. case 64:
  81. u64 = **(**uint64)(unsafe.Pointer(&p))
  82. }
  83. mask := numMask(code.NumBitSize)
  84. n := u64 & mask
  85. negative := (u64>>(code.NumBitSize-1))&1 == 1
  86. if !negative {
  87. if n < 10 {
  88. return append(out, byte(n+'0'))
  89. } else if n < 100 {
  90. u := intLELookup[n]
  91. return append(out, byte(u), byte(u>>8))
  92. }
  93. } else {
  94. n = -n & mask
  95. }
  96. lookup := intLookup[endianness]
  97. var b [22]byte
  98. u := (*[11]uint16)(unsafe.Pointer(&b))
  99. i := 11
  100. for n >= 100 {
  101. j := n % 100
  102. n /= 100
  103. i--
  104. u[i] = lookup[j]
  105. }
  106. i--
  107. u[i] = lookup[n]
  108. i *= 2 // convert to byte index
  109. if n < 10 {
  110. i++ // remove leading zero
  111. }
  112. if negative {
  113. i--
  114. b[i] = '-'
  115. }
  116. return append(out, b[i:]...)
  117. }
  118. func AppendUint(_ *RuntimeContext, out []byte, p uintptr, code *Opcode) []byte {
  119. var u64 uint64
  120. switch code.NumBitSize {
  121. case 8:
  122. u64 = (uint64)(**(**uint8)(unsafe.Pointer(&p)))
  123. case 16:
  124. u64 = (uint64)(**(**uint16)(unsafe.Pointer(&p)))
  125. case 32:
  126. u64 = (uint64)(**(**uint32)(unsafe.Pointer(&p)))
  127. case 64:
  128. u64 = **(**uint64)(unsafe.Pointer(&p))
  129. }
  130. mask := numMask(code.NumBitSize)
  131. n := u64 & mask
  132. if n < 10 {
  133. return append(out, byte(n+'0'))
  134. } else if n < 100 {
  135. u := intLELookup[n]
  136. return append(out, byte(u), byte(u>>8))
  137. }
  138. lookup := intLookup[endianness]
  139. var b [22]byte
  140. u := (*[11]uint16)(unsafe.Pointer(&b))
  141. i := 11
  142. for n >= 100 {
  143. j := n % 100
  144. n /= 100
  145. i--
  146. u[i] = lookup[j]
  147. }
  148. i--
  149. u[i] = lookup[n]
  150. i *= 2 // convert to byte index
  151. if n < 10 {
  152. i++ // remove leading zero
  153. }
  154. return append(out, b[i:]...)
  155. }