-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigint.c
6017 lines (5143 loc) · 181 KB
/
bigint.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 "bigint.h"
#include "general.h"
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#ifdef BIGINT_ENABLE_LIBMATH
#include <math.h>
#endif
#ifdef BIGINT_ENABLE_WINTHREADS
#include <windows.h>
#endif
#ifdef BIGINT_ENABLE_PTHREADS
#include <pthread.h>
#endif
#if BIGINT_MINLEAFS < 1
#error BIGINT_MINLEAFS must be at least 1
#endif
void mul128(uint64_t a, uint64_t b, uint64_t *h, uint64_t *l)
{
/* final result is equal to ha*hb*2^64 + (la*hb+ha*lb)*2^32 + la*lb */
uint64_t t, m;
uint64_t la = a & 0xFFFFFFFF, lb = b & 0xFFFFFFFF, ha = a >> 32, hb = b >> 32;
t = la * hb;
*h = t >> 32;
m = ha * lb;
*h += m >> 32;
*h += ha * hb;
*l = a * b;
}
/* initialize a new bigint with given size to 0 and return it */
/* returns bi, or NULL on out of memory condition */
/* bi is NOT destroyed by this function, but will have a NULL data pointer if this function returns NULL */
static bigint *bi_init_sized(bigint *bi, size_t size)
{
bi->size = size;
bi->flags = BIGINT_FLAG_GROWABLE | BIGINT_FLAG_FREEABLE;
bi->data = calloc(size, sizeof(*bi->data));
if (bi->data == NULL)
return NULL;
return bi;
}
/* create a new bigint with given size initialized to 0 and return it */
/* returns NULL on out of memory condition */
static bigint *bi_new_sized(size_t size)
{
bigint *ptr = malloc(sizeof(*ptr));
if (ptr == NULL || bi_init_sized(ptr, size) == NULL)
{
free(ptr);
return NULL;
}
ptr->flags |= BIGINT_FLAG_DESTROYABLE; // Make destroyable, which bi_init_sized() does not do
return ptr;
}
/* resize bi to given size, preserving the value, and return bi */
/* returns NULL and destroys bi on out of memory condition */
static bigint *bi_resize(bigint *bi, size_t size)
{
void *temp;
size_t old_size;
if (bi->size >= size) return bi;
if (!bi_is_growable(bi))
{
old_size = bi->size;
bi->size = size;
temp = calloc(size, sizeof(bi_leaf));
if (temp == NULL)
{
bi_destroy(bi);
return NULL;
}
memcpy(temp, bi->data, old_size * sizeof(bi_leaf));
bi->data = temp;
bi->flags |= BIGINT_FLAG_GROWABLE | BIGINT_FLAG_FREEABLE;
}
else
{
old_size = bi->size;
bi->size = size;
temp = realloc(bi->data, size * sizeof(bi_leaf));
if (temp == NULL)
{
free(bi->data);
free(bi);
return NULL;
}
bi->data = temp;
memset(bi->data + old_size, 0, (size-old_size) * sizeof(bi_leaf));
}
return bi;
}
/* returns 1 if the topmost leaf of bi is non-zero */
static size_t bi_full(const bigint *bi)
{
return bi->size == 0 || bi->data[bi->size - 1] != 0;
}
/* returns bit position of highest set bit, 1-indexed
* (equivalent to the number of bits required to store the number) */
static size_t bi_bitcount(const bigint *bi)
{
size_t i = bi->size;
while (i > 0)
if (bi->data[--i] != 0)
{
bi_leaf l = bi->data[i];
i *= BIGINT_LEAFBITS;
do ++i; while (l >>= 1);
return i;
}
return 0;
}
/* returns bit at specified position, 0-indexed */
/* returns -1 if specified bit is out of range */
bi_signed_leaf bi_bit(const bigint *bi, size_t bit)
{
size_t word = bit / BIGINT_LEAFBITS;
if (word >= bi->size)
return -1;
return (bi->data[word] >> (bit % BIGINT_LEAFBITS)) & 1;
}
/* returns new bit value on success */
/* returns -1 if specified bit is out of range */
int bi_set_bit(bigint *bi, size_t bit, int value)
{
size_t word = bit / BIGINT_LEAFBITS;
if (word >= bi->size)
return -1;
bit %= BIGINT_LEAFBITS;
bi->data[word] = (bi->data[word] & ~((bi_leaf) 1 << bit)) | ((bi_leaf) (value != 0) << bit);
return value != 0;
}
/* returns number of leaves used in bi */
size_t bi_used(const bigint *bi)
{
size_t i = bi->size;
for (; i > 0; )
if (bi->data[--i] != 0)
return i+1;
return 0;
}
/* create a new bigint initialized to 0 and return it */
/* returns NULL on out of memory condition */
bigint *bi_new()
{
return bi_new_sized(BIGINT_MINLEAFS);
}
/* initialize bi (from an uninitialized state) to 0 and return it */
/* returns bi, or returns NULL on out of memory condition */
bigint *bi_init(bigint *bi)
{
return bi_init_sized(bi, BIGINT_MINLEAFS);
}
bigint *bi_new_valueu(bi_leaf value)
{
bigint *b = bi_new_sized(BIGINT_MINLEAFS);
if (b == NULL || bi_assignu(b, value) == NULL)
return NULL;
return b;
}
bigint *bi_init_valueu(bigint *bi, bi_leaf value)
{
if (bi_init_sized(bi, BIGINT_MINLEAFS) == NULL ||
bi_assignu(bi, value) == NULL)
return NULL;
return bi;
}
bigint *bi_new_value(bi_signed_leaf value)
{
bigint *b = bi_new_sized(BIGINT_MINLEAFS);
if (b == NULL || bi_assign(b, value) == NULL)
return NULL;
return b;
}
bigint *bi_init_value(bigint *bi, bi_signed_leaf value)
{
if (bi_init_sized(bi, BIGINT_MINLEAFS) == NULL ||
bi_assign(bi, value) == NULL)
return NULL;
return bi;
}
bigint *bi_new_valuelu(bi_uintmax value)
{
bigint *b = bi_new_sized(BIGINT_MINLEAFS);
if (b == NULL || bi_assignlu(b, value) == NULL)
return NULL;
return b;
}
bigint *bi_init_valuelu(bigint *bi, bi_uintmax value)
{
if (bi_init_sized(bi, BIGINT_MINLEAFS) == NULL ||
bi_assignlu(bi, value) == NULL)
return NULL;
return bi;
}
bigint *bi_new_valuel(bi_intmax value)
{
bigint *b = bi_new_sized(BIGINT_MINLEAFS);
if (b == NULL || bi_assignl(b, value) == NULL)
return NULL;
return b;
}
bigint *bi_init_valuel(bigint *bi, bi_intmax value)
{
if (bi_init_sized(bi, BIGINT_MINLEAFS) == NULL ||
bi_assignl(bi, value) == NULL)
return NULL;
return bi;
}
/* returns a bigint with the value 0 */
bigint bi_zero()
{
static bi_leaf leaf = 0;
bigint b;
b.data = &leaf;
b.size = 1;
b.flags = 0;
return b;
}
/* returns a bigint with the value 1 */
bigint bi_one()
{
static bi_leaf leaf = 1;
bigint b;
b.data = &leaf;
b.size = 1;
b.flags = 0;
return b;
}
/* returns a bigint with the value 2 */
bigint bi_two()
{
static bi_leaf leaf = 2;
bigint b;
b.data = &leaf;
b.size = 1;
b.flags = 0;
return b;
}
/* returns a bigint with the value -1 */
bigint bi_minus_one()
{
static bi_leaf leaf = 1;
bigint b;
b.data = &leaf;
b.size = 1;
b.flags = BIGINT_FLAG_SIGN;
return b;
}
/* create a direct copy of bi and return it */
/* returns NULL on out of memory condition */
bigint *bi_copy(const bigint *bi)
{
bigint *ptr;
ptr = bi_new_sized(bi->size);
if (ptr == NULL) return NULL;
memcpy(ptr->data, bi->data, bi->size * sizeof(bi_leaf));
bi_set_negative(ptr, bi_is_negative(bi));
return ptr;
}
/* create a direct copy of src in dst and return it */
/* this function expects dst to be uninitialized when called */
/* returns NULL on out of memory condition */
bigint *bi_init_copy(bigint *dst, const bigint *src)
{
if (bi_init(dst) == NULL ||
bi_copy_to(dst, src) == NULL)
return NULL;
return dst;
}
/* copy magnitude of src to previously created bigint dst */
/* returns NULL and destroys dst on out of memory condition, otherwise returns dst */
/* copying to the same bigint has no effect */
bigint *bi_copy_mag_to(bigint *dst, const bigint *src)
{
if (dst == src)
return dst;
if (bi_resize(dst, src->size) == NULL) return NULL;
memcpy(dst->data, src->data, src->size * sizeof(bi_leaf));
memset(dst->data + src->size, 0, (dst->size - src->size) * sizeof(bi_leaf));
return dst;
}
/* copy src to previously created bigint dst */
/* returns NULL and destroys dst on out of memory condition, otherwise returns dst */
/* copying to the same bigint has no effect */
bigint *bi_copy_to(bigint *dst, const bigint *src)
{
if (bi_copy_mag_to(dst, src) == NULL) return NULL;
bi_set_negative(dst, bi_is_negative(src));
return dst;
}
/* assigns a previously created bigint the value 0 */
bigint *bi_clear(bigint *bi)
{
bi_set_negative(bi, 0);
memset(bi->data, 0, bi->size * sizeof(bi_leaf));
return bi;
}
/* assigns a previously created bigint a specified unsigned value */
bigint *bi_assignu(bigint *bi, bi_leaf value)
{
bi_clear(bi);
if (bi->size == 0)
return NULL;
bi->data[0] = value;
return bi;
}
/* assigns a previously created bigint a specified value */
bigint *bi_assign(bigint *bi, bi_signed_leaf value)
{
bi_leaf uvalue;
if (value >= 0)
uvalue = value;
else if (value == -value)
uvalue = (bi_leaf) 1 << (sizeof(value)*CHAR_BIT - 1);
else
uvalue = (bi_leaf) -value;
if (bi_assignu(bi, uvalue) == NULL)
return NULL;
bi_set_negative(bi, value < 0);
return bi;
}
/* assigns a previously created bigint a specified value */
/* returns NULL and destroys bi on out of memory condition */
bigint *bi_assignl(bigint *bi, bi_intmax value)
{
bi_uintmax uvalue;
if (value >= 0)
uvalue = value;
else if (-value == value)
uvalue = (bi_uintmax) 1 << (sizeof(bi_intmax)*CHAR_BIT-1);
else
uvalue = -value;
if (bi_assignlu(bi, uvalue) == NULL)
return NULL;
bi_set_negative(bi, value < 0);
return bi;
}
/* assigns a previously created bigint a specified value */
/* returns NULL and destroys bi on out of memory condition */
bigint *bi_assignlu(bigint *bi, bi_uintmax uvalue)
{
size_t i = 0;
bi_clear(bi);
if (bi_resize(bi, BIGINT_LEAFS_PER_BI_INTMAX) == NULL) return NULL;
for (; i < sizeof(uvalue)*CHAR_BIT/8; i += BIGINT_LEAFBYTES)
{
bi->data[i/BIGINT_LEAFBYTES] = uvalue;
uvalue >>= BIGINT_LEAFBITS;
}
return bi;
}
/* converts a bigint to an unsigned leaf-sized integer */
bi_leaf bi_to_intu(const bigint *bi)
{
return bi->data[0];
}
/* converts a bigint to a signed leaf-sized integer */
bi_signed_leaf bi_to_int(const bigint *bi)
{
bi_signed_leaf result = bi->data[0] & BIGINT_SIGNED_LEAFMAX;
return bi_is_negative(bi)? -result: result;
}
/* converts a bigint to an bi_intmax */
bi_intmax bi_to_intl(const bigint *bi)
{
bi_uintmax uvalue = bi_to_intlu(bi);
/* TODO: may have portability issues */
return bi_is_negative(bi)? -(bi_intmax) uvalue: (bi_intmax) uvalue;
}
/* converts a bigint to a bi_uintmax */
bi_uintmax bi_to_intlu(const bigint *bi)
{
bi_uintmax uvalue = 0;
size_t i = min(sizeof(uvalue)*CHAR_BIT/8, bi->size*BIGINT_LEAFBYTES);
for (; i > 0; i -= BIGINT_LEAFBYTES)
{
uvalue <<= BIGINT_LEAFBITS;
uvalue |= bi->data[i/BIGINT_LEAFBYTES-1];
}
return uvalue;
}
#ifdef BIGINT_ENABLE_LIBMATH
/* assigns value of f to bi, with fractional part truncated toward zero */
/* destroys bi and returns NULL if out of memory */
/* `err` contains reason the value could not be converted to an integer (+-INFINITY, NAN) */
bigint *bi_assign_float(bigint *bi, float f, bi_libmath_err *err)
{
bi_uintmax i = 0, temp = 0;
int exp = 0;
float mantissa;
bi_clear(bi);
int neg;
if (!isfinite(f))
{
if (err != NULL)
*err = isnan(f)? bi_libmath_nan: bi_libmath_inf;
}
else
{
if (err != NULL)
*err = bi_libmath_none;
neg = f < 0.0;
/* convert to whole number */
mantissa = floorf(fabsf(f));
/* extract mantissa and exponent base 10 */
if (mantissa != 0)
exp = floorf(log10f(mantissa));
mantissa *= powf(10, FLT_DIG-exp);
/* convert mantissa to integer */
i = floorf(mantissa);
/* round by last digit */
mantissa = (mantissa - floorf(mantissa));
i += roundf(mantissa) > 0.5;
/* normalize integral mantissa and exponent */
if (i)
{
while (i % 10 == 0) i /= 10;
temp = i;
while (temp /= 10) --exp;
}
/* assign to result */
if (bi_assignlu(bi, i) == NULL)
return NULL;
/* scale to exponent if not zero */
while (exp-- > 0)
if (bi_mul_immediate_assign(bi, 10) == NULL)
return NULL;
/* negate if double was negative */
if (neg)
bi_negate_assign(bi);
}
return bi;
}
/* assigns value of f to bi, with fractional part truncated toward zero */
/* destroys bi and returns NULL if out of memory */
/* `err` contains reason the value could not be converted to an integer (+-INFINITY, NAN) */
bigint *bi_assign_double(bigint *bi, double f, bi_libmath_err *err)
{
bi_uintmax i = 0, temp = 0;
int exp = 0;
double mantissa;
bi_clear(bi);
int neg;
if (!isfinite(f))
{
if (err != NULL)
*err = isnan(f)? bi_libmath_nan: bi_libmath_inf;
}
else
{
if (err != NULL)
*err = bi_libmath_none;
neg = f < 0.0;
/* convert to whole number */
mantissa = floor(fabs(f));
/* extract mantissa and exponent base 10 */
if (mantissa != 0)
exp = floor(log10(mantissa));
mantissa *= pow(10, DBL_DIG-exp);
/* convert mantissa to integer */
i = floor(mantissa);
/* round by last digit */
mantissa = (mantissa - floor(mantissa));
i += round(mantissa) > 0.5;
/* normalize integral mantissa and exponent */
if (i)
{
while (i % 10 == 0) i /= 10;
temp = i;
while (temp /= 10) --exp;
}
/* assign to result */
if (bi_assignlu(bi, i) == NULL)
return NULL;
/* scale to exponent if not zero */
while (exp-- > 0)
if (bi_mul_immediate_assign(bi, 10) == NULL)
return NULL;
/* negate if double was negative */
if (neg)
bi_negate_assign(bi);
}
return bi;
}
/* assigns value of f to bi, with fractional part truncated toward zero */
/* destroys bi and returns NULL if out of memory */
/* `err` contains reason the value could not be converted to an integer (+-INFINITY, NAN) */
bigint *bi_assign_doublel(bigint *bi, long double f, bi_libmath_err *err)
{
bi_uintmax i = 0, temp = 0;
int exp = 0;
long double mantissa;
bi_clear(bi);
int neg;
if (!isfinite(f))
{
if (err != NULL)
*err = isnan(f)? bi_libmath_nan: bi_libmath_inf;
}
else
{
if (err != NULL)
*err = bi_libmath_none;
neg = f < 0.0;
/* convert to whole number */
mantissa = floorl(fabsl(f));
/* extract mantissa and exponent base 10 */
if (mantissa != 0)
exp = floorl(log10l(mantissa));
mantissa *= powl(10, LDBL_DIG-exp);
/* convert mantissa to integer */
i = floorl(mantissa);
/* round by last digit */
mantissa = (mantissa - floorl(mantissa));
i += roundl(mantissa) > 0.5;
/* normalize integral mantissa and exponent */
if (i)
{
while (i % 10 == 0) i /= 10;
temp = i;
while (temp /= 10) --exp;
}
/* assign to result */
if (bi_assignlu(bi, i) == NULL)
return NULL;
/* scale to exponent if not zero */
while (exp-- > 0)
if (bi_mul_immediate_assign(bi, 10) == NULL)
return NULL;
/* negate if double was negative */
if (neg)
bi_negate_assign(bi);
}
return bi;
}
/* converts a bigint to a float, returning +-INFINITY if out-of-range, returns NaN if out-of-memory */
float bi_to_float(const bigint *bi)
{
float result = 0.0;
size_t bits = bi_bitcount(bi), scale;
bigint *n = NULL;
if (bits > BIGINT_LEAFS_PER_BI_INTMAX*BIGINT_LEAFBITS)
{
scale = bits - BIGINT_LEAFS_PER_BI_INTMAX*BIGINT_LEAFBITS;
n = bi_shr(bi, scale);
if (n == NULL)
return NAN;
result = bi_to_intlu(n);
bi_destroy(n);
result *= powf(2.0, scale);
}
else
result = bi_to_intlu(bi);
if (bi_is_negative(bi))
result = -result;
return result;
}
/* converts a bigint to a double, returning +-INFINITY if out-of-range, returns NaN if out-of-memory */
double bi_to_double(const bigint *bi)
{
double result = 0.0;
size_t bits = bi_bitcount(bi), scale;
bigint *n = NULL;
if (bits > BIGINT_LEAFS_PER_BI_INTMAX*BIGINT_LEAFBITS)
{
scale = bits - BIGINT_LEAFS_PER_BI_INTMAX*BIGINT_LEAFBITS;
n = bi_shr(bi, scale);
if (n == NULL)
return NAN;
result = bi_to_intlu(n);
bi_destroy(n);
result *= pow(2.0, scale);
}
else
result = bi_to_intlu(bi);
if (bi_is_negative(bi))
result = -result;
return result;
}
/* converts a bigint to a long double, returning +-INFINITY if out-of-range, returns NaN if out-of-memory */
long double bi_to_doublel(const bigint *bi)
{
long double result = 0.0;
size_t bits = bi_bitcount(bi), scale;
bigint *n = NULL;
if (bits > BIGINT_LEAFS_PER_BI_INTMAX*BIGINT_LEAFBITS)
{
scale = bits - BIGINT_LEAFS_PER_BI_INTMAX*BIGINT_LEAFBITS;
n = bi_shr(bi, scale);
if (n == NULL)
return NAN;
result = bi_to_intlu(n);
bi_destroy(n);
result *= powl(2.0, scale);
}
else
result = bi_to_intlu(bi);
if (bi_is_negative(bi))
result = -result;
return result;
}
#endif // BIGINT_ENABLE_LIBMATH
/* compares two bigints. returns -1 if bi < bi2, 0 if bi == b2, 1 if bi > b2 */
/* only compares magnitude, does not compare signs */
int bi_cmp_mag(const bigint *bi, const bigint *bi2)
{
size_t i;
for (i = bi->size; i > bi2->size; --i)
if (bi->data[i-1] != 0)
return 1;
for (i = bi2->size; i > bi->size; --i)
if (bi2->data[i-1] != 0)
return -1;
for (; i > 0; --i)
{
bi_leaf cmp = bi->data[i-1] - bi2->data[i-1];
if (cmp)
return cmp > bi->data[i-1]? -1: 1;
}
return 0;
}
/* compares bi with val. returns -1 if bi < val, 0 if bi == val, 1 if bi > val */
/* compares signs as well as magnitude */
int bi_cmp_imm(const bigint *bi, bi_signed_leaf val)
{
int vsign = val < 0;
bi_leaf v = val >= 0? (bi_leaf) val: -val == val? (bi_leaf) 1 << (sizeof(val)*CHAR_BIT - 1): (bi_leaf) -val;
if (bi_is_negative(bi) > vsign) /* negative, positive */
return -1;
else if (bi_is_negative(bi) < vsign) /* positive, negative */
return 1;
else
{
int ret = bi_used(bi) > 1;
if (!ret)
ret = (bi->data[0] > v) - (bi->data[0] < v);
return vsign? -ret: ret;
}
}
/* compares bi with val. returns -1 if bi < val, 0 if bi == val, 1 if bi > val */
/* compares signs as well as magnitude */
int bi_cmp_immu(const bigint *bi, bi_leaf val)
{
if (bi_is_negative(bi)) /* negative, positive */
return -1;
else
{
int ret = bi_used(bi) > 1;
if (!ret)
ret = (bi->data[0] > val) - (bi->data[0] < val);
return ret;
}
}
/* returns 1 if bi is negative, 0 if positive or zero */
int bi_is_negative(const bigint *bi)
{
return (bi->flags & BIGINT_FLAG_SIGN) >> BIGINT_FLAG_SIGN_SHIFT;
}
/* sets sign of bi to `value` */
void bi_set_negative(bigint *bi, int value)
{
bi->flags = (bi->flags & ~BIGINT_FLAG_SIGN) | ((value != 0) << BIGINT_FLAG_SIGN_SHIFT);
}
/* returns 1 if bi is growable, 0 otherwise */
int bi_is_growable(const bigint *bi)
{
return (bi->flags & BIGINT_FLAG_GROWABLE) >> BIGINT_FLAG_GROWABLE_SHIFT;
}
/* sets "growable" flag of bi to `value` */
void bi_set_growable(bigint *bi, int value)
{
bi->flags = (bi->flags & ~BIGINT_FLAG_GROWABLE) | ((value != 0) << BIGINT_FLAG_GROWABLE_SHIFT);
}
/* returns 1 if bi has a freeable data pointer, 0 otherwise */
int bi_is_freeable(const bigint *bi)
{
return (bi->flags & BIGINT_FLAG_FREEABLE) >> BIGINT_FLAG_FREEABLE_SHIFT;
}
/* sets whether bi has a freeable data pointer or not */
void bi_set_freeable(bigint *bi, int value)
{
bi->flags = (bi->flags & ~BIGINT_FLAG_FREEABLE) | ((value != 0) << BIGINT_FLAG_FREEABLE_SHIFT);
}
/* returns 1 if bi is a destroyable struct, 0 otherwise */
int bi_is_destroyable(const bigint *bi)
{
return (bi->flags & BIGINT_FLAG_DESTROYABLE) >> BIGINT_FLAG_DESTROYABLE_SHIFT;
}
/* sets whether bi is a destroyable struct or not */
void bi_set_destroyable(bigint *bi, int value)
{
bi->flags = (bi->flags & ~BIGINT_FLAG_DESTROYABLE) | ((value != 0) << BIGINT_FLAG_DESTROYABLE_SHIFT);
}
/* compares bi with 0. returns -1 if bi < 0, 0 if bi == 0, 1 if bi > 0 */
/* compares signs as well as magnitude */
int bi_cmp_zero(const bigint *bi)
{
if (bi_is_negative(bi))
return -1;
else if (bi_is_zero(bi))
return 0;
else
return 1;
}
/* compares two bigints. returns -1 if bi < bi2, 0 if bi == b2, 1 if bi > b2 */
/* compares signs as well as magnitude */
int bi_cmp(const bigint *bi, const bigint *bi2)
{
if (bi_is_negative(bi) > bi_is_negative(bi2)) /* negative, positive */
return -1;
else if (bi_is_negative(bi) < bi_is_negative(bi2)) /* positive, negative */
return 1;
else if (bi_is_negative(bi) == 0 && bi_is_negative(bi2) == 0) /* positive, positive */
return bi_cmp_mag(bi, bi2);
else /* negative, negative */
return -bi_cmp_mag(bi, bi2);
}
/* returns 1 if bi is zero, zero otherwise */
int bi_is_zero(const bigint *bi)
{
return bi_used(bi) == 0;
}
/* returns 1 if bi is positive one, zero otherwise */
int bi_is_one(const bigint *bi)
{
return bi->data[0] == 1 && !bi_is_negative(bi) && bi_used(bi) == 1;
}
/* returns 1 if bi is a power of 2 (has only one bit set), zero otherwise */
int bi_is_power_of_2(const bigint *bi)
{
size_t i = 0, cnt = 0, idx = 0;
bi_leaf lf;
for (; i < bi->size; ++i)
if (bi->data[i] != 0)
{
if (cnt++) return 0;
idx = i;
}
if (cnt != 1) return 0;
lf = bi->data[idx];
return (lf & (lf-1)) == 0;
}
/* returns log2(bi), rounded down */
/* returns -1 if bi is zero */
int bi_log2(const bigint *bi)
{
return (int) bi_bitcount(bi) - 1;
}
/* returns log2(bi), rounded down */
/* returns -1 if bi is zero */
long long bi_log2l(const bigint *bi)
{
return (long long) bi_bitcount(bi) - 1;
}
#ifdef BIGINT_ENABLE_LIBMATH
// TODO: remove floating-point dependence?
/* returns approximation of log10(bi), rounded down */
/* returns -1 if bi is zero */
/* this function will be equal to log10(bi) or log10(bi)-1 */
int bi_log10_approx(const bigint *bi)
{
return bi_logn_approx(bi, 10);
}
/* returns approximation of log10(bi), rounded down */
/* returns -1 if bi is zero */
/* this function will be equal to log10(bi) or log10(bi)-1 */
long long bi_log10l_approx(const bigint *bi)
{
return bi_lognl_approx(bi, 10);
}
/* returns log10(bi), rounded down */
/* returns -1 if bi is zero, -2 if out of memory */
int bi_log10(const bigint *bi)
{
return bi_logn(bi, 10);
}
/* returns log10(bi), rounded down */
/* returns -1 if bi is zero, -2 if out of memory */
long long bi_log10l(const bigint *bi)
{
return bi_lognl(bi, 10);
}
// TODO: remove floating-point dependence?
/* returns approximation of logn(bi), rounded down */
/* returns -1 if bi is zero or n < 2 */
/* this function will be equal to logn(bi) or logn(bi)-1 */
int bi_logn_approx(const bigint *bi, uintmax_t n)
{
int i = bi_log2(bi);
if (n == 2)
return i;
if (i < 0 || n < 2)
return -1;
else
return (int) (i / log2(n));
}
/* returns approximation of logn(bi), rounded down */
/* returns -1 if bi is zero or n < 2 */
/* this function will be equal to logn(bi) or logn(bi)-1 */
long long bi_lognl_approx(const bigint *bi, uintmax_t n)
{
long long i = bi_log2l(bi);
if (n == 2)
return i;
if (i < 0 || n < 2)
return -1;
else
return i / log2(n);
}
/* returns logn(bi), rounded down */
/* returns -1 if bi is zero or n < 2, -2 if out of memory */
int bi_logn(const bigint *bi, uintmax_t n)
{
int approx = bi_logn_approx(bi, n);
bigint *t;
if (n < 2)
return -1;
else if (n == 2)
return approx;
t = bi_new_valuelu(n);
if (t == NULL || bi_uexp_assign(t, approx+1) == NULL) return -2;
approx += bi_cmp_mag(bi, t) >= 0;
bi_destroy(t);
return approx;
}
/* returns logn(bi), rounded down */
/* returns -1 if bi is zero or n < 2, -2 if out of memory */
long long bi_lognl(const bigint *bi, uintmax_t n)
{
long long approx = bi_lognl_approx(bi, n);
bigint *t;
if (n < 2)
return -1;
else if (n == 2)
return approx;
t = bi_new_valuelu(n);
if (t == NULL || bi_uexp_assign(t, approx+1) == NULL) return -2;
approx += bi_cmp_mag(bi, t) >= 0;
bi_destroy(t);
return approx;
}
#endif
/* returns the number of trailing zeroes of bi */
/* returns -1 if bi is zero */
int bi_trailing_zeroes(const bigint *bi)
{
size_t i = 0;
for (; i < bi->size; ++i)
if (bi->data[i] != 0)
{
bi_leaf l = bi->data[i];
i *= BIGINT_LEAFBITS;
while (!(l & 1)) ++i, l >>= 1;
return i;
}