bit-twiddle 0.0.1 → 0.0.2

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: 1e99fcc28158a3f2bd16c5e37e6cf4ad41761b1c
4
- data.tar.gz: 5456fbb26c9f7ef7ac8bfd825ad06212ee99d757
3
+ metadata.gz: 6c88090274260692eb69445316299a13b3b63b89
4
+ data.tar.gz: d5b7938cfeb8c8de999b052f4c08b59038f97bcf
5
5
  SHA512:
6
- metadata.gz: 26e0e2e18939a02a45e728360fa49befe19a42569e7669de1fa422f22c0c9611832bc069c7a91bf8782dda942814a319c6093981de8e9ea53aa1d68daae74a5e
7
- data.tar.gz: a220ae44f99ea25432b8df8a3ff5ce7ec48781c9416775bff9088ff6243ebd6656c95273cc0cf2ead096e9e640bae7a48529077cb32304f9e0757d23f2630fc0
6
+ metadata.gz: 4b13792ba786af66d85c982aab50165cdeabd8569f3927e9508732190f8cf3e9f2598d7ebbce2bfb68eb8f6de4c0af3f11cb627d9882802acbf2d03aa58ee73a
7
+ data.tar.gz: f4e55b16fe4753d66ddf7e87b3d25692c6e8d5ed0331e6f2eee36122c3f35baba50194638532ac4cf0c5bbfa89452886fc522afdba3ed2421e55d16d61b2e7f3
data/README.md CHANGED
@@ -1,18 +1,116 @@
1
1
  #Fast bitwise operations for Ruby
2
2
 
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. All operations on integers are implemented on both `Fixnum` and `Bignum`. Install this goodness with:
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). All operations on integers are implemented for both `Fixnum`s and `Bignum`s.
4
+
5
+ Install this goodness with:
4
6
 
5
7
  ```ruby
6
8
  gem install bit-twiddle
7
9
  ```
8
10
 
9
- Here is what it offers:
11
+ If you want all operations to be namespaced under the `BitTwiddle` module, load it with:
10
12
 
11
- ### Population count
13
+ ```ruby
14
+ require "bit-twiddle"
15
+ ```
12
16
 
13
- "Popcount" or "population count" refers to the number of 1 bits in a binary number. For example, the popcount of 11 (binary 1011) is 3. Typically, Ruby programmers use goofy tricks like `num.to_s(2).count("1")` to compute this quantity. Don't bother with that nonsense. Just use the fastest native code which GCC can generate for your CPU architecture. (On x86 CPUs with SSE4, that is a single POPCNT instruction.) Like so:
17
+ Or for all operations to be defined as instance methods on `Fixnum` and `Bignum`:
14
18
 
19
+ ```ruby
20
+ require "bit-twiddle/core_ext"
15
21
  ```
22
+
23
+ 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
+ All methods automatically convert between `Fixnum` and `Bignum` as is most appropriate to represent their result.
26
+
27
+ ## Examples
28
+
29
+ ### Population count
30
+
31
+ "Popcount" or "population count" refers to the number of 1 bits in a binary number. For example, the popcount of decimal 11 (binary 1011) is 3.
32
+
33
+ Typically, Ruby programmers use goofy tricks like `num.to_s(2).count("1")` to compute this quantity. This is much faster, and doesn't needlessly allocate memory:
34
+
35
+ ```ruby
16
36
  7.popcount # => 3
17
37
  255.popcount # => 255
18
38
  ```
39
+
40
+ ### Highest/lowest set bit
41
+
42
+ ```ruby
43
+ 8.hi_bit # => 4
44
+ 255.hi_bit # => 8
45
+ 8.lo_bit # => 4
46
+ 255.lo_bit # => 1
47
+ ```
48
+
49
+ ### Rotating bits
50
+
51
+ ```ruby
52
+ 0b10010011.rrot8(1).to_s(2).rjust(8,'0') # => "11001001"
53
+ 0b10010011.rrot8(2).to_s(2).rjust(8,'0') # => "11100100"
54
+ 0b10010011.rrot8(3).to_s(2).rjust(8,'0') # => "01110010"
55
+ 0b10010011.rrot8(4).to_s(2).rjust(8,'0') # => "00111001"
56
+ 0b10010011.rrot8(5).to_s(2).rjust(8,'0') # => "10011100"
57
+ 0b10010011.rrot8(6).to_s(2).rjust(8,'0') # => "01001110"
58
+
59
+ 0b10010011.lrot8(1).to_s(2).rjust(8,'0') # => "00100111"
60
+ 0b10010011.lrot8(2).to_s(2).rjust(8,'0') # => "01001110"
61
+ 0b10010011.lrot8(3).to_s(2).rjust(8,'0') # => "10011100"
62
+ 0b10010011.lrot8(4).to_s(2).rjust(8,'0') # => "00111001"
63
+ 0b10010011.lrot8(5).to_s(2).rjust(8,'0') # => "01110010"
64
+ 0b10010011.lrot8(6).to_s(2).rjust(8,'0') # => "11100100"
65
+ ```
66
+
67
+ 8/16/32/64 bit variants are available.
68
+
69
+ ### Reversing bytes
70
+
71
+ ```ruby
72
+ 0x11223344.bswap16.to_s(16) # => "11224433"
73
+ 0x11223344.bswap32.to_s(16) # => "44332211"
74
+ 0x11223344.bswap64.to_s(16) # => "4433221100000000"
75
+ ```
76
+
77
+ ### Reversing bits
78
+
79
+ ```ruby
80
+ 0b10010011.bitreverse8.to_s(2) # => "11001001"
81
+ ```
82
+
83
+ 8/16/32/64 bit variants are available.
84
+
85
+ ### "Arithmetic" right bitshift
86
+
87
+ An arithmetic right shift fills the vacated bit positions with copies of the most-significant (or "sign") bit. In contrast, Ruby's `Integer#>>` is a "logical" right shift -- it fills the vacated bit positions with zeroes.
88
+
89
+ ```ruby
90
+ 0b10001111.arith_rshift8(3).to_s(2).rjust(8,'0') # => "11110001"
91
+ 0b00001111.arith_rshift8(3).to_s(2).rjust(8,'0') # => "00000001"
92
+ ```
93
+
94
+ 8/16/32/64 bit variants are available.
95
+
96
+ ### Logical left and right bitshifts
97
+
98
+ Ruby already provides `Integer#<<` and `#>>`, which perform logical left and right bitshifts, so these are less useful than the other `BitTwiddle` methods. Probably the only reason to use them is if you want to explicitly operate on the low 8/16/32/64 bits:
99
+
100
+ ```ruby
101
+ 0b10001111.rshift8(3).to_s(2).rjust(8,'0') # => "01000111"
102
+ 0b10001111.lshift8(2).to_s(2).rjust(8,'0') # => "00111100"
103
+ ```
104
+
105
+ 8/16/32/64 bit variants are available.
106
+
107
+ ## Detailed documentation
108
+
109
+ Clone yourself up a copy of this repo, then generate some local HTML documentation (with examples for each and every method):
110
+
111
+ ```
112
+ git clone https://github.com/alexdowad/bit-twiddle.git
113
+ cd bit-twiddle
114
+ bundle install
115
+ rake yard
116
+ ```
@@ -0,0 +1,31 @@
1
+ require 'mkmf'
2
+
3
+ dir = File.dirname(__FILE__)
4
+
5
+ $CFLAGS << ' -Wall -Werror -O3 -march=native -mtune=native '
6
+
7
+ if RUBY_VERSION < '2.2.0'
8
+ check_sizeof 'BDIGIT'
9
+ $CFLAGS << " -I#{File.join(dir, 'ruby21')} "
10
+ else
11
+ $CFLAGS << " -I#{File.join(dir, 'ruby22')} "
12
+ end
13
+
14
+ check_sizeof 'short'
15
+ check_sizeof 'int'
16
+ check_sizeof 'long'
17
+ check_sizeof 'long long'
18
+
19
+ checking_for("whether >> on a signed long is arithmetic shift or logical shift", "%s") do
20
+ is_arith = try_static_assert("(-1L >> (sizeof(long)/8)) == -1L")
21
+ $defs.push("-DRSHIFT_IS_ARITH=#{is_arith ? '1' : '0'}")
22
+ is_arith ? "arithmetic" : "logical"
23
+ end
24
+
25
+ checking_for("presence of __builtin_bswap16", "%s") do
26
+ have_bswap16 = try_static_assert("__builtin_bswap16(0xAABB) == 0xBBAA")
27
+ $defs.push("-DHAVE_BSWAP16=#{have_bswap16 ? '1' : '0'}")
28
+ have_bswap16 ? "oh yeah" : "nope...but we can sure fix that"
29
+ end
30
+
31
+ create_makefile 'bit_twiddle'
@@ -1 +1 @@
1
- require 'bit_twiddling' # C extension
1
+ require 'bit_twiddle' # C extension
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.1
4
+ version: 0.0.2
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-08-29 00:00:00.000000000 Z
11
+ date: 2015-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -66,19 +66,33 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '2.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.8'
69
83
  description: Fast (native) bitwise operations for Ruby, in addition to the ones provided
70
84
  by the standard library
71
85
  email: alexinbeijing@gmail.com
72
86
  executables: []
73
87
  extensions:
74
- - ext/bit_twiddling/extconf.rb
88
+ - ext/bit_twiddle/extconf.rb
75
89
  extra_rdoc_files: []
76
90
  files:
77
91
  - LICENSE
78
92
  - README.md
79
- - ext/bit_twiddling/extconf.rb
93
+ - ext/bit_twiddle/extconf.rb
80
94
  - lib/bit-twiddle.rb
81
- - lib/bit_twiddling.so
95
+ - lib/bit_twiddle.so
82
96
  homepage: http://github.com/alexdowad/bit-twiddle
83
97
  licenses:
84
98
  - None (Public Domain)
@@ -99,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
113
  version: '0'
100
114
  requirements: []
101
115
  rubyforge_project:
102
- rubygems_version: 2.2.2
116
+ rubygems_version: 2.4.5
103
117
  signing_key:
104
118
  specification_version: 4
105
119
  summary: Fast bitwise operations for Ruby
@@ -1,6 +0,0 @@
1
- require 'mkmf'
2
- $CFLAGS << ' -Wall -Werror -O3 -march=native -mtune=native '
3
- check_sizeof 'BDIGIT'
4
- check_sizeof 'int'
5
- check_sizeof 'long'
6
- create_makefile 'bit_twiddling/bit_twiddling'
Binary file