| 1 |
package wl |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"sync" |
| 6 |
"unsafe" |
| 7 |
) |
| 8 |
|
| 9 |
type BytePool struct { |
| 10 |
sync.Pool |
| 11 |
} |
| 12 |
|
| 13 |
var ( |
| 14 |
order binary.ByteOrder |
| 15 |
bytePool = &BytePool{ |
| 16 |
sync.Pool{ |
| 17 |
New: func() interface{} { |
| 18 |
return make([]byte, 16) |
| 19 |
}, |
| 20 |
}, |
| 21 |
} |
| 22 |
) |
| 23 |
|
| 24 |
func (bp *BytePool) Take(n int) []byte { |
| 25 |
buf := bp.Get().([]byte) |
| 26 |
if cap(buf) < n { |
| 27 |
t := make([]byte, len(buf), n) |
| 28 |
copy(t, buf) |
| 29 |
buf = t |
| 30 |
} |
| 31 |
return buf[:n] |
| 32 |
} |
| 33 |
|
| 34 |
func (bp *BytePool) Give(b []byte) { |
| 35 |
bp.Put(b) |
| 36 |
} |
| 37 |
|
| 38 |
func init() { |
| 39 |
var x uint32 = 0x01020304 |
| 40 |
if *(*byte)(unsafe.Pointer(&x)) == 0x01 { |
| 41 |
order = binary.BigEndian |
| 42 |
} else { |
| 43 |
order = binary.LittleEndian |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func Float64frombits(b uint64) float64 { return *(*float64)(unsafe.Pointer(&b)) } |
| 49 |
func Float64bits(f float64) uint64 { return *(*uint64)(unsafe.Pointer(&f)) } |
| 50 |
|
| 51 |
func fixedToFloat64(fixed int32) float64 { |
| 52 |
dat := ((int64(1023 + 44)) << 52) + (1 << 51) + int64(fixed) |
| 53 |
return Float64frombits(uint64(dat)) - float64(3<<43) |
| 54 |
} |
| 55 |
|
| 56 |
func float64ToFixed(v float64) int32 { |
| 57 |
dat := v + float64(int64(3)<<(51-8)) |
| 58 |
return int32(Float64bits(dat)) |
| 59 |
} |