blob: 0cbd665e257f0301306af1d0799983d05aaff66b [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001// 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
4package codec
5
6// All non-std package dependencies live in this file,
7// so porting to different environment is easy (just update functions).
8
9func 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
21// validate that this function is correct ...
22// culled from OGRE (Object-Oriented Graphics Rendering Engine)
23// function: halfToFloatI (http://stderr.org/doc/ogre-doc/api/OgreBitwise_8h-source.html)
24func halfFloatToFloatBits(yy uint16) (d uint32) {
25 y := uint32(yy)
26 s := (y >> 15) & 0x01
27 e := (y >> 10) & 0x1f
28 m := y & 0x03ff
29
30 if e == 0 {
31 if m == 0 { // plu or minus 0
32 return s << 31
33 }
34 // Denormalized number -- renormalize it
35 for (m & 0x00000400) == 0 {
36 m <<= 1
37 e -= 1
38 }
39 e += 1
40 const zz uint32 = 0x0400
41 m &= ^zz
42 } else if e == 31 {
43 if m == 0 { // Inf
44 return (s << 31) | 0x7f800000
45 }
46 return (s << 31) | 0x7f800000 | (m << 13) // NaN
47 }
48 e = e + (127 - 15)
49 m = m << 13
50 return (s << 31) | (e << 23) | m
51}
52
53// GrowCap will return a new capacity for a slice, given the following:
54// - oldCap: current capacity
55// - unit: in-memory size of an element
56// - num: number of elements to add
57func growCap(oldCap, unit, num int) (newCap int) {
58 // appendslice logic (if cap < 1024, *2, else *1.25):
59 // leads to many copy calls, especially when copying bytes.
60 // bytes.Buffer model (2*cap + n): much better for bytes.
61 // smarter way is to take the byte-size of the appended element(type) into account
62
63 // maintain 3 thresholds:
64 // t1: if cap <= t1, newcap = 2x
65 // t2: if cap <= t2, newcap = 1.75x
66 // t3: if cap <= t3, newcap = 1.5x
67 // else newcap = 1.25x
68 //
69 // t1, t2, t3 >= 1024 always.
70 // i.e. if unit size >= 16, then always do 2x or 1.25x (ie t1, t2, t3 are all same)
71 //
72 // With this, appending for bytes increase by:
73 // 100% up to 4K
74 // 75% up to 8K
75 // 50% up to 16K
76 // 25% beyond that
77
78 // unit can be 0 e.g. for struct{}{}; handle that appropriately
79 var t1, t2, t3 int // thresholds
80 if unit <= 1 {
81 t1, t2, t3 = 4*1024, 8*1024, 16*1024
82 } else if unit < 16 {
83 t3 = 16 / unit * 1024
84 t1 = t3 * 1 / 4
85 t2 = t3 * 2 / 4
86 } else {
87 t1, t2, t3 = 1024, 1024, 1024
88 }
89
90 var x int // temporary variable
91
92 // x is multiplier here: one of 5, 6, 7 or 8; incr of 25%, 50%, 75% or 100% respectively
93 if oldCap <= t1 { // [0,t1]
94 x = 8
95 } else if oldCap > t3 { // (t3,infinity]
96 x = 5
97 } else if oldCap <= t2 { // (t1,t2]
98 x = 7
99 } else { // (t2,t3]
100 x = 6
101 }
102 newCap = x * oldCap / 4
103
104 if num > 0 {
105 newCap += num
106 }
107
108 // ensure newCap is a multiple of 64 (if it is > 64) or 16.
109 if newCap > 64 {
110 if x = newCap % 64; x != 0 {
111 x = newCap / 64
112 newCap = 64 * (x + 1)
113 }
114 } else {
115 if x = newCap % 16; x != 0 {
116 x = newCap / 16
117 newCap = 16 * (x + 1)
118 }
119 }
120 return
121}