forked from ruby/ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bignum.c
7046 lines (6225 loc) · 178 KB
/
bignum.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
/**********************************************************************
bignum.c -
$Author$
created at: Fri Jun 10 00:48:55 JST 1994
Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
#include "internal.h"
#include "ruby/thread.h"
#include "ruby/util.h"
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <math.h>
#include <float.h>
#include <ctype.h>
#ifdef HAVE_IEEEFP_H
#include <ieeefp.h>
#endif
#include <assert.h>
#if defined(HAVE_LIBGMP) && defined(HAVE_GMP_H)
#define USE_GMP
#include <gmp.h>
#endif
#define RB_BIGNUM_TYPE_P(x) RB_TYPE_P((x), T_BIGNUM)
VALUE rb_cBignum;
const char ruby_digitmap[] = "0123456789abcdefghijklmnopqrstuvwxyz";
#ifndef SIZEOF_BDIGIT_DBL
# if SIZEOF_INT*2 <= SIZEOF_LONG_LONG
# define SIZEOF_BDIGIT_DBL SIZEOF_LONG_LONG
# else
# define SIZEOF_BDIGIT_DBL SIZEOF_LONG
# endif
#endif
STATIC_ASSERT(sizeof_bdigit_dbl, sizeof(BDIGIT_DBL) == SIZEOF_BDIGIT_DBL);
STATIC_ASSERT(sizeof_bdigit_dbl_signed, sizeof(BDIGIT_DBL_SIGNED) == SIZEOF_BDIGIT_DBL);
STATIC_ASSERT(sizeof_bdigit, SIZEOF_BDIGIT <= sizeof(BDIGIT));
STATIC_ASSERT(sizeof_bdigit_and_dbl, SIZEOF_BDIGIT*2 <= SIZEOF_BDIGIT_DBL);
STATIC_ASSERT(bdigit_signedness, 0 < (BDIGIT)-1);
STATIC_ASSERT(bdigit_dbl_signedness, 0 < (BDIGIT_DBL)-1);
STATIC_ASSERT(bdigit_dbl_signed_signedness, 0 > (BDIGIT_DBL_SIGNED)-1);
STATIC_ASSERT(rbignum_embed_len_max, BIGNUM_EMBED_LEN_MAX <= (BIGNUM_EMBED_LEN_MASK >> BIGNUM_EMBED_LEN_SHIFT));
#if SIZEOF_BDIGIT < SIZEOF_LONG
STATIC_ASSERT(sizeof_long_and_sizeof_bdigit, SIZEOF_LONG % SIZEOF_BDIGIT == 0);
#else
STATIC_ASSERT(sizeof_long_and_sizeof_bdigit, SIZEOF_BDIGIT % SIZEOF_LONG == 0);
#endif
#ifdef WORDS_BIGENDIAN
# define HOST_BIGENDIAN_P 1
#else
# define HOST_BIGENDIAN_P 0
#endif
#define ALIGNOF(type) ((int)offsetof(struct { char f1; type f2; }, f2))
/* (!LSHIFTABLE(d, n) ? 0 : (n)) is same as n but suppress a warning, C4293, by Visual Studio. */
#define LSHIFTABLE(d, n) ((n) < sizeof(d) * CHAR_BIT)
#define LSHIFTX(d, n) (!LSHIFTABLE(d, n) ? 0 : ((d) << (!LSHIFTABLE(d, n) ? 0 : (n))))
#define CLEAR_LOWBITS(d, numbits) ((d) & LSHIFTX(~((d)*0), (numbits)))
#define FILL_LOWBITS(d, numbits) ((d) | (LSHIFTX(((d)*0+1), (numbits))-1))
#define POW2_P(x) (((x)&((x)-1))==0)
#define BDIGITS(x) (BIGNUM_DIGITS(x))
#define BITSPERDIG (SIZEOF_BDIGIT*CHAR_BIT)
#define BIGRAD ((BDIGIT_DBL)1 << BITSPERDIG)
#define BIGRAD_HALF ((BDIGIT)(BIGRAD >> 1))
#define BDIGIT_MSB(d) (((d) & BIGRAD_HALF) != 0)
#define BIGUP(x) LSHIFTX(((x) + (BDIGIT_DBL)0), BITSPERDIG)
#define BIGDN(x) RSHIFT((x),BITSPERDIG)
#define BIGLO(x) ((BDIGIT)((x) & BDIGMAX))
#define BDIGMAX ((BDIGIT)(BIGRAD-1))
#define BDIGIT_DBL_MAX (~(BDIGIT_DBL)0)
#if SIZEOF_BDIGIT == 2
# define swap_bdigit(x) swap16(x)
#elif SIZEOF_BDIGIT == 4
# define swap_bdigit(x) swap32(x)
#elif SIZEOF_BDIGIT == 8
# define swap_bdigit(x) swap64(x)
#endif
#define BIGZEROP(x) (BIGNUM_LEN(x) == 0 || \
(BDIGITS(x)[0] == 0 && \
(BIGNUM_LEN(x) == 1 || bigzero_p(x))))
#define BIGSIZE(x) (BIGNUM_LEN(x) == 0 ? (size_t)0 : \
BDIGITS(x)[BIGNUM_LEN(x)-1] ? \
(size_t)(BIGNUM_LEN(x)*SIZEOF_BDIGIT - nlz(BDIGITS(x)[BIGNUM_LEN(x)-1])/CHAR_BIT) : \
rb_absint_size(x, NULL))
#define BIGDIVREM_EXTRA_WORDS 1
#define roomof(n, m) ((long)(((n)+(m)-1) / (m)))
#define bdigit_roomof(n) roomof(n, SIZEOF_BDIGIT)
#define BARY_ARGS(ary) ary, numberof(ary)
#define BARY_ADD(z, x, y) bary_add(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))
#define BARY_SUB(z, x, y) bary_sub(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))
#define BARY_SHORT_MUL(z, x, y) bary_short_mul(BARY_ARGS(z), BARY_ARGS(x), BARY_ARGS(y))
#define BARY_DIVMOD(q, r, x, y) bary_divmod(BARY_ARGS(q), BARY_ARGS(r), BARY_ARGS(x), BARY_ARGS(y))
#define BARY_ZERO_P(x) bary_zero_p(BARY_ARGS(x))
#define BIGNUM_SET_NEGATIVE_SIGN(b) BIGNUM_SET_SIGN(b, 0)
#define BIGNUM_SET_POSITIVE_SIGN(b) BIGNUM_SET_SIGN(b, 1)
#define bignew(len,sign) bignew_1(rb_cBignum,(len),(sign))
#define BDIGITS_ZERO(ptr, n) do { \
BDIGIT *bdigitz_zero_ptr = (ptr); \
size_t bdigitz_zero_n = (n); \
while (bdigitz_zero_n) { \
*bdigitz_zero_ptr++ = 0; \
bdigitz_zero_n--; \
} \
} while (0)
#define BARY_TRUNC(ds, n) do { \
while (0 < (n) && (ds)[(n)-1] == 0) \
(n)--; \
} while (0)
#define KARATSUBA_BALANCED(xn, yn) ((yn)/2 < (xn))
#define TOOM3_BALANCED(xn, yn) (((yn)+2)/3 * 2 < (xn))
#define GMP_MUL_DIGITS 20
#define KARATSUBA_MUL_DIGITS 70
#define TOOM3_MUL_DIGITS 150
#define GMP_DIV_DIGITS 20
#define GMP_BIG2STR_DIGITS 20
#define GMP_STR2BIG_DIGITS 20
typedef void (mulfunc_t)(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn);
static mulfunc_t bary_mul_toom3_start;
static mulfunc_t bary_mul_karatsuba_start;
static BDIGIT bigdivrem_single(BDIGIT *qds, const BDIGIT *xds, size_t xn, BDIGIT y);
static void bary_divmod(BDIGIT *qds, size_t qn, BDIGIT *rds, size_t rn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn);
static VALUE bigmul0(VALUE x, VALUE y);
static void bary_mul_toom3(BDIGIT *zds, size_t zn, const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn, BDIGIT *wds, size_t wn);
static VALUE bignew_1(VALUE klass, size_t len, int sign);
static inline VALUE bigtrunc(VALUE x);
static VALUE bigsq(VALUE x);
static void bigdivmod(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp);
static inline VALUE power_cache_get_power(int base, int power_level, size_t *numdigits_ret);
#if SIZEOF_BDIGIT <= SIZEOF_INT
static int nlz(BDIGIT x) { return nlz_int((unsigned int)x) - (SIZEOF_INT-SIZEOF_BDIGIT) * CHAR_BIT; }
#elif SIZEOF_BDIGIT <= SIZEOF_LONG
static int nlz(BDIGIT x) { return nlz_long((unsigned long)x) - (SIZEOF_LONG-SIZEOF_BDIGIT) * CHAR_BIT; }
#elif SIZEOF_BDIGIT <= SIZEOF_LONG_LONG
static int nlz(BDIGIT x) { return nlz_long_long((unsigned LONG_LONG)x) - (SIZEOF_LONG_LONG-SIZEOF_BDIGIT) * CHAR_BIT; }
#elif SIZEOF_BDIGIT <= SIZEOF_INT128_T
static int nlz(BDIGIT x) { return nlz_int128((uint128_t)x) - (SIZEOF_INT128_T-SIZEOF_BDIGIT) * CHAR_BIT; }
#endif
#define U16(a) ((uint16_t)(a))
#define U32(a) ((uint32_t)(a))
#ifdef HAVE_UINT64_T
#define U64(a,b) (((uint64_t)(a) << 32) | (b))
#endif
#ifdef HAVE_UINT128_T
#define U128(a,b,c,d) (((uint128_t)U64(a,b) << 64) | U64(c,d))
#endif
/* The following script, maxpow.rb, generates the tables follows.
def big(n, bits)
ns = []
((bits+31)/32).times {
ns << sprintf("0x%08x", n & 0xffff_ffff)
n >>= 32
}
"U#{bits}(" + ns.reverse.join(",") + ")"
end
def values(ary, width, indent)
lines = [""]
ary.each {|e|
lines << "" if !ary.last.empty? && width < (lines.last + e + ", ").length
lines.last << e + ", "
}
lines.map {|line| " " * indent + line.chomp(" ") + "\n" }.join
end
[16,32,64,128].each {|bits|
max = 2**bits-1
exps = []
nums = []
2.upto(36) {|base|
exp = 0
n = 1
while n * base <= max
exp += 1
n *= base
end
exps << exp.to_s
nums << big(n, bits)
}
puts "#ifdef HAVE_UINT#{bits}_T"
puts "static const int maxpow#{bits}_exp[35] = {"
print values(exps, 70, 4)
puts "};"
puts "static const uint#{bits}_t maxpow#{bits}_num[35] = {"
print values(nums, 70, 4)
puts "};"
puts "#endif"
}
*/
#if SIZEOF_BDIGIT_DBL == 2
static const int maxpow16_exp[35] = {
15, 10, 7, 6, 6, 5, 5, 5, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
};
static const uint16_t maxpow16_num[35] = {
U16(0x00008000), U16(0x0000e6a9), U16(0x00004000), U16(0x00003d09),
U16(0x0000b640), U16(0x000041a7), U16(0x00008000), U16(0x0000e6a9),
U16(0x00002710), U16(0x00003931), U16(0x00005100), U16(0x00006f91),
U16(0x00009610), U16(0x0000c5c1), U16(0x00001000), U16(0x00001331),
U16(0x000016c8), U16(0x00001acb), U16(0x00001f40), U16(0x0000242d),
U16(0x00002998), U16(0x00002f87), U16(0x00003600), U16(0x00003d09),
U16(0x000044a8), U16(0x00004ce3), U16(0x000055c0), U16(0x00005f45),
U16(0x00006978), U16(0x0000745f), U16(0x00008000), U16(0x00008c61),
U16(0x00009988), U16(0x0000a77b), U16(0x0000b640),
};
#elif SIZEOF_BDIGIT_DBL == 4
static const int maxpow32_exp[35] = {
31, 20, 15, 13, 12, 11, 10, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7,
7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
};
static const uint32_t maxpow32_num[35] = {
U32(0x80000000), U32(0xcfd41b91), U32(0x40000000), U32(0x48c27395),
U32(0x81bf1000), U32(0x75db9c97), U32(0x40000000), U32(0xcfd41b91),
U32(0x3b9aca00), U32(0x8c8b6d2b), U32(0x19a10000), U32(0x309f1021),
U32(0x57f6c100), U32(0x98c29b81), U32(0x10000000), U32(0x18754571),
U32(0x247dbc80), U32(0x3547667b), U32(0x4c4b4000), U32(0x6b5a6e1d),
U32(0x94ace180), U32(0xcaf18367), U32(0x0b640000), U32(0x0e8d4a51),
U32(0x1269ae40), U32(0x17179149), U32(0x1cb91000), U32(0x23744899),
U32(0x2b73a840), U32(0x34e63b41), U32(0x40000000), U32(0x4cfa3cc1),
U32(0x5c13d840), U32(0x6d91b519), U32(0x81bf1000),
};
#elif SIZEOF_BDIGIT_DBL == 8 && defined HAVE_UINT64_T
static const int maxpow64_exp[35] = {
63, 40, 31, 27, 24, 22, 21, 20, 19, 18, 17, 17, 16, 16, 15, 15, 15,
15, 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12,
12,
};
static const uint64_t maxpow64_num[35] = {
U64(0x80000000,0x00000000), U64(0xa8b8b452,0x291fe821),
U64(0x40000000,0x00000000), U64(0x6765c793,0xfa10079d),
U64(0x41c21cb8,0xe1000000), U64(0x36427987,0x50226111),
U64(0x80000000,0x00000000), U64(0xa8b8b452,0x291fe821),
U64(0x8ac72304,0x89e80000), U64(0x4d28cb56,0xc33fa539),
U64(0x1eca170c,0x00000000), U64(0x780c7372,0x621bd74d),
U64(0x1e39a505,0x7d810000), U64(0x5b27ac99,0x3df97701),
U64(0x10000000,0x00000000), U64(0x27b95e99,0x7e21d9f1),
U64(0x5da0e1e5,0x3c5c8000), U64(0xd2ae3299,0xc1c4aedb),
U64(0x16bcc41e,0x90000000), U64(0x2d04b7fd,0xd9c0ef49),
U64(0x5658597b,0xcaa24000), U64(0xa0e20737,0x37609371),
U64(0x0c29e980,0x00000000), U64(0x14adf4b7,0x320334b9),
U64(0x226ed364,0x78bfa000), U64(0x383d9170,0xb85ff80b),
U64(0x5a3c23e3,0x9c000000), U64(0x8e651373,0x88122bcd),
U64(0xdd41bb36,0xd259e000), U64(0x0aee5720,0xee830681),
U64(0x10000000,0x00000000), U64(0x172588ad,0x4f5f0981),
U64(0x211e44f7,0xd02c1000), U64(0x2ee56725,0xf06e5c71),
U64(0x41c21cb8,0xe1000000),
};
#elif SIZEOF_BDIGIT_DBL == 16 && defined HAVE_UINT128_T
static const int maxpow128_exp[35] = {
127, 80, 63, 55, 49, 45, 42, 40, 38, 37, 35, 34, 33, 32, 31, 31, 30,
30, 29, 29, 28, 28, 27, 27, 27, 26, 26, 26, 26, 25, 25, 25, 25, 24,
24,
};
static const uint128_t maxpow128_num[35] = {
U128(0x80000000,0x00000000,0x00000000,0x00000000),
U128(0x6f32f1ef,0x8b18a2bc,0x3cea5978,0x9c79d441),
U128(0x40000000,0x00000000,0x00000000,0x00000000),
U128(0xd0cf4b50,0xcfe20765,0xfff4b4e3,0xf741cf6d),
U128(0x6558e2a0,0x921fe069,0x42860000,0x00000000),
U128(0x5080c7b7,0xd0e31ba7,0x5911a67d,0xdd3d35e7),
U128(0x40000000,0x00000000,0x00000000,0x00000000),
U128(0x6f32f1ef,0x8b18a2bc,0x3cea5978,0x9c79d441),
U128(0x4b3b4ca8,0x5a86c47a,0x098a2240,0x00000000),
U128(0xffd1390a,0x0adc2fb8,0xdabbb817,0x4d95c99b),
U128(0x2c6fdb36,0x4c25e6c0,0x00000000,0x00000000),
U128(0x384bacd6,0x42c343b4,0xe90c4272,0x13506d29),
U128(0x31f5db32,0xa34aced6,0x0bf13a0e,0x00000000),
U128(0x20753ada,0xfd1e839f,0x53686d01,0x3143ee01),
U128(0x10000000,0x00000000,0x00000000,0x00000000),
U128(0x68ca11d6,0xb4f6d1d1,0xfaa82667,0x8073c2f1),
U128(0x223e493b,0xb3bb69ff,0xa4b87d6c,0x40000000),
U128(0xad62418d,0x14ea8247,0x01c4b488,0x6cc66f59),
U128(0x2863c1f5,0xcdae42f9,0x54000000,0x00000000),
U128(0xa63fd833,0xb9386b07,0x36039e82,0xbe651b25),
U128(0x1d1f7a9c,0xd087a14d,0x28cdf3d5,0x10000000),
U128(0x651b5095,0xc2ea8fc1,0xb30e2c57,0x77aaf7e1),
U128(0x0ddef20e,0xff760000,0x00000000,0x00000000),
U128(0x29c30f10,0x29939b14,0x6664242d,0x97d9f649),
U128(0x786a435a,0xe9558b0e,0x6aaf6d63,0xa8000000),
U128(0x0c5afe6f,0xf302bcbf,0x94fd9829,0xd87f5079),
U128(0x1fce575c,0xe1692706,0x07100000,0x00000000),
U128(0x4f34497c,0x8597e144,0x36e91802,0x00528229),
U128(0xbf3a8e1d,0x41ef2170,0x7802130d,0x84000000),
U128(0x0e7819e1,0x7f1eb0fb,0x6ee4fb89,0x01d9531f),
U128(0x20000000,0x00000000,0x00000000,0x00000000),
U128(0x4510460d,0xd9e879c0,0x14a82375,0x2f22b321),
U128(0x91abce3c,0x4b4117ad,0xe76d35db,0x22000000),
U128(0x08973ea3,0x55d75bc2,0x2e42c391,0x727d69e1),
U128(0x10e425c5,0x6daffabc,0x35c10000,0x00000000),
};
#endif
static BDIGIT_DBL
maxpow_in_bdigit_dbl(int base, int *exp_ret)
{
BDIGIT_DBL maxpow;
int exponent;
assert(2 <= base && base <= 36);
{
#if SIZEOF_BDIGIT_DBL == 2
maxpow = maxpow16_num[base-2];
exponent = maxpow16_exp[base-2];
#elif SIZEOF_BDIGIT_DBL == 4
maxpow = maxpow32_num[base-2];
exponent = maxpow32_exp[base-2];
#elif SIZEOF_BDIGIT_DBL == 8 && defined HAVE_UINT64_T
maxpow = maxpow64_num[base-2];
exponent = maxpow64_exp[base-2];
#elif SIZEOF_BDIGIT_DBL == 16 && defined HAVE_UINT128_T
maxpow = maxpow128_num[base-2];
exponent = maxpow128_exp[base-2];
#else
maxpow = base;
exponent = 1;
while (maxpow <= BDIGIT_DBL_MAX / base) {
maxpow *= base;
exponent++;
}
#endif
}
*exp_ret = exponent;
return maxpow;
}
static inline BDIGIT_DBL
bary2bdigitdbl(const BDIGIT *ds, size_t n)
{
assert(n <= 2);
if (n == 2)
return ds[0] | BIGUP(ds[1]);
if (n == 1)
return ds[0];
return 0;
}
static inline void
bdigitdbl2bary(BDIGIT *ds, size_t n, BDIGIT_DBL num)
{
assert(n == 2);
ds[0] = BIGLO(num);
ds[1] = (BDIGIT)BIGDN(num);
}
static int
bary_cmp(const BDIGIT *xds, size_t xn, const BDIGIT *yds, size_t yn)
{
BARY_TRUNC(xds, xn);
BARY_TRUNC(yds, yn);
if (xn < yn)
return -1;
if (xn > yn)
return 1;
while (xn-- && xds[xn] == yds[xn])
;
if (xn == (size_t)-1)
return 0;
return xds[xn] < yds[xn] ? -1 : 1;
}
static BDIGIT
bary_small_lshift(BDIGIT *zds, const BDIGIT *xds, size_t n, int shift)
{
size_t i;
BDIGIT_DBL num = 0;
assert(0 <= shift && shift < BITSPERDIG);
for (i=0; i<n; i++) {
num = num | (BDIGIT_DBL)*xds++ << shift;
*zds++ = BIGLO(num);
num = BIGDN(num);
}
return BIGLO(num);
}
static void
bary_small_rshift(BDIGIT *zds, const BDIGIT *xds, size_t n, int shift, BDIGIT higher_bdigit)
{
BDIGIT_DBL num = 0;
BDIGIT x;
assert(0 <= shift && shift < BITSPERDIG);
num = BIGUP(higher_bdigit);
while (n--) {
num = (num | xds[n]) >> shift;
x = xds[n];
zds[n] = BIGLO(num);
num = BIGUP(x);
}
}
static int
bary_zero_p(BDIGIT *xds, size_t xn)
{
if (xn == 0)
return 1;
do {
if (xds[--xn]) return 0;
} while (xn);
return 1;
}
static void
bary_neg(BDIGIT *ds, size_t n)
{
while (n--)
ds[n] = BIGLO(~ds[n]);
}
static int
bary_2comp(BDIGIT *ds, size_t n)
{
size_t i;
i = 0;
for (i = 0; i < n; i++) {
if (ds[i] != 0) {
goto non_zero;
}
}
return 1;
non_zero:
ds[i] = BIGLO(~ds[i] + 1);
i++;
for (; i < n; i++) {
ds[i] = BIGLO(~ds[i]);
}
return 0;
}
static void
bary_swap(BDIGIT *ds, size_t num_bdigits)
{
BDIGIT *p1 = ds;
BDIGIT *p2 = ds + num_bdigits - 1;
for (; p1 < p2; p1++, p2--) {
BDIGIT tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
}
#define INTEGER_PACK_WORDORDER_MASK \
(INTEGER_PACK_MSWORD_FIRST | \
INTEGER_PACK_LSWORD_FIRST)
#define INTEGER_PACK_BYTEORDER_MASK \
(INTEGER_PACK_MSBYTE_FIRST | \
INTEGER_PACK_LSBYTE_FIRST | \
INTEGER_PACK_NATIVE_BYTE_ORDER)
static void
validate_integer_pack_format(size_t numwords, size_t wordsize, size_t nails, int flags, int supported_flags)
{
int wordorder_bits = flags & INTEGER_PACK_WORDORDER_MASK;
int byteorder_bits = flags & INTEGER_PACK_BYTEORDER_MASK;
if (flags & ~supported_flags) {
rb_raise(rb_eArgError, "unsupported flags specified");
}
if (wordorder_bits == 0) {
if (1 < numwords)
rb_raise(rb_eArgError, "word order not specified");
}
else if (wordorder_bits != INTEGER_PACK_MSWORD_FIRST &&
wordorder_bits != INTEGER_PACK_LSWORD_FIRST)
rb_raise(rb_eArgError, "unexpected word order");
if (byteorder_bits == 0) {
rb_raise(rb_eArgError, "byte order not specified");
}
else if (byteorder_bits != INTEGER_PACK_MSBYTE_FIRST &&
byteorder_bits != INTEGER_PACK_LSBYTE_FIRST &&
byteorder_bits != INTEGER_PACK_NATIVE_BYTE_ORDER)
rb_raise(rb_eArgError, "unexpected byte order");
if (wordsize == 0)
rb_raise(rb_eArgError, "invalid wordsize: %"PRI_SIZE_PREFIX"u", wordsize);
if (SSIZE_MAX < wordsize)
rb_raise(rb_eArgError, "too big wordsize: %"PRI_SIZE_PREFIX"u", wordsize);
if (wordsize <= nails / CHAR_BIT)
rb_raise(rb_eArgError, "too big nails: %"PRI_SIZE_PREFIX"u", nails);
if (SIZE_MAX / wordsize < numwords)
rb_raise(rb_eArgError, "too big numwords * wordsize: %"PRI_SIZE_PREFIX"u * %"PRI_SIZE_PREFIX"u", numwords, wordsize);
}
static void
integer_pack_loop_setup(
size_t numwords, size_t wordsize, size_t nails, int flags,
size_t *word_num_fullbytes_ret,
int *word_num_partialbits_ret,
size_t *word_start_ret,
ssize_t *word_step_ret,
size_t *word_last_ret,
size_t *byte_start_ret,
int *byte_step_ret)
{
int wordorder_bits = flags & INTEGER_PACK_WORDORDER_MASK;
int byteorder_bits = flags & INTEGER_PACK_BYTEORDER_MASK;
size_t word_num_fullbytes;
int word_num_partialbits;
size_t word_start;
ssize_t word_step;
size_t word_last;
size_t byte_start;
int byte_step;
word_num_partialbits = CHAR_BIT - (int)(nails % CHAR_BIT);
if (word_num_partialbits == CHAR_BIT)
word_num_partialbits = 0;
word_num_fullbytes = wordsize - (nails / CHAR_BIT);
if (word_num_partialbits != 0) {
word_num_fullbytes--;
}
if (wordorder_bits == INTEGER_PACK_MSWORD_FIRST) {
word_start = wordsize*(numwords-1);
word_step = -(ssize_t)wordsize;
word_last = 0;
}
else {
word_start = 0;
word_step = wordsize;
word_last = wordsize*(numwords-1);
}
if (byteorder_bits == INTEGER_PACK_NATIVE_BYTE_ORDER) {
#ifdef WORDS_BIGENDIAN
byteorder_bits = INTEGER_PACK_MSBYTE_FIRST;
#else
byteorder_bits = INTEGER_PACK_LSBYTE_FIRST;
#endif
}
if (byteorder_bits == INTEGER_PACK_MSBYTE_FIRST) {
byte_start = wordsize-1;
byte_step = -1;
}
else {
byte_start = 0;
byte_step = 1;
}
*word_num_partialbits_ret = word_num_partialbits;
*word_num_fullbytes_ret = word_num_fullbytes;
*word_start_ret = word_start;
*word_step_ret = word_step;
*word_last_ret = word_last;
*byte_start_ret = byte_start;
*byte_step_ret = byte_step;
}
static inline void
integer_pack_fill_dd(BDIGIT **dpp, BDIGIT **dep, BDIGIT_DBL *ddp, int *numbits_in_dd_p)
{
if (*dpp < *dep && BITSPERDIG <= (int)sizeof(*ddp) * CHAR_BIT - *numbits_in_dd_p) {
*ddp |= (BDIGIT_DBL)(*(*dpp)++) << *numbits_in_dd_p;
*numbits_in_dd_p += BITSPERDIG;
}
else if (*dpp == *dep) {
/* higher bits are infinity zeros */
*numbits_in_dd_p = (int)sizeof(*ddp) * CHAR_BIT;
}
}
static inline BDIGIT_DBL
integer_pack_take_lowbits(int n, BDIGIT_DBL *ddp, int *numbits_in_dd_p)
{
BDIGIT_DBL ret;
ret = (*ddp) & (((BDIGIT_DBL)1 << n) - 1);
*ddp >>= n;
*numbits_in_dd_p -= n;
return ret;
}
#if !defined(WORDS_BIGENDIAN)
static int
bytes_2comp(unsigned char *buf, size_t len)
{
size_t i;
for (i = 0; i < len; i++)
buf[i] = ~buf[i];
for (i = 0; i < len; i++) {
buf[i]++;
if (buf[i] != 0)
return 0;
}
return 1;
}
#endif
static int
bary_pack(int sign, BDIGIT *ds, size_t num_bdigits, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
{
BDIGIT *dp, *de;
unsigned char *buf, *bufend;
dp = ds;
de = ds + num_bdigits;
validate_integer_pack_format(numwords, wordsize, nails, flags,
INTEGER_PACK_MSWORD_FIRST|
INTEGER_PACK_LSWORD_FIRST|
INTEGER_PACK_MSBYTE_FIRST|
INTEGER_PACK_LSBYTE_FIRST|
INTEGER_PACK_NATIVE_BYTE_ORDER|
INTEGER_PACK_2COMP|
INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION);
while (dp < de && de[-1] == 0)
de--;
if (dp == de) {
sign = 0;
}
if (!(flags & INTEGER_PACK_FORCE_GENERIC_IMPLEMENTATION)) {
if (sign == 0) {
MEMZERO(words, unsigned char, numwords * wordsize);
return 0;
}
if (nails == 0 && numwords == 1) {
int need_swap = wordsize != 1 &&
(flags & INTEGER_PACK_BYTEORDER_MASK) != INTEGER_PACK_NATIVE_BYTE_ORDER &&
((flags & INTEGER_PACK_MSBYTE_FIRST) ? !HOST_BIGENDIAN_P : HOST_BIGENDIAN_P);
if (0 < sign || !(flags & INTEGER_PACK_2COMP)) {
BDIGIT d;
if (wordsize == 1) {
*((unsigned char *)words) = (unsigned char)(d = dp[0]);
return ((1 < de - dp || CLEAR_LOWBITS(d, 8) != 0) ? 2 : 1) * sign;
}
#if defined(HAVE_UINT16_T) && 2 <= SIZEOF_BDIGIT
if (wordsize == 2 && (uintptr_t)words % ALIGNOF(uint16_t) == 0) {
uint16_t u = (uint16_t)(d = dp[0]);
if (need_swap) u = swap16(u);
*((uint16_t *)words) = u;
return ((1 < de - dp || CLEAR_LOWBITS(d, 16) != 0) ? 2 : 1) * sign;
}
#endif
#if defined(HAVE_UINT32_T) && 4 <= SIZEOF_BDIGIT
if (wordsize == 4 && (uintptr_t)words % ALIGNOF(uint32_t) == 0) {
uint32_t u = (uint32_t)(d = dp[0]);
if (need_swap) u = swap32(u);
*((uint32_t *)words) = u;
return ((1 < de - dp || CLEAR_LOWBITS(d, 32) != 0) ? 2 : 1) * sign;
}
#endif
#if defined(HAVE_UINT64_T) && 8 <= SIZEOF_BDIGIT
if (wordsize == 8 && (uintptr_t)words % ALIGNOF(uint64_t) == 0) {
uint64_t u = (uint64_t)(d = dp[0]);
if (need_swap) u = swap64(u);
*((uint64_t *)words) = u;
return ((1 < de - dp || CLEAR_LOWBITS(d, 64) != 0) ? 2 : 1) * sign;
}
#endif
}
else { /* sign < 0 && (flags & INTEGER_PACK_2COMP) */
BDIGIT_DBL_SIGNED d;
if (wordsize == 1) {
*((unsigned char *)words) = (unsigned char)(d = -(BDIGIT_DBL_SIGNED)dp[0]);
return (1 < de - dp || FILL_LOWBITS(d, 8) != -1) ? -2 : -1;
}
#if defined(HAVE_UINT16_T) && 2 <= SIZEOF_BDIGIT
if (wordsize == 2 && (uintptr_t)words % ALIGNOF(uint16_t) == 0) {
uint16_t u = (uint16_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]);
if (need_swap) u = swap16(u);
*((uint16_t *)words) = u;
return (wordsize == SIZEOF_BDIGIT && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 :
(1 < de - dp || FILL_LOWBITS(d, 16) != -1) ? -2 : -1;
}
#endif
#if defined(HAVE_UINT32_T) && 4 <= SIZEOF_BDIGIT
if (wordsize == 4 && (uintptr_t)words % ALIGNOF(uint32_t) == 0) {
uint32_t u = (uint32_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]);
if (need_swap) u = swap32(u);
*((uint32_t *)words) = u;
return (wordsize == SIZEOF_BDIGIT && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 :
(1 < de - dp || FILL_LOWBITS(d, 32) != -1) ? -2 : -1;
}
#endif
#if defined(HAVE_UINT64_T) && 8 <= SIZEOF_BDIGIT
if (wordsize == 8 && (uintptr_t)words % ALIGNOF(uint64_t) == 0) {
uint64_t u = (uint64_t)(d = -(BDIGIT_DBL_SIGNED)dp[0]);
if (need_swap) u = swap64(u);
*((uint64_t *)words) = u;
return (wordsize == SIZEOF_BDIGIT && de - dp == 2 && dp[1] == 1 && dp[0] == 0) ? -1 :
(1 < de - dp || FILL_LOWBITS(d, 64) != -1) ? -2 : -1;
}
#endif
}
}
#if !defined(WORDS_BIGENDIAN)
if (nails == 0 && SIZEOF_BDIGIT == sizeof(BDIGIT) &&
(flags & INTEGER_PACK_WORDORDER_MASK) == INTEGER_PACK_LSWORD_FIRST &&
(flags & INTEGER_PACK_BYTEORDER_MASK) != INTEGER_PACK_MSBYTE_FIRST) {
size_t src_size = (de - dp) * SIZEOF_BDIGIT;
size_t dst_size = numwords * wordsize;
int overflow = 0;
while (0 < src_size && ((unsigned char *)ds)[src_size-1] == 0)
src_size--;
if (src_size <= dst_size) {
MEMCPY(words, dp, char, src_size);
MEMZERO((char*)words + src_size, char, dst_size - src_size);
}
else {
MEMCPY(words, dp, char, dst_size);
overflow = 1;
}
if (sign < 0 && (flags & INTEGER_PACK_2COMP)) {
int zero_p = bytes_2comp(words, dst_size);
if (zero_p && overflow) {
unsigned char *p = (unsigned char *)dp;
if (dst_size == src_size-1 &&
p[dst_size] == 1) {
overflow = 0;
}
}
}
if (overflow)
sign *= 2;
return sign;
}
#endif
if (nails == 0 && SIZEOF_BDIGIT == sizeof(BDIGIT) &&
wordsize % SIZEOF_BDIGIT == 0 && (uintptr_t)words % ALIGNOF(BDIGIT) == 0) {
size_t bdigits_per_word = wordsize / SIZEOF_BDIGIT;
size_t src_num_bdigits = de - dp;
size_t dst_num_bdigits = numwords * bdigits_per_word;
int overflow = 0;
int mswordfirst_p = (flags & INTEGER_PACK_MSWORD_FIRST) != 0;
int msbytefirst_p = (flags & INTEGER_PACK_NATIVE_BYTE_ORDER) ? HOST_BIGENDIAN_P :
(flags & INTEGER_PACK_MSBYTE_FIRST) != 0;
if (src_num_bdigits <= dst_num_bdigits) {
MEMCPY(words, dp, BDIGIT, src_num_bdigits);
BDIGITS_ZERO((BDIGIT*)words + src_num_bdigits, dst_num_bdigits - src_num_bdigits);
}
else {
MEMCPY(words, dp, BDIGIT, dst_num_bdigits);
overflow = 1;
}
if (sign < 0 && (flags & INTEGER_PACK_2COMP)) {
int zero_p = bary_2comp(words, dst_num_bdigits);
if (zero_p && overflow &&
dst_num_bdigits == src_num_bdigits-1 &&
dp[dst_num_bdigits] == 1)
overflow = 0;
}
if (msbytefirst_p != HOST_BIGENDIAN_P) {
size_t i;
for (i = 0; i < dst_num_bdigits; i++) {
BDIGIT d = ((BDIGIT*)words)[i];
((BDIGIT*)words)[i] = swap_bdigit(d);
}
}
if (mswordfirst_p ? !msbytefirst_p : msbytefirst_p) {
size_t i;
BDIGIT *p = words;
for (i = 0; i < numwords; i++) {
bary_swap(p, bdigits_per_word);
p += bdigits_per_word;
}
}
if (mswordfirst_p) {
bary_swap(words, dst_num_bdigits);
}
if (overflow)
sign *= 2;
return sign;
}
}
buf = words;
bufend = buf + numwords * wordsize;
if (buf == bufend) {
/* overflow if non-zero*/
if (!(flags & INTEGER_PACK_2COMP) || 0 <= sign)
sign *= 2;
else {
if (de - dp == 1 && dp[0] == 1)
sign = -1; /* val == -1 == -2**(numwords*(wordsize*CHAR_BIT-nails)) */
else
sign = -2; /* val < -1 == -2**(numwords*(wordsize*CHAR_BIT-nails)) */
}
}
else if (dp == de) {
memset(buf, '\0', bufend - buf);
}
else if (dp < de && buf < bufend) {
int word_num_partialbits;
size_t word_num_fullbytes;
ssize_t word_step;
size_t byte_start;
int byte_step;
size_t word_start, word_last;
unsigned char *wordp, *last_wordp;
BDIGIT_DBL dd;
int numbits_in_dd;
integer_pack_loop_setup(numwords, wordsize, nails, flags,
&word_num_fullbytes, &word_num_partialbits,
&word_start, &word_step, &word_last, &byte_start, &byte_step);
wordp = buf + word_start;
last_wordp = buf + word_last;
dd = 0;
numbits_in_dd = 0;
#define FILL_DD \
integer_pack_fill_dd(&dp, &de, &dd, &numbits_in_dd)
#define TAKE_LOWBITS(n) \
integer_pack_take_lowbits(n, &dd, &numbits_in_dd)
while (1) {
size_t index_in_word = 0;
unsigned char *bytep = wordp + byte_start;
while (index_in_word < word_num_fullbytes) {
FILL_DD;
*bytep = TAKE_LOWBITS(CHAR_BIT);
bytep += byte_step;
index_in_word++;
}
if (word_num_partialbits) {
FILL_DD;
*bytep = TAKE_LOWBITS(word_num_partialbits);
bytep += byte_step;
index_in_word++;
}
while (index_in_word < wordsize) {
*bytep = 0;
bytep += byte_step;
index_in_word++;
}
if (wordp == last_wordp)
break;
wordp += word_step;
}
FILL_DD;
/* overflow tests */
if (dp != de || 1 < dd) {
/* 2**(numwords*(wordsize*CHAR_BIT-nails)+1) <= abs(val) */
sign *= 2;
}
else if (dd == 1) {
/* 2**(numwords*(wordsize*CHAR_BIT-nails)) <= abs(val) < 2**(numwords*(wordsize*CHAR_BIT-nails)+1) */
if (!(flags & INTEGER_PACK_2COMP) || 0 <= sign)
sign *= 2;
else { /* overflow_2comp && sign == -1 */
/* test lower bits are all zero. */
dp = ds;
while (dp < de && *dp == 0)
dp++;
if (de - dp == 1 && /* only one non-zero word. */
POW2_P(*dp)) /* *dp contains only one bit set. */
sign = -1; /* val == -2**(numwords*(wordsize*CHAR_BIT-nails)) */
else
sign = -2; /* val < -2**(numwords*(wordsize*CHAR_BIT-nails)) */
}
}
}
if ((flags & INTEGER_PACK_2COMP) && (sign < 0 && numwords != 0)) {
unsigned char *buf;
int word_num_partialbits;
size_t word_num_fullbytes;
ssize_t word_step;
size_t byte_start;
int byte_step;
size_t word_start, word_last;
unsigned char *wordp, *last_wordp;
unsigned int partialbits_mask;
int carry;
integer_pack_loop_setup(numwords, wordsize, nails, flags,
&word_num_fullbytes, &word_num_partialbits,
&word_start, &word_step, &word_last, &byte_start, &byte_step);
partialbits_mask = (1 << word_num_partialbits) - 1;
buf = words;
wordp = buf + word_start;
last_wordp = buf + word_last;
carry = 1;
while (1) {
size_t index_in_word = 0;
unsigned char *bytep = wordp + byte_start;
while (index_in_word < word_num_fullbytes) {
carry += (unsigned char)~*bytep;
*bytep = (unsigned char)carry;
carry >>= CHAR_BIT;
bytep += byte_step;
index_in_word++;
}
if (word_num_partialbits) {
carry += (*bytep & partialbits_mask) ^ partialbits_mask;
*bytep = carry & partialbits_mask;
carry >>= word_num_partialbits;
bytep += byte_step;
index_in_word++;
}
if (wordp == last_wordp)
break;
wordp += word_step;
}
}
return sign;
#undef FILL_DD
#undef TAKE_LOWBITS
}
static size_t
integer_unpack_num_bdigits_small(size_t numwords, size_t wordsize, size_t nails, int *nlp_bits_ret)
{
/* nlp_bits stands for number of leading padding bits */
size_t num_bits = (wordsize * CHAR_BIT - nails) * numwords;
size_t num_bdigits = (num_bits + BITSPERDIG - 1) / BITSPERDIG;
*nlp_bits_ret = (int)(num_bdigits * BITSPERDIG - num_bits);
return num_bdigits;
}
static size_t
integer_unpack_num_bdigits_generic(size_t numwords, size_t wordsize, size_t nails, int *nlp_bits_ret)
{
/* BITSPERDIG = SIZEOF_BDIGIT * CHAR_BIT */
/* num_bits = (wordsize * CHAR_BIT - nails) * numwords */
/* num_bdigits = (num_bits + BITSPERDIG - 1) / BITSPERDIG */
/* num_bits = CHAR_BIT * (wordsize * numwords) - nails * numwords = CHAR_BIT * num_bytes1 - nails * numwords */
size_t num_bytes1 = wordsize * numwords;
/* q1 * CHAR_BIT + r1 = numwords */
size_t q1 = numwords / CHAR_BIT;
size_t r1 = numwords % CHAR_BIT;
/* num_bits = CHAR_BIT * num_bytes1 - nails * (q1 * CHAR_BIT + r1) = CHAR_BIT * num_bytes2 - nails * r1 */
size_t num_bytes2 = num_bytes1 - nails * q1;
/* q2 * CHAR_BIT + r2 = nails */
size_t q2 = nails / CHAR_BIT;
size_t r2 = nails % CHAR_BIT;
/* num_bits = CHAR_BIT * num_bytes2 - (q2 * CHAR_BIT + r2) * r1 = CHAR_BIT * num_bytes3 - r1 * r2 */
size_t num_bytes3 = num_bytes2 - q2 * r1;
/* q3 * BITSPERDIG + r3 = num_bytes3 */
size_t q3 = num_bytes3 / BITSPERDIG;
size_t r3 = num_bytes3 % BITSPERDIG;
/* num_bits = CHAR_BIT * (q3 * BITSPERDIG + r3) - r1 * r2 = BITSPERDIG * num_digits1 + CHAR_BIT * r3 - r1 * r2 */
size_t num_digits1 = CHAR_BIT * q3;
/*
* if CHAR_BIT * r3 >= r1 * r2
* CHAR_BIT * r3 - r1 * r2 = CHAR_BIT * BITSPERDIG - (CHAR_BIT * BITSPERDIG - (CHAR_BIT * r3 - r1 * r2))
* q4 * BITSPERDIG + r4 = CHAR_BIT * BITSPERDIG - (CHAR_BIT * r3 - r1 * r2)