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