helper_internal.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2012-2015 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. // All non-std package dependencies live in this file,
  5. // so porting to different environment is easy (just update functions).
  6. func pruneSignExt(v []byte, pos bool) (n int) {
  7. if len(v) < 2 {
  8. } else if pos && v[0] == 0 {
  9. for ; v[n] == 0 && n+1 < len(v) && (v[n+1]&(1<<7) == 0); n++ {
  10. }
  11. } else if !pos && v[0] == 0xff {
  12. for ; v[n] == 0xff && n+1 < len(v) && (v[n+1]&(1<<7) != 0); n++ {
  13. }
  14. }
  15. return
  16. }
  17. // validate that this function is correct ...
  18. // culled from OGRE (Object-Oriented Graphics Rendering Engine)
  19. // function: halfToFloatI (http://stderr.org/doc/ogre-doc/api/OgreBitwise_8h-source.html)
  20. func halfFloatToFloatBits(yy uint16) (d uint32) {
  21. y := uint32(yy)
  22. s := (y >> 15) & 0x01
  23. e := (y >> 10) & 0x1f
  24. m := y & 0x03ff
  25. if e == 0 {
  26. if m == 0 { // plu or minus 0
  27. return s << 31
  28. }
  29. // Denormalized number -- renormalize it
  30. for (m & 0x00000400) == 0 {
  31. m <<= 1
  32. e -= 1
  33. }
  34. e += 1
  35. const zz uint32 = 0x0400
  36. m &= ^zz
  37. } else if e == 31 {
  38. if m == 0 { // Inf
  39. return (s << 31) | 0x7f800000
  40. }
  41. return (s << 31) | 0x7f800000 | (m << 13) // NaN
  42. }
  43. e = e + (127 - 15)
  44. m = m << 13
  45. return (s << 31) | (e << 23) | m
  46. }
  47. // GrowCap will return a new capacity for a slice, given the following:
  48. // - oldCap: current capacity
  49. // - unit: in-memory size of an element
  50. // - num: number of elements to add
  51. func growCap(oldCap, unit, num int) (newCap int) {
  52. // appendslice logic (if cap < 1024, *2, else *1.25):
  53. // leads to many copy calls, especially when copying bytes.
  54. // bytes.Buffer model (2*cap + n): much better for bytes.
  55. // smarter way is to take the byte-size of the appended element(type) into account
  56. // maintain 2 thresholds:
  57. // t1: if cap <= t1, newcap = 2x
  58. // t2: if cap <= t2, newcap = 1.5x
  59. // else newcap = 1.25x
  60. //
  61. // t1, t2 >= 1024 always.
  62. // This means that, if unit size >= 16, then always do 2x or 1.25x (ie t1, t2, t3 are all same)
  63. //
  64. // With this, appending for bytes increase by:
  65. // 100% up to 4K
  66. // 75% up to 16K
  67. // 25% beyond that
  68. // unit can be 0 e.g. for struct{}{}; handle that appropriately
  69. if unit <= 0 {
  70. if uint64(^uint(0)) == ^uint64(0) { // 64-bit
  71. var maxInt64 uint64 = 1<<63 - 1 // prevent failure with overflow int on 32-bit (386)
  72. return int(maxInt64) // math.MaxInt64
  73. }
  74. return 1<<31 - 1 // math.MaxInt32
  75. }
  76. // handle if num < 0, cap=0, etc.
  77. var t1, t2 int // thresholds
  78. if unit <= 4 {
  79. t1, t2 = 4*1024, 16*1024
  80. } else if unit <= 16 {
  81. t1, t2 = unit*1*1024, unit*4*1024
  82. } else {
  83. t1, t2 = 1024, 1024
  84. }
  85. if oldCap <= 0 {
  86. newCap = 2
  87. } else if oldCap <= t1 { // [0,t1]
  88. newCap = oldCap * 8 / 4
  89. } else if oldCap <= t2 { // (t1,t2]
  90. newCap = oldCap * 6 / 4
  91. } else { // (t2,infinity]
  92. newCap = oldCap * 5 / 4
  93. }
  94. if num > 0 && newCap < num+oldCap {
  95. newCap = num + oldCap
  96. }
  97. // ensure newCap takes multiples of a cache line (size is a multiple of 64)
  98. t1 = newCap * unit
  99. t2 = t1 % 64
  100. if t2 != 0 {
  101. t1 += 64 - t2
  102. newCap = t1 / unit
  103. }
  104. return
  105. }