helper_internal.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 3 thresholds:
  57. // t1: if cap <= t1, newcap = 2x
  58. // t2: if cap <= t2, newcap = 1.75x
  59. // t3: if cap <= t3, newcap = 1.5x
  60. // else newcap = 1.25x
  61. //
  62. // t1, t2, t3 >= 1024 always.
  63. // i.e. if unit size >= 16, then always do 2x or 1.25x (ie t1, t2, t3 are all same)
  64. //
  65. // With this, appending for bytes increase by:
  66. // 100% up to 4K
  67. // 75% up to 8K
  68. // 50% up to 16K
  69. // 25% beyond that
  70. // unit can be 0 e.g. for struct{}{}; handle that appropriately
  71. var t1, t2, t3 int // thresholds
  72. if unit <= 1 {
  73. t1, t2, t3 = 4*1024, 8*1024, 16*1024
  74. } else if unit < 16 {
  75. t3 = 16 / unit * 1024
  76. t1 = t3 * 1 / 4
  77. t2 = t3 * 2 / 4
  78. } else {
  79. t1, t2, t3 = 1024, 1024, 1024
  80. }
  81. var x int // temporary variable
  82. // x is multiplier here: one of 5, 6, 7 or 8; incr of 25%, 50%, 75% or 100% respectively
  83. if oldCap <= t1 { // [0,t1]
  84. x = 8
  85. } else if oldCap > t3 { // (t3,infinity]
  86. x = 5
  87. } else if oldCap <= t2 { // (t1,t2]
  88. x = 7
  89. } else { // (t2,t3]
  90. x = 6
  91. }
  92. newCap = x * oldCap / 4
  93. if num > 0 {
  94. newCap += num
  95. }
  96. // ensure newCap is a multiple of 64 (if it is > 64) or 16.
  97. if newCap > 64 {
  98. if x = newCap % 64; x != 0 {
  99. x = newCap / 64
  100. newCap = 64 * (x + 1)
  101. }
  102. } else {
  103. if x = newCap % 16; x != 0 {
  104. x = newCap / 16
  105. newCap = 16 * (x + 1)
  106. }
  107. }
  108. return
  109. }