-
Notifications
You must be signed in to change notification settings - Fork 0
/
bignum.c
4684 lines (4159 loc) · 105 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 "ruby/ruby.h"
#include "ruby/thread.h"
#include "ruby/util.h"
#include "internal.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>
VALUE rb_cBignum;
static VALUE big_three = Qnil;
#if defined __MINGW32__
#define USHORT _USHORT
#endif
#define BDIGITS(x) (RBIGNUM_DIGITS(x))
#define BITSPERDIG (SIZEOF_BDIGITS*CHAR_BIT)
#define BIGRAD ((BDIGIT_DBL)1 << BITSPERDIG)
#define BIGRAD_HALF ((BDIGIT)(BIGRAD >> 1))
#define DIGSPERLONG (SIZEOF_LONG/SIZEOF_BDIGITS)
#if HAVE_LONG_LONG
# define DIGSPERLL (SIZEOF_LONG_LONG/SIZEOF_BDIGITS)
#endif
#define BIGUP(x) ((BDIGIT_DBL)(x) << BITSPERDIG)
#define BIGDN(x) RSHIFT((x),BITSPERDIG)
#define BIGLO(x) ((BDIGIT)((x) & (BIGRAD-1)))
#define BDIGMAX ((BDIGIT)-1)
#define BIGZEROP(x) (RBIGNUM_LEN(x) == 0 || \
(BDIGITS(x)[0] == 0 && \
(RBIGNUM_LEN(x) == 1 || bigzero_p(x))))
static int nlz(BDIGIT x);
#define BIGNUM_DEBUG 0
#if BIGNUM_DEBUG
#define ON_DEBUG(x) do { x; } while (0)
static void
dump_bignum(VALUE x)
{
long i;
printf("%c0x0", RBIGNUM_SIGN(x) ? '+' : '-');
for (i = RBIGNUM_LEN(x); i--; ) {
printf("_%08"PRIxBDIGIT, BDIGITS(x)[i]);
}
printf(", len=%lu", RBIGNUM_LEN(x));
puts("");
}
static VALUE
rb_big_dump(VALUE x)
{
dump_bignum(x);
return x;
}
#else
#define ON_DEBUG(x)
#endif
static int
bigzero_p(VALUE x)
{
long i;
BDIGIT *ds = BDIGITS(x);
for (i = RBIGNUM_LEN(x) - 1; 0 <= i; i--) {
if (ds[i]) return 0;
}
return 1;
}
int
rb_bigzero_p(VALUE x)
{
return BIGZEROP(x);
}
int
rb_cmpint(VALUE val, VALUE a, VALUE b)
{
if (NIL_P(val)) {
rb_cmperr(a, b);
}
if (FIXNUM_P(val)) {
long l = FIX2LONG(val);
if (l > 0) return 1;
if (l < 0) return -1;
return 0;
}
if (RB_TYPE_P(val, T_BIGNUM)) {
if (BIGZEROP(val)) return 0;
if (RBIGNUM_SIGN(val)) return 1;
return -1;
}
if (RTEST(rb_funcall(val, '>', 1, INT2FIX(0)))) return 1;
if (RTEST(rb_funcall(val, '<', 1, INT2FIX(0)))) return -1;
return 0;
}
#define RBIGNUM_SET_LEN(b,l) \
((RBASIC(b)->flags & RBIGNUM_EMBED_FLAG) ? \
(void)(RBASIC(b)->flags = \
(RBASIC(b)->flags & ~RBIGNUM_EMBED_LEN_MASK) | \
((l) << RBIGNUM_EMBED_LEN_SHIFT)) : \
(void)(RBIGNUM(b)->as.heap.len = (l)))
static void
rb_big_realloc(VALUE big, long len)
{
BDIGIT *ds;
if (RBASIC(big)->flags & RBIGNUM_EMBED_FLAG) {
if (RBIGNUM_EMBED_LEN_MAX < len) {
ds = ALLOC_N(BDIGIT, len);
MEMCPY(ds, RBIGNUM(big)->as.ary, BDIGIT, RBIGNUM_EMBED_LEN_MAX);
RBIGNUM(big)->as.heap.len = RBIGNUM_LEN(big);
RBIGNUM(big)->as.heap.digits = ds;
RBASIC(big)->flags &= ~RBIGNUM_EMBED_FLAG;
}
}
else {
if (len <= RBIGNUM_EMBED_LEN_MAX) {
ds = RBIGNUM(big)->as.heap.digits;
RBASIC(big)->flags |= RBIGNUM_EMBED_FLAG;
RBIGNUM_SET_LEN(big, len);
if (ds) {
MEMCPY(RBIGNUM(big)->as.ary, ds, BDIGIT, len);
xfree(ds);
}
}
else {
if (RBIGNUM_LEN(big) == 0) {
RBIGNUM(big)->as.heap.digits = ALLOC_N(BDIGIT, len);
}
else {
REALLOC_N(RBIGNUM(big)->as.heap.digits, BDIGIT, len);
}
}
}
}
void
rb_big_resize(VALUE big, long len)
{
rb_big_realloc(big, len);
RBIGNUM_SET_LEN(big, len);
}
static VALUE
bignew_1(VALUE klass, long len, int sign)
{
NEWOBJ_OF(big, struct RBignum, klass, T_BIGNUM | (RGENGC_WB_PROTECTED_BIGNUM ? FL_WB_PROTECTED : 0));
RBIGNUM_SET_SIGN(big, sign?1:0);
if (len <= RBIGNUM_EMBED_LEN_MAX) {
RBASIC(big)->flags |= RBIGNUM_EMBED_FLAG;
RBIGNUM_SET_LEN(big, len);
}
else {
RBIGNUM(big)->as.heap.digits = ALLOC_N(BDIGIT, len);
RBIGNUM(big)->as.heap.len = len;
}
OBJ_FREEZE(big);
return (VALUE)big;
}
#define bignew(len,sign) bignew_1(rb_cBignum,(len),(sign))
VALUE
rb_big_new(long len, int sign)
{
return bignew(len, sign != 0);
}
VALUE
rb_big_clone(VALUE x)
{
long len = RBIGNUM_LEN(x);
VALUE z = bignew_1(CLASS_OF(x), len, RBIGNUM_SIGN(x));
MEMCPY(BDIGITS(z), BDIGITS(x), BDIGIT, len);
return z;
}
/* modify a bignum by 2's complement */
static void
get2comp(VALUE x)
{
long i = RBIGNUM_LEN(x);
BDIGIT *ds = BDIGITS(x);
BDIGIT_DBL num;
if (!i) return;
while (i--) ds[i] = ~ds[i];
i = 0; num = 1;
do {
num += ds[i];
ds[i++] = BIGLO(num);
num = BIGDN(num);
} while (i < RBIGNUM_LEN(x));
if (num != 0) {
rb_big_resize(x, RBIGNUM_LEN(x)+1);
ds = BDIGITS(x);
ds[RBIGNUM_LEN(x)-1] = 1;
}
}
void
rb_big_2comp(VALUE x) /* get 2's complement */
{
get2comp(x);
}
static inline VALUE
bigtrunc(VALUE x)
{
long len = RBIGNUM_LEN(x);
BDIGIT *ds = BDIGITS(x);
if (len == 0) return x;
while (--len && !ds[len]);
if (RBIGNUM_LEN(x) > len+1) {
rb_big_resize(x, len+1);
}
return x;
}
static inline VALUE
bigfixize(VALUE x)
{
long len = RBIGNUM_LEN(x);
BDIGIT *ds = BDIGITS(x);
if (len == 0) return INT2FIX(0);
if ((size_t)(len*SIZEOF_BDIGITS) <= sizeof(long)) {
long num = 0;
#if 2*SIZEOF_BDIGITS > SIZEOF_LONG
num = (long)ds[0];
#else
while (len--) {
num = (long)(BIGUP(num) + ds[len]);
}
#endif
if (num >= 0) {
if (RBIGNUM_SIGN(x)) {
if (POSFIXABLE(num)) return LONG2FIX(num);
}
else {
if (NEGFIXABLE(-num)) return LONG2FIX(-num);
}
}
}
return x;
}
static VALUE
bignorm(VALUE x)
{
if (RB_TYPE_P(x, T_BIGNUM)) {
x = bigfixize(bigtrunc(x));
}
return x;
}
VALUE
rb_big_norm(VALUE x)
{
return bignorm(x);
}
VALUE
rb_uint2big(VALUE n)
{
BDIGIT_DBL num = n;
long i = 0;
BDIGIT *digits;
VALUE big;
big = bignew(DIGSPERLONG, 1);
digits = BDIGITS(big);
while (i < DIGSPERLONG) {
digits[i++] = BIGLO(num);
num = BIGDN(num);
}
i = DIGSPERLONG;
while (--i && !digits[i]) ;
RBIGNUM_SET_LEN(big, i+1);
return big;
}
VALUE
rb_int2big(SIGNED_VALUE n)
{
long neg = 0;
VALUE u;
VALUE big;
if (n < 0) {
u = 1 + (VALUE)(-(n + 1)); /* u = -n avoiding overflow */
neg = 1;
}
else {
u = n;
}
big = rb_uint2big(u);
if (neg) {
RBIGNUM_SET_SIGN(big, 0);
}
return big;
}
VALUE
rb_uint2inum(VALUE n)
{
if (POSFIXABLE(n)) return LONG2FIX(n);
return rb_uint2big(n);
}
VALUE
rb_int2inum(SIGNED_VALUE n)
{
if (FIXABLE(n)) return LONG2FIX(n);
return rb_int2big(n);
}
#if SIZEOF_LONG % SIZEOF_BDIGITS != 0
# error unexpected SIZEOF_LONG : SIZEOF_BDIGITS ratio
#endif
/*
* buf is an array of long integers.
* buf is ordered from least significant word to most significant word.
* buf[0] is the least significant word and
* buf[num_longs-1] is the most significant word.
* This means words in buf is little endian.
* However each word in buf is native endian.
* (buf[i]&1) is the least significant bit and
* (buf[i]&(1<<(SIZEOF_LONG*CHAR_BIT-1))) is the most significant bit
* for each 0 <= i < num_longs.
* So buf is little endian at whole on a little endian machine.
* But buf is mixed endian on a big endian machine.
*
* The buf represents negative integers as two's complement.
* So, the most significant bit of the most significant word,
* (buf[num_longs-1]>>(SIZEOF_LONG*CHAR_BIT-1)),
* is the sign bit: 1 means negative and 0 means zero or positive.
*
* If given size of buf (num_longs) is not enough to represent val,
* higher words (including a sign bit) are ignored.
*/
void
rb_big_pack(VALUE val, unsigned long *buf, long num_longs)
{
val = rb_to_int(val);
if (num_longs == 0)
return;
if (FIXNUM_P(val)) {
long i;
long tmp = FIX2LONG(val);
buf[0] = (unsigned long)tmp;
tmp = tmp < 0 ? ~0L : 0;
for (i = 1; i < num_longs; i++)
buf[i] = (unsigned long)tmp;
return;
}
else {
long len = RBIGNUM_LEN(val);
BDIGIT *ds = BDIGITS(val), *dend = ds + len;
long i, j;
for (i = 0; i < num_longs && ds < dend; i++) {
unsigned long l = 0;
for (j = 0; j < DIGSPERLONG && ds < dend; j++, ds++) {
l |= ((unsigned long)*ds << (j * BITSPERDIG));
}
buf[i] = l;
}
for (; i < num_longs; i++)
buf[i] = 0;
if (RBIGNUM_NEGATIVE_P(val)) {
for (i = 0; i < num_longs; i++) {
buf[i] = ~buf[i];
}
for (i = 0; i < num_longs; i++) {
buf[i]++;
if (buf[i] != 0)
return;
}
}
}
}
/* See rb_big_pack comment for endianness and sign of buf. */
VALUE
rb_big_unpack(unsigned long *buf, long num_longs)
{
while (2 <= num_longs) {
if (buf[num_longs-1] == 0 && (long)buf[num_longs-2] >= 0)
num_longs--;
else if (buf[num_longs-1] == ~0UL && (long)buf[num_longs-2] < 0)
num_longs--;
else
break;
}
if (num_longs == 0)
return INT2FIX(0);
else if (num_longs == 1)
return LONG2NUM((long)buf[0]);
else {
VALUE big;
BDIGIT *ds;
long len = num_longs * DIGSPERLONG;
long i;
big = bignew(len, 1);
ds = BDIGITS(big);
for (i = 0; i < num_longs; i++) {
unsigned long d = buf[i];
#if SIZEOF_LONG == SIZEOF_BDIGITS
*ds++ = d;
#else
int j;
for (j = 0; j < DIGSPERLONG; j++) {
*ds++ = BIGLO(d);
d = BIGDN(d);
}
#endif
}
if ((long)buf[num_longs-1] < 0) {
get2comp(big);
RBIGNUM_SET_SIGN(big, 0);
}
return bignorm(big);
}
}
/* number of bytes of abs(val). additionaly number of leading zeros can be returned. */
/*
* Calculate the number of bytes to be required to represent
* the absolute value of the integer given as _val_.
*
* [val] an integer.
* [nlz_bits_ret] number of leading zero bits in the most significant byte is returned if not NULL.
*
* This function returns ((val_numbits * CHAR_BIT + CHAR_BIT - 1) / CHAR_BIT)
* where val_numbits is the number of bits of abs(val).
* This function should not overflow.
*
* If nlz_bits_ret is not NULL,
* (return_value * CHAR_BIT - val_numbits) is stored in *nlz_bits_ret.
* In this case, 0 <= *nlz_bits_ret < CHAR_BIT.
*
*/
size_t
rb_absint_size(VALUE val, int *nlz_bits_ret)
{
BDIGIT *dp;
BDIGIT *de;
BDIGIT fixbuf[(sizeof(long) + SIZEOF_BDIGITS - 1) / SIZEOF_BDIGITS];
int num_leading_zeros;
val = rb_to_int(val);
if (FIXNUM_P(val)) {
long v = FIX2LONG(val);
if (v < 0) {
v = -v;
}
#if SIZEOF_BDIGITS == SIZEOF_LONG
fixbuf[0] = v;
#else
{
int i;
for (i = 0; i < numberof(fixbuf); i++) {
fixbuf[i] = (BDIGIT)(v & ((1L << (SIZEOF_BDIGITS * CHAR_BIT)) - 1));
v >>= SIZEOF_BDIGITS * CHAR_BIT;
}
}
#endif
dp = fixbuf;
de = fixbuf + numberof(fixbuf);
}
else {
dp = BDIGITS(val);
de = dp + RBIGNUM_LEN(val);
}
while (dp < de && de[-1] == 0)
de--;
if (dp == de) {
if (nlz_bits_ret)
*nlz_bits_ret = 0;
return 0;
}
num_leading_zeros = nlz(de[-1]);
if (nlz_bits_ret)
*nlz_bits_ret = num_leading_zeros % CHAR_BIT;
return (de - dp) * SIZEOF_BDIGITS - num_leading_zeros / CHAR_BIT;
}
size_t
absint_numwords_bytes(size_t numbytes, int nlz_bits_in_msbyte, size_t word_numbits, size_t *nlz_bits_ret)
{
/*
* word_numbytes = word_numbits / CHAR_BIT
* div, mod = val_numbits.divmod(word_numbits)
*
* q, r = numbytes.divmod(word_numbytes)
* s = q if r * CHAR_BIT >= nlz_bits_in_msbyte
* = q - 1 if otherwise
* t = r * CHAR_BIT - nlz_bits_in_msbyte if r * CHAR_BIT >= nlz_bits_in_msbyte
* = word_numbits + r * CHAR_BIT - nlz_bits_in_msbyte if otherwise
*
* div = (numbytes * CHAR_BIT - nlz_bits_in_msbyte) / word_numbits
* = ((q * word_numbytes + r) * CHAR_BIT - nlz_bits_in_msbyte) / word_numbits
* = (q * word_numbytes * CHAR_BIT + r * CHAR_BIT - nlz_bits_in_msbyte) / word_numbits
* = q + (r * CHAR_BIT - nlz_bits_in_msbyte) / word_numbits if r * CHAR_BIT >= nlz_bits_in_msbyte
* q - 1 + (word_numbits + r * CHAR_BIT - nlz_bits_in_msbyte) / word_numbits if r * CHAR_BIT < nlz_bits_in_msbyte
* = s + t / word_numbits
* mod = (r * CHAR_BIT - nlz_bits_in_msbyte) % word_numbits if r * CHAR_BIT >= nlz_bits_in_msbyte
* (word_numbits + r * CHAR_BIT - nlz_bits_in_msbyte) % word_numbits if r * CHAR_BIT < nlz_bits_in_msbyte
* = t % word_numbits
*
* numwords = mod == 0 ? div : div + 1
* nlz_bits = mod == 0 ? 0 : word_numbits - mod
*/
size_t word_numbytes = word_numbits / CHAR_BIT;
size_t q = numbytes / word_numbytes;
size_t r = numbytes % word_numbytes;
size_t s, t;
size_t div, mod;
size_t numwords;
size_t nlz_bits;
if (r * CHAR_BIT >= (size_t)nlz_bits_in_msbyte) {
s = q;
t = r * CHAR_BIT - nlz_bits_in_msbyte;
}
else {
s = q - 1;
t = word_numbits - nlz_bits_in_msbyte + r * CHAR_BIT;
}
div = s + t / word_numbits;
mod = t % word_numbits;
numwords = mod == 0 ? div : div + 1;
nlz_bits = mod == 0 ? 0 : word_numbits - mod;
*nlz_bits_ret = nlz_bits;
return numwords;
}
size_t
absint_numwords_small(size_t numbytes, int nlz_bits_in_msbyte, size_t word_numbits, size_t *nlz_bits_ret)
{
size_t val_numbits = numbytes * CHAR_BIT - nlz_bits_in_msbyte;
size_t div = val_numbits / word_numbits;
size_t mod = val_numbits % word_numbits;
size_t numwords;
size_t nlz_bits;
numwords = mod == 0 ? div : div + 1;
nlz_bits = mod == 0 ? 0 : word_numbits - mod;
*nlz_bits_ret = nlz_bits;
return numwords;
}
size_t
absint_numwords_generic(size_t numbytes, int nlz_bits_in_msbyte, size_t word_numbits, size_t *nlz_bits_ret)
{
VALUE val_numbits, word_numbits_v;
VALUE div_mod, div, mod;
int sign;
size_t numwords;
size_t nlz_bits;
/*
* val_numbits = numbytes * CHAR_BIT - nlz_bits_in_msbyte
* div, mod = val_numbits.divmod(word_numbits)
* numwords = mod == 0 ? div : div + 1
* nlz_bits = mod == 0 ? 0 : word_numbits - mod
*/
val_numbits = SIZET2NUM(numbytes);
val_numbits = rb_funcall(val_numbits, '*', 1, LONG2FIX(CHAR_BIT));
if (nlz_bits_in_msbyte)
val_numbits = rb_funcall(val_numbits, '-', 1, LONG2FIX(nlz_bits_in_msbyte));
word_numbits_v = SIZET2NUM(word_numbits);
div_mod = rb_funcall(val_numbits, rb_intern("divmod"), 1, word_numbits_v);
div = RARRAY_AREF(div_mod, 0);
mod = RARRAY_AREF(div_mod, 1);
if (mod == LONG2FIX(0)) {
nlz_bits = 0;
}
else {
div = rb_funcall(div, '+', 1, LONG2FIX(1));
nlz_bits = word_numbits - NUM2SIZET(mod);
}
sign = rb_integer_pack(div, &numwords, 1, sizeof(numwords), 0,
INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
if (sign == 2)
return (size_t)-1;
*nlz_bits_ret = nlz_bits;
return numwords;
}
/*
* Calculate the number of words to be required to represent
* the absolute value of the integer given as _val_.
*
* [val] an integer.
* [word_numbits] number of bits in a word.
* [nlz_bits_ret] number of leading zero bits in the most significant word is returned if not NULL.
*
* This function returns ((val_numbits * CHAR_BIT + word_numbits - 1) / word_numbits)
* where val_numbits is the number of bits of abs(val).
*
* This function can overflow.
* When overflow occur, (size_t)-1 is returned.
*
* If nlz_bits_ret is not NULL and overflow is not occur,
* (return_value * word_numbits - val_numbits) is stored in *nlz_bits_ret.
* In this case, 0 <= *nlz_bits_ret < word_numbits.
*
*/
size_t
rb_absint_numwords(VALUE val, size_t word_numbits, size_t *nlz_bits_ret)
{
size_t numbytes;
int nlz_bits_in_msbyte;
size_t numwords;
size_t nlz_bits;
if (word_numbits == 0)
return (size_t)-1;
numbytes = rb_absint_size(val, &nlz_bits_in_msbyte);
if (numbytes <= SIZE_MAX / CHAR_BIT) {
numwords = absint_numwords_small(numbytes, nlz_bits_in_msbyte, word_numbits, &nlz_bits);
#if 0
size_t numwords0, nlz_bits0;
numwords0 = absint_numwords_generic(numbytes, nlz_bits_in_msbyte, word_numbits, &nlz_bits0);
assert(numwords0 == numwords);
assert(nlz_bits0 == nlz_bits);
#endif
}
else if (word_numbits % CHAR_BIT == 0) {
numwords = absint_numwords_bytes(numbytes, nlz_bits_in_msbyte, word_numbits, &nlz_bits);
#if 0
size_t numwords0, nlz_bits0;
numwords0 = absint_numwords_generic(numbytes, nlz_bits_in_msbyte, word_numbits, &nlz_bits0);
assert(numwords0 == numwords);
assert(nlz_bits0 == nlz_bits);
#endif
}
else {
numwords = absint_numwords_generic(numbytes, nlz_bits_in_msbyte, word_numbits, &nlz_bits);
}
if (numwords == (size_t)-1)
return numwords;
if (nlz_bits_ret)
*nlz_bits_ret = nlz_bits;
return numwords;
}
int
rb_absint_singlebit_p(VALUE val)
{
BDIGIT *dp;
BDIGIT *de;
BDIGIT fixbuf[(sizeof(long) + SIZEOF_BDIGITS - 1) / SIZEOF_BDIGITS];
BDIGIT d;
val = rb_to_int(val);
if (FIXNUM_P(val)) {
long v = FIX2LONG(val);
if (v < 0) {
v = -v;
}
#if SIZEOF_BDIGITS == SIZEOF_LONG
fixbuf[0] = v;
#else
{
int i;
for (i = 0; i < numberof(fixbuf); i++) {
fixbuf[i] = (BDIGIT)(v & ((1L << (SIZEOF_BDIGITS * CHAR_BIT)) - 1));
v >>= SIZEOF_BDIGITS * CHAR_BIT;
}
}
#endif
dp = fixbuf;
de = fixbuf + numberof(fixbuf);
}
else {
dp = BDIGITS(val);
de = dp + RBIGNUM_LEN(val);
}
while (dp < de && de[-1] == 0)
de--;
while (dp < de && dp[0] == 0)
dp++;
if (dp == de) /* no bit set. */
return 0;
if (dp != de-1) /* two non-zero words. two bits set, at least. */
return 0;
d = *dp;
d = d & (d - 1); /* Clear the least significant bit set */
return d == 0;
}
#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 wordsize, size_t nails, int flags)
{
int wordorder_bits = flags & INTEGER_PACK_WORDORDER_MASK;
int byteorder_bits = flags & INTEGER_PACK_BYTEORDER_MASK;
if (wordorder_bits != INTEGER_PACK_MSWORD_FIRST &&
wordorder_bits != INTEGER_PACK_LSWORD_FIRST)
rb_raise(rb_eArgError, "unexpected word order");
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);
}
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 && SIZEOF_BDIGITS * CHAR_BIT <= (int)sizeof(*ddp) * CHAR_BIT - *numbits_in_dd_p) {
*ddp |= (BDIGIT_DBL)(*(*dpp)++) << *numbits_in_dd_p;
*numbits_in_dd_p += SIZEOF_BDIGITS * CHAR_BIT;
}
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;
}
/*
* Export an integer into a buffer.
*
* This function fills the buffer specified by _words_ and _numwords_ as
* abs(val) in the format specified by _wordsize_, _nails_ and _flags_.
*
* [val] Fixnum, Bignum or another integer like object which has to_int method.
* [words] buffer to export abs(val).
* [numwords] the size of given buffer as number of words.
* [wordsize] the size of word as number of bytes.
* [nails] number of padding bits in a word.
* Most significant nails bits of each word are filled by zero.
* [flags] bitwise or of constants which name starts "INTEGER_PACK_".
* It specifies word order and byte order.
*
* This function returns the signedness and overflow condition as follows:
* -2 : negative overflow. val <= -2**(numwords*(wordsize*CHAR_BIT-nails))
* -1 : negative without overflow. -2**(numwords*(wordsize*CHAR_BIT-nails)) < val < 0
* 0 : zero. val == 0
* 1 : positive without overflow. 0 < val < 2**(numwords*(wordsize*CHAR_BIT-nails))
* 2 : positive overflow. 2**(numwords*(wordsize*CHAR_BIT-nails)) <= val
*
* The least significant words of abs(val) are filled in the buffer when overflow occur.
*/
int
rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
{
int sign;
BDIGIT *dp;
BDIGIT *de;
BDIGIT fixbuf[(sizeof(long) + SIZEOF_BDIGITS - 1) / SIZEOF_BDIGITS];
unsigned char *buf, *bufend;
val = rb_to_int(val);
validate_integer_pack_format(wordsize, nails, flags);
if (SIZE_MAX / wordsize < numwords)
rb_raise(rb_eArgError, "too big numwords * wordsize: %"PRI_SIZE_PREFIX"u * %"PRI_SIZE_PREFIX"u", numwords, wordsize);
if (FIXNUM_P(val)) {
long v = FIX2LONG(val);
if (v < 0) {
sign = -1;
v = -v;
}
else {
sign = 1;
}
#if SIZEOF_BDIGITS == SIZEOF_LONG
fixbuf[0] = v;
#else
{
int i;
for (i = 0; i < numberof(fixbuf); i++) {
fixbuf[i] = (BDIGIT)(v & ((1L << (SIZEOF_BDIGITS * CHAR_BIT)) - 1));
v >>= SIZEOF_BDIGITS * CHAR_BIT;
}
}
#endif
dp = fixbuf;
de = fixbuf + numberof(fixbuf);
}
else {
sign = RBIGNUM_POSITIVE_P(val) ? 1 : -1;
dp = BDIGITS(val);
de = dp + RBIGNUM_LEN(val);
}
while (dp < de && de[-1] == 0)
de--;
if (dp == de) {
sign = 0;
}
buf = words;
bufend = buf + numwords * wordsize;
if (buf == bufend) {
sign *= 2; /* overflow if non-zero*/
}
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;
}
if (dp != de || dd)
sign *= 2; /* overflow */
}
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)
{
size_t num_bits;
num_bits = (wordsize * CHAR_BIT - nails) * numwords;
return (num_bits + SIZEOF_BDIGITS*CHAR_BIT - 1) / (SIZEOF_BDIGITS*CHAR_BIT);
}
static size_t
integer_unpack_num_bdigits_bytes(size_t numwords, size_t wordsize)