helper_internal.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. // maxArrayLen is the size of uint, which determines
  5. // the maximum length of any array.
  6. const maxArrayLen = 1<<((32<<(^uint(0)>>63))-1) - 1
  7. // All non-std package dependencies live in this file,
  8. // so porting to different environment is easy (just update functions).
  9. func pruneSignExt(v []byte, pos bool) (n int) {
  10. if len(v) < 2 {
  11. } else if pos && v[0] == 0 {
  12. for ; v[n] == 0 && n+1 < len(v) && (v[n+1]&(1<<7) == 0); n++ {
  13. }
  14. } else if !pos && v[0] == 0xff {
  15. for ; v[n] == 0xff && n+1 < len(v) && (v[n+1]&(1<<7) != 0); n++ {
  16. }
  17. }
  18. return
  19. }
  20. func halfFloatToFloatBits(h uint16) (f uint32) {
  21. // retrofitted from:
  22. // - OGRE (Object-Oriented Graphics Rendering Engine)
  23. // function: halfToFloatI https://www.ogre3d.org/docs/api/1.9/_ogre_bitwise_8h_source.html
  24. s := uint32(h >> 15)
  25. m := uint32(h & 0x03ff)
  26. e := int32((h >> 10) & 0x1f)
  27. if e == 0 {
  28. if m == 0 { // plus or minus 0
  29. return s << 31
  30. }
  31. // Denormalized number -- renormalize it
  32. for (m & 0x0400) == 0 {
  33. m <<= 1
  34. e -= 1
  35. }
  36. e += 1
  37. m &= ^uint32(0x0400)
  38. } else if e == 31 {
  39. if m == 0 { // Inf
  40. return (s << 31) | 0x7f800000
  41. }
  42. return (s << 31) | 0x7f800000 | (m << 13) // NaN
  43. }
  44. e = e + (127 - 15)
  45. m = m << 13
  46. return (s << 31) | (uint32(e) << 23) | m
  47. }
  48. func floatToHalfFloatBits(i uint32) (h uint16) {
  49. // retrofitted from:
  50. // - OGRE (Object-Oriented Graphics Rendering Engine)
  51. // function: halfToFloatI https://www.ogre3d.org/docs/api/1.9/_ogre_bitwise_8h_source.html
  52. // - http://www.java2s.com/example/java-utility-method/float-to/floattohalf-float-f-fae00.html
  53. s := (i >> 16) & 0x8000
  54. e := int32(((i >> 23) & 0xff) - (127 - 15))
  55. m := i & 0x7fffff
  56. var h32 uint32
  57. if e <= 0 {
  58. if e < -10 { // zero
  59. h32 = s // track -0 vs +0
  60. } else {
  61. m = (m | 0x800000) >> uint32(1-e)
  62. h32 = s | (m >> 13)
  63. }
  64. } else if e == 0xff-(127-15) {
  65. if m == 0 { // Inf
  66. h32 = s | 0x7c00
  67. } else { // NAN
  68. m >>= 13
  69. var me uint32
  70. if m == 0 {
  71. me = 1
  72. }
  73. h32 = s | 0x7c00 | m | me
  74. }
  75. } else {
  76. if e > 30 { // Overflow
  77. h32 = s | 0x7c00
  78. } else {
  79. h32 = s | (uint32(e) << 10) | (m >> 13)
  80. }
  81. }
  82. h = uint16(h32)
  83. return
  84. }
  85. // growCap will return a new capacity for a slice, given the following:
  86. // - oldCap: current capacity
  87. // - unit: in-memory size of an element
  88. // - num: number of elements to add
  89. func growCap(oldCap, unit, num uint) (newCap uint) {
  90. // appendslice logic (if cap < 1024, *2, else *1.25):
  91. // leads to many copy calls, especially when copying bytes.
  92. // bytes.Buffer model (2*cap + n): much better for bytes.
  93. // smarter way is to take the byte-size of the appended element(type) into account
  94. // maintain 1 thresholds:
  95. // t1: if cap <= t1, newcap = 2x
  96. // else newcap = 1.5x
  97. //
  98. // t1 is always >= 1024.
  99. // This means that, if unit size >= 16, then always do 2x or 1.5x (ie t1, t2, t3 are all same)
  100. //
  101. // With this, appending for bytes increase by:
  102. // 100% up to 4K
  103. // 50% beyond that
  104. // unit can be 0 e.g. for struct{}{}; handle that appropriately
  105. maxCap := num + (oldCap * 3 / 2)
  106. if unit == 0 || maxCap > maxArrayLen || maxCap < oldCap { // handle wraparound, etc
  107. return maxArrayLen
  108. }
  109. var t1 uint = 1024 // default thresholds for large values
  110. if unit <= 4 {
  111. t1 = 8 * 1024
  112. } else if unit <= 16 {
  113. t1 = 2 * 1024
  114. }
  115. newCap = 2 + num
  116. if oldCap > 0 {
  117. if oldCap <= t1 { // [0,t1]
  118. newCap = num + (oldCap * 2)
  119. } else { // (t1,infinity]
  120. newCap = maxCap
  121. }
  122. }
  123. // ensure newCap takes multiples of a cache line (size is a multiple of 64)
  124. t1 = newCap * unit
  125. if t2 := t1 % 64; t2 != 0 {
  126. t1 += 64 - t2
  127. newCap = t1 / unit
  128. }
  129. return
  130. }