-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixedraw.go
81 lines (72 loc) · 1.89 KB
/
fixedraw.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package fixed
import (
"math"
"math/bits"
)
const (
fracBits = 56
fracMask = oneValue - 1
halfValue = oneValue >> 1
oneValue int64 = 1 << fracBits
)
const (
ln2 = int64(0xb17217f7d1cf78) // logₑ(2)
invLog2E = ln2 // log₂(2)/log₂(e) = logₑ(2) => 1/log₂(e) = logₑ(2)
log2E = int64(0x171547652b82fe0) // log₂(e)
invLn2 = log2E // logₑ(e)/logₑ(2) = log₂(e) => 1/logₑ(2) = log₂(e)
)
func fixed56(v int64) int64 {
return v << fracBits
}
func mul56(x, y int64) int64 {
hi2, lo2 := bits.Mul64(uint64(x), uint64(y))
lo, c := bits.Add64(lo2, uint64(halfValue), 0)
hi := hi2 - uint64((x>>63)&y) - uint64((y>>63)&x) + c
if hi>>63 != hi>>(fracBits-1)&1 {
panic(ErrOverflow)
}
return int64(hi<<(64-fracBits) | (lo >> fracBits))
}
func mul56u(x, y int64) int64 {
hi, lo := bits.Mul64(uint64(x), uint64(y))
return int64(hi<<(64-fracBits) | (lo >> fracBits))
}
func div56(x, y int64) int64 {
xs := x >> 63
ys := y >> 63
a := uint64((x ^ xs) - xs) // abs
b := uint64((y ^ ys) - ys) // abs
hi, lo := a>>(64-fracBits), a<<fracBits
// will panic when divides by zero or occurs overflow
v, rem := bits.Div64(hi, lo, b)
// rem < b && (b>>63) == 0 => (rem<<1) < ^uint64(0)
// (rem<<1)/b ∈ {0,1}
// round to near
v, carry := bits.Add64(v, (rem<<1)/b, 0)
if carry != 0 {
panic(ErrOverflow)
}
return int64(v) * ((xs^ys)*2 + 1)
}
func floor56(x int64) int64 {
return x & ^fracMask
}
func float56(x int64) float64 {
xs := x >> 63
a := uint64((x ^ xs) - xs)
l := bits.Len64(a)
if a != 0 {
if l > 52 { // float64 significat bits count
a = a >> (l - 52 - 1)
l = l - fracBits - 1
} else {
a = a << (52 - l + 1)
l -= fracBits + 1
}
a &= ^(uint64(1) << 52)
a |= uint64(1023+l) << 52
a |= uint64(xs) & (uint64(1) << 63) // sign
}
f := math.Float64frombits(a)
return f
}