bit-twiddle 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21f2a0957be75960781800875823e68dccce1148
4
- data.tar.gz: 1f425d6f4730abd9f318abd6fc183421604707f4
3
+ metadata.gz: 5c493212409e8f17cb33cdaa7b6432b0af607380
4
+ data.tar.gz: bb92c9458d0acc157d2ae8ba8b81b9275b805f96
5
5
  SHA512:
6
- metadata.gz: cb14a13b0a2d58a0538a7f16609b9782d9a79a7aee9efc4a2a0ae670fac35ac41c2326be5d0c481abe50926dace7094a68472d16f760ce2ac06ed369bb1635ea
7
- data.tar.gz: dac2a365b3b19a5acfd4fb30ba932de68b15e8daa42cd522a9db2c828a5b88f2556a408e91ce232c19b54069312596b2271605745f91819113dde3ec8083e6d6
6
+ metadata.gz: 9f038490f6680cf2a0dcce2c495b06750437878e7a076721629c7318da76ebffaecef28006e33ec1b62d76c459e1e45fbf06c4736d1231c9805136e00b56ac23
7
+ data.tar.gz: f1ce36353a33855ca3b4b424f25e5554d2df3e0550ba6837450b23b4ba0ae8b8197e4dc57cc91975260d7b371f4790b7cfa7ac6922881e1e143102be6c0417dd
@@ -1,3 +1,6 @@
1
+ /* Fast C implementation of additional bitwise operations for Ruby
2
+ * Hand-crafted with ♥ by Alex Dowad, using ONLY the finest 1s and 0s */
3
+
1
4
  #include <ruby.h>
2
5
  #include "bt_bignum.h"
3
6
 
@@ -1291,9 +1294,24 @@ bnum_bitreverse64(VALUE bnum)
1291
1294
  return modify_lo64_in_bignum(bnum, reverse64(load_64_from_bignum(bnum)));
1292
1295
  }
1293
1296
 
1297
+ /* Document-class: Fixnum
1298
+ * Ruby's good old Fixnum.
1299
+ *
1300
+ * `require "bit-twiddle/core_ext"` before trying to use any of the below methods.
1301
+ */
1302
+ /* Document-class: Bignum
1303
+ * Ruby's good old Bignum.
1304
+ *
1305
+ * `require "bit-twiddle/core_ext"` before trying to use any of the below methods.
1306
+ */
1307
+ /* Document-class: String
1308
+ * Ruby's good old String.
1309
+ *
1310
+ * `require "bit-twiddle/core_ext"` before trying to use any of the below methods.
1311
+ */
1312
+
1294
1313
  /* Add all `bit-twiddle` methods directly to `Fixnum` and `Bignum`. */
1295
- static VALUE
1296
- bt_add_core_extensions(VALUE self)
1314
+ static void init_core_extensions()
1297
1315
  {
1298
1316
  rb_define_method(rb_cFixnum, "popcount", fnum_popcount, 0);
1299
1317
  rb_define_method(rb_cBignum, "popcount", bnum_popcount, 0);
@@ -1364,7 +1382,13 @@ bt_add_core_extensions(VALUE self)
1364
1382
  rb_define_method(rb_cBignum, "bitreverse32", bnum_bitreverse32, 0);
1365
1383
  rb_define_method(rb_cFixnum, "bitreverse64", fnum_bitreverse64, 0);
1366
1384
  rb_define_method(rb_cBignum, "bitreverse64", bnum_bitreverse64, 0);
1385
+ }
1367
1386
 
1387
+ static VALUE
1388
+ bt_add_core_extensions(VALUE self)
1389
+ {
1390
+ /* this is so Yardoc can find method definitions */
1391
+ init_core_extensions();
1368
1392
  return Qnil;
1369
1393
  }
1370
1394
 
@@ -1421,21 +1445,6 @@ def_wrapper(bitreverse16);
1421
1445
  def_wrapper(bitreverse32);
1422
1446
  def_wrapper(bitreverse64);
1423
1447
 
1424
- /* Document-class: Fixnum
1425
- * Ruby's good old Fixnum.
1426
- *
1427
- * `require "bit-twiddle/core_ext"` before trying to use any of the below methods.
1428
- */
1429
- /* Document-class: Bignum
1430
- * Ruby's good old Bignum.
1431
- *
1432
- * `require "bit-twiddle/core_ext"` before trying to use any of the below methods.
1433
- */
1434
- /* Document-class: String
1435
- * Ruby's good old String.
1436
- *
1437
- * `require "bit-twiddle/core_ext"` before trying to use any of the below methods.
1438
- */
1439
1448
  void Init_bit_twiddle(void)
1440
1449
  {
1441
1450
  VALUE rb_mBitTwiddle = rb_define_module("BitTwiddle");
@@ -1848,79 +1857,4 @@ void Init_bit_twiddle(void)
1848
1857
  * @return [Integer]
1849
1858
  */
1850
1859
  rb_define_singleton_method(rb_mBitTwiddle, "bitreverse64", bt_bitreverse64, 1);
1851
-
1852
- #if 0
1853
- /* The following definitions are executed only on BitTwiddle.add_core_extensions
1854
- * This is a hack for Yardoc -- so Yardoc can find them: */
1855
-
1856
- rb_define_method(rb_cFixnum, "popcount", fnum_popcount, 0);
1857
- rb_define_method(rb_cBignum, "popcount", bnum_popcount, 0);
1858
- rb_define_method(rb_cString, "popcount", str_popcount, 0);
1859
-
1860
- rb_define_method(rb_cFixnum, "lo_bit", fnum_lo_bit, 0);
1861
- rb_define_method(rb_cBignum, "lo_bit", bnum_lo_bit, 0);
1862
- rb_define_method(rb_cFixnum, "hi_bit", fnum_hi_bit, 0);
1863
- rb_define_method(rb_cBignum, "hi_bit", bnum_hi_bit, 0);
1864
-
1865
- rb_define_method(rb_cFixnum, "bswap16", fnum_bswap16, 0);
1866
- rb_define_method(rb_cBignum, "bswap16", bnum_bswap16, 0);
1867
- rb_define_method(rb_cFixnum, "bswap32", fnum_bswap32, 0);
1868
- rb_define_method(rb_cBignum, "bswap32", bnum_bswap32, 0);
1869
- rb_define_method(rb_cFixnum, "bswap64", fnum_bswap64, 0);
1870
- rb_define_method(rb_cBignum, "bswap64", bnum_bswap64, 0);
1871
-
1872
- rb_define_method(rb_cFixnum, "rrot8", fnum_rrot8, 1);
1873
- rb_define_method(rb_cBignum, "rrot8", bnum_rrot8, 1);
1874
- rb_define_method(rb_cFixnum, "rrot16", fnum_rrot16, 1);
1875
- rb_define_method(rb_cBignum, "rrot16", bnum_rrot16, 1);
1876
- rb_define_method(rb_cFixnum, "rrot32", fnum_rrot32, 1);
1877
- rb_define_method(rb_cBignum, "rrot32", bnum_rrot32, 1);
1878
- rb_define_method(rb_cFixnum, "rrot64", fnum_rrot64, 1);
1879
- rb_define_method(rb_cBignum, "rrot64", bnum_rrot64, 1);
1880
-
1881
- rb_define_method(rb_cFixnum, "lrot8", fnum_lrot8, 1);
1882
- rb_define_method(rb_cBignum, "lrot8", bnum_lrot8, 1);
1883
- rb_define_method(rb_cFixnum, "lrot16", fnum_lrot16, 1);
1884
- rb_define_method(rb_cBignum, "lrot16", bnum_lrot16, 1);
1885
- rb_define_method(rb_cFixnum, "lrot32", fnum_lrot32, 1);
1886
- rb_define_method(rb_cBignum, "lrot32", bnum_lrot32, 1);
1887
- rb_define_method(rb_cFixnum, "lrot64", fnum_lrot64, 1);
1888
- rb_define_method(rb_cBignum, "lrot64", bnum_lrot64, 1);
1889
-
1890
- rb_define_method(rb_cFixnum, "lshift8", fnum_lshift8, 1);
1891
- rb_define_method(rb_cBignum, "lshift8", bnum_lshift8, 1);
1892
- rb_define_method(rb_cFixnum, "lshift16", fnum_lshift16, 1);
1893
- rb_define_method(rb_cBignum, "lshift16", bnum_lshift16, 1);
1894
- rb_define_method(rb_cFixnum, "lshift32", fnum_lshift32, 1);
1895
- rb_define_method(rb_cBignum, "lshift32", bnum_lshift32, 1);
1896
- rb_define_method(rb_cFixnum, "lshift64", fnum_lshift64, 1);
1897
- rb_define_method(rb_cBignum, "lshift64", bnum_lshift64, 1);
1898
-
1899
- rb_define_method(rb_cFixnum, "rshift8", fnum_rshift8, 1);
1900
- rb_define_method(rb_cBignum, "rshift8", bnum_rshift8, 1);
1901
- rb_define_method(rb_cFixnum, "rshift16", fnum_rshift16, 1);
1902
- rb_define_method(rb_cBignum, "rshift16", bnum_rshift16, 1);
1903
- rb_define_method(rb_cFixnum, "rshift32", fnum_rshift32, 1);
1904
- rb_define_method(rb_cBignum, "rshift32", bnum_rshift32, 1);
1905
- rb_define_method(rb_cFixnum, "rshift64", fnum_rshift64, 1);
1906
- rb_define_method(rb_cBignum, "rshift64", bnum_rshift64, 1);
1907
-
1908
- rb_define_method(rb_cFixnum, "arith_rshift8", fnum_arith_rshift8, 1);
1909
- rb_define_method(rb_cBignum, "arith_rshift8", bnum_arith_rshift8, 1);
1910
- rb_define_method(rb_cFixnum, "arith_rshift16", fnum_arith_rshift16, 1);
1911
- rb_define_method(rb_cBignum, "arith_rshift16", bnum_arith_rshift16, 1);
1912
- rb_define_method(rb_cFixnum, "arith_rshift32", fnum_arith_rshift32, 1);
1913
- rb_define_method(rb_cBignum, "arith_rshift32", bnum_arith_rshift32, 1);
1914
- rb_define_method(rb_cFixnum, "arith_rshift64", fnum_arith_rshift64, 1);
1915
- rb_define_method(rb_cBignum, "arith_rshift64", bnum_arith_rshift64, 1);
1916
-
1917
- rb_define_method(rb_cFixnum, "bitreverse8", fnum_bitreverse8, 0);
1918
- rb_define_method(rb_cBignum, "bitreverse8", bnum_bitreverse8, 0);
1919
- rb_define_method(rb_cFixnum, "bitreverse16", fnum_bitreverse16, 0);
1920
- rb_define_method(rb_cBignum, "bitreverse16", bnum_bitreverse16, 0);
1921
- rb_define_method(rb_cFixnum, "bitreverse32", fnum_bitreverse32, 0);
1922
- rb_define_method(rb_cBignum, "bitreverse32", bnum_bitreverse32, 0);
1923
- rb_define_method(rb_cFixnum, "bitreverse64", fnum_bitreverse64, 0);
1924
- rb_define_method(rb_cBignum, "bitreverse64", bnum_bitreverse64, 0);
1925
- #endif
1926
1860
  }
@@ -14,8 +14,10 @@ if RUBY_ENGINE == 'rbx'
14
14
  elsif RUBY_VERSION < '2.2.0'
15
15
  check_sizeof 'BDIGIT'
16
16
  $CFLAGS << " -I#{File.join(dir, 'ruby21')} "
17
- else
17
+ elsif RUBY_VERSION < '2.3.0'
18
18
  $CFLAGS << " -I#{File.join(dir, 'ruby22')} "
19
+ else
20
+ $CFLAGS << " -I#{File.join(dir, 'ruby23')} "
19
21
  end
20
22
 
21
23
  check_sizeof 'short'
@@ -0,0 +1,52 @@
1
+
2
+ #if SIZEOF_INT*2 <= SIZEOF_LONG_LONG
3
+ # define BDIGIT unsigned int
4
+ # define SIZEOF_BDIGIT SIZEOF_INT
5
+ #elif SIZEOF_INT*2 <= SIZEOF_LONG
6
+ # define BDIGIT unsigned int
7
+ # define SIZEOF_BDIGIT SIZEOF_INT
8
+ #elif SIZEOF_SHORT*2 <= SIZEOF_LONG
9
+ # define BDIGIT unsigned short
10
+ # define SIZEOF_BDIGIT SIZEOF_SHORT
11
+ #else
12
+ # define BDIGIT unsigned short
13
+ # define SIZEOF_BDIGIT (SIZEOF_LONG/2)
14
+ # define SIZEOF_ACTUAL_BDIGIT SIZEOF_LONG
15
+ #endif
16
+ #ifndef SIZEOF_ACTUAL_BDIGIT
17
+ # define SIZEOF_ACTUAL_BDIGIT SIZEOF_BDIGIT
18
+ #endif
19
+
20
+ #define BIGNUM_EMBED_LEN_NUMBITS 3
21
+ #if (SIZEOF_VALUE*3/SIZEOF_ACTUAL_BDIGIT) < (1 << BIGNUM_EMBED_LEN_NUMBITS)-1
22
+ # define BIGNUM_EMBED_LEN_MAX (SIZEOF_VALUE*3/SIZEOF_ACTUAL_BDIGIT)
23
+ #else
24
+ # define BIGNUM_EMBED_LEN_MAX ((1 << BIGNUM_EMBED_LEN_NUMBITS)-1)
25
+ #endif
26
+
27
+ struct RBignum {
28
+ struct RBasic basic;
29
+ union {
30
+ struct {
31
+ size_t len;
32
+ BDIGIT *digits;
33
+ } heap;
34
+ BDIGIT ary[BIGNUM_EMBED_LEN_MAX];
35
+ } as;
36
+ };
37
+
38
+ #define BIGNUM_EMBED_FLAG FL_USER2
39
+ #define BIGNUM_EMBED_LEN_MASK (FL_USER5|FL_USER4|FL_USER3)
40
+ #define BIGNUM_EMBED_LEN_SHIFT (FL_USHIFT+BIGNUM_EMBED_LEN_NUMBITS)
41
+ #define RBIGNUM_LEN(b) \
42
+ ((RBASIC(b)->flags & BIGNUM_EMBED_FLAG) ? \
43
+ (long)((RBASIC(b)->flags >> BIGNUM_EMBED_LEN_SHIFT) & \
44
+ (BIGNUM_EMBED_LEN_MASK >> BIGNUM_EMBED_LEN_SHIFT)) : \
45
+ RBIGNUM(b)->as.heap.len)
46
+ /* LSB:BIGNUM_DIGITS(b)[0], MSB:BIGNUM_DIGITS(b)[BIGNUM_LEN(b)-1] */
47
+ #define RBIGNUM_DIGITS(b) \
48
+ ((RBASIC(b)->flags & BIGNUM_EMBED_FLAG) ? \
49
+ RBIGNUM(b)->as.ary : \
50
+ RBIGNUM(b)->as.heap.digits)
51
+
52
+ #define RBIGNUM(obj) (R_CAST(RBignum)(obj))
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
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Dowad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-25 00:00:00.000000000 Z
11
+ date: 2015-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -94,6 +94,7 @@ files:
94
94
  - ext/bit_twiddle/extconf.rb
95
95
  - ext/bit_twiddle/ruby21/bt_bignum.h
96
96
  - ext/bit_twiddle/ruby22/bt_bignum.h
97
+ - ext/bit_twiddle/ruby23/bt_bignum.h
97
98
  - lib/bit-twiddle.rb
98
99
  - lib/bit-twiddle/core_ext.rb
99
100
  - lib/bit_twiddle.so
@@ -117,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
118
  version: '0'
118
119
  requirements: []
119
120
  rubyforge_project:
120
- rubygems_version: 2.2.2
121
+ rubygems_version: 2.5.0
121
122
  signing_key:
122
123
  specification_version: 4
123
124
  summary: Fast bitwise operations for Ruby