forked from erlang/otp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbig.c
More file actions
3342 lines (2982 loc) · 73.4 KB
/
Copy pathbig.c
File metadata and controls
3342 lines (2982 loc) · 73.4 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
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
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2023. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "big.h"
#include "error.h"
#include "bif.h"
#include "erl_binary.h"
#define ZERO_DIGITS(v, sz) do { \
dsize_t _t_sz = sz; \
ErtsDigit* _t_v = v; \
while(_t_sz--) *_t_v++ = 0; \
} while(0)
#define MOVE_DIGITS(dst, src, sz) do { \
dsize_t _t_sz = sz; \
ErtsDigit* _t_dst; \
ErtsDigit* _t_src; \
if (dst < src) { \
_t_dst = dst; \
_t_src = src; \
while(_t_sz--) *_t_dst++ = *_t_src++; \
} \
else if (dst > src) { \
_t_dst = (dst)+((sz)-1); \
_t_src = (src)+((sz)-1); \
while(_t_sz--) *_t_dst-- = *_t_src--; \
} \
} while(0)
/* add a and b with carry out */
#define DSUM(a,b,c,s) do { \
ErtsDigit ___xr = (a); \
ErtsDigit ___yr = (b); \
___xr = ___yr + ___xr; \
s = ___xr; \
c = (___xr < ___yr); \
} while(0)
#define DSUBb(a,b,r,d) do { \
ErtsDigit ___cr = (r); \
ErtsDigit ___xr = (a); \
ErtsDigit ___yr = (b)+___cr; \
___cr = (___yr < ___cr); \
___yr = ___xr - ___yr; \
___cr += (___yr > ___xr); \
d = ___yr; \
r = ___cr; \
} while(0)
#define DSUB(a,b,r,d) do { \
ErtsDigit ___xr = (a); \
ErtsDigit ___yr = (b); \
___yr = ___xr - ___yr; \
r = (___yr > ___xr); \
d = ___yr; \
} while(0)
/* type a constant as a ErtsDigit - to get shifts correct */
#define DCONST(n) ((ErtsDigit)(n))
/*
* BIG_HAVE_DOUBLE_DIGIT is defined if we have defined
* the type ErtsDoubleDigit which MUST have
* sizeof(ErtsDoubleDigit) >= sizeof(ErtsDigit)
*/
#ifdef BIG_HAVE_DOUBLE_DIGIT
/* ErtsDoubleDigit => ErtsDigit */
#define DLOW(x) ((ErtsDigit)(x))
#define DHIGH(x) ((ErtsDigit)(((ErtsDoubleDigit)(x)) >> D_EXP))
/* ErtsDigit => ErtsDoubleDigit */
#define DLOW2HIGH(x) (((ErtsDoubleDigit)(x)) << D_EXP)
#define DDIGIT(a1,a0) (DLOW2HIGH(a1) + (a0))
#define DMULc(a,b,c,p) do { \
ErtsDoubleDigit _t = ((ErtsDoubleDigit)(a))*(b) + (c); \
p = DLOW(_t); \
c = DHIGH(_t); \
} while(0)
#define DMUL(a,b,c1,c0) do { \
ErtsDoubleDigit _t = ((ErtsDoubleDigit)(a))*(b); \
c0 = DLOW(_t); \
c1 = DHIGH(_t); \
} while(0)
#define DDIV(a1,a0,b,q) do { \
ErtsDoubleDigit _t = DDIGIT((a1),(a0)); \
q = _t / (b); \
} while(0)
#define DDIV2(a1,a0,b1,b0,q) do { \
ErtsDoubleDigit _t = DDIGIT((a1),(a0)); \
q = _t / DDIGIT((b1),(b0)); \
} while(0)
#define DREM(a1,a0,b,r) do { \
ErtsDoubleDigit _t = DDIGIT((a1),(a0)); \
r = _t % (b); \
} while(0)
/* add a and b with carry in + out */
#define DSUMc(a,b,c,s) do { \
ErtsDoubleDigit _t = (ErtsDoubleDigit)(a) + (b) + (c); \
s = DLOW(_t); \
c = DHIGH(_t); \
} while(0)
#else
/* If we do not have double digit then we have some more work to do */
#define H_EXP (D_EXP >> 1)
#define LO_MASK ((ErtsDigit)((DCONST(1) << H_EXP)-1))
#define HI_MASK ((ErtsDigit)(LO_MASK << H_EXP))
#define DGT(a,b) ((a)>(b))
#define DEQ(a,b) ((a)==(b))
#define D2GT(a1,a0,b1,b0) (DGT(a1,b1) || (((a1)==(b1)) && DGT(a0,b0)))
#define D2EQ(a1,a0,b1,b0) (DEQ(a1,b1) && DEQ(a0,b0))
#define D2LT(a1,a0,b1,b0) D2GT(b1,b0,a1,a0)
#define D2GTE(a1,a0,b1,b0) (!D2LT(a1,a0,b1,b0))
#define D2LTE(a1,a0,b1,b0) (!D2GT(a1,a0,b1,b0))
/* Add (A+B), A=(a1B+a0) B=(b1B+b0) */
#define D2ADD(a1,a0,b1,b0,c1,c0) do { \
ErtsDigit __ci = 0; \
DSUM(a0,b0,__ci,c0); \
DSUMc(a1,b1,__ci,c1); \
} while(0)
/* Subtract (A-B), A=(a1B+a0), B=(b1B+b0) (A>=B) */
#define D2SUB(a1,a0,b1,b0,c1,c0) do { \
ErtsDigit __bi; \
DSUB(a0,b0,__bi,c0); \
DSUBb(a1,b1,__bi,c1); \
} while(0)
/* Left shift (multiply by 2) (A <<= 1 where A=a1*B+a0) */
#define D2LSHIFT1(a1,a0) do { \
a1 = ((a0) >> (D_EXP-1)) | ((a1)<<1); \
a0 = (a0) << 1; \
} while(0)
/* Right shift (divide by 2) (A >>= 1 where A=a1*B+a0) */
#define D2RSHIFT1(a1,a0) do { \
a0 = (((a1) & 1) << (D_EXP-1)) | ((a0)>>1); \
a1 = ((a1) >> 1); \
} while(0)
/* Calculate a*b + d1 and store double prec result in d1, d0 */
#define DMULc(a,b,d1,d0) do { \
ErtsHalfDigit __a0 = (a); \
ErtsHalfDigit __a1 = ((a) >> H_EXP); \
ErtsHalfDigit __b0 = (b); \
ErtsHalfDigit __b1 = ((b) >> H_EXP); \
ErtsDigit __a0b0 = (ErtsDigit)__a0*__b0; \
ErtsDigit __a0b1 = (ErtsDigit)__a0*__b1; \
ErtsDigit __a1b0 = (ErtsDigit)__a1*__b0; \
ErtsDigit __a1b1 = (ErtsDigit)__a1*__b1; \
ErtsDigit __p0,__p1,__p2,__c0; \
DSUM(__a0b0,d1,__c0,__p0); \
DSUM((__c0<<H_EXP),(__p0>>H_EXP),__p2,__p1); \
DSUM(__p1,__a0b1,__c0,__p1); \
__p2 += __c0; \
DSUM(__p1,__a1b0,__c0,__p1); \
__p2 += __c0; \
DSUM(__p1,__a1b1<<H_EXP,__c0,__p1); \
__p2 += __c0; \
DSUM(__a1b1, (__p2<<H_EXP),__c0,__p2); \
d1 = (__p2 & HI_MASK) | (__p1 >> H_EXP); \
d0 = (__p1 << H_EXP) | (__p0 & LO_MASK); \
} while(0)
#define DMUL(a,b,d1,d0) do { \
ErtsDigit _ds = 0; \
DMULc(a,b,_ds,d0); \
d1 = _ds; \
} while(0)
/* Calculate a*(Bb1 + b0) + d2 = a*b1B + a*b0 + d2 */
#define D2MULc(a,b1,b0,d2,d1,d0) do { \
DMULc(a, b0, d2, d0); \
DMULc(a, b1, d2, d1); \
} while(0)
/* Calculate s in a = 2^s*a1 */
/* NOTE since D2PF is used by other macros variables is prefixed bt __ */
#if D_EXP == 64
#define D2PF(a, s) do { \
ErtsDigit __x = (a); \
int __s = 0; \
if (__x <= 0x00000000FFFFFFFF) { __s += 32; __x <<= 32; } \
if (__x <= 0x0000FFFFFFFFFFFF) { __s += 16; __x <<= 16; } \
if (__x <= 0x00FFFFFFFFFFFFFF) { __s += 8; __x <<= 8; } \
if (__x <= 0x0FFFFFFFFFFFFFFF) { __s += 4; __x <<= 4; } \
if (__x <= 0x3FFFFFFFFFFFFFFF) { __s += 2; __x <<= 2; } \
if (__x <= 0x7FFFFFFFFFFFFFFF) { __s += 1; } \
s = __s; \
} while(0)
#elif D_EXP == 32
#define D2PF(a, s) do { \
ErtsDigit __x = (a); \
int __s = 0; \
if (__x <= 0x0000FFFF) { __s += 16; __x <<= 16; } \
if (__x <= 0x00FFFFFF) { __s += 8; __x <<= 8; } \
if (__x <= 0x0FFFFFFF) { __s += 4; __x <<= 4; } \
if (__x <= 0x3FFFFFFF) { __s += 2; __x <<= 2; } \
if (__x <= 0x7FFFFFFF) { __s += 1; } \
s = __s; \
} while(0)
#elif D_EXP == 16
#define D2PF(a, s) do { \
ErtsDigit __x = (a); \
int __s = 0; \
if (__x <= 0x00FF) { __s += 8; __x <<= 8; } \
if (__x <= 0x0FFF) { __s += 4; __x <<= 4; } \
if (__x <= 0x3FFF) { __s += 2; __x <<= 2; } \
if (__x <= 0x7FFF) { __s += 1; } \
s = __s; \
} while(0)
#elif D_EXP == 8
#define D2PF(a, s) do { \
ErtsDigit __x = (a); \
int __s = 0; \
if (__x <= 0x0F) { __s += 4; __x <<= 4; } \
if (__x <= 0x3F) { __s += 2; __x <<= 2; } \
if (__x <= 0x7F) { __s += 1; } \
s = _s; \
} while(0)
#endif
/* Calculate q = (a1B + a0) / b, assume a1 < b */
#define DDIVREM(a1,a0,b,q,r) do { \
ErtsDigit _a1 = (a1); \
ErtsDigit _a0 = (a0); \
ErtsDigit _b = (b); \
ErtsHalfDigit _un1, _un0; \
ErtsHalfDigit _vn1, _vn0; \
ErtsDigit _q1, _q0; \
ErtsDigit _un32, _un21, _un10; \
ErtsDigit _rh; \
Sint _s; \
D2PF(_b, _s); \
_b = _b << _s; \
_vn1 = _b >> H_EXP; \
_vn0 = _b & LO_MASK; \
/* If needed to avoid undefined behaviour */ \
if (_s) _un32 = (_a1 << _s) | ((_a0>>(D_EXP-_s)) & (-_s >> (D_EXP-1))); \
else _un32 = _a1; \
_un10 = _a0 << _s; \
_un1 = _un10 >> H_EXP; \
_un0 = _un10 & LO_MASK; \
_q1 = _un32/_vn1; \
_rh = _un32 - _q1*_vn1; \
while ((_q1 >= (DCONST(1)<<H_EXP))||(_q1*_vn0 > (_rh<<H_EXP)+_un1)) { \
_q1--; \
_rh += _vn1; \
if (_rh >= (DCONST(1)<<H_EXP)) break; \
} \
_un21 = (_un32<<H_EXP) + _un1 - _q1*_b; \
_q0 = _un21/_vn1; \
_rh = _un21 - _q0*_vn1; \
while ((_q0 >= (DCONST(1)<<H_EXP))||(_q0*_vn0 > ((_rh<<H_EXP)+_un0))) { \
_q0--; \
_rh += _vn1; \
if (_rh >= (DCONST(1)<<H_EXP)) break; \
} \
r = ((_un21<<H_EXP) + _un0 - _q0*_b) >> _s; \
q = (_q1<<H_EXP) + _q0; \
} while(0)
/* divide any a=(a1*B + a0) with b */
#define DDIVREM2(a1,a0,b,q1,q0,r) do { \
ErtsDigit __a1 = (a1); \
ErtsDigit __b = (b); \
q1 = __a1 / __b; \
DDIVREM(__a1 % __b, (a0), __b, q0, r); \
} while(0)
/* Calculate q = (a1B + a0) % b */
#define DREM(a1,a0,b,r) do { \
ErtsDigit __a1 = (a1); \
ErtsDigit __b = (b); \
ERTS_DECLARE_DUMMY(ErtsDigit __q0); \
DDIVREM((__a1 % __b), (a0), __b, __q0, r); \
} while(0)
#define DDIV(a1,a0,b,q) do { \
ERTS_DECLARE_DUMMY(ErtsDigit _tmp); \
DDIVREM(a1,a0,b,q,_tmp); \
} while(0)
/* Calculate q, r A = Bq+R when, assume A1 >= B */
#if (SIZEOF_VOID_P == 8)
#define QUOT_LIM 0x7FFFFFFFFFFFFFFF
#else
#define QUOT_LIM 0x7FFFFFFF
#endif
#define D2DIVREM(a1,a0,b1,b0,q0,r1,r0) do { \
ErtsDigit _a1 = (a1); \
ErtsDigit _a0 = (a0); \
ErtsDigit _b1 = (b1); \
ErtsDigit _b0 = (b0); \
ErtsDigit _q = 0; \
int _as = 1; \
while(D2GTE(_a1,_a0,_b1,_b0)) { \
ErtsDigit _q1; \
ErtsDigit _t2=0, _t1, _t0; \
if ((_b1 == 1) && (_a1 > 1)) \
_q1 = _a1 / 2; \
else if ((_a1 > QUOT_LIM) && (_b1 < _a1)) \
_q1 = _a1/(_b1+1); \
else \
_q1 = _a1/_b1; \
if (_as<0) \
_q -= _q1; \
else \
_q += _q1; \
D2MULc(_q1, _b1, _b0, _t2, _t1, _t0); \
ASSERT(_t2 == 0); \
if (D2GT(_t1,_t0,_a1,_a0)) { \
D2SUB(_t1,_t0,_a1,_a0,_a1,_a0); \
_as = -_as; \
} \
else { \
D2SUB(_a1,_a0,_t1,_t0,_a1,_a0); \
} \
} \
if (_as < 0) { \
_q--; \
D2SUB(_b1,_b0,_a1,_a0,_a1,_a0); \
} \
q0 = _q; \
r1 = _a1; \
r0 = _a0; \
} while(0)
/* Calculate q, r A = Bq+R when assume B>0 */
#define D2DIVREM_0(a1,a0,b1,b0,q1,q0,r1,r0) do { \
ErtsDigit _a1 = (a1); \
ErtsDigit _a0 = (a0); \
ErtsDigit _b1 = (b1); \
ErtsDigit _b0 = (b0); \
if (D2EQ(_a1,_a0,0,0)) { \
q1 = q0 = 0; \
r1 = r0 = 0; \
} \
else { \
ErtsDigit _res1 = 0; \
ErtsDigit _res0 = 0; \
ErtsDigit _d1 = 0; \
ErtsDigit _d0 = 1; \
ErtsDigit _e1 = (1 << (D_EXP-1)); \
ErtsDigit _e0 = 0; \
while(_e1 && !(_a1 & _e1)) \
_e1 >>= 1; \
if (_e1 == 0) { \
_e0 = (1 << (D_EXP-1)); \
while(_e0 && !(_a0 & _e0)) \
_e0 >>= 1; \
} \
if (D2GT(_b1,_b0,0,0)) { \
while(D2GT(_e1,_e0,_b1,_b0)) { \
D2LSHIFT1(_b1,_b0); \
D2LSHIFT1(_d1,_d0); \
} \
} \
do { \
if (!D2GT(_b1,_b0,_a1,_a0)) { \
D2SUB(_a1,_a0, _b1, _b0, _a1, _a0); \
D2ADD(_d1,_d0, _res1,_res0, _res1, _res0); \
} \
D2RSHIFT1(_b1,_b0); \
D2RSHIFT1(_d1,_d0); \
} while (!D2EQ(_d1,_d0,0,0)); \
r1 = _a1; \
r0 = _a0; \
q1 = _res1; \
q0 = _res0; \
} \
} while(0)
#define DDIV2(a1,a0,b1,b0,q) do { \
ERTS_DECLARE_DUMMY(ErtsDigit _tmp_r1); \
ERTS_DECLARE_DUMMY(ErtsDigit _tmp_r0); \
D2DIVREM(a1,a0,b1,b0,q,_tmp_r1,_tmp_r0); \
} while(0)
/* add a and b with carry in + out */
#define DSUMc(a,b,c,s) do { \
ErtsDigit ___cr = (c); \
ErtsDigit ___xr = (a)+(___cr); \
ErtsDigit ___yr = (b); \
___cr = (___xr < ___cr); \
___xr = ___yr + ___xr; \
___cr += (___xr < ___yr); \
s = ___xr; \
c = ___cr; \
} while(0)
#endif
/* Forward declaration of lookup tables (See below in this file) used in list to
* integer conversions for different bases. Also used in bignum printing.
*/
static const byte digits_per_sint_lookup[36-1];
static const byte digits_per_small_lookup[36-1];
static const Sint largest_power_of_base_lookup[36-1];
static const double lg2_lookup[36-1];
static ERTS_INLINE byte get_digits_per_signed_int(Uint base) {
return digits_per_sint_lookup[base-2];
}
static ERTS_INLINE byte get_digits_per_small(Uint base) {
return digits_per_small_lookup[base-2];
}
static ERTS_INLINE Sint get_largest_power_of_base(Uint base) {
return largest_power_of_base_lookup[base-2];
}
static ERTS_INLINE double lookup_log2(Uint base) {
return lg2_lookup[base - 2];
}
/*
** compare two number vectors
*/
static int I_comp(ErtsDigit* x, dsize_t xl, ErtsDigit* y, dsize_t yl)
{
if (xl < yl)
return -1;
else if (xl > yl)
return 1;
else {
if (x == y)
return 0;
x += (xl-1);
y += (yl-1);
while((xl > 0) && (*x == *y)) {
x--;
y--;
xl--;
}
if (xl == 0)
return 0;
return (*x < *y) ? -1 : 1;
}
}
/*
** Add digits in x and y and store them in r
** assumption: (xl >= yl)
*/
static dsize_t I_add(ErtsDigit* x, dsize_t xl, ErtsDigit* y, dsize_t yl, ErtsDigit* r)
{
dsize_t sz = xl;
register ErtsDigit yr, xr;
register ErtsDigit c = 0;
ASSERT(xl >= yl);
xl -= yl;
do {
xr = *x++;
yr = *y++;
DSUMc(xr, yr, c, xr);
*r++ = xr;
} while(--yl);
while(xl--) {
xr = *x++ + c;
c = (xr < c);
*r++ = xr;
}
if (c) {
*r = 1;
return sz+1;
}
return sz;
}
/*
** Add a digits in v1 and store result in vr
*/
static dsize_t D_add(ErtsDigit* x, dsize_t xl, ErtsDigit c, ErtsDigit* r)
{
dsize_t sz = xl;
register ErtsDigit xr;
while(xl--) {
xr = *x++ + c;
c = (xr < c);
*r++ = xr;
}
if (c) {
*r = 1;
return sz+1;
}
return sz;
}
/*
** Subtract digits v2 from v1 and store result in v3
** Assert I_comp(x, xl, y, yl) >= 0
**
*/
static dsize_t I_sub(ErtsDigit* x, dsize_t xl, ErtsDigit* y, dsize_t yl, ErtsDigit* r)
{
ErtsDigit* r0 = r;
register ErtsDigit yr, xr;
register ErtsDigit c = 0;
ASSERT(I_comp(x, xl, y, yl) >= 0);
xl -= yl;
do {
yr = *y++ + c;
xr = *x++;
c = (yr < c);
yr = xr - yr;
c += (yr > xr);
*r++ = yr;
} while(--yl);
while(xl--) {
xr = *x++;
yr = xr - c;
c = (yr > xr);
*r++ = yr;
}
do {
r--;
} while(*r == 0 && r != r0);
return (r - r0) + 1;
}
/*
** Subtract digit d from v1 and store result in vr
*/
static dsize_t D_sub(ErtsDigit* x, dsize_t xl, ErtsDigit c, ErtsDigit* r)
{
ErtsDigit* r0 = r;
register ErtsDigit yr, xr;
ASSERT(I_comp(x, xl, x, 1) >= 0);
while(xl--) {
xr = *x++;
yr = xr - c;
c = (yr > xr);
*r++ = yr;
}
do {
r--;
} while(*r == 0 && r != r0);
return (r - r0) + 1;
}
/*
** subtract Z000...0 - y and store result in r, return new size
*/
static dsize_t Z_sub(ErtsDigit* y, dsize_t yl, ErtsDigit* r)
{
ErtsDigit* r0 = r;
register ErtsDigit yr;
register ErtsDigit c = 0;
while(yl--) {
yr = *y++ + c;
c = (yr < c);
yr = 0 - yr;
c += (yr > 0);
*r++ = yr;
}
do {
r--;
} while(*r == 0 && r != r0);
return (r - r0) + 1;
}
/*
** Multiply digits in x with digits in y and store in r
** Assumption: digits in r must be 0 (up to the size of x)
*/
static dsize_t I_mul(ErtsDigit* x, dsize_t xl, ErtsDigit* y, dsize_t yl, ErtsDigit* r)
{
ErtsDigit* r0 = r;
ErtsDigit* rt = r;
ASSERT(xl >= yl);
ZERO_DIGITS(r, xl);
while(xl--) {
ErtsDigit cp = 0;
ErtsDigit c = 0;
dsize_t n = yl;
ErtsDigit* yt = y;
ErtsDigit d;
ErtsDigit p;
d = *x;
x++;
rt = r;
switch(d) {
case 0:
rt = rt + n;
break;
case 1:
while(n--) {
DSUMc(*yt, *rt, c, p);
*rt++ = p;
yt++;
}
break;
case 2:
while(n--) {
p = *yt;
DSUMc(p, p, cp, p);
DSUMc(p, *rt, c, p);
*rt++ = p;
yt++;
}
break;
default:
while(n--) {
DMULc(d,*yt, cp, p);
DSUMc(p,*rt, c, p);
*rt++ = p;
yt++;
}
break;
}
*rt = c + cp;
r++;
}
if (*rt == 0)
return (rt - r0);
else
return (rt - r0) + 1;
}
/*
** Square digits in x store in r (x & r may point into a common area)
** Assumption: x is destroyed if common area and digits in r are zero
** to the size of xl+1
*/
static dsize_t I_sqr(ErtsDigit* x, dsize_t xl, ErtsDigit* r)
{
ErtsDigit d;
ErtsDigit* r0 = r;
ErtsDigit* s = r;
ZERO_DIGITS(r, (xl+1));
if ((r + xl) == x) /* "Inline" operation */
*x = 0;
while(xl--) {
dsize_t y_l = xl;
d = *x++;
s = r;
if (d == 0) {
s += y_l + 1;
if (xl != 0) {
*++s = 0;
r += 2;
}
} else {
ErtsDigit* y;
ErtsDigit y_0 = 0, y_1 = 0, y_2 = 0, y_3 = 0;
ErtsDigit b0, b1;
ErtsDigit z0, z1, z2;
ErtsDigit t;
y = x;
DMUL(d, d, b1, b0);
DSUMc(*s, b0, y_3, t);
*s++ = t;
z1 = b1;
while(y_l--) {
DMUL(d, *y, b1, b0);
y++;
DSUMc(b0, b0, y_0, z0);
DSUMc(z0, z1, y_2, z2);
DSUMc(*s, z2, y_3, t);
*s++ = t;
DSUMc(b1, b1, y_1, z1);
}
z0 = y_0;
DSUMc(z0, z1, y_2, z2);
DSUMc(*s, z2, y_3, t);
*s = t;
if (xl != 0) {
s++;
t = (y_1+y_2+y_3);
*s = t;
r += 2;
}
else {
ASSERT((y_1+y_2+y_3) == 0);
}
}
}
if (*s == 0)
return (s - r0);
else
return (s - r0) + 1;
}
/*
* Multiply using the Karatsuba algorithm.
*
* Reference: https://en.wikipedia.org/wiki/Karatsuba_algorithm
*/
static dsize_t I_mul_karatsuba(ErtsDigit* x, dsize_t xl, ErtsDigit* y,
dsize_t yl, ErtsDigit* r)
{
ASSERT(xl >= yl);
if (yl < 16) {
/* Use the basic algorithm. */
if (x == y && xl > 1) {
ASSERT(xl == yl);
return I_sqr(x, xl, r);
} else {
return I_mul(x, xl, y, yl, r);
}
} else {
/* Use the Karatsuba algorithm. */
Eterm *heap;
Uint temp_heap_size;
Uint z0_len, z1_len, z2_len, tmp_len, diff0_len, diff1_len, res_len;
Uint low_x_len, low_y_len, high_x_len, high_y_len;
Eterm *z0_buf, *z1_buf, *z2_buf, *tmp_buf;
Eterm *diff0_buf, *diff1_buf;
#ifdef DEBUG
Eterm *alloc_end;
#endif
Eterm *low_x, *low_y, *high_x, *high_y;
ErtsDigit zero = 0;
Uint m = (xl+1) / 2;
int tmp_prod_negative = 0;
int i;
/* Set up pointers and sizes. */
low_x = x;
low_x_len = m;
high_x = x + m;
high_x_len = xl - m;
while (low_x_len > 1 && low_x[low_x_len-1] == 0) {
low_x_len--;
}
low_y = y;
if (yl <= m) {
/* High part of y is zero. */
low_y_len = yl;
high_y = &zero;
high_y_len = 1;
} else {
low_y_len = m;
high_y = y + m;
high_y_len = yl - m;
}
while (low_y_len > 1 && low_y[low_y_len-1] == 0) {
low_y_len--;
}
ASSERT(low_x_len <= m);
ASSERT(high_x_len <= m);
ASSERT(low_y_len <= m);
ASSERT(high_y_len <= m);
/*
* Set up temporary buffers in allocated memory.
*
* z1_buf is not used at the same time as diff0_buf
* and diff1_buf, so they can share memory.
*/
temp_heap_size = (4*m + 1) * sizeof(Eterm);
#ifdef DEBUG
temp_heap_size += sizeof(Eterm);
#endif
heap = (Eterm *) erts_alloc(ERTS_ALC_T_TMP, temp_heap_size);
z1_buf = heap;
diff0_buf = z1_buf + 1;
diff1_buf = diff0_buf + m;
tmp_buf = diff1_buf + m;
#ifdef DEBUG
z1_buf[0] = ERTS_HOLE_MARKER;
diff0_buf[0] = ERTS_HOLE_MARKER;
diff1_buf[0] = ERTS_HOLE_MARKER;
tmp_buf[0] = ERTS_HOLE_MARKER;
alloc_end = tmp_buf + 2*m;
alloc_end[0] = ERTS_HOLE_MARKER;
ASSERT(alloc_end - heap + 1 == temp_heap_size / sizeof(Eterm));
#endif
/* Set up pointers for the result. */
z0_buf = r;
z2_buf = r + 2*m;
#ifdef DEBUG
z2_buf[0] = ERTS_HOLE_MARKER;
#endif
#define I_OPERATION(_result, _op, _p1, _sz1, _p2, _sz2, _buf) \
do { \
if ((_sz1) >= (_sz2)) { \
_result = _op(_p1, _sz1, _p2, _sz2, _buf); \
} else { \
_result = _op(_p2, _sz2, _p1, _sz1, _buf); \
} \
} while (0)
/*
* The Karatsuba algorithm is a divide and conquer algorithm
* for multi-word integer multiplication. The numbers to be
* multiplied are split up like this:
*
* high low
* +--------+--------+
* | high_x | low_x |
* +--------+--------+
*
* +--------+--------+
* | high_y | low_y |
* +--------+--------+
*
* Then the following values are calculated:
*
* z0 = low_x * low_y
* z2 = high_x + high_y
* z1 = (low_x - high_x) * (high_y - low_y) + z2 + z0
*
* Note that this expression for z1 produces the same result
* as:
*
* low_x * high_y + high_x * low_y
*
* Finally, the z2, z1, z0 values are combined to form the
* product of x and y:
*
* high low
* +--+--+ +--+--+
* | z2 | | z0 |
* +--+--+ +--+--+
* +--+--+
* add | z1 |
* +--+--+
*
* There is an alternate way to calculate z1 (commonly found
* in descriptions of the Karatsuba algorithm);
*
* z1 = (high_x + low_x) * (high_y + low_y) - z2 - z0
*
* But this way can lead to more additions and carry handling.
*/
/*
* z0 = low_x * low_y
*
* Store this product in its final location in the result buffer.
*/
I_OPERATION(z0_len, I_mul_karatsuba, low_x, low_x_len, low_y, low_y_len, z0_buf);
ASSERT(z2_buf[0] == ERTS_HOLE_MARKER);
for (i = z0_len; i < 2*m; i++) {
z0_buf[i] = 0;
}
while (z0_len > 1 && z0_buf[z0_len - 1] == 0) {
z0_len--;
}
ASSERT(z0_len == 1 || z0_buf[z0_len-1] != 0);
ASSERT(z0_len <= low_x_len + low_y_len);
/*
* z2 = high_x * high_y
*
* Store this product in its final location in the result buffer.
*/
if (high_y != &zero) {
I_OPERATION(z2_len, I_mul_karatsuba, high_x, high_x_len,
high_y, high_y_len, z2_buf);
while (z2_len > 1 && z2_buf[z2_len - 1] == 0) {
z2_len--;
}
ASSERT(z2_len == 1 || z2_buf[z2_len-1] != 0);
} else {
z2_buf[0] = 0;
z2_len = 1;
}
ASSERT(z2_len <= high_x_len + high_y_len);
/*
* tmp = abs(low_x - high_x) * abs(high_y - low_y)
*
* The absolute value of each difference will fit in m words.
*
* Save the sign of the product so that we later can choose to
* subtract or add this value.
*/
if (I_comp(low_x, low_x_len, high_x, high_x_len) >= 0) {
diff0_len = I_sub(low_x, low_x_len, high_x, high_x_len, diff0_buf);
} else {
tmp_prod_negative = !tmp_prod_negative;
diff0_len = I_sub(high_x, high_x_len, low_x, low_x_len, diff0_buf);
}
ASSERT(diff1_buf[0] == ERTS_HOLE_MARKER);
ASSERT(diff0_len == 1 || diff0_buf[diff0_len-1] != 0);
ASSERT(diff0_len <= m);
if (x == y) {
ASSERT(xl == yl);
tmp_prod_negative = 1;
diff1_buf = diff0_buf;
diff1_len = diff0_len;
} else if (I_comp(high_y, high_y_len, low_y, low_y_len) >= 0) {
diff1_len = I_sub(high_y, high_y_len, low_y, low_y_len, diff1_buf);
} else {
tmp_prod_negative = !tmp_prod_negative;
if (high_y != &zero) {
diff1_len = I_sub(low_y, low_y_len, high_y, high_y_len, diff1_buf);
} else {
diff1_buf = low_y;
diff1_len = low_y_len;
}
}
ASSERT(tmp_buf[0] == ERTS_HOLE_MARKER);
ASSERT(diff1_len == 1 || diff1_buf[diff1_len-1] != 0);
ASSERT(diff1_len <= m);
I_OPERATION(tmp_len, I_mul_karatsuba, diff0_buf, diff0_len, diff1_buf, diff1_len, tmp_buf);
ASSERT(alloc_end[0] == ERTS_HOLE_MARKER);
while (tmp_len > 1 && tmp_buf[tmp_len - 1] == 0) {
tmp_len--;
}
ASSERT(tmp_len == 1 || tmp_buf[tmp_len-1] != 0);
ASSERT(tmp_len <= diff0_len + diff1_len);
/*
* z1 = z0 + z2
*/
I_OPERATION(z1_len, I_add, z0_buf, z0_len, z2_buf, z2_len, z1_buf);
ASSERT(z1_len == 1 || z1_buf[z1_len-1] != 0);
if (tmp_prod_negative) {
/* z1 = z1 - tmp */
z1_len = I_sub(z1_buf, z1_len, tmp_buf, tmp_len, z1_buf);
} else {
/* z1 = z1 + tmp */
I_OPERATION(z1_len, I_add, z1_buf, z1_len, tmp_buf, tmp_len, z1_buf);
}
/* Add z1 shifted into the result */
I_OPERATION(res_len, I_add, z0_buf+m, z2_len+m, z1_buf, z1_len, z0_buf+m);
/* Normalize */
res_len += m;
while (res_len > 1 && r[res_len - 1] == 0) {