/
bigintops.c
1360 lines (1196 loc) · 41.4 KB
/
bigintops.c
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
#include "moar.h"
#include <math.h>
#ifndef MANTISSA_BITS_IN_DOUBLE
#define MANTISSA_BITS_IN_DOUBLE 53
#endif
#ifndef MAX_BIGINT_BITS_IN_DOUBLE
#define MAX_BIGINT_BITS_IN_DOUBLE 1023
#endif
#ifndef MAX
#define MAX(x,y) ((x)>(y)?(x):(y))
#endif
#ifndef MIN
#define MIN(x,y) ((x)<(y)?(x):(y))
#endif
MVM_STATIC_INLINE void adjust_nursery(MVMThreadContext *tc, MVMP6bigintBody *body) {
if (MVM_BIGINT_IS_BIG(body)) {
int used = USED(body->u.bigint);
int adjustment = MIN(used, 32768) & ~0x7;
if (adjustment && (char *)tc->nursery_alloc_limit - adjustment > (char *)tc->nursery_alloc) {
tc->nursery_alloc_limit = (char *)(tc->nursery_alloc_limit) - adjustment;
}
}
}
/* Taken from mp_set_long, but portably accepts a 64-bit number. */
int MVM_bigint_mp_set_uint64(mp_int * a, MVMuint64 b) {
int x, res;
mp_zero (a);
/* set four bits at a time */
for (x = 0; x < sizeof(MVMuint64) * 2; x++) {
/* shift the number up four bits */
if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) {
return res;
}
/* OR in the top four bits of the source */
a->dp[0] |= (b >> ((sizeof(MVMuint64)) * 8 - 4)) & 15;
/* shift the source up to the next four bits */
b <<= 4;
/* ensure that digits are not clamped off */
a->used += 1;
}
mp_clamp(a);
return MP_OKAY;
}
/*
* Convert to double, assumes IEEE-754 conforming double. Taken from
* https://github.com/czurnieden/libtommath/blob/master/bn_mp_get_double.c
* and slightly modified to fit MoarVM's setup.
*/
static const int MVM_mp_get_double_digits_needed
= ((MANTISSA_BITS_IN_DOUBLE + DIGIT_BIT) / DIGIT_BIT) + 1;
static const double MVM_mp_get_double_multiplier = (double)(MP_MASK + 1);
static MVMnum64 MVM_mp_get_double_shift(mp_int *a, int shift) {
MVMnum64 d;
int i, limit, final_shift;
d = 0.0;
mp_clamp(a);
i = a->used;
limit = (i <= MVM_mp_get_double_digits_needed)
? 0 : i - MVM_mp_get_double_digits_needed;
while (i-- > limit) {
d += a->dp[i];
d *= MVM_mp_get_double_multiplier;
}
if (a->sign == MP_NEG)
d *= -1.0;
final_shift = i * DIGIT_BIT - shift;
if (final_shift < 0) {
while (final_shift < -1023) {
d *= pow(2.0, -1023);
final_shift += 1023;
}
}
else {
while (final_shift > 1023) {
d *= pow(2.0, 1023);
final_shift -= 1023;
}
}
d *= pow(2.0, final_shift);
return d;
}
static void from_num(MVMnum64 d, mp_int *a) {
MVMnum64 d_digit = pow(2, DIGIT_BIT);
MVMnum64 da = fabs(d);
MVMnum64 upper;
MVMnum64 lower;
MVMnum64 lowest;
MVMnum64 rest;
int digits = 0;
mp_zero(a);
while (da > d_digit * d_digit * d_digit) {;
da /= d_digit;
digits++;
}
mp_grow(a, digits + 3);
/* populate the top 3 digits */
upper = da / (d_digit*d_digit);
rest = fmod(da, d_digit*d_digit);
lower = rest / d_digit;
lowest = fmod(rest,d_digit );
if (upper >= 1) {
MVM_bigint_mp_set_uint64(a, (MVMuint64) upper);
mp_mul_2d(a, DIGIT_BIT , a);
DIGIT(a, 0) = (mp_digit) lower;
mp_mul_2d(a, DIGIT_BIT , a);
} else {
if (lower >= 1) {
MVM_bigint_mp_set_uint64(a, (MVMuint64) lower);
mp_mul_2d(a, DIGIT_BIT , a);
a->used = 2;
} else {
a->used = 1;
}
}
DIGIT(a, 0) = (mp_digit) lowest;
/* shift the rest */
mp_mul_2d(a, DIGIT_BIT * digits, a);
if (d < 0)
mp_neg(a, a);
mp_clamp(a);
mp_shrink(a);
}
/* Returns the body of a P6bigint, containing the bigint/smallint union, for
* operations that want to explicitly handle the two. */
static MVMP6bigintBody * get_bigint_body(MVMThreadContext *tc, MVMObject *obj) {
if (IS_CONCRETE(obj))
return (MVMP6bigintBody *)REPR(obj)->box_funcs.get_boxed_ref(tc,
STABLE(obj), obj, OBJECT_BODY(obj), MVM_REPR_ID_P6bigint);
else
MVM_exception_throw_adhoc(tc,
"Can only perform big integer operations on concrete objects");
}
/* Checks if a bigint can be stored small. */
static int can_be_smallint(const mp_int *i) {
if (USED(i) != 1)
return 0;
return MVM_IS_32BIT_INT(DIGIT(i, 0));
}
/* Forces a bigint, even if we only have a smallint. Takes a parameter that
* indicates where to allocate a temporary mp_int if needed. */
static mp_int * force_bigint(MVMThreadContext *tc, const MVMP6bigintBody *body, int idx) {
if (MVM_BIGINT_IS_BIG(body)) {
return body->u.bigint;
}
else {
if (sizeof(mp_digit) > 4) {
MVMint64 value = body->u.smallint.value;
mp_int *i = tc->temp_bigints[idx];
if (value >= 0) {
mp_digit d = value;
mp_set(i, d);
}
else {
mp_digit d = -value;
mp_set(i, d);
mp_neg(i, i);
}
return i;
}
else {
MVMint32 value = body->u.smallint.value;
mp_int *i = tc->temp_bigints[idx];
if (value >= 0) {
mp_set_int(i, value);
}
else {
mp_set_int(i, -value);
mp_neg(i, i);
}
return i;
}
}
}
/* Stores an int64 in a bigint result body, either as a 32-bit smallint if it
* is in range, or a big integer if not. */
static void store_int64_result(MVMP6bigintBody *body, MVMint64 result) {
if (MVM_IS_32BIT_INT(result)) {
body->u.smallint.flag = MVM_BIGINT_32_FLAG;
body->u.smallint.value = (MVMint32)result;
}
else {
mp_int *i = MVM_malloc(sizeof(mp_int));
mp_init(i);
if (result >= 0) {
MVM_bigint_mp_set_uint64(i, (MVMuint64)result);
}
else {
MVM_bigint_mp_set_uint64(i, (MVMuint64)-result);
mp_neg(i, i);
}
body->u.bigint = i;
}
}
/* Stores a bigint in a bigint result body, either as a 32-bit smallint if it
* is in range, or a big integer if not. Clears and frees the passed bigint if
* it is not being used. */
static void store_bigint_result(MVMP6bigintBody *body, mp_int *i) {
if (can_be_smallint(i)) {
body->u.smallint.flag = MVM_BIGINT_32_FLAG;
body->u.smallint.value = SIGN(i) == MP_NEG ? -DIGIT(i, 0) : DIGIT(i, 0);
mp_clear(i);
MVM_free(i);
}
else {
body->u.bigint = i;
}
}
/* Bitops on libtomath (no two's complement API) are horrendously inefficient and
* really should be hand-coded to work DIGIT-by-DIGIT with in-loop carry
* handling. For now we have these fixups.
*
* The following inverts the bits of a negative bigint, adds 1 to that, and
* appends sign-bit extension DIGITs to it to give us a 2s complement
* representation in memory. Do not call it on positive bigints.
*/
static void grow_and_negate(const mp_int *a, int size, mp_int *b) {
int i;
/* Always add an extra DIGIT so we can tell positive values
* with a one in the highest bit apart from negative values.
*/
int actual_size = MAX(size, USED(a)) + 1;
SIGN(b) = MP_ZPOS;
mp_grow(b, actual_size);
USED(b) = actual_size;
for (i = 0; i < USED(a); i++) {
DIGIT(b, i) = (~DIGIT(a, i)) & MP_MASK;
}
for (; i < actual_size; i++) {
DIGIT(b, i) = MP_MASK;
}
/* Note: This add cannot cause another grow assuming nobody ever
* tries to use tommath -0 for anything, and nobody tries to use
* this on positive bigints.
*/
mp_add_d(b, 1, b);
}
static void two_complement_bitop(mp_int *a, mp_int *b, mp_int *c,
int (*mp_bitop)(mp_int *, mp_int *, mp_int *)) {
mp_int d;
mp_int e;
mp_int *f;
mp_int *g;
f = a;
g = b;
if (MP_NEG == SIGN(a)) {
mp_init(&d);
grow_and_negate(a, USED(b), &d);
f = &d;
}
if (MP_NEG == SIGN(b)) {
mp_init(&e);
grow_and_negate(b, USED(a), &e);
g = &e;
}
/* f and g now guaranteed to each point to positive bigints containing
* a two's complement representation of the values in a and b. If either
* a or b was negative, the representation is one tomath "digit" longer
* than it need be and sign extended.
*/
mp_bitop(f, g, c);
if (f == &d) mp_clear(&d);
if (g == &e) mp_clear(&e);
/* Use the fact that tomath clamps to detect results that should be
* signed. If we created extra tomath "digits" and they resulted in
* sign bits of 0, they have been clamped away. If the resulting sign
* bits were 1, they remain, and c will have more digits than either of
* original operands. Note this only works because we do not
* support NOR/NAND/NXOR, and so two zero sign bits can never create 1s.
*/
if (USED(c) > MAX(USED(a),USED(b))) {
int i;
for (i = 0; i < USED(c); i++) {
DIGIT(c, i) = (~DIGIT(c, i)) & MP_MASK;
}
mp_add_d(c, 1, c);
mp_neg(c, c);
}
}
static void two_complement_shl(mp_int *result, mp_int *value, MVMint64 count) {
if (count >= 0) {
mp_mul_2d(value, count, result);
}
else if (MP_NEG == SIGN(value)) {
/* fake two's complement semantics on top of sign-magnitude
* algorithm appears to work [citation needed]
*/
mp_add_d(value, 1, result);
mp_div_2d(result, -count, result, NULL);
mp_sub_d(result, 1, result);
}
else {
mp_div_2d(value, -count, result, NULL);
}
}
#define MVM_BIGINT_UNARY_OP(opname, SMALLINT_OP) \
MVMObject * MVM_bigint_##opname(MVMThreadContext *tc, MVMObject *result_type, MVMObject *source) { \
MVMP6bigintBody *bb; \
MVMObject *result; \
MVMROOT(tc, source, { \
result = MVM_repr_alloc_init(tc, result_type);\
}); \
bb = get_bigint_body(tc, result); \
if (!IS_CONCRETE(source)) { \
store_int64_result(bb, 0); \
} \
else { \
MVMP6bigintBody *ba = get_bigint_body(tc, source); \
if (MVM_BIGINT_IS_BIG(ba)) { \
mp_int *ia = ba->u.bigint; \
mp_int *ib = MVM_malloc(sizeof(mp_int)); \
mp_init(ib); \
mp_##opname(ia, ib); \
store_bigint_result(bb, ib); \
adjust_nursery(tc, bb); \
} \
else { \
MVMint64 sb; \
MVMint64 sa = ba->u.smallint.value; \
SMALLINT_OP; \
store_int64_result(bb, sb); \
} \
} \
return result; \
}
#define MVM_BIGINT_BINARY_OP(opname) \
MVMObject * MVM_bigint_##opname(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a, MVMObject *b) { \
MVMP6bigintBody *ba, *bb, *bc; \
MVMObject *result; \
mp_int *ia, *ib, *ic; \
MVMROOT2(tc, a, b, { \
result = MVM_repr_alloc_init(tc, result_type);\
}); \
ba = get_bigint_body(tc, a); \
bb = get_bigint_body(tc, b); \
bc = get_bigint_body(tc, result); \
ia = force_bigint(tc, ba, 0); \
ib = force_bigint(tc, bb, 1); \
ic = MVM_malloc(sizeof(mp_int)); \
mp_init(ic); \
mp_##opname(ia, ib, ic); \
store_bigint_result(bc, ic); \
adjust_nursery(tc, bc); \
return result; \
}
#define MVM_BIGINT_BINARY_OP_SIMPLE(opname, SMALLINT_OP) \
void MVM_bigint_fallback_##opname(MVMThreadContext *tc, MVMP6bigintBody *ba, MVMP6bigintBody *bb, \
MVMP6bigintBody *bc) { \
mp_int *ia, *ib, *ic; \
ia = force_bigint(tc, ba, 0); \
ib = force_bigint(tc, bb, 1); \
ic = MVM_malloc(sizeof(mp_int)); \
mp_init(ic); \
mp_##opname(ia, ib, ic); \
store_bigint_result(bc, ic); \
adjust_nursery(tc, bc); \
} \
MVMObject * MVM_bigint_##opname(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a, MVMObject *b) { \
MVMP6bigintBody *ba, *bb, *bc; \
MVMObject *result; \
ba = get_bigint_body(tc, a); \
bb = get_bigint_body(tc, b); \
if (MVM_BIGINT_IS_BIG(ba) || MVM_BIGINT_IS_BIG(bb)) { \
mp_int *ia, *ib, *ic; \
MVMROOT2(tc, a, b, { \
result = MVM_repr_alloc_init(tc, result_type);\
}); \
ba = get_bigint_body(tc, a); \
bb = get_bigint_body(tc, b); \
bc = get_bigint_body(tc, result); \
ia = force_bigint(tc, ba, 0); \
ib = force_bigint(tc, bb, 1); \
ic = MVM_malloc(sizeof(mp_int)); \
mp_init(ic); \
mp_##opname(ia, ib, ic); \
store_bigint_result(bc, ic); \
adjust_nursery(tc, bc); \
} \
else { \
MVMint64 sc; \
MVMint64 sa = ba->u.smallint.value; \
MVMint64 sb = bb->u.smallint.value; \
SMALLINT_OP; \
result = MVM_intcache_get(tc, result_type, sc); \
if (result) \
return result; \
result = MVM_repr_alloc_init(tc, result_type);\
bc = get_bigint_body(tc, result); \
store_int64_result(bc, sc); \
} \
return result; \
}
#define MVM_BIGINT_BINARY_OP_2(opname, SMALLINT_OP) \
MVMObject * MVM_bigint_##opname(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a, MVMObject *b) { \
MVMP6bigintBody *ba = get_bigint_body(tc, a); \
MVMP6bigintBody *bb = get_bigint_body(tc, b); \
MVMP6bigintBody *bc; \
MVMObject *result; \
MVMROOT2(tc, a, b, { \
result = MVM_repr_alloc_init(tc, result_type);\
}); \
bc = get_bigint_body(tc, result); \
if (MVM_BIGINT_IS_BIG(ba) || MVM_BIGINT_IS_BIG(bb)) { \
mp_int *ia = force_bigint(tc, ba, 0); \
mp_int *ib = force_bigint(tc, bb, 1); \
mp_int *ic = MVM_malloc(sizeof(mp_int)); \
mp_init(ic); \
two_complement_bitop(ia, ib, ic, mp_##opname); \
store_bigint_result(bc, ic); \
adjust_nursery(tc, bc); \
} \
else { \
MVMint64 sc; \
MVMint64 sa = ba->u.smallint.value; \
MVMint64 sb = bb->u.smallint.value; \
SMALLINT_OP; \
store_int64_result(bc, sc); \
} \
return result; \
}
MVM_BIGINT_UNARY_OP(abs, { sb = labs(sa); })
MVM_BIGINT_UNARY_OP(neg, { sb = -sa; })
/* unused */
/* MVM_BIGINT_UNARY_OP(sqrt) */
MVM_BIGINT_BINARY_OP_SIMPLE(add, { sc = sa + sb; })
MVM_BIGINT_BINARY_OP_SIMPLE(sub, { sc = sa - sb; })
MVM_BIGINT_BINARY_OP_SIMPLE(mul, { sc = sa * sb; })
MVM_BIGINT_BINARY_OP(lcm)
MVMObject *MVM_bigint_gcd(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a, MVMObject *b) {
MVMP6bigintBody *ba = get_bigint_body(tc, a);
MVMP6bigintBody *bb = get_bigint_body(tc, b);
MVMP6bigintBody *bc;
MVMObject *result;
MVMROOT2(tc, a, b, {
result = MVM_repr_alloc_init(tc, result_type);
});
bc = get_bigint_body(tc, result);
if (MVM_BIGINT_IS_BIG(ba) || MVM_BIGINT_IS_BIG(bb)) {
mp_int *ia = force_bigint(tc, ba, 0);
mp_int *ib = force_bigint(tc, bb, 1);
mp_int *ic = MVM_malloc(sizeof(mp_int));
mp_init(ic);
mp_gcd(ia, ib, ic);
store_bigint_result(bc, ic);
adjust_nursery(tc, bc);
} else {
MVMint32 sa = ba->u.smallint.value;
MVMint32 sb = bb->u.smallint.value;
MVMint32 t;
sa = abs(sa);
sb = abs(sb);
while (sb != 0) {
t = sb;
sb = sa % sb;
sa = t;
}
store_int64_result(bc, sa);
}
return result;
}
MVM_BIGINT_BINARY_OP_2(or , { sc = sa | sb; })
MVM_BIGINT_BINARY_OP_2(xor, { sc = sa ^ sb; })
MVM_BIGINT_BINARY_OP_2(and, { sc = sa & sb; })
MVMint64 MVM_bigint_cmp(MVMThreadContext *tc, MVMObject *a, MVMObject *b) {
MVMP6bigintBody *ba = get_bigint_body(tc, a);
MVMP6bigintBody *bb = get_bigint_body(tc, b);
if (MVM_BIGINT_IS_BIG(ba) || MVM_BIGINT_IS_BIG(bb)) {
mp_int *ia = force_bigint(tc, ba, 0);
mp_int *ib = force_bigint(tc, bb, 1);
MVMint64 r = (MVMint64)mp_cmp(ia, ib);
return r;
}
else {
MVMint64 sa = ba->u.smallint.value;
MVMint64 sb = bb->u.smallint.value;
return sa == sb ? 0 : sa < sb ? -1 : 1;
}
}
MVMObject * MVM_bigint_mod(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a, MVMObject *b) {
MVMP6bigintBody *ba = get_bigint_body(tc, a);
MVMP6bigintBody *bb = get_bigint_body(tc, b);
MVMP6bigintBody *bc;
MVMObject *result;
MVMROOT2(tc, a, b, {
result = MVM_repr_alloc_init(tc, result_type);
});
bc = get_bigint_body(tc, result);
/* XXX the behavior of C's mod operator is not correct
* for our purposes. So we rely on mp_mod for all our modulus
* calculations for now. */
if (1 || MVM_BIGINT_IS_BIG(ba) || MVM_BIGINT_IS_BIG(bb)) {
mp_int *ia = force_bigint(tc, ba, 0);
mp_int *ib = force_bigint(tc, bb, 1);
mp_int *ic = MVM_malloc(sizeof(mp_int));
int mp_result;
mp_init(ic);
mp_result = mp_mod(ia, ib, ic);
if (mp_result == MP_VAL) {
MVM_exception_throw_adhoc(tc, "Division by zero");
}
store_bigint_result(bc, ic);
adjust_nursery(tc, bc);
} else {
store_int64_result(bc, ba->u.smallint.value % bb->u.smallint.value);
}
return result;
}
MVMObject *MVM_bigint_div(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a, MVMObject *b) {
MVMP6bigintBody *ba = get_bigint_body(tc, a);
MVMP6bigintBody *bb = get_bigint_body(tc, b);
MVMP6bigintBody *bc;
mp_int *ia, *ib, *ic;
int cmp_a;
int cmp_b;
mp_int remainder;
mp_int intermediate;
MVMObject *result;
int mp_result;
if (!MVM_BIGINT_IS_BIG(bb) && bb->u.smallint.value == 1 && STABLE(a) == STABLE(b)) {
return a;
}
MVMROOT2(tc, a, b, {
result = MVM_repr_alloc_init(tc, result_type);
});
bc = get_bigint_body(tc, result);
/* we only care about MP_LT or !MP_LT, so we give MP_GT even for 0. */
if (MVM_BIGINT_IS_BIG(ba)) {
cmp_a = !mp_iszero(ba->u.bigint) && SIGN(ba->u.bigint) == MP_NEG ? MP_LT : MP_GT;
} else {
cmp_a = ba->u.smallint.value < 0 ? MP_LT : MP_GT;
}
if (MVM_BIGINT_IS_BIG(bb)) {
cmp_b = !mp_iszero(bb->u.bigint) && SIGN(bb->u.bigint) == MP_NEG ? MP_LT : MP_GT;
} else {
cmp_b = bb->u.smallint.value < 0 ? MP_LT : MP_GT;
}
if (MVM_BIGINT_IS_BIG(ba) || MVM_BIGINT_IS_BIG(bb)) {
ia = force_bigint(tc, ba, 0);
ib = force_bigint(tc, bb, 1);
ic = MVM_malloc(sizeof(mp_int));
mp_init(ic);
/* if we do a div with a negative, we need to make sure
* the result is floored rather than rounded towards
* zero, like C and libtommath would do. */
if ((cmp_a == MP_LT) ^ (cmp_b == MP_LT)) {
mp_init(&remainder);
mp_init(&intermediate);
mp_result = mp_div(ia, ib, &intermediate, &remainder);
if (mp_result == MP_VAL) {
mp_clear(&remainder);
mp_clear(&intermediate);
MVM_exception_throw_adhoc(tc, "Division by zero");
}
if (mp_iszero(&remainder) == 0) {
mp_sub_d(&intermediate, 1, ic);
} else {
mp_copy(&intermediate, ic);
}
mp_clear(&remainder);
mp_clear(&intermediate);
} else {
mp_result = mp_div(ia, ib, ic, NULL);
if (mp_result == MP_VAL) {
MVM_exception_throw_adhoc(tc, "Division by zero");
}
}
store_bigint_result(bc, ic);
adjust_nursery(tc, bc);
} else {
MVMint32 num = ba->u.smallint.value;
MVMint32 denom = bb->u.smallint.value;
MVMint64 value;
if ((cmp_a == MP_LT) ^ (cmp_b == MP_LT)) {
if (denom == 0) {
MVM_exception_throw_adhoc(tc, "Division by zero");
}
if ((num % denom) != 0) {
value = num / denom - 1;
} else {
value = num / denom;
}
} else {
value = (MVMint64)num / denom;
}
store_int64_result(bc, value);
}
return result;
}
MVMObject * MVM_bigint_pow(MVMThreadContext *tc, MVMObject *a, MVMObject *b,
MVMObject *num_type, MVMObject *int_type) {
MVMP6bigintBody *ba = get_bigint_body(tc, a);
MVMP6bigintBody *bb = get_bigint_body(tc, b);
MVMObject *r = NULL;
mp_int *base = force_bigint(tc, ba, 0);
mp_int *exponent = force_bigint(tc, bb, 1);
mp_digit exponent_d = 0;
if (mp_iszero(exponent) || (MP_EQ == mp_cmp_d(base, 1))) {
r = MVM_repr_box_int(tc, int_type, 1);
}
else if (SIGN(exponent) == MP_ZPOS) {
exponent_d = mp_get_int(exponent);
if ((MP_GT == mp_cmp_d(exponent, exponent_d))) {
if (mp_iszero(base)) {
r = MVM_repr_box_int(tc, int_type, 0);
}
else if (mp_get_int(base) == 1) {
r = MVM_repr_box_int(tc, int_type, MP_ZPOS == SIGN(base) || mp_iseven(exponent) ? 1 : -1);
}
else {
MVMnum64 inf;
if (MP_ZPOS == SIGN(base) || mp_iseven(exponent)) {
inf = MVM_num_posinf(tc);
}
else {
inf = MVM_num_neginf(tc);
}
r = MVM_repr_box_num(tc, num_type, inf);
}
}
else {
mp_int *ic = MVM_malloc(sizeof(mp_int));
MVMP6bigintBody *resbody;
mp_init(ic);
MVM_gc_mark_thread_blocked(tc);
mp_expt_d(base, exponent_d, ic);
MVM_gc_mark_thread_unblocked(tc);
r = MVM_repr_alloc_init(tc, int_type);
resbody = get_bigint_body(tc, r);
store_bigint_result(resbody, ic);
adjust_nursery(tc, resbody);
}
}
else {
MVMnum64 f_base = MVM_mp_get_double_shift(base, 0);
MVMnum64 f_exp = MVM_mp_get_double_shift(exponent, 0);
r = MVM_repr_box_num(tc, num_type, pow(f_base, f_exp));
}
return r;
}
MVMObject *MVM_bigint_shl(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a, MVMint64 n) {
MVMP6bigintBody *ba = get_bigint_body(tc, a);
MVMP6bigintBody *bb;
MVMObject *result;
MVMROOT(tc, a, {
result = MVM_repr_alloc_init(tc, result_type);
});
bb = get_bigint_body(tc, result);
if (MVM_BIGINT_IS_BIG(ba) || n >= 31) {
mp_int *ia = force_bigint(tc, ba, 0);
mp_int *ib = MVM_malloc(sizeof(mp_int));
mp_init(ib);
two_complement_shl(ib, ia, n);
store_bigint_result(bb, ib);
adjust_nursery(tc, bb);
} else {
MVMint64 value;
if (n < 0)
value = ((MVMint64)ba->u.smallint.value) >> -n;
else
value = ((MVMint64)ba->u.smallint.value) << n;
store_int64_result(bb, value);
}
return result;
}
/* Checks if a MVMP6bigintBody is negative. Handles cases where it is stored as
* a small int as well as cases when it is stored as a bigint */
int BIGINT_IS_NEGATIVE (MVMP6bigintBody *ba) {
mp_int *mp_a = ba->u.bigint;
if (MVM_BIGINT_IS_BIG(ba)) {
return SIGN(mp_a) == MP_NEG;
}
else {
return ba->u.smallint.value < 0;
}
}
MVMObject *MVM_bigint_shr(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a, MVMint64 n) {
MVMP6bigintBody *ba = get_bigint_body(tc, a);
MVMP6bigintBody *bb;
MVMObject *result;
MVMROOT(tc, a, {
result = MVM_repr_alloc_init(tc, result_type);
});
bb = get_bigint_body(tc, result);
if (MVM_BIGINT_IS_BIG(ba) || n < 0) {
mp_int *ia = force_bigint(tc, ba, 0);
mp_int *ib = MVM_malloc(sizeof(mp_int));
mp_init(ib);
two_complement_shl(ib, ia, -n);
store_bigint_result(bb, ib);
adjust_nursery(tc, bb);
} else if (n >= 32) {
store_int64_result(bb, BIGINT_IS_NEGATIVE(ba) ? -1 : 0);
} else {
MVMint32 value = ba->u.smallint.value;
value = value >> n;
store_int64_result(bb, value);
}
return result;
}
MVMObject *MVM_bigint_not(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a) {
MVMP6bigintBody *ba = get_bigint_body(tc, a);
MVMP6bigintBody *bb;
MVMObject *result;
MVMROOT(tc, a, {
result = MVM_repr_alloc_init(tc, result_type);
});
bb = get_bigint_body(tc, result);
if (MVM_BIGINT_IS_BIG(ba)) {
mp_int *ia = ba->u.bigint;
mp_int *ib = MVM_malloc(sizeof(mp_int));
mp_init(ib);
/* two's complement not: add 1 and negate */
mp_add_d(ia, 1, ib);
mp_neg(ib, ib);
store_bigint_result(bb, ib);
adjust_nursery(tc, bb);
} else {
MVMint32 value = ba->u.smallint.value;
value = ~value;
store_int64_result(bb, value);
}
return result;
}
MVMObject *MVM_bigint_expmod(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a, MVMObject *b, MVMObject *c) {
MVMP6bigintBody *ba = get_bigint_body(tc, a);
MVMP6bigintBody *bb = get_bigint_body(tc, b);
MVMP6bigintBody *bc = get_bigint_body(tc, c);
MVMP6bigintBody *bd;
MVMObject *result;
mp_int *ia = force_bigint(tc, ba, 0);
mp_int *ib = force_bigint(tc, bb, 1);
mp_int *ic = force_bigint(tc, bc, 2);
mp_int *id = MVM_malloc(sizeof(mp_int));
mp_init(id);
MVMROOT3(tc, a, b, c, {
result = MVM_repr_alloc_init(tc, result_type);
});
bd = get_bigint_body(tc, result);
mp_exptmod(ia, ib, ic, id);
store_bigint_result(bd, id);
adjust_nursery(tc, bd);
return result;
}
void MVM_bigint_from_str(MVMThreadContext *tc, MVMObject *a, const char *buf) {
MVMP6bigintBody *body = get_bigint_body(tc, a);
mp_int *i = alloca(sizeof(mp_int));
mp_init(i);
mp_read_radix(i, buf, 10);
adjust_nursery(tc, body);
if (can_be_smallint(i)) {
body->u.smallint.flag = MVM_BIGINT_32_FLAG;
body->u.smallint.value = SIGN(i) == MP_NEG ? -DIGIT(i, 0) : DIGIT(i, 0);
mp_clear(i);
}
else {
mp_int *i_cpy = MVM_malloc(sizeof(mp_int));
memcpy(i_cpy, i, sizeof(mp_int));
body->u.bigint = i_cpy;
}
}
#define can_fit_into_8bit(g) ((-128 <= (g) && (g) <= 127))
MVMObject * MVM_coerce_sI(MVMThreadContext *tc, MVMString *s, MVMObject *type) {
char *buf = NULL;
int is_malloced = 0;
MVMStringIndex i;
MVMObject *a = MVM_repr_alloc_init(tc, type);
if (s->body.num_graphs < 120) {
buf = alloca(s->body.num_graphs + 1);
}
else {
buf = MVM_malloc(s->body.num_graphs + 1);
is_malloced = 1;
}
/* We just ignore synthetics since parsing will fail if a synthetic is
* encountered anyway. */
switch (s->body.storage_type) {
case MVM_STRING_GRAPHEME_ASCII:
case MVM_STRING_GRAPHEME_8:
memcpy(buf, s->body.storage.blob_8, sizeof(MVMGrapheme8) * s->body.num_graphs);
break;
case MVM_STRING_GRAPHEME_32:
for (i = 0; i < s->body.num_graphs; i++) {
buf[i] = can_fit_into_8bit(s->body.storage.blob_32[i])
? s->body.storage.blob_32[i]
: '?'; /* Add a filler bogus char if it can't fit */
}
break;
case MVM_STRING_STRAND: {
MVMGraphemeIter gi;
MVM_string_gi_init(tc, &gi, s);
for (i = 0; i < s->body.num_graphs; i++) {
MVMGrapheme32 g = MVM_string_gi_get_grapheme(tc, &gi);
buf[i] = can_fit_into_8bit(g) ? g : '?';
}
break;
}
default:
if (is_malloced) MVM_free(buf);
MVM_exception_throw_adhoc(tc, "String corruption found in MVM_coerce_sI");
}
buf[s->body.num_graphs] = 0;
MVM_bigint_from_str(tc, a, buf);
if (is_malloced) MVM_free(buf);
return a;
}
MVMObject * MVM_bigint_from_bigint(MVMThreadContext *tc, MVMObject *result_type, MVMObject *a) {
MVMP6bigintBody *a_body;
MVMP6bigintBody *r_body;
MVMObject *result;
MVMROOT(tc, a, {
result = MVM_repr_alloc_init(tc, result_type);
});
a_body = get_bigint_body(tc, a);
r_body = get_bigint_body(tc, result);
if (MVM_BIGINT_IS_BIG(a_body)) {
mp_int *i = MVM_malloc(sizeof(mp_int));
mp_init_copy(i, a_body->u.bigint);
store_bigint_result(r_body, i);
adjust_nursery(tc, r_body);
}
else {
r_body->u.smallint = a_body->u.smallint;
r_body->u.smallint.flag = a_body->u.smallint.flag;
r_body->u.smallint.value = a_body->u.smallint.value;
}
return result;
}
/* returns size of ASCII reprensentation */
static int mp_faster_radix_size (mp_int *a, int radix, int *size)
{
int res, digs;
mp_int t;
mp_digit d;
*size = 0;
/* make sure the radix is in range */
if ((radix < 2) || (radix > 64))
return MP_VAL;
if (mp_iszero(a) == MP_YES) {
*size = 2;
return MP_OKAY;
}
/* special case for binary */
if (radix == 2) {
*size = mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1;
return MP_OKAY;
}
digs = 0; /* digit count */
if (a->sign == MP_NEG)
++digs;
/* init a copy of the input */
if ((res = mp_init_copy(&t, a)) != MP_OKAY)
return res;
/* force temp to positive */
t.sign = MP_ZPOS;
/* fetch out all of the digits */
#if DIGIT_BIT == 60
/* Optimization for base-10 numbers.
* Logic is designed for 60-bit mp_digit, with 100000000000000000
* being the largest 10**n that can fit into it, which gives us 17 digits.
* So we reduce the number in 17 digit chunks, until we get to a number
* small enough to fit into a single mp_digit.
*/
if (radix == 10) {
mp_clamp(&t);
while ((&t)->used > 1) {
if ((res = mp_div_d(&t, (mp_digit) 100000000000000000, &t, &d)) != MP_OKAY) {
mp_clear(&t);
return res;
}
digs += 17;
}
}
#endif
while (mp_iszero(&t) == MP_NO) {
if ((res = mp_div_d(&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
mp_clear(&t);
return res;
}
++digs;
}
mp_clear(&t);
/* return digs + 1, the 1 is for the NULL byte that would be required. */
*size = digs + 1;
return MP_OKAY;
}
MVMString * MVM_bigint_to_str(MVMThreadContext *tc, MVMObject *a, int base) {
MVMP6bigintBody *body = get_bigint_body(tc, a);
if (MVM_BIGINT_IS_BIG(body)) {
mp_int *i = body->u.bigint;
int len;
char *buf;
MVMString *result;