-
Notifications
You must be signed in to change notification settings - Fork 4
/
uint128.go
284 lines (249 loc) · 5.73 KB
/
uint128.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
//https://github.com/lukechampine/uint128/blob/master/uint128.go
package main
import (
"encoding/binary"
"math"
"math/big"
"math/bits"
)
// Zero is a zero-valued uint128.
var Zero Uint128
// Max is the largest possible uint128 value.
var Max = New(math.MaxUint64, math.MaxUint64)
// A Uint128 is an unsigned 128-bit number.
type Uint128 struct {
lo, hi uint64
}
// IsZero returns true if u == 0.
func (u Uint128) IsZero() bool {
return u == Zero
}
// Equals returns true if u == v.
//
// Uint128 values can be compared directly with ==, but use of the Equals method
// is preferred for consistency.
func (u Uint128) Equals(v Uint128) bool {
return u == v
}
// Equals64 returns true if u == v.
func (u Uint128) Equals64(v uint64) bool {
return u.lo == v && u.hi == 0
}
// Cmp compares u and v and returns:
//
// -1 if u < v
// 0 if u == v
// +1 if u > v
//
func (u Uint128) Cmp(v Uint128) int {
if u == v {
return 0
} else if u.hi < v.hi || (u.hi == v.hi && u.lo < v.lo) {
return -1
} else {
return 1
}
}
// Cmp64 compares u and v and returns:
//
// -1 if u < v
// 0 if u == v
// +1 if u > v
//
func (u Uint128) Cmp64(v uint64) int {
if u.hi == 0 && u.lo == v {
return 0
} else if u.hi == 0 && u.lo < v {
return -1
} else {
return 1
}
}
// And returns u&v.
func (u Uint128) And(v Uint128) Uint128 {
return Uint128{u.lo & v.lo, u.hi & v.hi}
}
// And64 returns u&v.
func (u Uint128) And64(v uint64) Uint128 {
return Uint128{u.lo & v, u.hi & 0}
}
// Or returns u|v.
func (u Uint128) Or(v Uint128) Uint128 {
return Uint128{u.lo | v.lo, u.hi | v.hi}
}
// Or64 returns u|v.
func (u Uint128) Or64(v uint64) Uint128 {
return Uint128{u.lo | v, u.hi | 0}
}
// Xor returns u^v.
func (u Uint128) Xor(v Uint128) Uint128 {
return Uint128{u.lo ^ v.lo, u.hi ^ v.hi}
}
// Xor64 returns u^v.
func (u Uint128) Xor64(v uint64) Uint128 {
return Uint128{u.lo ^ v, u.hi ^ 0}
}
// Add returns u+v.
func (u Uint128) Add(v Uint128) Uint128 {
lo, carry := bits.Add64(u.lo, v.lo, 0)
hi, _ := bits.Add64(u.hi, v.hi, carry)
return Uint128{lo, hi}
}
// Add64 returns u+v.
func (u Uint128) Add64(v uint64) Uint128 {
lo, carry := bits.Add64(u.lo, v, 0)
hi := u.hi + carry
return Uint128{lo, hi}
}
// Sub returns u-v.
func (u Uint128) Sub(v Uint128) Uint128 {
lo, borrow := bits.Sub64(u.lo, v.lo, 0)
hi, _ := bits.Sub64(u.hi, v.hi, borrow)
return Uint128{lo, hi}
}
// Sub64 returns u-v.
func (u Uint128) Sub64(v uint64) Uint128 {
lo, borrow := bits.Sub64(u.lo, v, 0)
hi := u.hi - borrow
return Uint128{lo, hi}
}
// Mul returns u*v.
func (u Uint128) Mul(v Uint128) Uint128 {
hi, lo := bits.Mul64(u.lo, v.lo)
hi += u.hi*v.lo + u.lo*v.hi
return Uint128{lo, hi}
}
// Mul64 returns u*v.
func (u Uint128) Mul64(v uint64) Uint128 {
hi, lo := bits.Mul64(u.lo, v)
hi += u.hi * v
return Uint128{lo, hi}
}
// Div returns u/v.
func (u Uint128) Div(v Uint128) Uint128 {
q, _ := u.QuoRem(v)
return q
}
// Div64 returns u/v.
func (u Uint128) Div64(v uint64) Uint128 {
q, _ := u.QuoRem64(v)
return q
}
// QuoRem returns q = u/v and r = u%v.
func (u Uint128) QuoRem(v Uint128) (q, r Uint128) {
if v.hi == 0 {
var r64 uint64
q, r64 = u.QuoRem64(v.lo)
r = From64(r64)
} else {
// generate a "trial quotient," guaranteed to be within 1 of the actual
// quotient, then adjust.
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 = From64(tq)
// calculate remainder using trial quotient, then adjust if remainder is
// greater than divisor
r = u.Sub(v.Mul64(tq))
if r.Cmp(v) >= 0 {
q = q.Add64(1)
r = r.Sub(v)
}
}
return
}
// QuoRem64 returns q = u/v and r = u%v.
func (u Uint128) QuoRem64(v uint64) (q Uint128, 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 Uint128) Lsh(n uint) (s Uint128) {
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
}
// Rsh returns u>>n.
func (u Uint128) Rsh(n uint) (s Uint128) {
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
}
// String returns the base-10 representation of u as a string.
func (u Uint128) 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
}
}
// PutBytes stores u in b in little-endian order. It panics if len(b) < 16.
func (u Uint128) PutBytes(b []byte) {
binary.LittleEndian.PutUint64(b[:8], u.lo)
binary.LittleEndian.PutUint64(b[8:], u.hi)
}
// Big returns u as a *big.Int.
func (u Uint128) Big() *big.Int {
i := new(big.Int).SetUint64(u.hi)
i = i.Lsh(i, 64)
i = i.Xor(i, new(big.Int).SetUint64(u.lo))
return i
}
// New returns the Uint128 value (lo,hi).
func New(lo, hi uint64) Uint128 {
return Uint128{lo, hi}
}
// From64 converts v to a Uint128 value.
func From64(v uint64) Uint128 {
return New(v, 0)
}
// FromBytes converts b to a Uint128 value.
func FromBytes(b []byte) Uint128 {
return New(
binary.LittleEndian.Uint64(b[:8]),
binary.LittleEndian.Uint64(b[8:]),
)
}
// FromBig converts i to a Uint128 value. It panics if i is negative or
// overflows 128 bits.
func FromBig(i *big.Int) (u Uint128) {
if i.Sign() < 0 {
panic("value cannot be negative")
} else if i.BitLen() > 128 {
panic("value overflows Uint128")
}
u.lo = i.Uint64()
u.hi = new(big.Int).Rsh(i, 64).Uint64()
return u
}