bit-twiddle 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -4
- data/ext/bit_twiddle/bit_twiddle.c +290 -278
- data/ext/bit_twiddle/extconf.rb +0 -3
- data/lib/bit_twiddle.so +0 -0
- metadata +12 -14
- data/ext/bit_twiddle/ruby21/bt_bignum.h +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13445e64bb55645046d83cb47ac06f5fa99bf6e1
|
4
|
+
data.tar.gz: 6502337f2795864ed19612dfa6dfcc8e8f07f474
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3d234cfb1f52c72d8a582a99c0a48054c1fa521aee7669ab8d9190b6939d00cc23755f5232758342acdde06916833497eb4074c88bc0729708c1eab3311b043
|
7
|
+
data.tar.gz: e082a8a0592ec1d07774e1841cadd597292d40b24c14c0b3209b1a5ad2662771a56e4aef479de349ce2d62c674a33e7f81c665d5a7bf60c6189867732f0215fe
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
|
1
|
+
Fast bitwise operations for Ruby
|
2
|
+
================================
|
2
3
|
|
3
|
-
Ruby has built-in implementations of the "workhorses" of bit manipulation: bitwise AND, OR, NOT, and XOR operations and bit shifts. This library adds more bitwise operations, which may be useful in implementing some algorithms. All the added operations are implemented in optimized C code (so this is MRI-only).
|
4
|
+
Ruby has built-in implementations of the "workhorses" of bit manipulation: bitwise AND, OR, NOT, and XOR operations and bit shifts. This library adds more bitwise operations, which may be useful in implementing some algorithms. All the added operations are implemented in optimized C code (so this is MRI-only).
|
4
5
|
|
5
6
|
Install this goodness with:
|
6
7
|
|
@@ -22,8 +23,6 @@ require "bit-twiddle/core_ext"
|
|
22
23
|
|
23
24
|
In many cases, `bit-twiddle` operations explicitly work on the low 8, 16, 32, or 64 bits of an integer. (For example, it defines `#bitreverse8`, `#bitreverse16`, `#bitreverse32`, and `#bitreverse64` methods.) If an integer's bit width is larger than the number of bits operated on, the higher-end bits are passed through unchanged.
|
24
25
|
|
25
|
-
All methods automatically convert between `Fixnum` and `Bignum` as is most appropriate to represent their result.
|
26
|
-
|
27
26
|
## Examples
|
28
27
|
|
29
28
|
### Population count
|
@@ -24,11 +24,11 @@
|
|
24
24
|
#endif
|
25
25
|
|
26
26
|
#if SIZEOF_BDIGIT < 4
|
27
|
-
#error "Sorry,
|
27
|
+
#error "Sorry, Integer#bswap32 and Integer#arith_rshift32 will not work if sizeof(BDIGIT) < 4. Please report this error."
|
28
28
|
#elif SIZEOF_BDIGIT > 8
|
29
29
|
#error "Sorry, several methods will not work if sizeof(BDIGIT) > 8. Please report this error."
|
30
30
|
#elif SIZEOF_LONG > 8
|
31
|
-
#error "Sorry,
|
31
|
+
#error "Sorry, Integer#arith_rshift64 will not work if sizeof(long) > 8. Please report this error."
|
32
32
|
#endif
|
33
33
|
|
34
34
|
#if HAVE_BSWAP16 == 0
|
@@ -204,17 +204,23 @@ modify_lo64_in_bignum(VALUE bnum, uint64_t lo64)
|
|
204
204
|
return result;
|
205
205
|
}
|
206
206
|
|
207
|
-
/*
|
208
|
-
*
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
207
|
+
/* Now start defining implementations of actual Ruby methods
|
208
|
+
* First two helper macros: */
|
209
|
+
#define def_int_method(name) \
|
210
|
+
static VALUE int_ ## name(VALUE integer) { \
|
211
|
+
if (FIXNUM_P(integer)) \
|
212
|
+
return fnum_ ## name(integer); \
|
213
|
+
else \
|
214
|
+
return bnum_ ## name(integer); \
|
215
|
+
}
|
216
|
+
#define def_int_method_with_arg(name) \
|
217
|
+
static VALUE int_ ## name(VALUE integer, VALUE arg) { \
|
218
|
+
if (FIXNUM_P(integer)) \
|
219
|
+
return fnum_ ## name(integer, arg); \
|
220
|
+
else \
|
221
|
+
return bnum_ ## name(integer, arg); \
|
222
|
+
}
|
223
|
+
|
218
224
|
static VALUE
|
219
225
|
fnum_popcount(VALUE fnum)
|
220
226
|
{
|
@@ -242,10 +248,22 @@ bnum_popcount(VALUE bnum)
|
|
242
248
|
return LONG2FIX(bits);
|
243
249
|
}
|
244
250
|
|
251
|
+
/* Document-method: Integer#popcount
|
252
|
+
* Return the number of 1 bits in this integer.
|
253
|
+
*
|
254
|
+
* If the receiver is negative, raise `RangeError`.
|
255
|
+
*
|
256
|
+
* @example
|
257
|
+
* 7.popcount # => 3
|
258
|
+
* 255.popcount # => 8
|
259
|
+
* @return [Integer]
|
260
|
+
*/
|
261
|
+
def_int_method(popcount);
|
262
|
+
|
245
263
|
/* Return the number of 1 bits in all the bytes of this `String`.
|
246
264
|
* @example
|
247
265
|
* "abc".popcount # => 10
|
248
|
-
* @return [
|
266
|
+
* @return [Integer]
|
249
267
|
*/
|
250
268
|
static VALUE
|
251
269
|
str_popcount(VALUE str)
|
@@ -262,18 +280,6 @@ str_popcount(VALUE str)
|
|
262
280
|
return LONG2FIX(bits);
|
263
281
|
}
|
264
282
|
|
265
|
-
/* Document-method: Fixnum#lo_bit
|
266
|
-
* Document-method: Bignum#lo_bit
|
267
|
-
* Return the index of the lowest 1 bit, where the least-significant bit is index 1.
|
268
|
-
* If the receiver is 0, return 0.
|
269
|
-
*
|
270
|
-
* If the receiver is negative, raise `RangeError`.
|
271
|
-
*
|
272
|
-
* @example
|
273
|
-
* 1.lo_bit # => 1
|
274
|
-
* 128.lo_bit # => 8
|
275
|
-
* @return [Fixnum]
|
276
|
-
*/
|
277
283
|
static VALUE
|
278
284
|
fnum_lo_bit(VALUE fnum)
|
279
285
|
{
|
@@ -309,18 +315,19 @@ bnum_lo_bit(VALUE bnum)
|
|
309
315
|
return LONG2FIX(bits);
|
310
316
|
}
|
311
317
|
|
312
|
-
/* Document-method:
|
313
|
-
*
|
314
|
-
* Return the index of the highest 1 bit, where the least-significant bit is index 1.
|
318
|
+
/* Document-method: Integer#lo_bit
|
319
|
+
* Return the index of the lowest 1 bit, where the least-significant bit is index 1.
|
315
320
|
* If the receiver is 0, return 0.
|
316
321
|
*
|
317
322
|
* If the receiver is negative, raise `RangeError`.
|
318
323
|
*
|
319
324
|
* @example
|
320
|
-
* 1.
|
321
|
-
*
|
322
|
-
* @return [
|
325
|
+
* 1.lo_bit # => 1
|
326
|
+
* 128.lo_bit # => 8
|
327
|
+
* @return [Integer]
|
323
328
|
*/
|
329
|
+
def_int_method(lo_bit);
|
330
|
+
|
324
331
|
static VALUE
|
325
332
|
fnum_hi_bit(VALUE fnum)
|
326
333
|
{
|
@@ -350,17 +357,19 @@ bnum_hi_bit(VALUE bnum)
|
|
350
357
|
return LONG2FIX(bits);
|
351
358
|
}
|
352
359
|
|
353
|
-
/* Document-method:
|
354
|
-
*
|
355
|
-
*
|
360
|
+
/* Document-method: Integer#hi_bit
|
361
|
+
* Return the index of the highest 1 bit, where the least-significant bit is index 1.
|
362
|
+
* If the receiver is 0, return 0.
|
356
363
|
*
|
357
364
|
* If the receiver is negative, raise `RangeError`.
|
358
365
|
*
|
359
366
|
* @example
|
360
|
-
*
|
361
|
-
*
|
367
|
+
* 1.hi_bit # => 1
|
368
|
+
* 255.hi_bit # => 8
|
362
369
|
* @return [Integer]
|
363
370
|
*/
|
371
|
+
def_int_method(hi_bit);
|
372
|
+
|
364
373
|
static VALUE
|
365
374
|
fnum_bswap16(VALUE fnum)
|
366
375
|
{
|
@@ -379,18 +388,18 @@ bnum_bswap16(VALUE bnum)
|
|
379
388
|
rb_raise(rb_eRangeError, "can't swap bytes in a negative number");
|
380
389
|
}
|
381
390
|
|
382
|
-
/* Document-method:
|
383
|
-
*
|
384
|
-
* Reverse the least-significant 4 bytes of this integer.
|
391
|
+
/* Document-method: Integer#bswap16
|
392
|
+
* Reverse the least-significant and second least-significant bytes of this integer.
|
385
393
|
*
|
386
|
-
*
|
387
|
-
* of a 32-bit integer. If the receiver is negative, raise `RangeError`.
|
394
|
+
* If the receiver is negative, raise `RangeError`.
|
388
395
|
*
|
389
396
|
* @example
|
390
|
-
*
|
391
|
-
*
|
397
|
+
* 0xFF00.bswap16 # => 255
|
398
|
+
* 0x00FF.bswap16 # => 65280
|
392
399
|
* @return [Integer]
|
393
400
|
*/
|
401
|
+
def_int_method(bswap16);
|
402
|
+
|
394
403
|
static VALUE
|
395
404
|
fnum_bswap32(VALUE fnum)
|
396
405
|
{
|
@@ -418,18 +427,19 @@ bnum_bswap32(VALUE bnum)
|
|
418
427
|
rb_raise(rb_eRangeError, "can't swap bytes in a negative number");
|
419
428
|
}
|
420
429
|
|
421
|
-
/* Document-method:
|
422
|
-
*
|
423
|
-
* Reverse the least-significant 8 bytes of this integer.
|
430
|
+
/* Document-method: Integer#bswap32
|
431
|
+
* Reverse the least-significant 4 bytes of this integer.
|
424
432
|
*
|
425
433
|
* Does not reverse bits within each byte. This can be used to swap endianness
|
426
|
-
* of a
|
434
|
+
* of a 32-bit integer. If the receiver is negative, raise `RangeError`.
|
427
435
|
*
|
428
436
|
* @example
|
429
|
-
* 0xaabbccdd.
|
437
|
+
* 0xaabbccdd.bswap32.to_s(16) # => "ddccbbaa"
|
430
438
|
*
|
431
439
|
* @return [Integer]
|
432
440
|
*/
|
441
|
+
def_int_method(bswap32);
|
442
|
+
|
433
443
|
static VALUE
|
434
444
|
fnum_bswap64(VALUE fnum)
|
435
445
|
{
|
@@ -448,6 +458,19 @@ bnum_bswap64(VALUE bnum)
|
|
448
458
|
rb_raise(rb_eRangeError, "can't swap bytes in a negative number");
|
449
459
|
}
|
450
460
|
|
461
|
+
/* Document-method: Integer#bswap64
|
462
|
+
* Reverse the least-significant 8 bytes of this integer.
|
463
|
+
*
|
464
|
+
* Does not reverse bits within each byte. This can be used to swap endianness
|
465
|
+
* of a 64-bit integer. If the receiver is negative, raise `RangeError`.
|
466
|
+
*
|
467
|
+
* @example
|
468
|
+
* 0xaabbccdd.bswap64.to_s(16) # => "ddccbbaa00000000"
|
469
|
+
*
|
470
|
+
* @return [Integer]
|
471
|
+
*/
|
472
|
+
def_int_method(bswap64);
|
473
|
+
|
451
474
|
#define def_rot_helpers(bits) \
|
452
475
|
static inline uint##bits##_t rrot##bits(uint##bits##_t value, VALUE rotdist) { \
|
453
476
|
ulong rotd = value_to_rotdist(rotdist, bits, bits-1); \
|
@@ -463,20 +486,6 @@ def_rot_helpers(16);
|
|
463
486
|
def_rot_helpers(32);
|
464
487
|
def_rot_helpers(64);
|
465
488
|
|
466
|
-
/* Document-method: Fixnum#rrot8
|
467
|
-
* Document-method: Bignum#rrot8
|
468
|
-
* Right-rotation ("circular shift") of the low 8 bits in this integer.
|
469
|
-
*
|
470
|
-
* If the rotate distance is negative, the bit rotation will be to the left
|
471
|
-
* instead.
|
472
|
-
*
|
473
|
-
* @example
|
474
|
-
* 0b01110001.rrot8(1).to_s(2) # => "10111000"
|
475
|
-
* 0b01110001.rrot8(3).to_s(2) # => "101110"
|
476
|
-
*
|
477
|
-
* @param rotdist [Integer] Number of bit positions to rotate by
|
478
|
-
* @return [Integer]
|
479
|
-
*/
|
480
489
|
static VALUE
|
481
490
|
fnum_rrot8(VALUE fnum, VALUE rotdist)
|
482
491
|
{
|
@@ -490,20 +499,21 @@ bnum_rrot8(VALUE bnum, VALUE rotdist)
|
|
490
499
|
return modify_lo8_in_bignum(bnum, rrot8(*RBIGNUM_DIGITS(bnum), rotdist));
|
491
500
|
}
|
492
501
|
|
493
|
-
/* Document-method:
|
494
|
-
*
|
495
|
-
* Right-rotation ("circular shift") of the low 16 bits in this integer.
|
502
|
+
/* Document-method: Integer#rrot8
|
503
|
+
* Right-rotation ("circular shift") of the low 8 bits in this integer.
|
496
504
|
*
|
497
505
|
* If the rotate distance is negative, the bit rotation will be to the left
|
498
506
|
* instead.
|
499
507
|
*
|
500
508
|
* @example
|
501
|
-
*
|
502
|
-
*
|
509
|
+
* 0b01110001.rrot8(1).to_s(2) # => "10111000"
|
510
|
+
* 0b01110001.rrot8(3).to_s(2) # => "101110"
|
503
511
|
*
|
504
512
|
* @param rotdist [Integer] Number of bit positions to rotate by
|
505
513
|
* @return [Integer]
|
506
514
|
*/
|
515
|
+
def_int_method_with_arg(rrot8);
|
516
|
+
|
507
517
|
static VALUE
|
508
518
|
fnum_rrot16(VALUE fnum, VALUE rotdist)
|
509
519
|
{
|
@@ -517,19 +527,21 @@ bnum_rrot16(VALUE bnum, VALUE rotdist)
|
|
517
527
|
return modify_lo16_in_bignum(bnum, rrot16(*RBIGNUM_DIGITS(bnum), rotdist));
|
518
528
|
}
|
519
529
|
|
520
|
-
/* Document-method:
|
521
|
-
*
|
522
|
-
* Right-rotation ("circular shift") of the low 32 bits in this integer.
|
530
|
+
/* Document-method: Integer#rrot16
|
531
|
+
* Right-rotation ("circular shift") of the low 16 bits in this integer.
|
523
532
|
*
|
524
533
|
* If the rotate distance is negative, the bit rotation will be to the left
|
525
534
|
* instead.
|
526
535
|
*
|
527
536
|
* @example
|
528
|
-
*
|
537
|
+
* 0b0111000101110001.rrot16(1).to_s(2) # => "1011100010111000"
|
538
|
+
* 0b0111000101110001.rrot16(3).to_s(2) # => "10111000101110"
|
529
539
|
*
|
530
540
|
* @param rotdist [Integer] Number of bit positions to rotate by
|
531
541
|
* @return [Integer]
|
532
542
|
*/
|
543
|
+
def_int_method_with_arg(rrot16);
|
544
|
+
|
533
545
|
static VALUE
|
534
546
|
fnum_rrot32(VALUE fnum, VALUE rotdist)
|
535
547
|
{
|
@@ -546,19 +558,20 @@ bnum_rrot32(VALUE bnum, VALUE rotdist)
|
|
546
558
|
return modify_lo32_in_bignum(bnum, rrot32(*RBIGNUM_DIGITS(bnum), rotdist));
|
547
559
|
}
|
548
560
|
|
549
|
-
/* Document-method:
|
550
|
-
*
|
551
|
-
* Right-rotation ("circular shift") of the low 64 bits in this integer.
|
561
|
+
/* Document-method: Integer#rrot32
|
562
|
+
* Right-rotation ("circular shift") of the low 32 bits in this integer.
|
552
563
|
*
|
553
564
|
* If the rotate distance is negative, the bit rotation will be to the left
|
554
565
|
* instead.
|
555
566
|
*
|
556
567
|
* @example
|
557
|
-
*
|
568
|
+
* 0xaabbccdd.rrot32(4).to_s(16) # => "daabbccd"
|
558
569
|
*
|
559
570
|
* @param rotdist [Integer] Number of bit positions to rotate by
|
560
571
|
* @return [Integer]
|
561
572
|
*/
|
573
|
+
def_int_method_with_arg(rrot32);
|
574
|
+
|
562
575
|
static VALUE
|
563
576
|
fnum_rrot64(VALUE fnum, VALUE rotdist)
|
564
577
|
{
|
@@ -571,20 +584,20 @@ bnum_rrot64(VALUE bnum, VALUE rotdist)
|
|
571
584
|
return modify_lo64_in_bignum(bnum, rrot64(load_64_from_bignum(bnum), rotdist));
|
572
585
|
}
|
573
586
|
|
574
|
-
/* Document-method:
|
575
|
-
*
|
576
|
-
* Left-rotation ("circular shift") of the low 8 bits in this integer.
|
587
|
+
/* Document-method: Integer#rrot64
|
588
|
+
* Right-rotation ("circular shift") of the low 64 bits in this integer.
|
577
589
|
*
|
578
|
-
* If the rotate distance is negative, the bit rotation will be to the
|
590
|
+
* If the rotate distance is negative, the bit rotation will be to the left
|
579
591
|
* instead.
|
580
592
|
*
|
581
593
|
* @example
|
582
|
-
*
|
583
|
-
* 0b01110001.lrot8(3).to_s(2) # => "10001011"
|
594
|
+
* 0x11223344aabbccdd.rrot64(4).to_s(16) # => "d11223344aabbccd"
|
584
595
|
*
|
585
596
|
* @param rotdist [Integer] Number of bit positions to rotate by
|
586
597
|
* @return [Integer]
|
587
598
|
*/
|
599
|
+
def_int_method_with_arg(rrot64);
|
600
|
+
|
588
601
|
static VALUE
|
589
602
|
fnum_lrot8(VALUE fnum, VALUE rotdist)
|
590
603
|
{
|
@@ -598,20 +611,21 @@ bnum_lrot8(VALUE bnum, VALUE rotdist)
|
|
598
611
|
return modify_lo8_in_bignum(bnum, lrot8(*RBIGNUM_DIGITS(bnum), rotdist));
|
599
612
|
}
|
600
613
|
|
601
|
-
/* Document-method:
|
602
|
-
*
|
603
|
-
* Left-rotation ("circular shift") of the low 16 bits in this integer.
|
614
|
+
/* Document-method: Integer#lrot8
|
615
|
+
* Left-rotation ("circular shift") of the low 8 bits in this integer.
|
604
616
|
*
|
605
617
|
* If the rotate distance is negative, the bit rotation will be to the right
|
606
618
|
* instead.
|
607
619
|
*
|
608
620
|
* @example
|
609
|
-
*
|
610
|
-
*
|
621
|
+
* 0b01110001.lrot8(1).to_s(2) # => "11100010"
|
622
|
+
* 0b01110001.lrot8(3).to_s(2) # => "10001011"
|
611
623
|
*
|
612
624
|
* @param rotdist [Integer] Number of bit positions to rotate by
|
613
625
|
* @return [Integer]
|
614
626
|
*/
|
627
|
+
def_int_method_with_arg(lrot8);
|
628
|
+
|
615
629
|
static VALUE
|
616
630
|
fnum_lrot16(VALUE fnum, VALUE rotdist)
|
617
631
|
{
|
@@ -625,19 +639,21 @@ bnum_lrot16(VALUE bnum, VALUE rotdist)
|
|
625
639
|
return modify_lo16_in_bignum(bnum, lrot16(*RBIGNUM_DIGITS(bnum), rotdist));
|
626
640
|
}
|
627
641
|
|
628
|
-
/* Document-method:
|
629
|
-
*
|
630
|
-
* Left-rotation ("circular shift") of the low 32 bits in this integer.
|
642
|
+
/* Document-method: Integer#lrot16
|
643
|
+
* Left-rotation ("circular shift") of the low 16 bits in this integer.
|
631
644
|
*
|
632
645
|
* If the rotate distance is negative, the bit rotation will be to the right
|
633
646
|
* instead.
|
634
647
|
*
|
635
648
|
* @example
|
636
|
-
*
|
649
|
+
* 0b0111000101110001.lrot16(1).to_s(2) # => "1110001011100010"
|
650
|
+
* 0b0111000101110001.lrot16(3).to_s(2) # => "1000101110001011"
|
637
651
|
*
|
638
652
|
* @param rotdist [Integer] Number of bit positions to rotate by
|
639
653
|
* @return [Integer]
|
640
654
|
*/
|
655
|
+
def_int_method_with_arg(lrot16);
|
656
|
+
|
641
657
|
static VALUE
|
642
658
|
fnum_lrot32(VALUE fnum, VALUE rotdist)
|
643
659
|
{
|
@@ -651,19 +667,20 @@ bnum_lrot32(VALUE bnum, VALUE rotdist)
|
|
651
667
|
return modify_lo32_in_bignum(bnum, lrot32(*RBIGNUM_DIGITS(bnum), rotdist));
|
652
668
|
}
|
653
669
|
|
654
|
-
/* Document-method:
|
655
|
-
*
|
656
|
-
* Left-rotation ("circular shift") of the low 64 bits in this integer.
|
670
|
+
/* Document-method: Integer#lrot32
|
671
|
+
* Left-rotation ("circular shift") of the low 32 bits in this integer.
|
657
672
|
*
|
658
673
|
* If the rotate distance is negative, the bit rotation will be to the right
|
659
674
|
* instead.
|
660
675
|
*
|
661
676
|
* @example
|
662
|
-
*
|
677
|
+
* 0xaabbccdd.lrot32(4).to_s(16) # => "abbccdda"
|
663
678
|
*
|
664
679
|
* @param rotdist [Integer] Number of bit positions to rotate by
|
665
680
|
* @return [Integer]
|
666
681
|
*/
|
682
|
+
def_int_method_with_arg(lrot32);
|
683
|
+
|
667
684
|
static VALUE
|
668
685
|
fnum_lrot64(VALUE fnum, VALUE rotdist)
|
669
686
|
{
|
@@ -676,6 +693,20 @@ bnum_lrot64(VALUE bnum, VALUE rotdist)
|
|
676
693
|
return modify_lo64_in_bignum(bnum, lrot64(load_64_from_bignum(bnum), rotdist));
|
677
694
|
}
|
678
695
|
|
696
|
+
/* Document-method: Integer#lrot64
|
697
|
+
* Left-rotation ("circular shift") of the low 64 bits in this integer.
|
698
|
+
*
|
699
|
+
* If the rotate distance is negative, the bit rotation will be to the right
|
700
|
+
* instead.
|
701
|
+
*
|
702
|
+
* @example
|
703
|
+
* 0x11223344aabbccdd.lrot64(4).to_s(16) # => "1223344aabbccdd1"
|
704
|
+
*
|
705
|
+
* @param rotdist [Integer] Number of bit positions to rotate by
|
706
|
+
* @return [Integer]
|
707
|
+
*/
|
708
|
+
def_int_method_with_arg(lrot64);
|
709
|
+
|
679
710
|
#define def_shift_helpers(bits) \
|
680
711
|
static uint##bits##_t lshift##bits(uint##bits##_t value, VALUE shiftdist) { \
|
681
712
|
long sdist = value_to_shiftdist(shiftdist, bits); \
|
@@ -714,21 +745,6 @@ def_shift_helpers(16);
|
|
714
745
|
def_shift_helpers(32);
|
715
746
|
def_shift_helpers(64);
|
716
747
|
|
717
|
-
/* Document-method: Fixnum#lshift8
|
718
|
-
* Document-method: Bignum#lshift8
|
719
|
-
* Left-shift of the low 8 bits in this integer.
|
720
|
-
*
|
721
|
-
* If the shift distance is negative, a right shift will be performed instead.
|
722
|
-
* The vacated bit positions will be filled with 0 bits. If shift distance is
|
723
|
-
* more than 7 or less than -7, the low 8 bits will all be zeroed.
|
724
|
-
*
|
725
|
-
* @example
|
726
|
-
* 0x11223344.lshift8(1).to_s(16) # => "11223388"
|
727
|
-
* 0x11223344.lshift8(2).to_s(16) # => "11223310"
|
728
|
-
*
|
729
|
-
* @param shiftdist [Integer] Number of bit positions to shift by
|
730
|
-
* @return [Integer]
|
731
|
-
*/
|
732
748
|
static VALUE
|
733
749
|
fnum_lshift8(VALUE fnum, VALUE shiftdist)
|
734
750
|
{
|
@@ -748,21 +764,22 @@ bnum_lshift8(VALUE bnum, VALUE shiftdist)
|
|
748
764
|
return modify_lo8_in_bignum(bnum, lshift8(*RBIGNUM_DIGITS(bnum), shiftdist));
|
749
765
|
}
|
750
766
|
|
751
|
-
/* Document-method:
|
752
|
-
*
|
753
|
-
* Left-shift of the low 16 bits in this integer.
|
767
|
+
/* Document-method: Integer#lshift8
|
768
|
+
* Left-shift of the low 8 bits in this integer.
|
754
769
|
*
|
755
770
|
* If the shift distance is negative, a right shift will be performed instead.
|
756
771
|
* The vacated bit positions will be filled with 0 bits. If shift distance is
|
757
|
-
* more than
|
772
|
+
* more than 7 or less than -7, the low 8 bits will all be zeroed.
|
758
773
|
*
|
759
774
|
* @example
|
760
|
-
* 0x11223344.
|
761
|
-
* 0x11223344.
|
775
|
+
* 0x11223344.lshift8(1).to_s(16) # => "11223388"
|
776
|
+
* 0x11223344.lshift8(2).to_s(16) # => "11223310"
|
762
777
|
*
|
763
778
|
* @param shiftdist [Integer] Number of bit positions to shift by
|
764
779
|
* @return [Integer]
|
765
780
|
*/
|
781
|
+
def_int_method_with_arg(lshift8);
|
782
|
+
|
766
783
|
static VALUE
|
767
784
|
fnum_lshift16(VALUE fnum, VALUE shiftdist)
|
768
785
|
{
|
@@ -782,21 +799,22 @@ bnum_lshift16(VALUE bnum, VALUE shiftdist)
|
|
782
799
|
return modify_lo16_in_bignum(bnum, lshift16(*RBIGNUM_DIGITS(bnum), shiftdist));
|
783
800
|
}
|
784
801
|
|
785
|
-
/* Document-method:
|
786
|
-
*
|
787
|
-
* Left-shift of the low 32 bits in this integer.
|
802
|
+
/* Document-method: Integer#lshift16
|
803
|
+
* Left-shift of the low 16 bits in this integer.
|
788
804
|
*
|
789
805
|
* If the shift distance is negative, a right shift will be performed instead.
|
790
806
|
* The vacated bit positions will be filled with 0 bits. If shift distance is
|
791
|
-
* more than
|
807
|
+
* more than 15 or less than -15, the low 16 bits will all be zeroed.
|
792
808
|
*
|
793
809
|
* @example
|
794
|
-
* 0x11223344.
|
795
|
-
* 0x11223344.
|
810
|
+
* 0x11223344.lshift16(1).to_s(16) # => "11226688"
|
811
|
+
* 0x11223344.lshift16(2).to_s(16) # => "1122cd10"
|
796
812
|
*
|
797
813
|
* @param shiftdist [Integer] Number of bit positions to shift by
|
798
814
|
* @return [Integer]
|
799
815
|
*/
|
816
|
+
def_int_method_with_arg(lshift16);
|
817
|
+
|
800
818
|
static VALUE
|
801
819
|
fnum_lshift32(VALUE fnum, VALUE shiftdist)
|
802
820
|
{
|
@@ -816,21 +834,22 @@ bnum_lshift32(VALUE bnum, VALUE shiftdist)
|
|
816
834
|
return modify_lo32_in_bignum(bnum, lshift32(*RBIGNUM_DIGITS(bnum), shiftdist));
|
817
835
|
}
|
818
836
|
|
819
|
-
/* Document-method:
|
820
|
-
*
|
821
|
-
* Left-shift of the low 64 bits in this integer.
|
837
|
+
/* Document-method: Integer#lshift32
|
838
|
+
* Left-shift of the low 32 bits in this integer.
|
822
839
|
*
|
823
840
|
* If the shift distance is negative, a right shift will be performed instead.
|
824
841
|
* The vacated bit positions will be filled with 0 bits. If shift distance is
|
825
|
-
* more than
|
842
|
+
* more than 31 or less than -31, the low 32 bits will all be zeroed.
|
826
843
|
*
|
827
844
|
* @example
|
828
|
-
*
|
829
|
-
*
|
845
|
+
* 0x11223344.lshift32(1).to_s(16) # => "22446688"
|
846
|
+
* 0x11223344.lshift32(2).to_s(16) # => "4488cd10"
|
830
847
|
*
|
831
848
|
* @param shiftdist [Integer] Number of bit positions to shift by
|
832
849
|
* @return [Integer]
|
833
850
|
*/
|
851
|
+
def_int_method_with_arg(lshift32);
|
852
|
+
|
834
853
|
static VALUE
|
835
854
|
fnum_lshift64(VALUE fnum, VALUE shiftdist)
|
836
855
|
{
|
@@ -855,21 +874,22 @@ bnum_lshift64(VALUE bnum, VALUE shiftdist)
|
|
855
874
|
return modify_lo64_in_bignum(bnum, lshift64(load_64_from_bignum(bnum), shiftdist));
|
856
875
|
}
|
857
876
|
|
858
|
-
/* Document-method:
|
859
|
-
*
|
860
|
-
* Right-shift of the low 8 bits in this integer.
|
877
|
+
/* Document-method: Integer#lshift64
|
878
|
+
* Left-shift of the low 64 bits in this integer.
|
861
879
|
*
|
862
|
-
* If the shift distance is negative, a
|
880
|
+
* If the shift distance is negative, a right shift will be performed instead.
|
863
881
|
* The vacated bit positions will be filled with 0 bits. If shift distance is
|
864
|
-
* more than
|
882
|
+
* more than 63 or less than -63, the low 64 bits will all be zeroed.
|
865
883
|
*
|
866
884
|
* @example
|
867
|
-
*
|
868
|
-
*
|
885
|
+
* 0x1122334411223344.lshift64(1).to_s(16) # => "2244668822446688"
|
886
|
+
* 0x1122334411223344.lshift64(2).to_s(16) # => "4488cd104488cd10"
|
869
887
|
*
|
870
888
|
* @param shiftdist [Integer] Number of bit positions to shift by
|
871
889
|
* @return [Integer]
|
872
890
|
*/
|
891
|
+
def_int_method_with_arg(lshift64);
|
892
|
+
|
873
893
|
static VALUE
|
874
894
|
fnum_rshift8(VALUE fnum, VALUE shiftdist)
|
875
895
|
{
|
@@ -889,21 +909,22 @@ bnum_rshift8(VALUE bnum, VALUE shiftdist)
|
|
889
909
|
return modify_lo8_in_bignum(bnum, rshift8(*RBIGNUM_DIGITS(bnum), shiftdist));
|
890
910
|
}
|
891
911
|
|
892
|
-
/* Document-method:
|
893
|
-
*
|
894
|
-
* Right-shift of the low 16 bits in this integer.
|
912
|
+
/* Document-method: Integer#rshift8
|
913
|
+
* Right-shift of the low 8 bits in this integer.
|
895
914
|
*
|
896
915
|
* If the shift distance is negative, a left shift will be performed instead.
|
897
916
|
* The vacated bit positions will be filled with 0 bits. If shift distance is
|
898
|
-
* more than
|
917
|
+
* more than 7 or less than -7, the low 8 bits will all be zeroed.
|
899
918
|
*
|
900
919
|
* @example
|
901
|
-
* 0x11223344.
|
902
|
-
* 0x11223344.
|
920
|
+
* 0x11223344.rshift8(1).to_s(16) # => "11223322"
|
921
|
+
* 0x11223344.rshift8(2).to_s(16) # => "11223311"
|
903
922
|
*
|
904
923
|
* @param shiftdist [Integer] Number of bit positions to shift by
|
905
924
|
* @return [Integer]
|
906
925
|
*/
|
926
|
+
def_int_method_with_arg(rshift8);
|
927
|
+
|
907
928
|
static VALUE
|
908
929
|
fnum_rshift16(VALUE fnum, VALUE shiftdist)
|
909
930
|
{
|
@@ -923,21 +944,22 @@ bnum_rshift16(VALUE bnum, VALUE shiftdist)
|
|
923
944
|
return modify_lo16_in_bignum(bnum, rshift16(*RBIGNUM_DIGITS(bnum), shiftdist));
|
924
945
|
}
|
925
946
|
|
926
|
-
/* Document-method:
|
927
|
-
*
|
928
|
-
* Right-shift of the low 32 bits in this integer.
|
947
|
+
/* Document-method: Integer#rshift16
|
948
|
+
* Right-shift of the low 16 bits in this integer.
|
929
949
|
*
|
930
950
|
* If the shift distance is negative, a left shift will be performed instead.
|
931
951
|
* The vacated bit positions will be filled with 0 bits. If shift distance is
|
932
|
-
* more than
|
952
|
+
* more than 15 or less than -15, the low 16 bits will all be zeroed.
|
933
953
|
*
|
934
954
|
* @example
|
935
|
-
* 0x11223344.
|
936
|
-
* 0x11223344.
|
955
|
+
* 0x11223344.rshift16(1).to_s(16) # => "112219a2"
|
956
|
+
* 0x11223344.rshift16(2).to_s(16) # => "11220cd1"
|
937
957
|
*
|
938
958
|
* @param shiftdist [Integer] Number of bit positions to shift by
|
939
959
|
* @return [Integer]
|
940
960
|
*/
|
961
|
+
def_int_method_with_arg(rshift16);
|
962
|
+
|
941
963
|
static VALUE
|
942
964
|
fnum_rshift32(VALUE fnum, VALUE shiftdist)
|
943
965
|
{
|
@@ -957,21 +979,22 @@ bnum_rshift32(VALUE bnum, VALUE shiftdist)
|
|
957
979
|
return modify_lo32_in_bignum(bnum, rshift32(*RBIGNUM_DIGITS(bnum), shiftdist));
|
958
980
|
}
|
959
981
|
|
960
|
-
/* Document-method:
|
961
|
-
*
|
962
|
-
* Right-shift of the low 64 bits in this integer.
|
982
|
+
/* Document-method: Integer#rshift32
|
983
|
+
* Right-shift of the low 32 bits in this integer.
|
963
984
|
*
|
964
985
|
* If the shift distance is negative, a left shift will be performed instead.
|
965
986
|
* The vacated bit positions will be filled with 0 bits. If shift distance is
|
966
|
-
* more than
|
987
|
+
* more than 31 or less than -31, the low 32 bits will all be zeroed.
|
967
988
|
*
|
968
989
|
* @example
|
969
|
-
*
|
970
|
-
*
|
990
|
+
* 0x11223344.rshift32(1).to_s(16) # => "89119a2"
|
991
|
+
* 0x11223344.rshift32(2).to_s(16) # => "4488cd1"
|
971
992
|
*
|
972
993
|
* @param shiftdist [Integer] Number of bit positions to shift by
|
973
994
|
* @return [Integer]
|
974
995
|
*/
|
996
|
+
def_int_method_with_arg(rshift32);
|
997
|
+
|
975
998
|
static VALUE
|
976
999
|
fnum_rshift64(VALUE fnum, VALUE shiftdist)
|
977
1000
|
{
|
@@ -996,21 +1019,22 @@ bnum_rshift64(VALUE bnum, VALUE shiftdist)
|
|
996
1019
|
return modify_lo64_in_bignum(bnum, rshift64(load_64_from_bignum(bnum), shiftdist));
|
997
1020
|
}
|
998
1021
|
|
999
|
-
/* Document-method:
|
1000
|
-
*
|
1001
|
-
* Arithmetic right-shift of the low 8 bits in this integer.
|
1022
|
+
/* Document-method: Integer#rshift64
|
1023
|
+
* Right-shift of the low 64 bits in this integer.
|
1002
1024
|
*
|
1003
|
-
* If
|
1004
|
-
*
|
1005
|
-
*
|
1025
|
+
* If the shift distance is negative, a left shift will be performed instead.
|
1026
|
+
* The vacated bit positions will be filled with 0 bits. If shift distance is
|
1027
|
+
* more than 63 or less than -63, the low 64 bits will all be zeroed.
|
1006
1028
|
*
|
1007
1029
|
* @example
|
1008
|
-
*
|
1009
|
-
*
|
1030
|
+
* 0x1122334411223344.rshift64(1).to_s(16) # => "89119a2089119a2"
|
1031
|
+
* 0x1122334411223344.rshift64(2).to_s(16) # => "4488cd104488cd1"
|
1010
1032
|
*
|
1011
1033
|
* @param shiftdist [Integer] Number of bit positions to shift by
|
1012
1034
|
* @return [Integer]
|
1013
1035
|
*/
|
1036
|
+
def_int_method_with_arg(rshift64);
|
1037
|
+
|
1014
1038
|
static VALUE
|
1015
1039
|
fnum_arith_rshift8(VALUE fnum, VALUE shiftdist)
|
1016
1040
|
{
|
@@ -1030,21 +1054,22 @@ bnum_arith_rshift8(VALUE bnum, VALUE shiftdist)
|
|
1030
1054
|
return modify_lo8_in_bignum(bnum, arith_rshift8(*RBIGNUM_DIGITS(bnum), shiftdist));
|
1031
1055
|
}
|
1032
1056
|
|
1033
|
-
/* Document-method:
|
1034
|
-
*
|
1035
|
-
* Arithmetic right-shift of the low 16 bits in this integer.
|
1057
|
+
/* Document-method: Integer#arith_rshift8
|
1058
|
+
* Arithmetic right-shift of the low 8 bits in this integer.
|
1036
1059
|
*
|
1037
|
-
* If bit
|
1060
|
+
* If bit 8 is a 1, the vacated bit positions will be filled with 1s. Otherwise,
|
1038
1061
|
* they will be filled with 0s. Or, if the shift distance is negative, a left shift
|
1039
1062
|
* will be performed instead, and the vacated bit positions will be filled with 0s.
|
1040
1063
|
*
|
1041
1064
|
* @example
|
1042
|
-
* 0xaabbccdd.
|
1043
|
-
* 0xaabbccdd.
|
1065
|
+
* 0xaabbccdd.arith_rshift8(1).to_s(16) # => "aabbccee"
|
1066
|
+
* 0xaabbccdd.arith_rshift8(2).to_s(16) # => "aabbccf7"
|
1044
1067
|
*
|
1045
1068
|
* @param shiftdist [Integer] Number of bit positions to shift by
|
1046
1069
|
* @return [Integer]
|
1047
1070
|
*/
|
1071
|
+
def_int_method_with_arg(arith_rshift8);
|
1072
|
+
|
1048
1073
|
static VALUE
|
1049
1074
|
fnum_arith_rshift16(VALUE fnum, VALUE shiftdist)
|
1050
1075
|
{
|
@@ -1064,21 +1089,22 @@ bnum_arith_rshift16(VALUE bnum, VALUE shiftdist)
|
|
1064
1089
|
return modify_lo16_in_bignum(bnum, arith_rshift16(*RBIGNUM_DIGITS(bnum), shiftdist));
|
1065
1090
|
}
|
1066
1091
|
|
1067
|
-
/* Document-method:
|
1068
|
-
*
|
1069
|
-
* Arithmetic right-shift of the low 32 bits in this integer.
|
1092
|
+
/* Document-method: Integer#arith_rshift16
|
1093
|
+
* Arithmetic right-shift of the low 16 bits in this integer.
|
1070
1094
|
*
|
1071
|
-
* If bit
|
1095
|
+
* If bit 16 is a 1, the vacated bit positions will be filled with 1s. Otherwise,
|
1072
1096
|
* they will be filled with 0s. Or, if the shift distance is negative, a left shift
|
1073
1097
|
* will be performed instead, and the vacated bit positions will be filled with 0s.
|
1074
1098
|
*
|
1075
1099
|
* @example
|
1076
|
-
*
|
1077
|
-
*
|
1100
|
+
* 0xaabbccdd.arith_rshift16(1).to_s(16) # => "aabbe66e"
|
1101
|
+
* 0xaabbccdd.arith_rshift16(2).to_s(16) # => "aabbf337"
|
1078
1102
|
*
|
1079
1103
|
* @param shiftdist [Integer] Number of bit positions to shift by
|
1080
1104
|
* @return [Integer]
|
1081
1105
|
*/
|
1106
|
+
def_int_method_with_arg(arith_rshift16);
|
1107
|
+
|
1082
1108
|
static VALUE
|
1083
1109
|
fnum_arith_rshift32(VALUE fnum, VALUE shiftdist)
|
1084
1110
|
{
|
@@ -1098,21 +1124,22 @@ bnum_arith_rshift32(VALUE bnum, VALUE shiftdist)
|
|
1098
1124
|
return modify_lo32_in_bignum(bnum, arith_rshift32(*RBIGNUM_DIGITS(bnum), shiftdist));
|
1099
1125
|
}
|
1100
1126
|
|
1101
|
-
/* Document-method:
|
1102
|
-
*
|
1103
|
-
* Arithmetic right-shift of the low 64 bits in this integer.
|
1127
|
+
/* Document-method: Integer#arith_rshift32
|
1128
|
+
* Arithmetic right-shift of the low 32 bits in this integer.
|
1104
1129
|
*
|
1105
|
-
* If bit
|
1130
|
+
* If bit 32 is a 1, the vacated bit positions will be filled with 1s. Otherwise,
|
1106
1131
|
* they will be filled with 0s. Or, if the shift distance is negative, a left shift
|
1107
1132
|
* will be performed instead, and the vacated bit positions will be filled with 0s.
|
1108
1133
|
*
|
1109
1134
|
* @example
|
1110
|
-
* 0xaabbccddaabbccdd.
|
1111
|
-
* 0xaabbccddaabbccdd.
|
1135
|
+
* 0xaabbccddaabbccdd.arith_rshift32(1).to_s(16) # => "d55de66e"
|
1136
|
+
* 0xaabbccddaabbccdd.arith_rshift32(2).to_s(16) # => "eaaef337"
|
1112
1137
|
*
|
1113
1138
|
* @param shiftdist [Integer] Number of bit positions to shift by
|
1114
1139
|
* @return [Integer]
|
1115
1140
|
*/
|
1141
|
+
def_int_method_with_arg(arith_rshift32);
|
1142
|
+
|
1116
1143
|
static VALUE
|
1117
1144
|
fnum_arith_rshift64(VALUE fnum, VALUE shiftdist)
|
1118
1145
|
{
|
@@ -1131,6 +1158,22 @@ bnum_arith_rshift64(VALUE bnum, VALUE shiftdist)
|
|
1131
1158
|
return modify_lo64_in_bignum(bnum, arith_rshift64(load_64_from_bignum(bnum), shiftdist));
|
1132
1159
|
}
|
1133
1160
|
|
1161
|
+
/* Document-method: Integer#arith_rshift64
|
1162
|
+
* Arithmetic right-shift of the low 64 bits in this integer.
|
1163
|
+
*
|
1164
|
+
* If bit 64 is a 1, the vacated bit positions will be filled with 1s. Otherwise,
|
1165
|
+
* they will be filled with 0s. Or, if the shift distance is negative, a left shift
|
1166
|
+
* will be performed instead, and the vacated bit positions will be filled with 0s.
|
1167
|
+
*
|
1168
|
+
* @example
|
1169
|
+
* 0xaabbccddaabbccdd.arith_rshift64(1).to_s(16) # => "d55de66ed55de66e"
|
1170
|
+
* 0xaabbccddaabbccdd.arith_rshift64(2).to_s(16) # => "eaaef3376aaef337"
|
1171
|
+
*
|
1172
|
+
* @param shiftdist [Integer] Number of bit positions to shift by
|
1173
|
+
* @return [Integer]
|
1174
|
+
*/
|
1175
|
+
def_int_method_with_arg(arith_rshift64);
|
1176
|
+
|
1134
1177
|
static const uint8_t bitreverse_table[] =
|
1135
1178
|
{
|
1136
1179
|
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
|
@@ -1178,17 +1221,6 @@ static inline uint64_t reverse64(uint64_t value)
|
|
1178
1221
|
return ((uint64_t)reverse32(value) << 32) | reverse32(value >> 32);
|
1179
1222
|
}
|
1180
1223
|
|
1181
|
-
/* Document-method: Fixnum#bitreverse8
|
1182
|
-
* Document-method: Bignum#bitreverse8
|
1183
|
-
* Reverse the low 8 bits in this integer.
|
1184
|
-
*
|
1185
|
-
* If the receiver is negative, raise `RangeError`.
|
1186
|
-
*
|
1187
|
-
* @example
|
1188
|
-
* 0b01101011.bitreverse8.to_s(2) # => "11010110"
|
1189
|
-
*
|
1190
|
-
* @return [Integer]
|
1191
|
-
*/
|
1192
1224
|
static VALUE
|
1193
1225
|
fnum_bitreverse8(VALUE fnum)
|
1194
1226
|
{
|
@@ -1206,17 +1238,18 @@ bnum_bitreverse8(VALUE bnum)
|
|
1206
1238
|
return modify_lo8_in_bignum(bnum, reverse8(*RBIGNUM_DIGITS(bnum)));
|
1207
1239
|
}
|
1208
1240
|
|
1209
|
-
/* Document-method:
|
1210
|
-
*
|
1211
|
-
* Reverse the low 16 bits in this integer.
|
1241
|
+
/* Document-method: Integer#bitreverse8
|
1242
|
+
* Reverse the low 8 bits in this integer.
|
1212
1243
|
*
|
1213
1244
|
* If the receiver is negative, raise `RangeError`.
|
1214
1245
|
*
|
1215
1246
|
* @example
|
1216
|
-
*
|
1247
|
+
* 0b01101011.bitreverse8.to_s(2) # => "11010110"
|
1217
1248
|
*
|
1218
1249
|
* @return [Integer]
|
1219
1250
|
*/
|
1251
|
+
def_int_method(bitreverse8);
|
1252
|
+
|
1220
1253
|
static VALUE
|
1221
1254
|
fnum_bitreverse16(VALUE fnum)
|
1222
1255
|
{
|
@@ -1234,17 +1267,18 @@ bnum_bitreverse16(VALUE bnum)
|
|
1234
1267
|
return modify_lo16_in_bignum(bnum, reverse16(*RBIGNUM_DIGITS(bnum)));
|
1235
1268
|
}
|
1236
1269
|
|
1237
|
-
/* Document-method:
|
1238
|
-
*
|
1239
|
-
* Reverse the low 32 bits in this integer.
|
1270
|
+
/* Document-method: Integer#bitreverse16
|
1271
|
+
* Reverse the low 16 bits in this integer.
|
1240
1272
|
*
|
1241
1273
|
* If the receiver is negative, raise `RangeError`.
|
1242
1274
|
*
|
1243
1275
|
* @example
|
1244
|
-
*
|
1276
|
+
* 0b0110101100001011.bitreverse16.to_s(2) # => "1101000011010110"
|
1245
1277
|
*
|
1246
1278
|
* @return [Integer]
|
1247
1279
|
*/
|
1280
|
+
def_int_method(bitreverse16);
|
1281
|
+
|
1248
1282
|
static VALUE
|
1249
1283
|
fnum_bitreverse32(VALUE fnum)
|
1250
1284
|
{
|
@@ -1266,17 +1300,18 @@ bnum_bitreverse32(VALUE bnum)
|
|
1266
1300
|
return modify_lo32_in_bignum(bnum, reverse32(*RBIGNUM_DIGITS(bnum)));
|
1267
1301
|
}
|
1268
1302
|
|
1269
|
-
/* Document-method:
|
1270
|
-
*
|
1271
|
-
* Reverse the low 64 bits in this integer.
|
1303
|
+
/* Document-method: Integer#bitreverse32
|
1304
|
+
* Reverse the low 32 bits in this integer.
|
1272
1305
|
*
|
1273
1306
|
* If the receiver is negative, raise `RangeError`.
|
1274
1307
|
*
|
1275
1308
|
* @example
|
1276
|
-
*
|
1309
|
+
* 0x12341234.bitreverse32.to_s(16) # => "2c482c48"
|
1277
1310
|
*
|
1278
1311
|
* @return [Integer]
|
1279
1312
|
*/
|
1313
|
+
def_int_method(bitreverse32);
|
1314
|
+
|
1280
1315
|
static VALUE
|
1281
1316
|
fnum_bitreverse64(VALUE fnum)
|
1282
1317
|
{
|
@@ -1294,13 +1329,20 @@ bnum_bitreverse64(VALUE bnum)
|
|
1294
1329
|
return modify_lo64_in_bignum(bnum, reverse64(load_64_from_bignum(bnum)));
|
1295
1330
|
}
|
1296
1331
|
|
1297
|
-
/* Document-
|
1298
|
-
*
|
1332
|
+
/* Document-method: Integer#bitreverse64
|
1333
|
+
* Reverse the low 64 bits in this integer.
|
1299
1334
|
*
|
1300
|
-
*
|
1335
|
+
* If the receiver is negative, raise `RangeError`.
|
1336
|
+
*
|
1337
|
+
* @example
|
1338
|
+
* 0xabcd1234abcd1234.bitreverse64.to_s(16) # => "2c48b3d52c48b3d5"
|
1339
|
+
*
|
1340
|
+
* @return [Integer]
|
1301
1341
|
*/
|
1302
|
-
|
1303
|
-
|
1342
|
+
def_int_method(bitreverse64);
|
1343
|
+
|
1344
|
+
/* Document-class: Integer
|
1345
|
+
* Ruby's good old Integer.
|
1304
1346
|
*
|
1305
1347
|
* `require "bit-twiddle/core_ext"` before trying to use any of the below methods.
|
1306
1348
|
*/
|
@@ -1310,78 +1352,48 @@ bnum_bitreverse64(VALUE bnum)
|
|
1310
1352
|
* `require "bit-twiddle/core_ext"` before trying to use any of the below methods.
|
1311
1353
|
*/
|
1312
1354
|
|
1313
|
-
/* Add all `bit-twiddle` methods directly to `
|
1355
|
+
/* Add all `bit-twiddle` methods directly to `Integer`. */
|
1314
1356
|
static void init_core_extensions()
|
1315
1357
|
{
|
1316
|
-
rb_define_method(
|
1317
|
-
rb_define_method(rb_cBignum, "popcount", bnum_popcount, 0);
|
1358
|
+
rb_define_method(rb_cInteger, "popcount", int_popcount, 0);
|
1318
1359
|
rb_define_method(rb_cString, "popcount", str_popcount, 0);
|
1319
1360
|
|
1320
|
-
rb_define_method(
|
1321
|
-
rb_define_method(
|
1322
|
-
|
1323
|
-
rb_define_method(
|
1324
|
-
|
1325
|
-
rb_define_method(
|
1326
|
-
|
1327
|
-
rb_define_method(
|
1328
|
-
rb_define_method(
|
1329
|
-
rb_define_method(
|
1330
|
-
rb_define_method(
|
1331
|
-
|
1332
|
-
rb_define_method(
|
1333
|
-
rb_define_method(
|
1334
|
-
rb_define_method(
|
1335
|
-
rb_define_method(
|
1336
|
-
|
1337
|
-
rb_define_method(
|
1338
|
-
rb_define_method(
|
1339
|
-
rb_define_method(
|
1340
|
-
|
1341
|
-
|
1342
|
-
rb_define_method(
|
1343
|
-
rb_define_method(
|
1344
|
-
rb_define_method(
|
1345
|
-
rb_define_method(
|
1346
|
-
|
1347
|
-
rb_define_method(
|
1348
|
-
rb_define_method(
|
1349
|
-
|
1350
|
-
rb_define_method(
|
1351
|
-
|
1352
|
-
rb_define_method(
|
1353
|
-
rb_define_method(
|
1354
|
-
rb_define_method(
|
1355
|
-
rb_define_method(
|
1356
|
-
rb_define_method(rb_cFixnum, "lshift64", fnum_lshift64, 1);
|
1357
|
-
rb_define_method(rb_cBignum, "lshift64", bnum_lshift64, 1);
|
1358
|
-
|
1359
|
-
rb_define_method(rb_cFixnum, "rshift8", fnum_rshift8, 1);
|
1360
|
-
rb_define_method(rb_cBignum, "rshift8", bnum_rshift8, 1);
|
1361
|
-
rb_define_method(rb_cFixnum, "rshift16", fnum_rshift16, 1);
|
1362
|
-
rb_define_method(rb_cBignum, "rshift16", bnum_rshift16, 1);
|
1363
|
-
rb_define_method(rb_cFixnum, "rshift32", fnum_rshift32, 1);
|
1364
|
-
rb_define_method(rb_cBignum, "rshift32", bnum_rshift32, 1);
|
1365
|
-
rb_define_method(rb_cFixnum, "rshift64", fnum_rshift64, 1);
|
1366
|
-
rb_define_method(rb_cBignum, "rshift64", bnum_rshift64, 1);
|
1367
|
-
|
1368
|
-
rb_define_method(rb_cFixnum, "arith_rshift8", fnum_arith_rshift8, 1);
|
1369
|
-
rb_define_method(rb_cBignum, "arith_rshift8", bnum_arith_rshift8, 1);
|
1370
|
-
rb_define_method(rb_cFixnum, "arith_rshift16", fnum_arith_rshift16, 1);
|
1371
|
-
rb_define_method(rb_cBignum, "arith_rshift16", bnum_arith_rshift16, 1);
|
1372
|
-
rb_define_method(rb_cFixnum, "arith_rshift32", fnum_arith_rshift32, 1);
|
1373
|
-
rb_define_method(rb_cBignum, "arith_rshift32", bnum_arith_rshift32, 1);
|
1374
|
-
rb_define_method(rb_cFixnum, "arith_rshift64", fnum_arith_rshift64, 1);
|
1375
|
-
rb_define_method(rb_cBignum, "arith_rshift64", bnum_arith_rshift64, 1);
|
1376
|
-
|
1377
|
-
rb_define_method(rb_cFixnum, "bitreverse8", fnum_bitreverse8, 0);
|
1378
|
-
rb_define_method(rb_cBignum, "bitreverse8", bnum_bitreverse8, 0);
|
1379
|
-
rb_define_method(rb_cFixnum, "bitreverse16", fnum_bitreverse16, 0);
|
1380
|
-
rb_define_method(rb_cBignum, "bitreverse16", bnum_bitreverse16, 0);
|
1381
|
-
rb_define_method(rb_cFixnum, "bitreverse32", fnum_bitreverse32, 0);
|
1382
|
-
rb_define_method(rb_cBignum, "bitreverse32", bnum_bitreverse32, 0);
|
1383
|
-
rb_define_method(rb_cFixnum, "bitreverse64", fnum_bitreverse64, 0);
|
1384
|
-
rb_define_method(rb_cBignum, "bitreverse64", bnum_bitreverse64, 0);
|
1361
|
+
rb_define_method(rb_cInteger, "lo_bit", int_lo_bit, 0);
|
1362
|
+
rb_define_method(rb_cInteger, "hi_bit", int_hi_bit, 0);
|
1363
|
+
|
1364
|
+
rb_define_method(rb_cInteger, "bswap16", int_bswap16, 0);
|
1365
|
+
rb_define_method(rb_cInteger, "bswap32", int_bswap32, 0);
|
1366
|
+
rb_define_method(rb_cInteger, "bswap64", int_bswap64, 0);
|
1367
|
+
|
1368
|
+
rb_define_method(rb_cInteger, "rrot8", int_rrot8, 1);
|
1369
|
+
rb_define_method(rb_cInteger, "rrot16", int_rrot16, 1);
|
1370
|
+
rb_define_method(rb_cInteger, "rrot32", int_rrot32, 1);
|
1371
|
+
rb_define_method(rb_cInteger, "rrot64", int_rrot64, 1);
|
1372
|
+
|
1373
|
+
rb_define_method(rb_cInteger, "lrot8", int_lrot8, 1);
|
1374
|
+
rb_define_method(rb_cInteger, "lrot16", int_lrot16, 1);
|
1375
|
+
rb_define_method(rb_cInteger, "lrot32", int_lrot32, 1);
|
1376
|
+
rb_define_method(rb_cInteger, "lrot64", int_lrot64, 1);
|
1377
|
+
|
1378
|
+
rb_define_method(rb_cInteger, "lshift8", int_lshift8, 1);
|
1379
|
+
rb_define_method(rb_cInteger, "lshift16", int_lshift16, 1);
|
1380
|
+
rb_define_method(rb_cInteger, "lshift32", int_lshift32, 1);
|
1381
|
+
rb_define_method(rb_cInteger, "lshift64", int_lshift64, 1);
|
1382
|
+
|
1383
|
+
rb_define_method(rb_cInteger, "rshift8", int_rshift8, 1);
|
1384
|
+
rb_define_method(rb_cInteger, "rshift16", int_rshift16, 1);
|
1385
|
+
rb_define_method(rb_cInteger, "rshift32", int_rshift32, 1);
|
1386
|
+
rb_define_method(rb_cInteger, "rshift64", int_rshift64, 1);
|
1387
|
+
|
1388
|
+
rb_define_method(rb_cInteger, "arith_rshift8", int_arith_rshift8, 1);
|
1389
|
+
rb_define_method(rb_cInteger, "arith_rshift16", int_arith_rshift16, 1);
|
1390
|
+
rb_define_method(rb_cInteger, "arith_rshift32", int_arith_rshift32, 1);
|
1391
|
+
rb_define_method(rb_cInteger, "arith_rshift64", int_arith_rshift64, 1);
|
1392
|
+
|
1393
|
+
rb_define_method(rb_cInteger, "bitreverse8", int_bitreverse8, 0);
|
1394
|
+
rb_define_method(rb_cInteger, "bitreverse16", int_bitreverse16, 0);
|
1395
|
+
rb_define_method(rb_cInteger, "bitreverse32", int_bitreverse32, 0);
|
1396
|
+
rb_define_method(rb_cInteger, "bitreverse64", int_bitreverse64, 0);
|
1385
1397
|
}
|
1386
1398
|
|
1387
1399
|
static VALUE
|
@@ -1459,7 +1471,7 @@ void Init_bit_twiddle(void)
|
|
1459
1471
|
* If `int` is negative, raise `RangeError`.
|
1460
1472
|
*
|
1461
1473
|
* @param int [Integer] The integer to operate on
|
1462
|
-
* @return [
|
1474
|
+
* @return [Integer]
|
1463
1475
|
*/
|
1464
1476
|
rb_define_singleton_method(rb_mBitTwiddle, "popcount", bt_popcount, 1);
|
1465
1477
|
/* Return the index of the lowest 1 bit, where the least-significant bit is index 1.
|
@@ -1471,7 +1483,7 @@ void Init_bit_twiddle(void)
|
|
1471
1483
|
* If `int` is negative, raise `RangeError`.
|
1472
1484
|
*
|
1473
1485
|
* @param int [Integer] The integer to operate on
|
1474
|
-
* @return [
|
1486
|
+
* @return [Integer]
|
1475
1487
|
*/
|
1476
1488
|
rb_define_singleton_method(rb_mBitTwiddle, "lo_bit", bt_lo_bit, 1);
|
1477
1489
|
/* Return the index of the highest 1 bit, where the least-significant bit is index 1.
|
@@ -1483,7 +1495,7 @@ void Init_bit_twiddle(void)
|
|
1483
1495
|
* If `int` is negative, raise `RangeError`.
|
1484
1496
|
*
|
1485
1497
|
* @param int [Integer] The integer to operate on
|
1486
|
-
* @return [
|
1498
|
+
* @return [Integer]
|
1487
1499
|
*/
|
1488
1500
|
rb_define_singleton_method(rb_mBitTwiddle, "hi_bit", bt_hi_bit, 1);
|
1489
1501
|
/* Reverse the least-significant and second least-significant bytes of `int`.
|
data/ext/bit_twiddle/extconf.rb
CHANGED
@@ -11,9 +11,6 @@ $CFLAGS << ' -Wall -Werror -O3 -march=native -mtune=native -std=c99 '
|
|
11
11
|
|
12
12
|
if RUBY_ENGINE == 'rbx'
|
13
13
|
raise "bit-twiddle does not support Rubinius. Sorry!"
|
14
|
-
elsif RUBY_VERSION < '2.2.0'
|
15
|
-
check_sizeof 'BDIGIT'
|
16
|
-
$CFLAGS << " -I#{File.join(dir, 'ruby21')} "
|
17
14
|
elsif RUBY_VERSION < '2.3.0'
|
18
15
|
$CFLAGS << " -I#{File.join(dir, 'ruby22')} "
|
19
16
|
else
|
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.6
|
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: 2017-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '3.
|
19
|
+
version: '3.7'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '3.
|
26
|
+
version: '3.7'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '12.2'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '12.2'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake-compiler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,28 +58,28 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '2.
|
61
|
+
version: '2.7'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '2.
|
68
|
+
version: '2.7'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: yard
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0.
|
75
|
+
version: '0.9'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0.
|
82
|
+
version: '0.9'
|
83
83
|
description: Fast (native) bitwise operations for Ruby, in addition to the ones provided
|
84
84
|
by the standard library
|
85
85
|
email: alexinbeijing@gmail.com
|
@@ -92,7 +92,6 @@ files:
|
|
92
92
|
- README.md
|
93
93
|
- ext/bit_twiddle/bit_twiddle.c
|
94
94
|
- ext/bit_twiddle/extconf.rb
|
95
|
-
- ext/bit_twiddle/ruby21/bt_bignum.h
|
96
95
|
- ext/bit_twiddle/ruby22/bt_bignum.h
|
97
96
|
- ext/bit_twiddle/ruby23/bt_bignum.h
|
98
97
|
- lib/bit-twiddle.rb
|
@@ -110,7 +109,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
109
|
requirements:
|
111
110
|
- - ">="
|
112
111
|
- !ruby/object:Gem::Version
|
113
|
-
version:
|
112
|
+
version: 2.2.8
|
114
113
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
114
|
requirements:
|
116
115
|
- - ">="
|
@@ -118,9 +117,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
117
|
version: '0'
|
119
118
|
requirements: []
|
120
119
|
rubyforge_project:
|
121
|
-
rubygems_version: 2.
|
120
|
+
rubygems_version: 2.6.13
|
122
121
|
signing_key:
|
123
122
|
specification_version: 4
|
124
123
|
summary: Fast bitwise operations for Ruby
|
125
124
|
test_files: []
|
126
|
-
has_rdoc:
|
@@ -1 +0,0 @@
|
|
1
|
-
/* nothing special needed for MRI Ruby 1.9.3 - 2.1 */
|