-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dec.go
1105 lines (983 loc) · 23.9 KB
/
dec.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
// Copyright 2020 Denis Bernard <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package decimal
import (
"math/big"
"math/bits"
"sync"
)
// The following thresholds are hugely different from their counterparts
// in math/big.
// Operands that are shorter than decKaratsubaThreshold are multiplied using
// "grade school" multiplication; for longer operands the Karatsuba algorithm
// is used.
var decKaratsubaThreshold = 30 // computed by calibrate_test.go
// Operands that are shorter than decBasicSqrThreshold are squared using
// "grade school" multiplication; for operands longer than karatsubaSqrThreshold
// we use the Karatsuba algorithm optimized for x == y.
var decBasicSqrThreshold = 10 // computed by calibrate_test.go
var decKaratsubaSqrThreshold = 50 // computed by calibrate_test.go
// dec is an unsigned integer x of the form
//
// x = x[n-1]*_BD^(n-1) + x[n-2]*_BD^(n-2) + ... + x[1]*_BD + x[0]
//
// with 0 <= x[i] < _B and 0 <= i < n is stored in a slice of length n,
// with the digits x[i] as the slice elements.
//
// A number is normalized if the slice contains no leading 0 digits.
// During arithmetic operations, denormalized values may occur but are
// always normalized before returning the final result. The normalized
// representation of 0 is the empty or nil slice (length = 0).
type dec []Word
var (
decOne = dec{1}
decTen = dec{10}
)
func (z dec) clear() {
for i := range z {
z[i] = 0
}
}
func (z dec) norm() dec {
i := len(z)
for i > 0 && z[i-1] == 0 {
i--
}
return z[0:i]
}
// digits returns the number of digits of x.
func (x dec) digits() uint {
if i := len(x) - 1; i >= 0 {
return uint(i*_DW) + decDigits(uint(x[i]))
}
return 0
}
func (x dec) trailingZeroDigits() uint {
if len(x) == 0 {
return 0
}
var i uint
for x[i] == 0 {
i++
}
// x[i] != 0
return i*_DW + trailingZeroDigits(uint(x[i]))
}
func (x dec) digit(i uint) uint {
j, i := bits.Div(0, i, _DW)
if j >= uint(len(x)) {
return 0
}
// 0 <= j < len(x)
return uint(x[j]/pow10(i)) % 10
}
func (z dec) make(n int) dec {
if n <= cap(z) {
return z[:n] // reuse z
}
if n == 1 {
// Most decs start small and stay that way; don't over-allocate.
return make(dec, 1)
}
// Choosing a good value for e has significant performance impact
// because it increases the chance that a value can be reused.
const e = 4 // extra capacity
return make(dec, n, n+e)
}
func (z dec) set(x dec) dec {
z = z.make(len(x))
copy(z, x)
return z
}
func (z dec) setWord(x Word) dec {
if x == 0 {
return z[:0]
}
z = z.make(1)
z[0] = x
return z
}
func (z dec) setUint64(x uint64) dec {
if w := Word(x); uint64(w) == x && w < _DB {
return z.setWord(w)
}
// x could be a 2 to 3 words value
z = z.make(int(decDigits64(x)+_DW-1) / _DW)
for i := 0; i < len(z); i++ {
hi, lo := bits.Div64(0, x, _DB)
z[i] = Word(lo)
x = hi
}
return z.norm()
}
// toUint64 returns the low 64 bits of z or MaxUint64 and true if z <= MaxUint64.
func (x dec) toUint64() (uint64, bool) {
// using a decToNat style loop would modify x
// so we unroll the loop and cache the values.
if _W == 64 {
var lo, hi Word
switch l := len(x); l {
case 2:
lo = x[1]
fallthrough
case 1:
hi, lo = mulAddWWW_g(lo, _DB, x[0])
fallthrough
case 0:
return uint64(lo), hi == 0
default:
return ^uint64(0), false
}
}
var z2, z1, z0, r, lo Word
switch l := len(x); l {
case 3:
z2 = x[2]
fallthrough
case 2:
z1 = x[1]
fallthrough
case 1:
z0 = x[0]
case 0:
return 0, true
default:
return ^uint64(0), false
}
z1, r = mulAddWWW_g(z2, _DB, z1)
z0, r = mulAddWWW_g(r, _DB, z0)
lo = r // low 32 bits
z0, r = mulAddWWW_g(z1, _DB, z0)
return uint64(r)<<32 | uint64(lo), z0 == 0
}
func decToNat(z []big.Word, x dec) []big.Word {
if len(x) == 0 {
return z[:0]
}
if len(x) == 1 {
z = makeNat(z, 1)
z[0] = big.Word(x[0])
return z
}
// bits = x.digits() * Log(10) / Log(2) + 1
// words = (bits + _W - 1)/_W
z = makeNat(z, (int(float64(x.digits())*log2_10)+_W)/_W)
zz := dec(nil).set(x)
for i := 0; i < len(z); i++ {
// r = zz & _B; zz = zz >> _W
var r Word
for j := len(zz) - 1; j >= 0; j-- {
zz[j], r = mulAddWWW_g(r, _DB, zz[j])
}
zz = zz.norm()
z[i] = big.Word(r)
}
// normalize
i := len(z)
for i > 0 && z[i-1] == 0 {
i--
}
return z[0:i]
}
// setNat sets z = x.mant
func (z dec) setNat(x []big.Word) dec {
// here we cannot directly copy(b, bb) because big.Word != decimal.Word.
b := make([]Word, len(x))
for i := 0; i < len(b) && i < len(x); i++ {
b[i] = Word(x[i])
}
for i := 0; i < len(z); i++ {
z[i] = divWVW(b, 0, b, _DB)
}
z = z.norm()
return z
}
// sticky returns 1 if there's a non zero digit within the
// i least significant digits, otherwise it returns 0.
func (x dec) sticky(i uint) uint {
j, i := bits.Div(0, i, _DW)
if j >= uint(len(x)) {
if len(x) == 0 {
return 0
}
return 1
}
// 0 <= j < len(x)
for _, x := range x[:j] {
if x != 0 {
return 1
}
}
if x[j]%pow10(i) != 0 {
return 1
}
return 0
}
func (z dec) add(x, y dec) dec {
m := len(x)
n := len(y)
switch {
case m < n:
return z.add(y, x)
case m == 0:
// n == 0 because m >= n; result is 0
return z[:0]
case n == 0:
// result is x
return z.set(x)
}
// m > 0
z = z.make(m + 1)
c := add10VV(z[0:n], x, y)
if m > n {
c = add10VW(z[n:m], x[n:], c)
}
z[m] = c
return z.norm()
}
func (z dec) sub(x, y dec) dec {
m := len(x)
n := len(y)
switch {
case m < n:
panic("underflow")
case m == 0:
// n == 0 because m >= n; result is 0
return z[:0]
case n == 0:
// result is x
return z.set(x)
}
// m > 0
z = z.make(m)
c := sub10VV(z[0:n], x, y)
if m > n {
c = sub10VW(z[n:], x[n:], c)
}
if c != 0 {
panic("underflow")
}
return z.norm()
}
func (x dec) cmp(y dec) (r int) {
m := len(x)
n := len(y)
if m != n || m == 0 {
switch {
case m < n:
r = -1
case m > n:
r = 1
}
return
}
i := m - 1
for i > 0 && x[i] == y[i] {
i--
}
switch {
case x[i] < y[i]:
r = -1
case x[i] > y[i]:
r = 1
}
return
}
// q = (x-r)/y, with 0 <= r < y
func (z dec) divW(x dec, y Word) (q dec, r Word) {
m := len(x)
switch {
case y == 0:
panic("division by zero")
case y == 1:
q = z.set(x) // result is x
return
case m == 0:
q = z[:0] // result is 0
return
}
// m > 0
z = z.make(m)
r = div10VWW(z, x, y, 0)
q = z.norm()
return
}
func (z dec) div(z2, u, v dec) (q, r dec) {
if len(v) == 0 {
panic("division by zero")
}
if u.cmp(v) < 0 {
q = z[:0]
r = z2.set(u)
return
}
if len(v) == 1 {
var r2 Word
q, r2 = z.divW(u, v[0])
r = z2.setWord(r2)
return
}
q, r = z.divLarge(z2, u, v)
return
}
// getDec returns a *dec of len n. The contents may not be zero.
// The pool holds *dec to avoid allocation when converting to interface{}.
func getDec(n int) *dec {
var z *dec
if v := decPool.Get(); v != nil {
z = v.(*dec)
}
if z == nil {
z = new(dec)
}
*z = z.make(n)
return z
}
func putDec(x *dec) {
decPool.Put(x)
}
var decPool sync.Pool
// q = (uIn-r)/vIn, with 0 <= r < vIn
// Uses z as storage for q, and u as storage for r if possible.
// See Knuth, Volume 2, section 4.3.1, Algorithm D.
// Preconditions:
// len(vIn) >= 2
// len(uIn) >= len(vIn)
// u must not alias z
func (z dec) divLarge(u, uIn, vIn dec) (q, r dec) {
n := len(vIn)
m := len(uIn)
// D1.
d := _DB / (vIn[n-1] + 1)
// do not modify vIn, it may be used by another goroutine simultaneously
vp := getDec(n)
v := *vp
mulAdd10VWW(v, vIn, d, 0)
// u may safely alias uIn or vIn, the value of uIn is used to set u and vIn was already used
u = u.make(m + 1)
u[m] = mulAdd10VWW(u[:m], uIn, d, 0)
// z may safely alias uIn or vIn, both values were used already
if alias(z, u) {
z = nil // z is an alias for u - cannot reuse
}
q = z.make(m - n + 1)
if n < divRecursiveThreshold {
q.divBasic(u, v)
} else {
q.divRecursive(u, v)
}
putDec(vp)
q = q.norm()
r, _ = u.divW(u, d)
r = r.norm()
return q, r
}
// divBasic performs word-by-word division of u by v.
// The quotient is written in pre-allocated q.
// The remainder overwrites input u.
//
// Precondition:
// - q is large enough to hold the quotient u / v
// which has a maximum length of len(u)-len(v)+1.
// - v[len(v)-1] >= _DB/2
func (q dec) divBasic(u, v dec) {
n := len(v)
m := len(u) - n
qhatvp := getDec(n + 1)
qhatv := *qhatvp
// D2.
vn1 := v[n-1]
for j := m; j >= 0; j-- {
// D3.
qhat := Word(_DMax)
var ujn Word
if j+n < len(u) {
ujn = u[j+n]
}
if ujn != vn1 {
var rhat Word
qhat, rhat = div10WW(ujn, u[j+n-1], vn1)
// x1 | x2 = q̂v_{n-2}
vn2 := v[n-2]
x1, x2 := mul10WW(qhat, vn2)
// test if q̂v_{n-2} > br̂ + u_{j+n-2}
ujn2 := u[j+n-2]
for greaterThan(x1, x2, rhat, ujn2) {
qhat--
prevRhat := rhat
rhat += vn1
// v[n-1] >= 0, so this tests for overflow.
if rhat < prevRhat {
break
}
x1, x2 = mul10WW(qhat, vn2)
}
}
// D4.
// Compute the remainder u - (q̂*v) * 10**(_DW*j).
// The subtraction may overflow if q̂ estimate was off by one.
qhatv[n] = mulAdd10VWW(qhatv[0:n], v, qhat, 0)
qhl := len(qhatv)
if j+qhl > len(u) && qhatv[n] == 0 {
qhl--
}
c := sub10VV(u[j:j+qhl], u[j:], qhatv)
if c != 0 {
c := add10VV(u[j:j+n], u[j:], v)
// If n == qhl, the carry from subVV and the carry from addVV
// cancel out and don't affect u[j+n].
if n < qhl {
u[j+n] += c
}
qhat--
}
if j == m && m == len(q) && qhat == 0 {
continue
}
q[j] = qhat
}
putDec(qhatvp)
}
// modW returns x % d.
func (x dec) modW(d Word) (r Word) {
for i := len(x) - 1; i >= 0; i-- {
_, r = div10WW(r, x[i], d)
}
return r
}
func (z dec) mulAddWW(x dec, y, r Word) dec {
m := len(x)
if m == 0 || y == 0 {
return z.setWord(r) // result is r
}
// m > 0
z = z.make(m + 1)
z[m] = mulAdd10VWW(z[0:m], x, y, r)
return z.norm()
}
// z = x * 10**s
func (z dec) shl(x dec, s uint) dec {
if s == 0 {
if same(z, x) {
return z
}
if !alias(z, x) {
return z.set(x)
}
}
m := len(x)
if m == 0 {
return z[:0]
}
// m > 0
n := m + int(s/_DW)
z = z.make(n + 1)
z[n] = shl10VU(z[n-m:n], x, s%_DW)
z[0 : n-m].clear()
return z.norm()
}
// z = x >> s
func (z dec) shr(x dec, s uint) dec {
if s == 0 {
if same(z, x) {
return z
}
if !alias(z, x) {
return z.set(x)
}
}
m := len(x)
n := m - int(s/_DW)
if n <= 0 {
return z[:0]
}
// n > 0
z = z.make(n)
shr10VU(z, x[m-n:], s%_DW)
return z.norm()
}
// Operands that are shorter than basicSqrThreshold are squared using
// "grade school" multiplication; for operands longer than karatsubaSqrThreshold
// we use the Karatsuba algorithm optimized for x == y.
// z = x*x
func (z dec) sqr(x dec) dec {
n := len(x)
switch {
case n == 0:
return z[:0]
case n == 1:
d := x[0]
z = z.make(2)
z[1], z[0] = mul10WW(d, d)
return z.norm()
}
if alias(z, x) {
z = nil // z is an alias for x - cannot reuse
}
if n < decBasicSqrThreshold {
z = z.make(2 * n)
decBasicMul(z, x, x)
return z.norm()
}
if n < decKaratsubaSqrThreshold {
z = z.make(2 * n)
decBasicSqr(z, x)
return z.norm()
}
// Use Karatsuba multiplication optimized for x == y.
// The algorithm and layout of z are the same as for mul.
// z = (x1*b + x0)^2 = x1^2*b^2 + 2*x1*x0*b + x0^2
k := karatsubaLen(n, decKaratsubaSqrThreshold)
x0 := x[0:k]
z = z.make(max(6*k, 2*n))
decKaratsubaSqr(z, x0) // z = x0^2
z = z[0 : 2*n]
z[2*k:].clear()
if k < n {
tp := getDec(2 * k)
t := *tp
x0 := x0.norm()
x1 := x[k:]
t = t.mul(x0, x1)
decAddAt(z, t, k)
decAddAt(z, t, k) // z = 2*x1*x0*b + x0^2
t = t.sqr(x1)
decAddAt(z, t, 2*k) // z = x1^2*b^2 + 2*x1*x0*b + x0^2
putDec(tp)
}
return z.norm()
}
// basicSqr sets z = x*x and is asymptotically faster than basicMul
// by about a factor of 2, but slower for small arguments due to overhead.
// Requirements: len(x) > 0, len(z) == 2*len(x)
// The (non-normalized) result is placed in z.
func decBasicSqr(z, x dec) {
n := len(x)
tp := getDec(2 * n)
t := *tp // temporary variable to hold the products
t.clear()
z[1], z[0] = mul10WW(x[0], x[0]) // the initial square
for i := 1; i < n; i++ {
d := x[i]
// z collects the squares x[i] * x[i]
z[2*i+1], z[2*i] = mul10WW(d, d)
// t collects the products x[i] * x[j] where j < i
t[2*i] = addMul10VVW(t[i:2*i], x[0:i], d)
}
// t[2*n-1] = shlVU(t[1:2*n-1], t[1:2*n-1], 1) // double the j < i products
t[2*n-1] = mulAdd10VWW(t[1:2*n-1], t[1:2*n-1], 2, 0)
add10VV(z, z, t) // combine the result
putDec(tp)
}
// decKaratsubaSqr squares x and leaves the result in z.
// len(x) must be a power of 2 and len(z) >= 6*len(x).
// The (non-normalized) result is placed in z[0 : 2*len(x)].
//
// The algorithm and the layout of z are the same as for karatsuba.
func decKaratsubaSqr(z, x dec) {
n := len(x)
if n&1 != 0 || n < decKaratsubaSqrThreshold || n < 2 {
decBasicSqr(z[:2*n], x)
return
}
n2 := n >> 1
x1, x0 := x[n2:], x[0:n2]
decKaratsubaSqr(z, x0)
decKaratsubaSqr(z[n:], x1)
// s = sign(xd*yd) == -1 for xd != 0; s == 1 for xd == 0
xd := z[2*n : 2*n+n2]
if sub10VV(xd, x1, x0) != 0 {
sub10VV(xd, x0, x1)
}
p := z[n*3:]
decKaratsubaSqr(p, xd)
r := z[n*4:]
copy(r, z[:n*2])
decKaratsubaAdd(z[n2:], r, n)
decKaratsubaAdd(z[n2:], r[n:], n)
decKaratsubaSub(z[n2:], p, n) // s == -1 for p != 0; s == 1 for p == 0
}
// decBasicMul multiplies x and y and leaves the result in z.
// The (non-normalized) result is placed in z[0 : len(x) + len(y)].
func decBasicMul(z, x, y dec) {
z[0 : len(x)+len(y)].clear() // initialize z
for i, d := range y {
if d != 0 {
z[len(x)+i] = addMul10VVW(z[i:i+len(x)], x, d)
}
}
}
func (z dec) mul(x, y dec) dec {
m := len(x)
n := len(y)
switch {
case m < n:
return z.mul(y, x)
case m == 0 || n == 0:
return z[:0]
case n == 1:
return z.mulAddWW(x, y[0], 0)
}
// m >= n > 1
// determine if z can be reused
if alias(z, x) || alias(z, y) {
z = nil // z is an alias for x or y - cannot reuse
}
// use basic multiplication if the numbers are small
if n < decKaratsubaThreshold {
z = z.make(m + n)
decBasicMul(z, x, y)
return z.norm()
}
// m >= n && n >= karatsubaThreshold && n >= 2
// determine Karatsuba length k such that
//
// x = xh*b + x0 (0 <= x0 < b)
// y = yh*b + y0 (0 <= y0 < b)
// b = 10**(_DW*k) ("base" of digits xi, yi)
//
k := karatsubaLen(n, decKaratsubaThreshold)
// k <= n
// // multiply x0 and y0 via Karatsuba
x0 := x[0:k] // x0 is not normalized
y0 := y[0:k] // y0 is not normalized
z = z.make(max(6*k, m+n)) // enough space for karatsuba of x0*y0 and full result of x*y
decKaratsuba(z, x0, y0)
z = z[0 : m+n] // z has final length but may be incomplete
z[2*k:].clear() // upper portion of z is garbage (and 2*k <= m+n since k <= n <= m)
// If xh != 0 or yh != 0, add the missing terms to z. For
//
// xh = xi*b^i + ... + x2*b^2 + x1*b (0 <= xi < b)
// yh = y1*b (0 <= y1 < b)
//
// the missing terms are
//
// x0*y1*b and xi*y0*b^i, xi*y1*b^(i+1) for i > 0
//
// since all the yi for i > 1 are 0 by choice of k: If any of them
// were > 0, then yh >= b^2 and thus y >= b^2. Then k' = k*2 would
// be a larger valid threshold contradicting the assumption about k.
//
if k < n || m != n {
tp := getDec(3 * k)
t := *tp
// add x0*y1*b
x0 := x0.norm()
y1 := y[k:] // y1 is normalized because y is
t = t.mul(x0, y1) // update t so we don't lose t's underlying array
decAddAt(z, t, k)
// add xi*y0<<i, xi*y1*b<<(i+k)
y0 := y0.norm()
for i := k; i < len(x); i += k {
xi := x[i:]
if len(xi) > k {
xi = xi[:k]
}
xi = xi.norm()
t = t.mul(xi, y0)
decAddAt(z, t, i)
t = t.mul(xi, y1)
decAddAt(z, t, i+k)
}
putDec(tp)
}
return z.norm()
}
// divRecursive performs word-by-word division of u by v.
// The quotient is written in pre-allocated z.
// The remainder overwrites input u.
//
// Precondition:
// - len(z) >= len(u)-len(v)
//
// See Burnikel, Ziegler, "Fast Recursive Division", Algorithm 1 and 2.
// TODO(db47h): review https://pure.mpg.de/rest/items/item_1819444_4/component/file_2599480/content
// and make sure that when calling divBasic, the preconditions are met.
func (z dec) divRecursive(u, v dec) {
// Recursion depth is less than 2 log2(len(v))
// Allocate a slice of temporaries to be reused across recursion.
recDepth := 2 * bits.Len(uint(len(v)))
// large enough to perform Karatsuba on operands as large as v
tmp := getDec(3 * len(v))
temps := make([]*dec, recDepth)
z.clear()
z.divRecursiveStep(u, v, 0, tmp, temps)
for _, n := range temps {
if n != nil {
putDec(n)
}
}
putDec(tmp)
}
// divRecursiveStep computes the division of u by v.
// - z must be large enough to hold the quotient
// - the quotient will overwrite z
// - the remainder will overwrite u
func (z dec) divRecursiveStep(u, v dec, depth int, tmp *dec, temps []*dec) {
u = u.norm()
v = v.norm()
if len(u) == 0 {
z.clear()
return
}
n := len(v)
if n < divRecursiveThreshold {
z.divBasic(u, v)
return
}
m := len(u) - n
if m < 0 {
return
}
// Produce the quotient by blocks of B words.
// Division by v (length n) is done using a length n/2 division
// and a length n/2 multiplication for each block. The final
// complexity is driven by multiplication complexity.
B := n / 2
// Allocate a nat for qhat below.
if temps[depth] == nil {
temps[depth] = getDec(n)
} else {
*temps[depth] = temps[depth].make(B + 1)
}
j := m
for j > B {
// Divide u[j-B:j+n] by vIn. Keep remainder in u
// for next block.
//
// The following property will be used (Lemma 2):
// if u = u1 << s + u0
// v = v1 << s + v0
// then floor(u1/v1) >= floor(u/v)
//
// Moreover, the difference is at most 2 if len(v1) >= len(u/v)
// We choose s = B-1 since len(v)-B >= B+1 >= len(u/v)
s := (B - 1)
// Except for the first step, the top bits are always
// a division remainder, so the quotient length is <= n.
uu := u[j-B:]
qhat := *temps[depth]
qhat.clear()
qhat.divRecursiveStep(uu[s:B+n], v[s:], depth+1, tmp, temps)
qhat = qhat.norm()
// Adjust the quotient:
// u = u_h << s + u_l
// v = v_h << s + v_l
// u_h = q̂ v_h + rh
// u = q̂ (v - v_l) + rh << s + u_l
// After the above step, u contains a remainder:
// u = rh << s + u_l
// and we need to subtract q̂ v_l
//
// But it may be a bit too large, in which case q̂ needs to be smaller.
qhatv := tmp.make(3 * n)
qhatv.clear()
qhatv = qhatv.mul(qhat, v[:s])
for i := 0; i < 2; i++ {
e := qhatv.cmp(uu.norm())
if e <= 0 {
break
}
sub10VW(qhat, qhat, 1)
c := sub10VV(qhatv[:s], qhatv[:s], v[:s])
if len(qhatv) > s {
sub10VW(qhatv[s:], qhatv[s:], c)
}
decAddAt(uu[s:], v[s:], 0)
}
if qhatv.cmp(uu.norm()) > 0 {
panic("impossible")
}
c := sub10VV(uu[:len(qhatv)], uu[:len(qhatv)], qhatv)
if c > 0 {
sub10VW(uu[len(qhatv):], uu[len(qhatv):], c)
}
decAddAt(z, qhat, j-B)
j -= B
}
// Now u < (v<<B), compute lower bits in the same way.
// Choose shift = B-1 again.
s := B
qhat := *temps[depth]
qhat.clear()
qhat.divRecursiveStep(u[s:].norm(), v[s:], depth+1, tmp, temps)
qhat = qhat.norm()
qhatv := tmp.make(3 * n)
qhatv.clear()
qhatv = qhatv.mul(qhat, v[:s])
// Set the correct remainder as before.
for i := 0; i < 2; i++ {
if e := qhatv.cmp(u.norm()); e > 0 {
sub10VW(qhat, qhat, 1)
c := sub10VV(qhatv[:s], qhatv[:s], v[:s])
if len(qhatv) > s {
sub10VW(qhatv[s:], qhatv[s:], c)
}
decAddAt(u[s:], v[s:], 0)
}
}
if qhatv.cmp(u.norm()) > 0 {
panic("impossible")
}
c := sub10VV(u[0:len(qhatv)], u[0:len(qhatv)], qhatv)
if c > 0 {
c = sub10VW(u[len(qhatv):], u[len(qhatv):], c)
}
if c > 0 {
panic("impossible")
}
// Done!
decAddAt(z, qhat.norm(), 0)
}
// addAt implements z += x*10**(_WD*i); z must be long enough.
// (we don't use dec.add because we need z to stay the same
// slice, and we don't need to normalize z after each addition)
func decAddAt(z, x dec, i int) {
if n := len(x); n > 0 {
if c := add10VV(z[i:i+n], z[i:], x); c != 0 {
j := i + n
if j < len(z) {
add10VW(z[j:], z[j:], c)
}
}
}
}
// Fast version of z[0:n+n>>1].add(z[0:n+n>>1], x[0:n]) w/o bounds checks.
// Factored out for readability - do not use outside karatsuba.
func decKaratsubaAdd(z, x dec, n int) {
if c := add10VV(z[0:n], z, x); c != 0 {
add10VW(z[n:n+n>>1], z[n:], c)
}
}
// Like karatsubaAdd, but does subtract.
func decKaratsubaSub(z, x dec, n int) {
if c := sub10VV(z[0:n], z, x); c != 0 {
sub10VW(z[n:n+n>>1], z[n:], c)
}
}
// karatsuba multiplies x and y and leaves the result in z.
// Both x and y must have the same length n and n must be a
// power of 2. The result vector z must have len(z) >= 6*n.
// The (non-normalized) result is placed in z[0 : 2*n].
func decKaratsuba(z, x, y dec) {
n := len(y)
// Switch to basic multiplication if numbers are odd or small.
// (n is always even if karatsubaThreshold is even, but be
// conservative)
if n&1 != 0 || n < decKaratsubaThreshold || n < 2 {
decBasicMul(z, x, y)
return
}
// n&1 == 0 && n >= karatsubaThreshold && n >= 2
// Karatsuba multiplication is based on the observation that
// for two numbers x and y with:
//
// x = x1*b + x0
// y = y1*b + y0
//
// the product x*y can be obtained with 3 products z2, z1, z0
// instead of 4:
//
// x*y = x1*y1*b*b + (x1*y0 + x0*y1)*b + x0*y0
// = z2*b*b + z1*b + z0
//
// with:
//
// xd = x1 - x0
// yd = y0 - y1
//
// z1 = xd*yd + z2 + z0
// = (x1-x0)*(y0 - y1) + z2 + z0
// = x1*y0 - x1*y1 - x0*y0 + x0*y1 + z2 + z0