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.
6 // All non-std package dependencies live in this file,
7 // so porting to different environment is easy (just update functions).
9 func pruneSignExt(v []byte, pos bool) (n int) {
11 } else if pos && v[0] == 0 {
12 for ; v[n] == 0 && n+1 < len(v) && (v[n+1]&(1<<7) == 0); n++ {
14 } else if !pos && v[0] == 0xff {
15 for ; v[n] == 0xff && n+1 < len(v) && (v[n+1]&(1<<7) != 0); n++ {
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)
24 func halfFloatToFloatBits(yy uint16) (d uint32) {
31 if m == 0 { // plu or minus 0
34 // Denormalized number -- renormalize it
35 for (m & 0x00000400) == 0 {
40 const zz uint32 = 0x0400
44 return (s << 31) | 0x7f800000
46 return (s << 31) | 0x7f800000 | (m << 13) // NaN
50 return (s << 31) | (e << 23) | m
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
57 func 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
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
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)
72 // With this, appending for bytes increase by:
78 // unit can be 0 e.g. for struct{}{}; handle that appropriately
79 var t1, t2, t3 int // thresholds
81 t1, t2, t3 = 4*1024, 8*1024, 16*1024
87 t1, t2, t3 = 1024, 1024, 1024
90 var x int // temporary variable
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]
95 } else if oldCap > t3 { // (t3,infinity]
97 } else if oldCap <= t2 { // (t1,t2]
102 newCap = x * oldCap / 4
108 // ensure newCap is a multiple of 64 (if it is > 64) or 16.
110 if x = newCap % 64; x != 0 {
112 newCap = 64 * (x + 1)
115 if x = newCap % 16; x != 0 {
117 newCap = 16 * (x + 1)