This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
u128.go
1056 lines (899 loc) · 22.9 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package num
import (
"fmt"
"math/big"
"math/bits"
"strconv"
)
type U128 struct {
hi, lo uint64
}
// U128FromRaw is the complement to U128.Raw(); it creates an U128 from two
// uint64s representing the hi and lo bits.
func U128FromRaw(hi, lo uint64) U128 { return U128{hi: hi, lo: lo} }
func U128From64(v uint64) U128 { return U128{lo: v} }
func U128From32(v uint32) U128 { return U128{lo: uint64(v)} }
func U128From16(v uint16) U128 { return U128{lo: uint64(v)} }
func U128From8(v uint8) U128 { return U128{lo: uint64(v)} }
func U128FromUint(v uint) U128 { return U128{lo: uint64(v)} }
// U128FromI64 creates a U128 from an int64 if the conversion is possible, and
// sets inRange to false if not.
func U128FromI64(v int64) (out U128, inRange bool) {
if v < 0 {
return zeroU128, false
}
return U128{lo: uint64(v)}, true
}
func MustU128FromI64(v int64) (out U128) {
out, inRange := U128FromI64(v)
if !inRange {
panic(fmt.Errorf("num: int64 %d was not in valid U128 range", v))
}
return out
}
// U128FromString creates a U128 from a string. Overflow truncates to MaxU128
// and sets inRange to 'false'. Only decimal strings are currently supported.
func U128FromString(s string) (out U128, inRange bool, err error) {
// This deliberately limits the scope of what we accept as input just in case
// we decide to hand-roll our own fast decimal-only parser:
b, ok := new(big.Int).SetString(s, 10)
if !ok {
return out, false, fmt.Errorf("num: u128 string %q invalid", s)
}
out, inRange = U128FromBigInt(b)
return out, inRange, nil
}
func MustU128FromString(s string) U128 {
out, inRange, err := U128FromString(s)
if err != nil {
panic(err)
}
if !inRange {
panic(fmt.Errorf("num: string %q was not in valid U128 range", s))
}
return out
}
// U128FromBigInt creates a U128 from a big.Int. Overflow truncates to MaxU128
// and sets inRange to 'false'.
func U128FromBigInt(v *big.Int) (out U128, inRange bool) {
if v.Sign() < 0 {
return out, false
}
words := v.Bits()
switch intSize {
case 64:
lw := len(words)
switch lw {
case 0:
return U128{}, true
case 1:
return U128{lo: uint64(words[0])}, true
case 2:
return U128{hi: uint64(words[1]), lo: uint64(words[0])}, true
default:
return MaxU128, false
}
case 32:
lw := len(words)
switch lw {
case 0:
return U128{}, true
case 1:
return U128{lo: uint64(words[0])}, true
case 2:
return U128{lo: (uint64(words[1]) << 32) | (uint64(words[0]))}, true
case 3:
return U128{hi: uint64(words[2]), lo: (uint64(words[1]) << 32) | (uint64(words[0]))}, true
case 4:
return U128{
hi: (uint64(words[3]) << 32) | (uint64(words[2])),
lo: (uint64(words[1]) << 32) | (uint64(words[0])),
}, true
default:
return MaxU128, false
}
default:
panic("num: unsupported bit size")
}
}
func MustU128FromBigInt(b *big.Int) U128 {
out, inRange := U128FromBigInt(b)
if !inRange {
panic(fmt.Errorf("num: big.Int %d was not in valid U128 range", b))
}
return out
}
func U128FromFloat32(f float32) (out U128, inRange bool) {
return U128FromFloat64(float64(f))
}
func MustU128FromFloat32(f float32) U128 {
out, inRange := U128FromFloat32(f)
if !inRange {
panic(fmt.Errorf("num: float32 %f was not in valid U128 range", f))
}
return out
}
// U128FromFloat64 creates a U128 from a float64.
//
// Any fractional portion will be truncated towards zero.
//
// Floats outside the bounds of a U128 may be discarded or clamped and inRange
// will be set to false.
//
// NaN is treated as 0, inRange is set to false. This may change to a panic
// at some point.
func U128FromFloat64(f float64) (out U128, inRange bool) {
// WARNING: casts from float64 to uint64 have some astonishing properties:
// https://github.com/golang/go/issues/29463
if f == 0 {
return U128{}, true
} else if f < 0 {
return U128{}, false
} else if f <= maxRepresentableUint64Float {
return U128{lo: uint64(f)}, true
} else if f <= maxRepresentableU128Float {
lo := modpos(f, wrapUint64Float) // f is guaranteed to be > 0 here.
return U128{hi: uint64(f / wrapUint64Float), lo: uint64(lo)}, true
} else if f != f { // (f != f) == NaN
return U128{}, false
} else {
return MaxU128, false
}
}
func MustU128FromFloat64(f float64) U128 {
out, inRange := U128FromFloat64(f)
if !inRange {
panic(fmt.Errorf("num: float64 %f was not in valid U128 range", f))
}
return out
}
// RandU128 generates an unsigned 128-bit random integer from an external source.
func RandU128(source RandSource) (out U128) {
return U128{hi: source.Uint64(), lo: source.Uint64()}
}
func (u U128) IsZero() bool { return u.lo == 0 && u.hi == 0 }
// Raw returns access to the U128 as a pair of uint64s. See U128FromRaw() for
// the counterpart.
func (u U128) Raw() (hi, lo uint64) { return u.hi, u.lo }
func (u U128) String() string {
// FIXME: This is good enough for now, but not forever.
if u.lo == 0 && u.hi == 0 {
return "0"
}
if u.hi == 0 {
return strconv.FormatUint(u.lo, 10)
}
v := u.AsBigInt()
return v.String()
}
func (u U128) Format(s fmt.State, c rune) {
// FIXME: This is good enough for now, but not forever.
u.AsBigInt().Format(s, c)
}
func (u *U128) Scan(state fmt.ScanState, verb rune) error {
t, err := state.Token(true, nil)
if err != nil {
return err
}
ts := string(t)
v, inRange, err := U128FromString(ts)
if err != nil {
return err
} else if !inRange {
return fmt.Errorf("num: u128 value %q is not in range", ts)
}
*u = v
return nil
}
func (u U128) IntoBigInt(b *big.Int) {
switch intSize {
case 64:
bits := b.Bits()
ln := len(bits)
if len(bits) < 2 {
bits = append(bits, make([]big.Word, 2-ln)...)
}
bits = bits[:2]
bits[0] = big.Word(u.lo)
bits[1] = big.Word(u.hi)
b.SetBits(bits)
case 32:
bits := b.Bits()
ln := len(bits)
if len(bits) < 4 {
bits = append(bits, make([]big.Word, 4-ln)...)
}
bits = bits[:4]
bits[0] = big.Word(u.lo & 0xFFFFFFFF)
bits[1] = big.Word(u.lo >> 32)
bits[2] = big.Word(u.hi & 0xFFFFFFFF)
bits[3] = big.Word(u.hi >> 32)
b.SetBits(bits)
default:
if u.hi > 0 {
b.SetUint64(u.hi)
b.Lsh(b, 64)
}
var lo big.Int
lo.SetUint64(u.lo)
b.Add(b, &lo)
}
}
// AsBigInt returns the U128 as a big.Int. This will allocate memory. If
// performance is a concern and you are able to re-use memory, use
// U128.IntoBigInt().
func (u U128) AsBigInt() (b *big.Int) {
var v big.Int
u.IntoBigInt(&v)
return &v
}
func (u U128) AsBigFloat() (b *big.Float) {
return new(big.Float).SetInt(u.AsBigInt())
}
func (u U128) AsFloat64() float64 {
if u.hi == 0 && u.lo == 0 {
return 0
} else if u.hi == 0 {
return float64(u.lo)
} else {
return (float64(u.hi) * wrapUint64Float) + float64(u.lo)
}
}
// AsI128 performs a direct cast of a U128 to an I128, which will interpret it
// as a two's complement value.
func (u U128) AsI128() I128 {
return I128{lo: u.lo, hi: u.hi}
}
// IsI128 reports whether i can be represented in an I128.
func (u U128) IsI128() bool {
return u.hi&signBit == 0
}
// AsUint64 truncates the U128 to fit in a uint64. Values outside the range
// will over/underflow. See IsUint64() if you want to check before you convert.
func (u U128) AsUint64() uint64 {
return u.lo
}
// IsUint64 reports whether u can be represented as a uint64.
func (u U128) IsUint64() bool {
return u.hi == 0
}
// MustUint64 converts i to an unsigned 64-bit integer if the conversion would succeed,
// and panics if it would not.
func (u U128) MustUint64() uint64 {
if u.hi != 0 {
panic(fmt.Errorf("U128 %v is not representable as a uint64", u))
}
return u.lo
}
func (u U128) Inc() (v U128) {
var carry uint64
v.lo, carry = bits.Add64(u.lo, 1, 0)
v.hi = u.hi + carry
return v
}
func (u U128) Dec() (v U128) {
var borrowed uint64
v.lo, borrowed = bits.Sub64(u.lo, 1, 0)
v.hi = u.hi - borrowed
return v
}
func (u U128) Add(n U128) (v U128) {
var carry uint64
v.lo, carry = bits.Add64(u.lo, n.lo, 0)
v.hi, _ = bits.Add64(u.hi, n.hi, carry)
return v
}
func (u U128) Add64(n uint64) (v U128) {
var carry uint64
v.lo, carry = bits.Add64(u.lo, n, 0)
v.hi = u.hi + carry
return v
}
func (u U128) Sub(n U128) (v U128) {
var borrowed uint64
v.lo, borrowed = bits.Sub64(u.lo, n.lo, 0)
v.hi, _ = bits.Sub64(u.hi, n.hi, borrowed)
return v
}
func (u U128) Sub64(n uint64) (v U128) {
var borrowed uint64
v.lo, borrowed = bits.Sub64(u.lo, n, 0)
v.hi = u.hi - borrowed
return v
}
// Cmp compares 'u' to 'n' and returns:
//
// < 0 if u < n
// 0 if u == n
// > 0 if u > n
//
// The specific value returned by Cmp is undefined, but it is guaranteed to
// satisfy the above constraints.
//
func (u U128) Cmp(n U128) int {
if u.hi == n.hi {
if u.lo > n.lo {
return 1
} else if u.lo < n.lo {
return -1
}
} else {
if u.hi > n.hi {
return 1
} else if u.hi < n.hi {
return -1
}
}
return 0
}
func (u U128) Cmp64(n uint64) int {
if u.hi > 0 || u.lo > n {
return 1
} else if u.lo < n {
return -1
}
return 0
}
func (u U128) Equal(n U128) bool {
return u.hi == n.hi && u.lo == n.lo
}
func (u U128) Equal64(n uint64) bool {
return u.hi == 0 && u.lo == n
}
func (u U128) GreaterThan(n U128) bool {
return u.hi > n.hi || (u.hi == n.hi && u.lo > n.lo)
}
func (u U128) GreaterThan64(n uint64) bool {
return u.hi > 0 || u.lo > n
}
func (u U128) GreaterOrEqualTo(n U128) bool {
return u.hi > n.hi || (u.hi == n.hi && u.lo >= n.lo)
}
func (u U128) GreaterOrEqualTo64(n uint64) bool {
return u.hi > 0 || u.lo >= n
}
func (u U128) LessThan(n U128) bool {
return u.hi < n.hi || (u.hi == n.hi && u.lo < n.lo)
}
func (u U128) LessThan64(n uint64) bool {
return u.hi == 0 && u.lo < n
}
func (u U128) LessOrEqualTo(n U128) bool {
return u.hi < n.hi || (u.hi == n.hi && u.lo <= n.lo)
}
func (u U128) LessOrEqualTo64(n uint64) bool {
return u.hi == 0 && u.lo <= n
}
func (u U128) And(n U128) U128 {
u.hi = u.hi & n.hi
u.lo = u.lo & n.lo
return u
}
func (u U128) And64(n uint64) U128 {
return U128{lo: u.lo & n}
}
func (u U128) AndNot(n U128) U128 {
u.hi = u.hi &^ n.hi
u.lo = u.lo &^ n.lo
return u
}
func (u U128) Not() (out U128) {
out.hi = ^u.hi
out.lo = ^u.lo
return out
}
func (u U128) Or(n U128) (out U128) {
out.hi = u.hi | n.hi
out.lo = u.lo | n.lo
return out
}
func (u U128) Or64(n uint64) U128 {
u.lo = u.lo | n
return u
}
func (u U128) Xor(v U128) U128 {
u.hi = u.hi ^ v.hi
u.lo = u.lo ^ v.lo
return u
}
func (u U128) Xor64(v uint64) U128 {
u.hi = u.hi ^ 0
u.lo = u.lo ^ v
return u
}
// BitLen returns the length of the absolute value of u in bits. The bit length of 0 is 0.
func (u U128) BitLen() int {
if u.hi > 0 {
return bits.Len64(u.hi) + 64
}
return bits.Len64(u.lo)
}
// OnesCount returns the number of one bits ("population count") in u.
func (u U128) OnesCount() int {
if u.hi > 0 {
return bits.OnesCount64(u.hi) + 64
}
return bits.OnesCount64(u.lo)
}
// Bit returns the value of the i'th bit of x. That is, it returns (x>>i)&1.
// The bit index i must be 0 <= i < 128
func (u U128) Bit(i int) uint {
if i < 0 || i >= 128 {
panic("num: bit out of range")
}
if i >= 64 {
return uint((u.hi >> uint(i-64)) & 1)
} else {
return uint((u.lo >> uint(i)) & 1)
}
}
// SetBit returns a U128 with u's i'th bit set to b (0 or 1).
// If b is not 0 or 1, SetBit will panic. If i < 0, SetBit will panic.
func (u U128) SetBit(i int, b uint) (out U128) {
if i < 0 || i >= 128 {
panic("num: bit out of range")
}
if b == 0 {
if i >= 64 {
u.hi = u.hi &^ (1 << uint(i-64))
} else {
u.lo = u.lo &^ (1 << uint(i))
}
} else if b == 1 {
if i >= 64 {
u.hi = u.hi | (1 << uint(i-64))
} else {
u.lo = u.lo | (1 << uint(i))
}
} else {
panic("num: bit value not 0 or 1")
}
return u
}
func (u U128) Lsh(n uint) (v U128) {
if n == 0 {
return u
} else if n > 64 {
v.hi = u.lo << (n - 64)
v.lo = 0
} else if n < 64 {
v.hi = (u.hi << n) | (u.lo >> (64 - n))
v.lo = u.lo << n
} else if n == 64 {
v.hi = u.lo
v.lo = 0
}
return v
}
func (u U128) Rsh(n uint) (v U128) {
if n == 0 {
return u
} else if n > 64 {
v.lo = u.hi >> (n - 64)
v.hi = 0
} else if n < 64 {
v.lo = (u.lo >> n) | (u.hi << (64 - n))
v.hi = u.hi >> n
} else if n == 64 {
v.lo = u.hi
v.hi = 0
}
return v
}
func (u U128) Mul(n U128) U128 {
hi, lo := bits.Mul64(u.lo, n.lo)
hi += u.hi*n.lo + u.lo*n.hi
return U128{hi, lo}
}
func (u U128) Mul64(n uint64) (dest U128) {
dest.hi, dest.lo = bits.Mul64(u.lo, n)
dest.hi += u.hi * n
return dest
}
// See BenchmarkU128QuoRemTZ for the test that helps determine this magic number:
const divAlgoLeading0Spill = 16
// Quo returns the quotient x/y for y != 0. If y == 0, a division-by-zero
// run-time panic occurs. Quo implements truncated division (like Go); see
// QuoRem for more details.
func (u U128) Quo(by U128) (q U128) {
if by.lo == 0 && by.hi == 0 {
panic("u128: division by zero")
}
if u.hi|by.hi == 0 {
q.lo = u.lo / by.lo // FIXME: div/0 risk?
return q
}
var byLoLeading0, byHiLeading0, byLeading0 uint
if by.hi == 0 {
byLoLeading0, byHiLeading0 = uint(bits.LeadingZeros64(by.lo)), 64
byLeading0 = byLoLeading0 + 64
} else {
byHiLeading0 = uint(bits.LeadingZeros64(by.hi))
byLeading0 = byHiLeading0
}
if byLeading0 == 127 {
return u
}
byTrailing0 := by.TrailingZeros()
if (byLeading0 + byTrailing0) == 127 {
return u.Rsh(byTrailing0)
}
if cmp := u.Cmp(by); cmp < 0 {
return q // it's 100% remainder
} else if cmp == 0 {
q.lo = 1 // dividend and divisor are the same
return q
}
uLeading0 := u.LeadingZeros()
if byLeading0-uLeading0 > divAlgoLeading0Spill {
q, _ = quorem128by128(u, by, byHiLeading0, byLoLeading0)
return q
} else {
return quo128bin(u, by, uLeading0, byLeading0)
}
}
func (u U128) Quo64(by uint64) (q U128) {
if u.hi < by {
q.lo, _ = bits.Div64(u.hi, u.lo, by)
} else {
q.hi = u.hi / by
q.lo, _ = bits.Div64(u.hi%by, u.lo, by)
}
return q
}
// QuoRem returns the quotient q and remainder r for y != 0. If y == 0, a
// division-by-zero run-time panic occurs.
//
// QuoRem implements T-division and modulus (like Go):
//
// q = x/y with the result truncated to zero
// r = x - y*q
//
// U128 does not support big.Int.DivMod()-style Euclidean division.
//
func (u U128) QuoRem(by U128) (q, r U128) {
if by.lo == 0 && by.hi == 0 {
panic("u128: division by zero")
}
if u.hi|by.hi == 0 {
// protected from div/0 because by.lo is guaranteed to be set if by.hi is 0:
q.lo = u.lo / by.lo
r.lo = u.lo % by.lo
return q, r
}
var byLoLeading0, byHiLeading0, byLeading0 uint
if by.hi == 0 {
byLoLeading0, byHiLeading0 = uint(bits.LeadingZeros64(by.lo)), 64
byLeading0 = byLoLeading0 + 64
} else {
byHiLeading0 = uint(bits.LeadingZeros64(by.hi))
byLeading0 = byHiLeading0
}
if byLeading0 == 127 {
return u, r
}
byTrailing0 := by.TrailingZeros()
if (byLeading0 + byTrailing0) == 127 {
q = u.Rsh(byTrailing0)
by = by.Dec()
r = by.And(u)
return
}
if cmp := u.Cmp(by); cmp < 0 {
return q, u // it's 100% remainder
} else if cmp == 0 {
q.lo = 1 // dividend and divisor are the same
return q, r
}
uLeading0 := u.LeadingZeros()
if byLeading0-uLeading0 > divAlgoLeading0Spill {
return quorem128by128(u, by, byHiLeading0, byLoLeading0)
} else {
return quorem128bin(u, by, uLeading0, byLeading0)
}
}
func (u U128) QuoRem64(by uint64) (q, r U128) {
if u.hi < by {
q.lo, r.lo = bits.Div64(u.hi, u.lo, by)
} else {
q.hi, r.lo = bits.Div64(0, u.hi, by)
q.lo, r.lo = bits.Div64(r.lo, u.lo, by)
}
return q, r
}
// Rem returns the remainder of x%y for y != 0. If y == 0, a division-by-zero
// run-time panic occurs. Rem implements truncated modulus (like Go); see
// QuoRem for more details.
func (u U128) Rem(by U128) (r U128) {
// FIXME: inline only the needed bits
_, r = u.QuoRem(by)
return r
}
func (u U128) Rem64(by uint64) (r U128) {
// XXX: bits.Rem64 (added in 1.14) shows no noticeable improvement on my 8th-gen i7
// (though it sounds like it isn't necessarily meant to):
// https://github.com/golang/go/issues/28970
// if u.hi < by {
// _, r.lo = bits.Rem64(u.hi, u.lo, by)
// } else {
// _, r.lo = bits.Rem64(bits.Rem64(0, u.hi, by), u.lo, by)
// }
if u.hi < by {
_, r.lo = bits.Div64(u.hi, u.lo, by)
} else {
_, r.lo = bits.Div64(0, u.hi, by)
_, r.lo = bits.Div64(r.lo, u.lo, by)
}
return r
}
func (u U128) Reverse() U128 {
return U128{hi: bits.Reverse64(u.lo), lo: bits.Reverse64(u.hi)}
}
func (u U128) ReverseBytes() U128 {
return U128{hi: bits.ReverseBytes64(u.lo), lo: bits.ReverseBytes64(u.hi)}
}
// To rotate u right by k bits, call u.RotateLeft(-k).
func (u U128) RotateLeft(k int) U128 {
s := uint(k) & (127)
if s > 64 {
s = 128 - s
l := 64 - s
return U128{
hi: u.hi>>s | u.lo<<l,
lo: u.lo>>s | u.hi<<l,
}
} else {
l := 64 - s
return U128{
hi: u.hi<<s | u.lo>>l,
lo: u.lo<<s | u.hi>>l,
}
}
}
func (u U128) LeadingZeros() uint {
if u.hi == 0 {
return uint(bits.LeadingZeros64(u.lo)) + 64
} else {
return uint(bits.LeadingZeros64(u.hi))
}
}
func (u U128) TrailingZeros() uint {
if u.lo == 0 {
return uint(bits.TrailingZeros64(u.hi)) + 64
} else {
return uint(bits.TrailingZeros64(u.lo))
}
}
// Hacker's delight 9-4, divlu:
func quo128by64(u1, u0, v uint64, vLeading0 uint) (q uint64) {
var b uint64 = 1 << 32
var un1, un0, vn1, vn0, q1, q0, un32, un21, un10, rhat, vs, left, right uint64
vs = v << vLeading0
vn1 = vs >> 32
vn0 = vs & 0xffffffff
if vLeading0 > 0 {
un32 = (u1 << vLeading0) | (u0 >> (64 - vLeading0))
un10 = u0 << vLeading0
} else {
un32 = u1
un10 = u0
}
un1 = un10 >> 32
un0 = un10 & 0xffffffff
q1 = un32 / vn1
rhat = un32 % vn1
left = q1 * vn0
right = (rhat << 32) | un1
again1:
if (q1 >= b) || (left > right) {
q1--
rhat += vn1
if rhat < b {
left -= vn0
right = (rhat << 32) | un1
goto again1
}
}
un21 = (un32 << 32) + (un1 - (q1 * vs))
q0 = un21 / vn1
rhat = un21 % vn1
left = q0 * vn0
right = (rhat << 32) | un0
again2:
if (q0 >= b) || (left > right) {
q0--
rhat += vn1
if rhat < b {
left -= vn0
right = (rhat << 32) | un0
goto again2
}
}
return (q1 << 32) | q0
}
// Hacker's delight 9-4, divlu:
func quorem128by64(u1, u0, v uint64, vLeading0 uint) (q, r uint64) {
var b uint64 = 1 << 32
var un1, un0, vn1, vn0, q1, q0, un32, un21, un10, rhat, left, right uint64
v <<= vLeading0
vn1 = v >> 32
vn0 = v & 0xffffffff
if vLeading0 > 0 {
un32 = (u1 << vLeading0) | (u0 >> (64 - vLeading0))
un10 = u0 << vLeading0
} else {
un32 = u1
un10 = u0
}
un1 = un10 >> 32
un0 = un10 & 0xffffffff
q1 = un32 / vn1
rhat = un32 % vn1
left = q1 * vn0
right = (rhat << 32) + un1
again1:
if (q1 >= b) || (left > right) {
q1--
rhat += vn1
if rhat < b {
left -= vn0
right = (rhat << 32) | un1
goto again1
}
}
un21 = (un32 << 32) + (un1 - (q1 * v))
q0 = un21 / vn1
rhat = un21 % vn1
left = q0 * vn0
right = (rhat << 32) | un0
again2:
if (q0 >= b) || (left > right) {
q0--
rhat += vn1
if rhat < b {
left -= vn0
right = (rhat << 32) | un0
goto again2
}
}
return (q1 << 32) | q0, ((un21 << 32) + (un0 - (q0 * v))) >> vLeading0
}
func quorem128by128(m, v U128, vHiLeading0, vLoLeading0 uint) (q, r U128) {
if v.hi == 0 {
if m.hi < v.lo {
q.lo, r.lo = quorem128by64(m.hi, m.lo, v.lo, vLoLeading0)
return q, r
} else {
q.hi = m.hi / v.lo
r.hi = m.hi % v.lo
q.lo, r.lo = quorem128by64(r.hi, m.lo, v.lo, vLoLeading0)
r.hi = 0
return q, r
}
} else {
v1 := v.Lsh(vHiLeading0)
u1 := m.Rsh(1)
var q1 U128
q1.lo = quo128by64(u1.hi, u1.lo, v1.hi, vLoLeading0)
q1 = q1.Rsh(63 - vHiLeading0)
if q1.hi|q1.lo != 0 {
q1 = q1.Dec()
}
q = q1
q1 = q1.Mul(v)
r = m.Sub(q1)
if r.Cmp(v) >= 0 {
q = q.Inc()
r = r.Sub(v)
}
return q, r
}
}
func quorem128bin(u, by U128, uLeading0, byLeading0 uint) (q, r U128) {
shift := int(byLeading0 - uLeading0)
by = by.Lsh(uint(shift))
for {
// q << 1
q.hi = (q.hi << 1) | (q.lo >> 63)
q.lo = q.lo << 1
// performance tweak: simulate greater than or equal by hand-inlining "not less than".
if u.hi > by.hi || (u.hi == by.hi && u.lo >= by.lo) {
u = u.Sub(by)
q.lo |= 1
}
// by >> 1
by.lo = (by.lo >> 1) | (by.hi << 63)
by.hi = by.hi >> 1
if shift <= 0 {
break
}
shift--
}
r = u
return q, r
}
func quo128bin(u, by U128, uLeading0, byLeading0 uint) (q U128) {
shift := int(byLeading0 - uLeading0)
by = by.Lsh(uint(shift))
for {
// q << 1
q.hi = (q.hi << 1) | (q.lo >> 63)
q.lo = q.lo << 1
// u >= by
if u.hi > by.hi || (u.hi == by.hi && u.lo >= by.lo) {
u = u.Sub(by)
q.lo |= 1
}
// q >> 1
by.lo = (by.lo >> 1) | (by.hi << 63)
by.hi = by.hi >> 1
if shift <= 0 {
break
}
shift--
}
return q
}
func (u U128) MarshalText() ([]byte, error) {
return []byte(u.String()), nil
}
func (u *U128) UnmarshalText(bts []byte) (err error) {
v, _, err := U128FromString(string(bts))
if err != nil {
return err
}
*u = v
return nil
}
func (u U128) MarshalJSON() ([]byte, error) {
return []byte(`"` + u.String() + `"`), nil