bit-twiddle 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/bit_twiddle/bit_twiddle.c +66 -59
- data/ext/bit_twiddle/extconf.rb +14 -2
- data/lib/bit_twiddle.so +0 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df07a55110e5af925bd93744a433285027625484
|
4
|
+
data.tar.gz: 83f5c1bc321432ca3e02337ed9753211059aafaa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de3c177b4ca0b61c0b1382c9cff137bca48c6fe3cf8195390448e4e34e80c1eb4907359598201c476e87d34681bb24900d38258089ec86ae7fbb7a4cee56c085
|
7
|
+
data.tar.gz: 10813e14ff47ce9a9f7352fd0b800f499320779563421cd078fb3c8463ba96c0cdc970e30f4b02815eedab118dcd326544146513dbd1c3da45ddea3397e93580
|
@@ -4,6 +4,13 @@
|
|
4
4
|
#include <ruby.h>
|
5
5
|
#include "bt_bignum.h"
|
6
6
|
|
7
|
+
#ifndef HAVE_TYPE_ULONG
|
8
|
+
typedef unsigned long ulong;
|
9
|
+
#endif
|
10
|
+
#ifndef HAVE_TYPE_UCHAR
|
11
|
+
typedef unsigned char uchar;
|
12
|
+
#endif
|
13
|
+
|
7
14
|
#define fix_zero LONG2FIX(0L)
|
8
15
|
#define BIGNUM_P(x) RB_TYPE_P((x), T_BIGNUM)
|
9
16
|
|
@@ -53,7 +60,7 @@ bnum_greater(VALUE bnum, BDIGIT value)
|
|
53
60
|
}
|
54
61
|
|
55
62
|
static long
|
56
|
-
value_to_shiftdist(VALUE shiftdist,
|
63
|
+
value_to_shiftdist(VALUE shiftdist, unsigned int bits)
|
57
64
|
{
|
58
65
|
for (;;) {
|
59
66
|
if (FIXNUM_P(shiftdist)) {
|
@@ -84,12 +91,12 @@ value_to_rotdist(VALUE rotdist, long bits, long mask)
|
|
84
91
|
rdist = FIX2LONG(rotdist) % bits;
|
85
92
|
if (rdist < 0)
|
86
93
|
rdist += bits;
|
87
|
-
return rdist;
|
94
|
+
return (ulong)rdist;
|
88
95
|
} else if (BIGNUM_P(rotdist)) {
|
89
96
|
rdist = *RBIGNUM_DIGITS(rotdist) & mask;
|
90
97
|
if (RBIGNUM_NEGATIVE_P(rotdist))
|
91
98
|
rdist = bits - rdist;
|
92
|
-
return rdist;
|
99
|
+
return (ulong)rdist;
|
93
100
|
} else {
|
94
101
|
rotdist = rb_to_int(rotdist);
|
95
102
|
}
|
@@ -102,22 +109,22 @@ store_64_into_bnum(VALUE bnum, uint64_t int64)
|
|
102
109
|
BDIGIT *dest = RBIGNUM_DIGITS(bnum);
|
103
110
|
size_t len = RBIGNUM_LEN(bnum);
|
104
111
|
|
105
|
-
|
112
|
+
#if (SIZEOF_BDIGIT == 8)
|
106
113
|
*dest = int64;
|
107
|
-
|
114
|
+
#else
|
108
115
|
if (len > 1) {
|
109
|
-
*dest = int64;
|
110
|
-
*(dest+1) = int64 >> 32;
|
116
|
+
*dest = (uint32_t)int64;
|
117
|
+
*(dest+1) = (uint32_t)(int64 >> 32);
|
111
118
|
} else if ((int64 & (0xFFFFFFFFULL << 32)) == 0) {
|
112
119
|
/* the high 4 bytes are zero anyways */
|
113
|
-
*dest = int64;
|
120
|
+
*dest = (uint32_t)int64;
|
114
121
|
} else {
|
115
122
|
rb_big_resize(bnum, 2);
|
116
123
|
dest = RBIGNUM_DIGITS(bnum); /* may have moved */
|
117
|
-
*dest = int64;
|
118
|
-
*(dest+1) = int64 >> 32;
|
124
|
+
*dest = (uint32_t)int64;
|
125
|
+
*(dest+1) = (uint32_t)(int64 >> 32);
|
119
126
|
}
|
120
|
-
|
127
|
+
#endif
|
121
128
|
}
|
122
129
|
|
123
130
|
static uint64_t
|
@@ -227,7 +234,7 @@ fnum_popcount(VALUE fnum)
|
|
227
234
|
long value = FIX2LONG(fnum);
|
228
235
|
if (value < 0)
|
229
236
|
rb_raise(rb_eRangeError, "can't take popcount of a negative number");
|
230
|
-
return LONG2FIX(__builtin_popcountl(value));
|
237
|
+
return LONG2FIX(__builtin_popcountl((ulong)value));
|
231
238
|
}
|
232
239
|
|
233
240
|
static VALUE
|
@@ -268,8 +275,8 @@ def_int_method(popcount);
|
|
268
275
|
static VALUE
|
269
276
|
str_popcount(VALUE str)
|
270
277
|
{
|
271
|
-
|
272
|
-
|
278
|
+
uchar *p = (uchar*)RSTRING_PTR(str);
|
279
|
+
long length = RSTRING_LEN(str);
|
273
280
|
long bits = 0;
|
274
281
|
|
275
282
|
/* This could be made faster by processing 4/8 bytes at a time */
|
@@ -343,7 +350,7 @@ static VALUE
|
|
343
350
|
bnum_hi_bit(VALUE bnum)
|
344
351
|
{
|
345
352
|
BDIGIT *digit = RBIGNUM_DIGITS(bnum) + (RBIGNUM_LEN(bnum)-1);
|
346
|
-
|
353
|
+
ulong bits = (sizeof(BDIGIT) * 8) * RBIGNUM_LEN(bnum);
|
347
354
|
|
348
355
|
if (RBIGNUM_NEGATIVE_P(bnum))
|
349
356
|
rb_raise(rb_eRangeError, "can't find highest 1 bit in a negative number");
|
@@ -376,14 +383,14 @@ fnum_bswap16(VALUE fnum)
|
|
376
383
|
long value = FIX2LONG(fnum);
|
377
384
|
if (value < 0)
|
378
385
|
rb_raise(rb_eRangeError, "can't swap bytes in a negative number");
|
379
|
-
return LONG2FIX((value & ~
|
386
|
+
return LONG2FIX(((ulong)value & ~0xFFFFUL) | __builtin_bswap16((uint16_t)value));
|
380
387
|
}
|
381
388
|
|
382
389
|
static VALUE
|
383
390
|
bnum_bswap16(VALUE bnum)
|
384
391
|
{
|
385
392
|
if (RBIGNUM_POSITIVE_P(bnum))
|
386
|
-
return modify_lo16_in_bignum(bnum, __builtin_bswap16(*RBIGNUM_DIGITS(bnum)));
|
393
|
+
return modify_lo16_in_bignum(bnum, __builtin_bswap16((uint16_t)*RBIGNUM_DIGITS(bnum)));
|
387
394
|
else
|
388
395
|
rb_raise(rb_eRangeError, "can't swap bytes in a negative number");
|
389
396
|
}
|
@@ -415,7 +422,7 @@ fnum_bswap32(VALUE fnum)
|
|
415
422
|
* That is why we have to use a '2NUM' function, not '2FIX' */
|
416
423
|
return ULONG2NUM(__builtin_bswap32(FIX2LONG(fnum)));
|
417
424
|
else
|
418
|
-
return LONG2FIX((value & ~
|
425
|
+
return LONG2FIX(((ulong)value & ~0xFFFFFFFFUL) | __builtin_bswap32((uint32_t)value));
|
419
426
|
}
|
420
427
|
|
421
428
|
static VALUE
|
@@ -446,7 +453,7 @@ fnum_bswap64(VALUE fnum)
|
|
446
453
|
long value = FIX2LONG(fnum);
|
447
454
|
if (value < 0)
|
448
455
|
rb_raise(rb_eRangeError, "can't swap bytes in a negative number");
|
449
|
-
return ULL2NUM(__builtin_bswap64(value));
|
456
|
+
return ULL2NUM(__builtin_bswap64((uint64_t)value));
|
450
457
|
}
|
451
458
|
|
452
459
|
static VALUE
|
@@ -490,13 +497,13 @@ static VALUE
|
|
490
497
|
fnum_rrot8(VALUE fnum, VALUE rotdist)
|
491
498
|
{
|
492
499
|
long value = FIX2LONG(fnum);
|
493
|
-
return LONG2FIX((value & ~
|
500
|
+
return LONG2FIX(((ulong)value & ~0xFFUL) | rrot8((uint8_t)value, rotdist));
|
494
501
|
}
|
495
502
|
|
496
503
|
static VALUE
|
497
504
|
bnum_rrot8(VALUE bnum, VALUE rotdist)
|
498
505
|
{
|
499
|
-
return modify_lo8_in_bignum(bnum, rrot8(*RBIGNUM_DIGITS(bnum), rotdist));
|
506
|
+
return modify_lo8_in_bignum(bnum, rrot8((uint8_t)*RBIGNUM_DIGITS(bnum), rotdist));
|
500
507
|
}
|
501
508
|
|
502
509
|
/* Document-method: Integer#rrot8
|
@@ -518,13 +525,13 @@ static VALUE
|
|
518
525
|
fnum_rrot16(VALUE fnum, VALUE rotdist)
|
519
526
|
{
|
520
527
|
long value = FIX2LONG(fnum);
|
521
|
-
return LONG2FIX((value & ~
|
528
|
+
return LONG2FIX(((ulong)value & ~0xFFFFUL) | rrot16((uint16_t)value, rotdist));
|
522
529
|
}
|
523
530
|
|
524
531
|
static VALUE
|
525
532
|
bnum_rrot16(VALUE bnum, VALUE rotdist)
|
526
533
|
{
|
527
|
-
return modify_lo16_in_bignum(bnum, rrot16(*RBIGNUM_DIGITS(bnum), rotdist));
|
534
|
+
return modify_lo16_in_bignum(bnum, rrot16((uint16_t)*RBIGNUM_DIGITS(bnum), rotdist));
|
528
535
|
}
|
529
536
|
|
530
537
|
/* Document-method: Integer#rrot16
|
@@ -547,15 +554,15 @@ fnum_rrot32(VALUE fnum, VALUE rotdist)
|
|
547
554
|
{
|
548
555
|
long value = FIX2LONG(fnum);
|
549
556
|
if (SIZEOF_LONG == 8)
|
550
|
-
return LONG2FIX((value & ~
|
557
|
+
return LONG2FIX(((ulong)value & ~0xFFFFFFFFUL) | rrot32((uint32_t)value, rotdist));
|
551
558
|
else
|
552
|
-
return ULONG2NUM(rrot32(value, rotdist));
|
559
|
+
return ULONG2NUM(rrot32((uint32_t)value, rotdist));
|
553
560
|
}
|
554
561
|
|
555
562
|
static VALUE
|
556
563
|
bnum_rrot32(VALUE bnum, VALUE rotdist)
|
557
564
|
{
|
558
|
-
return modify_lo32_in_bignum(bnum, rrot32(*RBIGNUM_DIGITS(bnum), rotdist));
|
565
|
+
return modify_lo32_in_bignum(bnum, rrot32((uint32_t)*RBIGNUM_DIGITS(bnum), rotdist));
|
559
566
|
}
|
560
567
|
|
561
568
|
/* Document-method: Integer#rrot32
|
@@ -602,13 +609,13 @@ static VALUE
|
|
602
609
|
fnum_lrot8(VALUE fnum, VALUE rotdist)
|
603
610
|
{
|
604
611
|
long value = FIX2LONG(fnum);
|
605
|
-
return LONG2FIX((value & ~
|
612
|
+
return LONG2FIX(((ulong)value & ~0xFFUL) | lrot8((uint8_t)value, rotdist));
|
606
613
|
}
|
607
614
|
|
608
615
|
static VALUE
|
609
616
|
bnum_lrot8(VALUE bnum, VALUE rotdist)
|
610
617
|
{
|
611
|
-
return modify_lo8_in_bignum(bnum, lrot8(*RBIGNUM_DIGITS(bnum), rotdist));
|
618
|
+
return modify_lo8_in_bignum(bnum, lrot8((uint8_t)*RBIGNUM_DIGITS(bnum), rotdist));
|
612
619
|
}
|
613
620
|
|
614
621
|
/* Document-method: Integer#lrot8
|
@@ -630,13 +637,13 @@ static VALUE
|
|
630
637
|
fnum_lrot16(VALUE fnum, VALUE rotdist)
|
631
638
|
{
|
632
639
|
long value = FIX2LONG(fnum);
|
633
|
-
return LONG2FIX((value & ~
|
640
|
+
return LONG2FIX(((ulong)value & ~0xFFFFUL) | lrot16((uint16_t)value, rotdist));
|
634
641
|
}
|
635
642
|
|
636
643
|
static VALUE
|
637
644
|
bnum_lrot16(VALUE bnum, VALUE rotdist)
|
638
645
|
{
|
639
|
-
return modify_lo16_in_bignum(bnum, lrot16(*RBIGNUM_DIGITS(bnum), rotdist));
|
646
|
+
return modify_lo16_in_bignum(bnum, lrot16((uint16_t)*RBIGNUM_DIGITS(bnum), rotdist));
|
640
647
|
}
|
641
648
|
|
642
649
|
/* Document-method: Integer#lrot16
|
@@ -658,13 +665,13 @@ static VALUE
|
|
658
665
|
fnum_lrot32(VALUE fnum, VALUE rotdist)
|
659
666
|
{
|
660
667
|
long value = FIX2LONG(fnum);
|
661
|
-
return LONG2FIX((value & ~
|
668
|
+
return LONG2FIX(((ulong)value & ~0xFFFFFFFFUL) | lrot32((uint32_t)value, rotdist));
|
662
669
|
}
|
663
670
|
|
664
671
|
static VALUE
|
665
672
|
bnum_lrot32(VALUE bnum, VALUE rotdist)
|
666
673
|
{
|
667
|
-
return modify_lo32_in_bignum(bnum, lrot32(*RBIGNUM_DIGITS(bnum), rotdist));
|
674
|
+
return modify_lo32_in_bignum(bnum, lrot32((uint32_t)*RBIGNUM_DIGITS(bnum), rotdist));
|
668
675
|
}
|
669
676
|
|
670
677
|
/* Document-method: Integer#lrot32
|
@@ -752,7 +759,7 @@ fnum_lshift8(VALUE fnum, VALUE shiftdist)
|
|
752
759
|
if (shiftdist == fix_zero)
|
753
760
|
return fnum;
|
754
761
|
else
|
755
|
-
return LONG2FIX((value & ~
|
762
|
+
return LONG2FIX(((ulong)value & ~0xFFUL) | lshift8((uint8_t)value, shiftdist));
|
756
763
|
}
|
757
764
|
|
758
765
|
static VALUE
|
@@ -761,7 +768,7 @@ bnum_lshift8(VALUE bnum, VALUE shiftdist)
|
|
761
768
|
if (shiftdist == fix_zero)
|
762
769
|
return bnum;
|
763
770
|
else
|
764
|
-
return modify_lo8_in_bignum(bnum, lshift8(*RBIGNUM_DIGITS(bnum), shiftdist));
|
771
|
+
return modify_lo8_in_bignum(bnum, lshift8((uint8_t)*RBIGNUM_DIGITS(bnum), shiftdist));
|
765
772
|
}
|
766
773
|
|
767
774
|
/* Document-method: Integer#lshift8
|
@@ -787,7 +794,7 @@ fnum_lshift16(VALUE fnum, VALUE shiftdist)
|
|
787
794
|
if (shiftdist == fix_zero)
|
788
795
|
return fnum;
|
789
796
|
else
|
790
|
-
return LONG2FIX((value & ~
|
797
|
+
return LONG2FIX(((ulong)value & ~0xFFFFUL) | lshift16((uint16_t)value, shiftdist));
|
791
798
|
}
|
792
799
|
|
793
800
|
static VALUE
|
@@ -796,7 +803,7 @@ bnum_lshift16(VALUE bnum, VALUE shiftdist)
|
|
796
803
|
if (shiftdist == fix_zero)
|
797
804
|
return bnum;
|
798
805
|
else
|
799
|
-
return modify_lo16_in_bignum(bnum, lshift16(*RBIGNUM_DIGITS(bnum), shiftdist));
|
806
|
+
return modify_lo16_in_bignum(bnum, lshift16((uint16_t)*RBIGNUM_DIGITS(bnum), shiftdist));
|
800
807
|
}
|
801
808
|
|
802
809
|
/* Document-method: Integer#lshift16
|
@@ -822,7 +829,7 @@ fnum_lshift32(VALUE fnum, VALUE shiftdist)
|
|
822
829
|
if (shiftdist == fix_zero)
|
823
830
|
return fnum;
|
824
831
|
else
|
825
|
-
return LONG2FIX((value & ~
|
832
|
+
return LONG2FIX(((ulong)value & ~0xFFFFFFFFUL) | lshift32((uint32_t)value, shiftdist));
|
826
833
|
}
|
827
834
|
|
828
835
|
static VALUE
|
@@ -831,7 +838,7 @@ bnum_lshift32(VALUE bnum, VALUE shiftdist)
|
|
831
838
|
if (shiftdist == fix_zero)
|
832
839
|
return bnum;
|
833
840
|
else
|
834
|
-
return modify_lo32_in_bignum(bnum, lshift32(*RBIGNUM_DIGITS(bnum), shiftdist));
|
841
|
+
return modify_lo32_in_bignum(bnum, lshift32((uint32_t)*RBIGNUM_DIGITS(bnum), shiftdist));
|
835
842
|
}
|
836
843
|
|
837
844
|
/* Document-method: Integer#lshift32
|
@@ -897,7 +904,7 @@ fnum_rshift8(VALUE fnum, VALUE shiftdist)
|
|
897
904
|
if (shiftdist == fix_zero)
|
898
905
|
return fnum;
|
899
906
|
else
|
900
|
-
return LONG2FIX((value & ~
|
907
|
+
return LONG2FIX(((ulong)value & ~0xFFUL) | rshift8((uint8_t)value, shiftdist));
|
901
908
|
}
|
902
909
|
|
903
910
|
static VALUE
|
@@ -906,7 +913,7 @@ bnum_rshift8(VALUE bnum, VALUE shiftdist)
|
|
906
913
|
if (shiftdist == fix_zero)
|
907
914
|
return bnum;
|
908
915
|
else
|
909
|
-
return modify_lo8_in_bignum(bnum, rshift8(*RBIGNUM_DIGITS(bnum), shiftdist));
|
916
|
+
return modify_lo8_in_bignum(bnum, rshift8((uint8_t)*RBIGNUM_DIGITS(bnum), shiftdist));
|
910
917
|
}
|
911
918
|
|
912
919
|
/* Document-method: Integer#rshift8
|
@@ -932,7 +939,7 @@ fnum_rshift16(VALUE fnum, VALUE shiftdist)
|
|
932
939
|
if (shiftdist == fix_zero)
|
933
940
|
return fnum;
|
934
941
|
else
|
935
|
-
return LONG2FIX((value & ~
|
942
|
+
return LONG2FIX(((ulong)value & ~0xFFFFUL) | rshift16((uint16_t)value, shiftdist));
|
936
943
|
}
|
937
944
|
|
938
945
|
static VALUE
|
@@ -941,7 +948,7 @@ bnum_rshift16(VALUE bnum, VALUE shiftdist)
|
|
941
948
|
if (shiftdist == fix_zero)
|
942
949
|
return bnum;
|
943
950
|
else
|
944
|
-
return modify_lo16_in_bignum(bnum, rshift16(*RBIGNUM_DIGITS(bnum), shiftdist));
|
951
|
+
return modify_lo16_in_bignum(bnum, rshift16((uint16_t)*RBIGNUM_DIGITS(bnum), shiftdist));
|
945
952
|
}
|
946
953
|
|
947
954
|
/* Document-method: Integer#rshift16
|
@@ -967,7 +974,7 @@ fnum_rshift32(VALUE fnum, VALUE shiftdist)
|
|
967
974
|
if (shiftdist == fix_zero)
|
968
975
|
return fnum;
|
969
976
|
else
|
970
|
-
return LONG2FIX((value & ~
|
977
|
+
return LONG2FIX(((ulong)value & ~0xFFFFFFFFUL) | rshift32((uint32_t)value, shiftdist));
|
971
978
|
}
|
972
979
|
|
973
980
|
static VALUE
|
@@ -1042,7 +1049,7 @@ fnum_arith_rshift8(VALUE fnum, VALUE shiftdist)
|
|
1042
1049
|
if (shiftdist == fix_zero)
|
1043
1050
|
return fnum;
|
1044
1051
|
else
|
1045
|
-
return LONG2FIX((value & ~0xFFUL) | arith_rshift8(value, shiftdist));
|
1052
|
+
return LONG2FIX(((ulong)value & ~0xFFUL) | arith_rshift8((uint8_t)value, shiftdist));
|
1046
1053
|
}
|
1047
1054
|
|
1048
1055
|
static VALUE
|
@@ -1051,7 +1058,7 @@ bnum_arith_rshift8(VALUE bnum, VALUE shiftdist)
|
|
1051
1058
|
if (shiftdist == fix_zero)
|
1052
1059
|
return bnum;
|
1053
1060
|
else
|
1054
|
-
return modify_lo8_in_bignum(bnum, arith_rshift8(*RBIGNUM_DIGITS(bnum), shiftdist));
|
1061
|
+
return modify_lo8_in_bignum(bnum, arith_rshift8((uint8_t)*RBIGNUM_DIGITS(bnum), shiftdist));
|
1055
1062
|
}
|
1056
1063
|
|
1057
1064
|
/* Document-method: Integer#arith_rshift8
|
@@ -1077,7 +1084,7 @@ fnum_arith_rshift16(VALUE fnum, VALUE shiftdist)
|
|
1077
1084
|
if (shiftdist == fix_zero)
|
1078
1085
|
return fnum;
|
1079
1086
|
else
|
1080
|
-
return LONG2FIX((value & ~0xFFFFUL) | arith_rshift16(value, shiftdist));
|
1087
|
+
return LONG2FIX(((ulong)value & ~0xFFFFUL) | arith_rshift16((uint16_t)value, shiftdist));
|
1081
1088
|
}
|
1082
1089
|
|
1083
1090
|
static VALUE
|
@@ -1086,7 +1093,7 @@ bnum_arith_rshift16(VALUE bnum, VALUE shiftdist)
|
|
1086
1093
|
if (shiftdist == fix_zero)
|
1087
1094
|
return bnum;
|
1088
1095
|
else
|
1089
|
-
return modify_lo16_in_bignum(bnum, arith_rshift16(*RBIGNUM_DIGITS(bnum), shiftdist));
|
1096
|
+
return modify_lo16_in_bignum(bnum, arith_rshift16((uint16_t)*RBIGNUM_DIGITS(bnum), shiftdist));
|
1090
1097
|
}
|
1091
1098
|
|
1092
1099
|
/* Document-method: Integer#arith_rshift16
|
@@ -1112,7 +1119,7 @@ fnum_arith_rshift32(VALUE fnum, VALUE shiftdist)
|
|
1112
1119
|
if (shiftdist == fix_zero)
|
1113
1120
|
return fnum;
|
1114
1121
|
else
|
1115
|
-
return LONG2FIX((value & ~0xFFFFFFFFUL) | arith_rshift32(value, shiftdist));
|
1122
|
+
return LONG2FIX(((ulong)value & ~0xFFFFFFFFUL) | arith_rshift32((uint32_t)value, shiftdist));
|
1116
1123
|
}
|
1117
1124
|
|
1118
1125
|
static VALUE
|
@@ -1146,7 +1153,7 @@ fnum_arith_rshift64(VALUE fnum, VALUE shiftdist)
|
|
1146
1153
|
if (shiftdist == fix_zero)
|
1147
1154
|
return fnum;
|
1148
1155
|
else
|
1149
|
-
return ULONG2NUM(arith_rshift64(FIX2LONG(fnum), shiftdist));
|
1156
|
+
return ULONG2NUM(arith_rshift64((uint64_t)FIX2LONG(fnum), shiftdist));
|
1150
1157
|
}
|
1151
1158
|
|
1152
1159
|
static VALUE
|
@@ -1200,7 +1207,7 @@ static inline uint8_t reverse8(uint8_t value)
|
|
1200
1207
|
/* 64-bit CPU
|
1201
1208
|
* Thanks to the Bit Twiddling Hacks page:
|
1202
1209
|
* http://graphics.stanford.edu/~seander/bithacks.html */
|
1203
|
-
return (value * 0x0202020202UL & 0x010884422010UL) % 1023;
|
1210
|
+
return (uint8_t)((value * 0x0202020202UL & 0x010884422010UL) % 1023);
|
1204
1211
|
else
|
1205
1212
|
/* 32-bit CPU */
|
1206
1213
|
return bitreverse_table[value];
|
@@ -1208,17 +1215,17 @@ static inline uint8_t reverse8(uint8_t value)
|
|
1208
1215
|
|
1209
1216
|
static inline uint16_t reverse16(uint16_t value)
|
1210
1217
|
{
|
1211
|
-
return (bitreverse_table[value & 0xFF] << 8) | bitreverse_table[value >> 8];
|
1218
|
+
return (uint16_t)(bitreverse_table[value & 0xFF] << 8) | bitreverse_table[value >> 8];
|
1212
1219
|
}
|
1213
1220
|
|
1214
1221
|
static inline uint32_t reverse32(uint32_t value)
|
1215
1222
|
{
|
1216
|
-
return ((uint32_t)reverse16(value) << 16) | reverse16(value >> 16);
|
1223
|
+
return ((uint32_t)reverse16((uint16_t)value) << 16) | reverse16(value >> 16);
|
1217
1224
|
}
|
1218
1225
|
|
1219
1226
|
static inline uint64_t reverse64(uint64_t value)
|
1220
1227
|
{
|
1221
|
-
return ((uint64_t)reverse32(value) << 32) | reverse32(value >> 32);
|
1228
|
+
return ((uint64_t)reverse32((uint32_t)value) << 32) | reverse32(value >> 32);
|
1222
1229
|
}
|
1223
1230
|
|
1224
1231
|
static VALUE
|
@@ -1227,7 +1234,7 @@ fnum_bitreverse8(VALUE fnum)
|
|
1227
1234
|
long value = FIX2LONG(fnum);
|
1228
1235
|
if (value < 0)
|
1229
1236
|
rb_raise(rb_eRangeError, "can't reverse bits in a negative number");
|
1230
|
-
return LONG2FIX((value & ~0xFFL) | reverse8(value));
|
1237
|
+
return LONG2FIX((value & ~0xFFL) | reverse8((uint8_t)value));
|
1231
1238
|
}
|
1232
1239
|
|
1233
1240
|
static VALUE
|
@@ -1235,7 +1242,7 @@ bnum_bitreverse8(VALUE bnum)
|
|
1235
1242
|
{
|
1236
1243
|
if (RBIGNUM_NEGATIVE_P(bnum))
|
1237
1244
|
rb_raise(rb_eRangeError, "can't reverse bits in a negative number");
|
1238
|
-
return modify_lo8_in_bignum(bnum, reverse8(*RBIGNUM_DIGITS(bnum)));
|
1245
|
+
return modify_lo8_in_bignum(bnum, reverse8((uint8_t)*RBIGNUM_DIGITS(bnum)));
|
1239
1246
|
}
|
1240
1247
|
|
1241
1248
|
/* Document-method: Integer#bitreverse8
|
@@ -1256,7 +1263,7 @@ fnum_bitreverse16(VALUE fnum)
|
|
1256
1263
|
long value = FIX2LONG(fnum);
|
1257
1264
|
if (value < 0)
|
1258
1265
|
rb_raise(rb_eRangeError, "can't reverse bits in a negative number");
|
1259
|
-
return LONG2FIX((value & ~0xFFFFL) | reverse16(value));
|
1266
|
+
return LONG2FIX((value & ~0xFFFFL) | reverse16((uint16_t)value));
|
1260
1267
|
}
|
1261
1268
|
|
1262
1269
|
static VALUE
|
@@ -1264,7 +1271,7 @@ bnum_bitreverse16(VALUE bnum)
|
|
1264
1271
|
{
|
1265
1272
|
if (RBIGNUM_NEGATIVE_P(bnum))
|
1266
1273
|
rb_raise(rb_eRangeError, "can't reverse bits in a negative number");
|
1267
|
-
return modify_lo16_in_bignum(bnum, reverse16(*RBIGNUM_DIGITS(bnum)));
|
1274
|
+
return modify_lo16_in_bignum(bnum, reverse16((uint16_t)*RBIGNUM_DIGITS(bnum)));
|
1268
1275
|
}
|
1269
1276
|
|
1270
1277
|
/* Document-method: Integer#bitreverse16
|
@@ -1283,7 +1290,7 @@ static VALUE
|
|
1283
1290
|
fnum_bitreverse32(VALUE fnum)
|
1284
1291
|
{
|
1285
1292
|
long value = FIX2LONG(fnum);
|
1286
|
-
uint32_t lo32 = value;
|
1293
|
+
uint32_t lo32 = (uint32_t)value;
|
1287
1294
|
if (value < 0)
|
1288
1295
|
rb_raise(rb_eRangeError, "can't reverse bits in a negative number");
|
1289
1296
|
else if (SIZEOF_LONG == 4)
|
@@ -1318,7 +1325,7 @@ fnum_bitreverse64(VALUE fnum)
|
|
1318
1325
|
long value = FIX2LONG(fnum);
|
1319
1326
|
if (value < 0)
|
1320
1327
|
rb_raise(rb_eRangeError, "can't reverse bits in a negative number");
|
1321
|
-
return ULL2NUM(reverse64(value));
|
1328
|
+
return ULL2NUM(reverse64((uint64_t)value));
|
1322
1329
|
}
|
1323
1330
|
|
1324
1331
|
static VALUE
|
data/ext/bit_twiddle/extconf.rb
CHANGED
@@ -7,7 +7,14 @@ if ENV['CC']
|
|
7
7
|
RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC']
|
8
8
|
end
|
9
9
|
|
10
|
-
$CFLAGS << ' -Wall
|
10
|
+
$CFLAGS << ' -Wall ' # turn on all warnings for more thorough code checking
|
11
|
+
# for clang; generated Makefile contains GCC-specific -W options which clang doesn't understand
|
12
|
+
$CFLAGS << ' -Wno-unknown-warning-option '
|
13
|
+
# for clang; ruby.h contains __error__ and __deprecated__, which clang chokes on
|
14
|
+
$CFLAGS << ' -Wno-unknown-attributes -Wno-ignored-attributes '
|
15
|
+
$CFLAGS << ' -Werror ' # convert all warnings to errors so we can't ignore them
|
16
|
+
$CFLAGS << ' -O3 -march=native -mtune=native ' # full optimization
|
17
|
+
$CFLAGS << ' -std=c99 ' # use a modern version of the C standard
|
11
18
|
|
12
19
|
if RUBY_ENGINE == 'rbx'
|
13
20
|
raise "bit-twiddle does not support Rubinius. Sorry!"
|
@@ -22,6 +29,11 @@ check_sizeof 'int'
|
|
22
29
|
check_sizeof 'long'
|
23
30
|
check_sizeof 'long long'
|
24
31
|
|
32
|
+
# if we already have ulong, HAVE_TYPE_ULONG will be defined as a macro
|
33
|
+
have_type 'ulong'
|
34
|
+
# likewise for uchar
|
35
|
+
have_type 'uchar'
|
36
|
+
|
25
37
|
checking_for("whether >> on a signed long is arithmetic shift or logical shift", "%s") do
|
26
38
|
is_arith = try_static_assert("(-1L >> (sizeof(long)/8)) == -1L")
|
27
39
|
$defs.push("-DRSHIFT_IS_ARITH=#{is_arith ? '1' : '0'}")
|
@@ -34,4 +46,4 @@ checking_for("presence of __builtin_bswap16", "%s") do
|
|
34
46
|
have_bswap16 ? "oh yeah" : "nope...but we can sure fix that"
|
35
47
|
end
|
36
48
|
|
37
|
-
create_makefile 'bit_twiddle'
|
49
|
+
create_makefile 'bit_twiddle'
|
data/lib/bit_twiddle.so
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bit-twiddle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Dowad
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|