forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbignum.cc
1020 lines (895 loc) · 31.4 KB
/
bignum.cc
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
// ****************************************************************************
// bignum.cc DB48X project
// ****************************************************************************
//
// File Description:
//
// Implementation of basic bignum operations
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2022 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
#include "bignum.h"
#include "arithmetic.h"
#include "fraction.h"
#include "integer.h"
#include "parser.h"
#include "renderer.h"
#include "runtime.h"
#include "settings.h"
#include "utf8.h"
#include <stdio.h>
RECORDER(bignum, 16, "Bignums");
bignum::bignum(id type, integer_g value)
// ----------------------------------------------------------------------------
// Create a bignum from an integer value
// ----------------------------------------------------------------------------
: text(type, value->payload(), bytesize(value))
{
byte *p = (byte *) payload();
size_t sz = leb128<size_t>(p);
if (sz)
{
byte_p q = value->payload();
uint c = 0;
uint bits = 0;
bool more;
do
{
byte b = *q++;
more = b & 0x80;
c |= (b & 0x7F) << bits;
bits += 7;
if (bits >= 8)
{
*p++ = byte(c);
c >>= 8;
bits -= 8;
}
} while (more);
if (c)
*p++ = byte(c);
}
}
size_t bignum::required_memory(id i, integer_g value)
// ----------------------------------------------------------------------------
// Compute the size to copy an integer value
// ----------------------------------------------------------------------------
{
size_t size = bytesize(value);
return leb128size(i) + leb128size(size) + size;
}
integer_p bignum::as_integer() const
// ----------------------------------------------------------------------------
// Check if this fits in a small integer, if so build it
// ----------------------------------------------------------------------------
{
size_t size = 0;
byte_p p = value(&size);
if (size > sizeof(ularge))
return nullptr;
ularge value = 0;
for (uint i = 0; i < size; i++)
value |= ularge(p[i]) << (i * 8);
id ty = type() == ID_neg_bignum ? ID_neg_integer : ID_integer;
return rt.make<integer>(ty, value);
}
HELP_BODY(bignum)
// ----------------------------------------------------------------------------
// Help topic for big integers
// ----------------------------------------------------------------------------
{
return utf8("Big integers");
}
static size_t render_num(renderer &r,
bignum_p num,
uint base,
cstring fmt)
// ----------------------------------------------------------------------------
// Convert an bignum value to the proper format
// ----------------------------------------------------------------------------
// This is necessary because the arm-none-eabi-gcc printf can't do 64-bit
// I'm getting non-sensible output
{
// If we render to a file, need to first render to scratchpad to be able to
// revert the digits in memory before writing
if (r.file_save())
{
renderer tmp(r.expression(), r.editing(), r.stack());
size_t result = render_num(tmp, num, base, fmt);
r.put(tmp.text(), result);
return result;
}
// Upper / lower rendering
bool upper = *fmt == '^';
bool lower = *fmt == 'v';
if (upper || lower)
fmt++;
if (!Settings.SmallFractions() || r.editing())
upper = lower = false;
static uint16_t fancy_upper_digits[10] =
{
L'⁰', L'¹', L'²', L'³', L'⁴',
L'⁵', L'⁶', L'⁷', L'⁸', L'⁹'
};
static uint16_t fancy_lower_digits[10] =
{
L'₀', L'₁', L'₂', L'₃', L'₄',
L'₅', L'₆', L'₇', L'₈', L'₉'
};
// Check which kind of spacing to use
bool based = *fmt == '#';
bool fancy_base = based && r.stack();
uint spacing = based ? Settings.BasedSpacing() : Settings.MantissaSpacing();
unicode space = based ? Settings.BasedSeparator() : Settings.NumberSeparator();
// Copy the '#' or '-' sign
if (*fmt)
r.put(*fmt++);
else
r.flush();
// Get denominator for the base
size_t findex = r.size();
bignum_g b = rt.make<bignum>(object::ID_bignum, base);
bignum_g n = (bignum *) num;
// Keep dividing by the base until we get 0
uint sep = 0;
do
{
bignum_g remainder = nullptr;
bignum_g quotient = nullptr;
if (!bignum::quorem(n, b, bignum::ID_bignum, "ient, &remainder))
break;
uint digit = remainder->value<uint>();
if (digit > base)
{
printf("Ooops: digit=%u, base=%u\n", digit, base);
bignum::quorem(n, b, bignum::ID_bignum, "ient, &remainder);
}
unicode c = upper ? fancy_upper_digits[digit]
: lower ? fancy_lower_digits[digit]
: (digit < 10) ? digit + '0'
: digit + ('A' - 10);
r.put(c);
n = quotient;
if (!n->is_zero() && ++sep == spacing)
{
sep = 0;
r.put(space);
}
} while (!n->is_zero());
// Revert the digits
byte *dest = (byte *) r.text();
bool multibyte = upper || lower || (spacing && space > 0xFF);
utf8_reverse(dest + findex, dest + r.size(), multibyte);
// Add suffix if there is one
if (fancy_base)
{
if (base / 10)
r.put(unicode(fancy_lower_digits[base/10]));
r.put(unicode(fancy_lower_digits[base%10]));
}
else if (*fmt)
r.put(*fmt++);
// Return the number of items we need
return r.size();
}
RENDER_BODY(bignum)
// ----------------------------------------------------------------------------
// Render the bignum into the given string buffer
// ----------------------------------------------------------------------------
{
size_t result = render_num(r, o, 10, "");
return result;
}
template<>
RENDER_BODY(neg_bignum)
// ----------------------------------------------------------------------------
// Render the negative bignum value into the given string buffer
// ----------------------------------------------------------------------------
{
return render_num(r, o, 10, "-");
}
#if CONFIG_FIXED_BASED_OBJECTS
template<>
RENDER_BODY(hex_bignum)
// ----------------------------------------------------------------------------
// Render the hexadecimal bignum value into the given string buffer
// ----------------------------------------------------------------------------
{
return render_num(r, o, 16, "#h");
}
template<>
RENDER_BODY(dec_bignum)
// ----------------------------------------------------------------------------
// Render the decimal based number
// ----------------------------------------------------------------------------
{
return render_num(r, o, 10, "#d");
}
template<>
RENDER_BODY(oct_bignum)
// ----------------------------------------------------------------------------
// Render the octal bignum value into the given string buffer
// ----------------------------------------------------------------------------
{
return render_num(r, o, 8, "#o");
}
template<>
RENDER_BODY(bin_bignum)
// ----------------------------------------------------------------------------
// Render the binary bignum value into the given string buffer
// ----------------------------------------------------------------------------
{
return render_num(r, o, 2, "#b");
}
#endif // CONFIG_FIXED_BASED_OBJECTS
template<>
RENDER_BODY(based_bignum)
// ----------------------------------------------------------------------------
// Render the hexadecimal bignum value into the given string buffer
// ----------------------------------------------------------------------------
{
return render_num(r, o, Settings.Base(), "#");
}
// ============================================================================
//
// Big bignum comparisons
//
// ============================================================================
int bignum::compare(bignum_r xg, bignum_r yg, bool magnitude)
// ----------------------------------------------------------------------------
// Compare two bignum values
// ----------------------------------------------------------------------------
{
id xt = xg->type();
id yt = yg->type();
// Negative bignums are always smaller than positive bignums
if (!magnitude)
{
if (xt == ID_neg_bignum && yt != ID_neg_bignum)
return -1;
else if (yt == ID_neg_bignum && xt != ID_neg_bignum)
return 1;
}
size_t xs = 0;
size_t ys = 0;
byte_p x = xg->value(&xs);
byte_p y = yg->value(&ys);
// First check if size difference is sufficient to let us decide
int result = xs - ys;
if (!result)
{
// Compare, starting with highest order
for (int i = xs - 1; !result && i >= 0; i--)
result = x[i] - y[i];
}
// If xt is ID_neg_bignum, then yt also must be, see test at top of function
if (!magnitude && xt == ID_neg_bignum)
result = -result;
return result;
}
// ============================================================================
//
// Big bignum arithmetic
//
// ============================================================================
// Operations with carry
static inline uint16_t add_op(byte x, byte y, byte c) { return x + y + (c != 0);}
static inline uint16_t sub_op(byte x, byte y, byte c) { return x - y - (c != 0);}
static inline uint16_t neg_op(byte x, byte c) { return -x - (c != 0); }
static inline byte not_op(byte x, byte ) { return ~x; }
static inline byte and_op(byte x, byte y, byte ) { return x & y; }
static inline byte or_op (byte x, byte y, byte ) { return x | y; }
static inline byte xor_op(byte x, byte y, byte ) { return x ^ y; }
inline object::id bignum::opposite_type(id type)
// ----------------------------------------------------------------------------
// Return the type of the opposite
// ----------------------------------------------------------------------------
{
switch(type)
{
case ID_bignum: return ID_neg_bignum;
case ID_neg_bignum: return ID_bignum;
default: return type;
}
}
bignum_p operator-(bignum_r xg)
// ----------------------------------------------------------------------------
// Negate the input value
// ----------------------------------------------------------------------------
{
object::id xt = xg->type();
size_t xs = 0;
byte_p x = xg->value(&xs);
// Deal with simple case where we can simply copy the payload
if (xt == object::ID_bignum)
return rt.make<bignum>(object::ID_neg_bignum, x, xs);
else if (xt == object::ID_neg_bignum)
return rt.make<bignum>(object::ID_bignum, x, xs);
// Complicated case of based numbers: need to actually compute the opposite
return bignum::unary<true>(neg_op, xg);
}
bignum_p operator~(bignum_r x)
// ----------------------------------------------------------------------------
// Boolean not
// ----------------------------------------------------------------------------
{
object::id xt = x->type();
// For bignum and neg_bignum, do a 0/1 logical not
if (xt == object::ID_bignum || xt == object::ID_neg_bignum)
return rt.make<bignum>(object::ID_bignum, x->is_zero());
// For hex_bignum and other based numbers, do a binary not
return bignum::unary<true>(not_op, x);
}
bignum_p bignum::add_sub(bignum_r y, bignum_r x, bool issub)
// ----------------------------------------------------------------------------
// Add the two bignum values
// ----------------------------------------------------------------------------
{
if (!x|| !y)
return nullptr;
id yt = y->type();
id xt = x->type();
bool based = is_based(xt) || is_based(yt);
bignum_g xg = x;
bignum_g yg = y;
// Check if we have opposite signs
bool samesgn = (xt == ID_neg_bignum) == (yt == ID_neg_bignum);
if (samesgn == issub)
{
int cmp = based ? 0 : compare(yg, xg, true);
if (cmp >= 0)
{
// abs Y > abs X: result has opposite type of X
id ty = based ? xt
: cmp == 0 ? ID_bignum
: issub ? xt
: opposite_type(xt);
return binary<false>(sub_op, yg, xg, ty);
}
else
{
// abs Y < abs X: result has type of X
id ty = issub ? opposite_type(xt) : xt;
return binary<false>(sub_op, xg, yg, ty);
}
}
// We have the same sign, add items
id ty = issub ? opposite_type(xt) : xt;
return binary<false>(add_op, yg, xg, ty);
}
template <bignum_p (*code)(bignum_r, bignum_r)>
arithmetic_fn target(algebraic_r x, algebraic_r y)
// ----------------------------------------------------------------------------
// Target function for bignum objects
// ----------------------------------------------------------------------------
{
return x->is_bignum() && y->is_bignum() ? arithmetic_fn(code) : nullptr;
}
bignum_p operator+(bignum_r y, bignum_r x)
// ----------------------------------------------------------------------------
// Add the two bignum values, result has type of x
// ----------------------------------------------------------------------------
{
add::remember(target< operator+ >);
return bignum::add_sub(y, x, false);
}
bignum_p operator-(bignum_r y, bignum_r x)
// ----------------------------------------------------------------------------
// Subtract two bignum values, result has type of x
// ----------------------------------------------------------------------------
{
subtract::remember(target< operator- >);
return bignum::add_sub(y, x, true);
}
bignum_p operator&(bignum_r y, bignum_r x)
// ----------------------------------------------------------------------------
// Perform a binary and operation
// ----------------------------------------------------------------------------
{
return bignum::binary<false>(and_op, x, y, x->type());
}
bignum_p operator|(bignum_r y, bignum_r x)
// ----------------------------------------------------------------------------
// Perform a binary or operation
// ----------------------------------------------------------------------------
{
return bignum::binary<false>(or_op, x, y, x->type());
}
bignum_p operator^(bignum_r y, bignum_r x)
// ----------------------------------------------------------------------------
// Perform a binary xor operation
// ----------------------------------------------------------------------------
{
return bignum::binary<false>(xor_op, x, y, x->type());
}
bignum_p bignum::multiply(bignum_r yg, bignum_r xg, id ty)
// ----------------------------------------------------------------------------
// Perform multiply operation on the two big nums, with result type ty
// ----------------------------------------------------------------------------
{
size_t xs = 0;
size_t ys = 0;
byte_p x = xg->value(&xs); // Read sizes and pointers
byte_p y = yg->value(&ys);
id xt = xg->type();
size_t wbits = wordsize(xt);
size_t wbytes = (wbits + 7) / 8;
size_t needed = xs + ys;
if (needed * 8 > Settings.MaxNumberBits())
{
rt.number_too_big_error();
return nullptr;
}
if (wbits && needed > wbytes)
needed = wbytes;
byte *buffer = rt.allocate(needed); // May GC here
if (!buffer)
return nullptr; // Out of memory
x = xg->value(&xs); // Re-read after potential GC
y = yg->value(&ys);
// Zero-initialie the result
for (size_t i = 0; i < needed; i++)
buffer[i] = 0;
// Loop on all bytes of x then y
for (size_t xi = 0; xi < xs; xi++)
{
byte xd = x[xi];
for (int bit = 0; xd && bit < 8; bit++)
{
if (xd & (1<<bit))
{
uint c = 0;
size_t yi;
for (yi = 0; yi < ys && xi + yi < needed; yi++)
{
c += buffer[xi + yi] + (y[yi] << bit);
buffer[xi + yi] = byte(c);
c >>= 8;
}
while (c && xi + yi < needed)
{
c += buffer[xi + yi];
buffer[xi + yi] = byte(c);
c >>= 8;
yi++;
}
xd &= ~(1<<bit);
}
}
}
size_t sz = needed;
while (sz > 0 && buffer[sz-1] == 0)
sz--;
gcbytes buf = buffer;
bignum_g result = rt.make<bignum>(ty, buf, sz);
rt.free(needed);
return result;
}
bignum_p operator*(bignum_r y, bignum_r x)
// ----------------------------------------------------------------------------
// Multiplication of bignums
// ----------------------------------------------------------------------------
{
if (!x || !y)
return nullptr;
multiply::remember(target< operator* >);
object::id xt = x->type();
object::id yt = y->type();
object::id prodtype = bignum::product_type(yt, xt);
return bignum::multiply(y, x, prodtype);
}
bool bignum::quorem(bignum_r yg, bignum_r xg, id ty, bignum_g *q, bignum_g *r)
// ----------------------------------------------------------------------------
// Compute quotient and remainder of two bignums, as bignums
// ----------------------------------------------------------------------------
// Result is placed in scratchpad, the function returns the size in bytes
{
if (xg->is_zero())
{
rt.zero_divide_error();
return false;
}
// In the computations below (e.g. needed), the size of the quotient is
// less than the size of y, and the size of the remainder is less than the
// size of x, therefore, we need at most xs + ys for both.
// However, the computation of the remainder requires a subtraction
// which can be one byte larger than x (see issue #70 for details).
// For example in 0x17B/0xEF, the first remainder subtraction will be
// 0x17B - 0xEF, which does require two bytes, not just one. It could
// be carried out by keeping the extra byte in a variable, but it's
// simpler to allocate one extra byte.
size_t xs = 0;
size_t ys = 0;
byte_p x = xg->value(&xs); // Read those after potential GC
byte_p y = yg->value(&ys);
id xt = xg->type();
size_t wbits = wordsize(xt);
size_t wbytes = (wbits + 7) / 8;
size_t needed = ys + xs + 1; // No need to check maxbignum
byte *buffer = rt.allocate(needed); // May GC here
if (!buffer)
return false; // Out of memory
x = xg->value(&xs); // Re-read after potential GC
y = yg->value(&ys);
// Pointers ot quotient and remainder, initializer to 0
byte *quotient = buffer;
byte *remainder = quotient + ys;
size_t rs = 0;
size_t qs = 0;
for (uint i = 0; i < needed; i++)
buffer[i] = 0;
// Loop on the numerator
for (int yi = ys-1; yi >= 0; yi--)
{
for (int bit = 7; bit >= 0; bit--)
{
// Shift remainder left by one bit, add numerator bit
uint16_t c = (y[yi] >> bit) & 1;
int delta = 0;
for (uint ri = 0; ri < rs; ri++)
{
c += (remainder[ri] << 1);
remainder[ri] = byte(c);
if (int d = remainder[ri] - x[ri])
delta = d;
c >>= 8;
}
if (c)
{
if (int d = c - x[rs])
delta = d;
remainder[rs++] = c;
}
if (rs != xs)
delta = int(rs) - int(xs);
// If remainder >= denominator, add to quotient, subtract from rem
if (delta >= 0)
{
quotient[yi] |= (1 << bit);
if (qs < size_t(yi + 1))
qs = yi + 1;
c = 0;
for (uint ri = 0; ri < rs; ri++)
{
c = remainder[ri] - (ri < xs ? x[ri] : 0) - c;
remainder[ri] = byte(c);
c = c > 0xFF;
}
// Strip zeroes at top of remainder
while (rs > 0 && remainder[rs-1] == 0)
rs--;
}
} // numerator bit loop
} // numerator byte loop
// Generate results
gcutf8 qg = quotient;
gcutf8 rg = remainder;
bool ok = true;
if (q)
{
if (wbits && qs > wbytes)
qs = wbytes;
*q = rt.make<bignum>(ty, qg, qs);
ok = bignum_p(*q) != nullptr;
}
if (r && ok)
{
if (wbits && rs > wbytes)
rs = wbytes;
*r = rt.make<bignum>(ty, rg, rs);
ok = bignum_p(*r) != nullptr;
}
rt.free(needed);
return ok;
}
static bignum_p divide_and_optimize(bignum_r y, bignum_r x)
// ----------------------------------------------------------------------------
// Invoked we are called through the arithmetic optimization target
// ----------------------------------------------------------------------------
{
object::id yt = y->type();
object::id xt = x->type();
object::id prodtype = bignum::product_type(yt, xt);
bignum_g q, r;
bignum::quorem(y, x, prodtype, &q, &r);
if (r->is_zero())
return q;
return bignum_p(big_fraction::make(y, x));
}
bignum_p operator/(bignum_r y, bignum_r x)
// ----------------------------------------------------------------------------
// Perform long division of y by x
// ----------------------------------------------------------------------------
{
if (!x || !y)
return nullptr;
// Can't do: div::remember(target<operator/>);
// because the division can generate fractions
divide::remember(target<divide_and_optimize>);
object::id yt = y->type();
object::id xt = x->type();
object::id prodtype = bignum::product_type(yt, xt);
bignum_g q;
bignum::quorem(y, x, prodtype, &q, nullptr);
return q;
}
bignum_p operator%(bignum_r y, bignum_r x)
// ----------------------------------------------------------------------------
// Perform long-remainder of y by x
// ----------------------------------------------------------------------------
{
if (!x || !y)
return nullptr;
rem::remember(target< operator% >);
object::id yt = y->type();
bignum_g r = nullptr;
bignum::quorem(y, x, yt, nullptr, &r);
return r;
}
bignum_p bignum::pow(bignum_r yr, bignum_r xr)
// ----------------------------------------------------------------------------
// Compute y^abs(x)
// ----------------------------------------------------------------------------
// Note that the case where x is negative should be filtered by caller
{
if (!xr || !yr)
return nullptr;
pow::remember(target<pow>);
bignum_g r = bignum::make(1);
size_t xs = 0;
byte_p x = xr->value(&xs);
bignum_g y = yr;
for (size_t xi = 0; xi < xs; xi++)
{
byte xv = x[xi];
for (uint bit = 0; (xv || xi+1 < xs) && bit < 8; bit++)
{
if (xv & 1)
r = r * y;
xv >>= 1;
if (xv || xi < xs-1)
y = y * y;
}
}
return r;
}
static size_t fraction_render(big_fraction_p o, renderer &r, bool negative)
// ----------------------------------------------------------------------------
// Common code for positive and negative fractions
// ----------------------------------------------------------------------------
{
bignum_g n = o->numerator();
bignum_g d = o->denominator();
if (negative)
r.put('-');
if (r.stack() && Settings.MixedFractions())
{
bignum_g quo, rem;
if (bignum::quorem(n, d, bignum::ID_bignum, &quo, &rem))
{
if (!quo->is_zero())
{
render_num(r, quo, 10, "");
r.put(unicode(settings::SPACE_MEDIUM_MATH));
n = rem;
}
}
}
render_num(r, n, 10, "^");
r.put('/');
render_num(r, d, 10, "v");
return r.size();
}
RENDER_BODY(big_fraction)
// ----------------------------------------------------------------------------
// Render the fraction as 'num/den'
// ----------------------------------------------------------------------------
{
return fraction_render(o, r, false);
}
RENDER_BODY(neg_big_fraction)
// ----------------------------------------------------------------------------
// Render the fraction as '-num/den'
// ----------------------------------------------------------------------------
{
return fraction_render(o, r, true);
}
// ============================================================================
//
// Shift and rotate operations
//
// ============================================================================
bignum_p bignum::shift(bignum_r xg, int bits, bool rotate, bool arith)
// ----------------------------------------------------------------------------
// Perform a shift / rotate operation on a bignum
// ----------------------------------------------------------------------------
// This uses the scratch pad AND can cause garbage collection
// Bits is signed like a memory offset, so bits>0 shifts left
{
if (bits == 0)
return +xg;
if (!xg)
return nullptr;
size_t xs = 0;
byte_p x = xg->value(&xs);
id xt = xg->type();
size_t ws = Settings.WordSize();
size_t wbits = (rotate || arith) ? ws : wordsize(xt);
size_t wbytes = (wbits + 7) / 8;
size_t abits = bits < 0 ? 0 : bits;
size_t needed = std::max(xs + (abits + 7) / 8, wbytes) ;
if (needed * 8 > Settings.MaxNumberBits())
{
rt.number_too_big_error();
return nullptr;
}
if (wbits)
needed = wbytes;
else
wbytes = needed;
byte *buffer = rt.allocate(needed); // May GC here
if (!buffer)
return nullptr; // Out of memory
x = xg->value(&xs); // Re-read after potential GC
byte *end = buffer + needed;
// Start with zeroes
for (uint i = 0; i < needed; i++)
buffer[i] = 0;
// Check if we shift by "too much"
bool done = false;
if (bits > int(ws) || bits < -int(ws))
{
// If we want to return 0, do so
done = (!rotate && !arith);
bits %= wbits;
}
if (!done)
{
// Process input data
size_t max = std::min(xs, needed);
int o = (bits / 8) % int(needed);
if (bits > 0)
{
// Shift left (we store data little endian)
uint lbits = bits % 8;
for (uint i = 0; i < max; i++)
{
uint xd = x[i] << lbits;
if (rotate || size_t(o) < needed)
buffer[o % needed] |= byte(xd);
o++;
if (rotate || size_t(o) < needed)
buffer[o % needed] |= byte(xd >> 8);
}
}
else
{
// Shift right
uint lbits = (-bits) % 8;
bool sbit = arith && xs >= wbytes && (x[xs-1]&(1<<((wbits-1) % 8)));
if (rotate)
o += needed;
o--;
for (uint i = 0; i < max; i++)
{
uint xd = (uint(x[i]) << 8) >> lbits;
if (o >= 0)
buffer[o % needed] |= byte(xd);
o++;
if (o >= 0)
buffer[o % needed] |= byte(xd>>8);
}
if (sbit)
{
byte *d = end - 1;
while (bits < -8)
{
*d-- = 0xFF;
bits += 8;
}
*d |= 0xFF << (8+bits);
}
}
}
// Drop highest zeros (this can reach i == 0 for value 0)
while (wbytes > 0 && buffer[wbytes - 1] == 0)
wbytes--;
// Check if we have a word size like 12 and we need to truncate result
if (wbytes == needed && (wbits % 8))
buffer[wbytes-1] &= byte(0xFFu >> (8 - wbits % 8));
// Create the resulting bignum
gcbytes buf = buffer;
bignum_g result = rt.make<bignum>(xt, buf, wbytes);
rt.free(needed);
return result;
}
bignum_p operator<<(bignum_r y, bignum_r x)
// ----------------------------------------------------------------------------
// Shift left
// ----------------------------------------------------------------------------
{
if (!x || !y)
return nullptr;
uint shift = x->as_uint32(0, true);
if (rt.error())
return nullptr;
return bignum::shift(y, int(shift), false, false);
}
bignum_p operator>>(bignum_r y, bignum_r x)
// ----------------------------------------------------------------------------
// Shift right (as an unsigned)
// ----------------------------------------------------------------------------
{
if (!x || !y)
return nullptr;
uint shift = x->as_uint32(0, true);
if (rt.error())
return nullptr;
return bignum::shift(y, -int(shift), false, false);
}
bignum_p operator<<(bignum_r y, uint x)
// ----------------------------------------------------------------------------
// Shift left
// ----------------------------------------------------------------------------
{
if (!y)
return nullptr;
return bignum::shift(y, int(x), false, false);
}
bignum_p operator>>(bignum_r y, uint x)
// ----------------------------------------------------------------------------
// Shift right (as an unsigned)
// ----------------------------------------------------------------------------
{
if (!y)
return nullptr;
return bignum::shift(y, -int(x), false, false);
}
bignum_p bignum::promote(object_p obj)
// ----------------------------------------------------------------------------
// Promote integer values to bignum
// ----------------------------------------------------------------------------
{
if (!obj)
return nullptr;
id oty = obj->type();
if (is_bignum(oty))
return bignum_p(obj);