-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbig.go
More file actions
176 lines (167 loc) · 3.5 KB
/
big.go
File metadata and controls
176 lines (167 loc) · 3.5 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package refloat
import (
"math/big"
)
var (
fiv = big.NewInt(5)
)
func bigParseFloat(num string, extend int) (uint64, int, error) {
const fnc = "ParseFloat"
width2 := 32 << extend
width10 := 10 << extend
var (
sign int
mant big.Int
exp10 int
)
var offset int
if offset >= len(num) {
panic("bigParseFloat was called with empty string")
} else if num[offset] == '+' {
offset++
} else if num[offset] == '-' {
offset++
sign = 1
}
// Infs and NaNs are checked in fast-path.
var temp big.Int
var point bool
for ; offset < len(num); offset++ {
char := num[offset]
if char == '.' && !point {
point = true
continue
}
if char == '_' {
// also checked by fast-path.
continue
}
char -= '0'
if char > '9'-'0' {
break
}
if point {
exp10--
}
// * 10 might compile down to multiplication unlike
// word size math, so we manually make shifts.
// (x*10 = x*8 + x*2 = x<<3 + x<<1)
temp.Lsh(&mant, 3)
mant.Lsh(&mant, 1)
mant.Add(&mant, &temp)
temp.SetUint64(uint64(char))
mant.Add(&mant, &temp)
}
if offset < len(num) && num[offset]|0x20 == 'e' {
// log10(2)*1024 ~= 308.
// max exponent + "mant" length + log10(2)*width.
limit := 308 + int(mant.BitLen()) + width10
var shift int
var esign bool
offset++
if offset >= len(num) {
return 0, 0, errorSyntax(fnc, num)
} else if num[offset] == '+' {
offset++
} else if num[offset] == '-' {
offset++
esign = true
}
for ; offset < len(num); offset++ {
char := num[offset]
if char == '_' {
continue
}
char -= '0'
if char > '9'-'0' {
break
}
// definitely an overflow/
if esign && exp10-shift < -limit || !esign && exp10+shift > limit {
continue
}
shift = shift*10 + int(char)
}
if esign {
exp10 -= shift
} else {
exp10 += shift
}
}
exp := exp10
abs := big.NewInt(0)
abs.SetUint64(uint64(max(exp10, -exp10)))
// in .Exp(), If m == nil or m == 0, z = x**y.
temp.Exp(fiv, abs, nil)
var trunc bool
if exp10 >= 0 {
mant.Mul(&mant, &temp)
log := mant.BitLen() - width2
trunc = int(mant.TrailingZeroBits()) < log
if log > 0 {
mant.Rsh(&mant, uint(log))
exp += log
}
} else {
var rem big.Int
log := temp.BitLen() - mant.BitLen() + width2
if log > 0 {
mant.Lsh(&mant, uint(log))
exp -= log
}
mant.DivMod(&mant, &temp, &rem)
trunc = rem.Sign() != 0 // .Sign() == 0 only for 0.
}
var prec, bias int
switch width2 {
case 64:
prec = 52
bias = 1023
case 32:
prec = 23
bias = 127
}
log := mant.BitLen() - prec - 1 - 1
exp += log + bias + prec + 1
if log > 0 {
// TODO(?): find a better way to find truncated bits.
// is TrailingZeros enough?
if int(mant.TrailingZeroBits()) < log {
trunc = true
}
mant.Rsh(&mant, uint(log))
} else {
mant.Lsh(&mant, uint(-log))
}
if exp <= 0 {
if int(mant.TrailingZeroBits()) < 1-exp {
trunc = true
}
mant.Rsh(&mant, uint(1-exp))
exp = 0
}
bit := mant.Uint64()
// see hex.go for explanation of rounding.
round := bit & 1
if round != 0 && !trunc {
round &= bit >> 1
}
bit += round
bit >>= 1
if bit>>prec != 0 && exp == 0 {
exp++
}
carry := bit >> (prec + 1)
bit >>= carry
exp += int(carry)
bit &= 1<<prec - 1
if extend == 1 && exp >= 0x7ff {
return uint64(0x7ff)<<prec | uint64(sign)<<(width2-1), offset, errorRange(fnc, num)
}
if extend == 0 && exp >= 0x0ff {
return uint64(0x0ff)<<prec | uint64(sign)<<(width2-1), offset, errorRange(fnc, num)
}
bit |= uint64(exp) << prec
bit |= uint64(sign) << (width2 - 1)
return bit, offset, nil
}