-
Notifications
You must be signed in to change notification settings - Fork 4
/
u128.go
326 lines (274 loc) · 5.78 KB
/
u128.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package udecimal
import (
"encoding/binary"
"math/big"
"math/bits"
)
var (
one128 = u128{lo: 1}
max128 = u128{hi: ^uint64(0), lo: ^uint64(0)}
)
// u128 (big unsigned-integer) is a 128-bits unsigned integer
// represents by two 64-bits unsigned integer
// value = hi*2^64 + lo
type u128 struct {
hi uint64
lo uint64
}
// IsZero returns true if u is zero
func (u u128) IsZero() bool {
return u == u128{}
}
// Cmp compares u, v and returns:
//
// -1 if u < v
// 0 if u == v
// 1 if u > v
func (u u128) Cmp(v u128) int {
if u.hi < v.hi {
return -1
}
if u.hi > v.hi {
return 1
}
// u.hi == v.hi
switch {
case u.lo < v.lo:
return -1
case u.lo > v.lo:
return 1
default:
return 0
}
}
func (u u128) Cmp64(v uint64) int {
if u.hi != 0 {
return 1
}
switch {
case u.lo < v:
return -1
case u.lo > v:
return 1
default:
return 0
}
}
func (u u128) Add(v u128) (u128, error) {
lo, carry := bits.Add64(u.lo, v.lo, 0)
hi, carry := bits.Add64(u.hi, v.hi, carry)
if carry != 0 {
return u128{}, errOverflow
}
return u128{hi: hi, lo: lo}, nil
}
// Add64 returns u+v where v is a 64-bits unsigned integer.
func (u u128) Add64(v uint64) (u128, error) {
if u.hi == 0 {
lo, carry := bits.Add64(u.lo, v, 0)
return u128{lo: lo, hi: carry}, nil
}
return u.Add(u128{lo: v})
}
func (u u128) Sub(v u128) (u128, error) {
lo, borrow := bits.Sub64(u.lo, v.lo, 0)
hi, borrow := bits.Sub64(u.hi, v.hi, borrow)
if borrow != 0 {
// borrow != 0 means u < v and this must not happen
return u128{}, errOverflow
}
return u128{hi: hi, lo: lo}, nil
}
// Mul64 returns u*v. If the result is greater than 2^128-1, Mul64 returns an overflow error.
func (u u128) Mul64(v uint64) (u128, error) {
if u.hi == 0 {
hi, lo := bits.Mul64(u.lo, v)
return u128{hi: hi, lo: lo}, nil
}
hi, lo := bits.Mul64(u.lo, v)
p0, p1 := bits.Mul64(u.hi, v)
hi, c0 := bits.Add64(hi, p1, 0)
if p0 != 0 || c0 != 0 {
return u128{}, errOverflow
}
return u128{hi: hi, lo: lo}, nil
}
func (u u128) Mul(v u128) (u128, error) {
if u.hi != 0 && v.hi != 0 {
return u128{}, errOverflow
}
if v.hi == 0 {
return u.Mul64(v.lo)
}
// u.hi == 0
return v.Mul64(u.lo)
}
// MulToU256 returns u*v and carry.
// The whole result will be stored in a 256-bits unsigned integer.
func (u u128) MulToU256(v u128) u256 {
// short path for small numbers
if u.hi == 0 && v.hi == 0 {
hi, lo := bits.Mul64(u.lo, v.lo)
return u256{lo: lo, hi: hi}
}
hi, lo := bits.Mul64(u.lo, v.lo)
p0, p1 := bits.Mul64(u.hi, v.lo)
p2, p3 := bits.Mul64(u.lo, v.hi)
// calculate hi + p1 + p3
// total carry = carry(hi+p1) + carry(hi+p1+p3)
hi, c0 := bits.Add64(hi, p1, 0)
hi, c1 := bits.Add64(hi, p3, 0)
c1 += c0
// calculate upper part of u256
e0, e1 := bits.Mul64(u.hi, v.hi)
d, d0 := bits.Add64(p0, p2, 0)
d, d1 := bits.Add64(d, c1, 0)
e2, e3 := bits.Add64(d, e1, 0)
carry := u128{
hi: e0 + d0 + d1 + e3, // result can't be overflow because max(pow) = pow[39] < 2^128
lo: e2,
}
return u256{
lo: lo,
hi: hi,
carry: carry,
}
}
func u128FromU64(v uint64) u128 {
return u128{lo: v}
}
func u128FromHiLo(hi, lo uint64) u128 {
return u128{hi: hi, lo: lo}
}
// QuoRem returns q = u/v and r = u%v.
func (u u128) QuoRem(v u128) (q, r u128, err error) {
if v.hi == 0 {
var r64 uint64
q, r64 = u.QuoRem64(v.lo)
r = u128FromU64(r64)
} else {
// generate a "trial quotient," guaranteed to be within 1 of the actual
// quotient, then adjust.
//nolint:gosec // 0 <= bits.LeadingZeros64(v.hi) <= 63, so it's safe to convert to uint
n := uint(bits.LeadingZeros64(v.hi))
v1 := v.Lsh(n)
u1 := u.Rsh(1)
tq, _ := bits.Div64(u1.hi, u1.lo, v1.hi)
tq >>= 63 - n
if tq != 0 {
tq--
}
q = u128FromU64(tq)
vq, err := v.Mul64(tq)
if err != nil {
return q, r, err
}
r, err = u.Sub(vq)
if err != nil {
return q, r, err
}
if r.Cmp(v) >= 0 {
q, err = q.Add64(1)
if err != nil {
return q, r, err
}
r, err = r.Sub(v)
if err != nil {
return q, r, err
}
}
}
return
}
// QuoRem64 returns q = u/v and r = u%v.
func (u u128) QuoRem64(v uint64) (q u128, r uint64) {
if u.hi < v {
q.lo, r = bits.Div64(u.hi, u.lo, v)
} else {
q.hi, r = bits.Div64(0, u.hi, v)
q.lo, r = bits.Div64(r, u.lo, v)
}
return
}
// Lsh returns u<<n.
func (u u128) Lsh(n uint) (s u128) {
if n >= 64 {
s.lo = 0
s.hi = u.lo << (n - 64)
} else {
s.lo = u.lo << n
s.hi = u.hi<<n | u.lo>>(64-n)
}
return
}
// for debugging
// func (u u128) String() string {
// if u.IsZero() {
// return "0"
// }
// buf := []byte("0000000000000000000000000000000000000000") // log10(2^128) < 40
// for i := len(buf); ; i -= 19 {
// q, r := u.QuoRem64(1e19) // largest power of 10 that fits in a uint64
// var n int
// for ; r != 0; r /= 10 {
// n++
// buf[i-n] += byte(r % 10)
// }
// if q.IsZero() {
// return string(buf[i-n:])
// }
// u = q
// }
// }
// Rsh returns u>>n.
func (u u128) Rsh(n uint) (s u128) {
if n >= 64 {
s.lo = u.hi >> (n - 64)
s.hi = 0
} else {
s.lo = u.lo>>n | u.hi<<(64-n)
s.hi = u.hi >> n
}
return
}
func (u u128) ToBigInt() *big.Int {
bytes := make([]byte, 16)
binary.BigEndian.PutUint64(bytes, u.hi)
binary.BigEndian.PutUint64(bytes[8:], u.lo)
return new(big.Int).SetBytes(bytes)
}
// getTrailingZeros64 returns the number of trailing zeros in u
// NOTE: this only works when maxPrec is 19
func getTrailingZeros64(u uint64) uint8 {
var zeros uint8
// max scale is 19, so we can start from 16
if u%1e16 == 0 {
zeros += 16
u /= 1e16
// short path, because we know that max scale is 19
if u%100 == 0 {
zeros += 2
u /= 100
}
if u%10 == 0 {
zeros++
}
return zeros
}
if u%1e8 == 0 {
zeros += 8
u /= 1e8
}
if u%1e4 == 0 {
zeros += 4
u /= 1e4
}
if u%100 == 0 {
zeros += 2
u /= 100
}
if u%10 == 0 {
zeros++
}
return zeros
}