bit-twiddle 0.0.4 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.md +3 -4
- data/ext/bit_twiddle/bit_twiddle.c +377 -424
- data/ext/bit_twiddle/extconf.rb +19 -6
- data/ext/bit_twiddle/ruby23/bt_bignum.h +52 -0
- data/ext/bit_twiddle/ruby30/bt_bignum.h +83 -0
- metadata +17 -19
- data/ext/bit_twiddle/ruby21/bt_bignum.h +0 -1
- data/lib/bit_twiddle.so +0 -0
data/ext/bit_twiddle/extconf.rb
CHANGED
@@ -7,15 +7,23 @@ 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!"
|
14
|
-
elsif RUBY_VERSION < '2.
|
15
|
-
check_sizeof 'BDIGIT'
|
16
|
-
$CFLAGS << " -I#{File.join(dir, 'ruby21')} "
|
17
|
-
else
|
21
|
+
elsif RUBY_VERSION < '2.3.0'
|
18
22
|
$CFLAGS << " -I#{File.join(dir, 'ruby22')} "
|
23
|
+
elsif RUBY_VERSION < '3.0.0'
|
24
|
+
$CFLAGS << " -I#{File.join(dir, 'ruby23')} "
|
25
|
+
else
|
26
|
+
$CFLAGS << " -I#{File.join(dir, 'ruby30')} "
|
19
27
|
end
|
20
28
|
|
21
29
|
check_sizeof 'short'
|
@@ -23,6 +31,11 @@ check_sizeof 'int'
|
|
23
31
|
check_sizeof 'long'
|
24
32
|
check_sizeof 'long long'
|
25
33
|
|
34
|
+
# if we already have ulong, HAVE_TYPE_ULONG will be defined as a macro
|
35
|
+
have_type 'ulong'
|
36
|
+
# likewise for uchar
|
37
|
+
have_type 'uchar'
|
38
|
+
|
26
39
|
checking_for("whether >> on a signed long is arithmetic shift or logical shift", "%s") do
|
27
40
|
is_arith = try_static_assert("(-1L >> (sizeof(long)/8)) == -1L")
|
28
41
|
$defs.push("-DRSHIFT_IS_ARITH=#{is_arith ? '1' : '0'}")
|
@@ -35,4 +48,4 @@ checking_for("presence of __builtin_bswap16", "%s") do
|
|
35
48
|
have_bswap16 ? "oh yeah" : "nope...but we can sure fix that"
|
36
49
|
end
|
37
50
|
|
38
|
-
create_makefile 'bit_twiddle'
|
51
|
+
create_makefile 'bit_twiddle'
|
@@ -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))
|
@@ -0,0 +1,83 @@
|
|
1
|
+
#if SIZEOF_INT*2 <= SIZEOF_LONG_LONG
|
2
|
+
# define BDIGIT unsigned int
|
3
|
+
# define SIZEOF_BDIGIT SIZEOF_INT
|
4
|
+
#elif SIZEOF_INT*2 <= SIZEOF_LONG
|
5
|
+
# define BDIGIT unsigned int
|
6
|
+
# define SIZEOF_BDIGIT SIZEOF_INT
|
7
|
+
#elif SIZEOF_SHORT*2 <= SIZEOF_LONG
|
8
|
+
# define BDIGIT unsigned short
|
9
|
+
# define SIZEOF_BDIGIT SIZEOF_SHORT
|
10
|
+
#else
|
11
|
+
# define BDIGIT unsigned short
|
12
|
+
# define SIZEOF_BDIGIT (SIZEOF_LONG/2)
|
13
|
+
# define SIZEOF_ACTUAL_BDIGIT SIZEOF_LONG
|
14
|
+
#endif
|
15
|
+
#ifndef SIZEOF_ACTUAL_BDIGIT
|
16
|
+
# define SIZEOF_ACTUAL_BDIGIT SIZEOF_BDIGIT
|
17
|
+
#endif
|
18
|
+
|
19
|
+
#define RBIGNUM(obj) ((struct RBignum *)(obj))
|
20
|
+
#define BIGNUM_SIGN_BIT FL_USER1
|
21
|
+
#define BIGNUM_EMBED_FLAG ((VALUE)FL_USER2)
|
22
|
+
#define BIGNUM_EMBED_LEN_NUMBITS 3
|
23
|
+
#define BIGNUM_EMBED_LEN_MASK \
|
24
|
+
(~(~(VALUE)0U << BIGNUM_EMBED_LEN_NUMBITS) << BIGNUM_EMBED_LEN_SHIFT)
|
25
|
+
#define BIGNUM_EMBED_LEN_SHIFT \
|
26
|
+
(FL_USHIFT+3) /* bit offset of BIGNUM_EMBED_LEN_MASK */
|
27
|
+
#ifndef BIGNUM_EMBED_LEN_MAX
|
28
|
+
# if (SIZEOF_VALUE*RVALUE_EMBED_LEN_MAX/SIZEOF_ACTUAL_BDIGIT) < (1 << BIGNUM_EMBED_LEN_NUMBITS)-1
|
29
|
+
# define BIGNUM_EMBED_LEN_MAX (SIZEOF_VALUE*RVALUE_EMBED_LEN_MAX/SIZEOF_ACTUAL_BDIGIT)
|
30
|
+
# else
|
31
|
+
# define BIGNUM_EMBED_LEN_MAX ((1 << BIGNUM_EMBED_LEN_NUMBITS)-1)
|
32
|
+
# endif
|
33
|
+
#endif
|
34
|
+
|
35
|
+
struct RBignum {
|
36
|
+
struct RBasic basic;
|
37
|
+
union {
|
38
|
+
struct {
|
39
|
+
size_t len;
|
40
|
+
BDIGIT *digits;
|
41
|
+
} heap;
|
42
|
+
BDIGIT ary[BIGNUM_EMBED_LEN_MAX];
|
43
|
+
} as;
|
44
|
+
};
|
45
|
+
|
46
|
+
static inline size_t BIGNUM_LEN(VALUE b);
|
47
|
+
static inline BDIGIT *BIGNUM_DIGITS(VALUE b);
|
48
|
+
static inline bool BIGNUM_EMBED_P(VALUE b);
|
49
|
+
|
50
|
+
static inline size_t
|
51
|
+
BIGNUM_LEN(VALUE b)
|
52
|
+
{
|
53
|
+
if (! BIGNUM_EMBED_P(b)) {
|
54
|
+
return RBIGNUM(b)->as.heap.len;
|
55
|
+
}
|
56
|
+
else {
|
57
|
+
size_t ret = RBASIC(b)->flags;
|
58
|
+
ret &= BIGNUM_EMBED_LEN_MASK;
|
59
|
+
ret >>= BIGNUM_EMBED_LEN_SHIFT;
|
60
|
+
return ret;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
/* LSB:BIGNUM_DIGITS(b)[0], MSB:BIGNUM_DIGITS(b)[BIGNUM_LEN(b)-1] */
|
65
|
+
static inline BDIGIT *
|
66
|
+
BIGNUM_DIGITS(VALUE b)
|
67
|
+
{
|
68
|
+
if (BIGNUM_EMBED_P(b)) {
|
69
|
+
return RBIGNUM(b)->as.ary;
|
70
|
+
}
|
71
|
+
else {
|
72
|
+
return RBIGNUM(b)->as.heap.digits;
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
static inline bool
|
77
|
+
BIGNUM_EMBED_P(VALUE b)
|
78
|
+
{
|
79
|
+
return FL_TEST_RAW(b, BIGNUM_EMBED_FLAG);
|
80
|
+
}
|
81
|
+
|
82
|
+
#define RBIGNUM_DIGITS BIGNUM_DIGITS
|
83
|
+
#define RBIGNUM_LEN BIGNUM_LEN
|
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.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Dowad
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-20 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,16 +92,16 @@ 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
|
96
|
+
- ext/bit_twiddle/ruby23/bt_bignum.h
|
97
|
+
- ext/bit_twiddle/ruby30/bt_bignum.h
|
97
98
|
- lib/bit-twiddle.rb
|
98
99
|
- lib/bit-twiddle/core_ext.rb
|
99
|
-
- lib/bit_twiddle.so
|
100
100
|
homepage: http://github.com/alexdowad/bit-twiddle
|
101
101
|
licenses:
|
102
102
|
- None (Public Domain)
|
103
103
|
metadata: {}
|
104
|
-
post_install_message:
|
104
|
+
post_install_message:
|
105
105
|
rdoc_options: []
|
106
106
|
require_paths:
|
107
107
|
- lib
|
@@ -109,17 +109,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
109
|
requirements:
|
110
110
|
- - ">="
|
111
111
|
- !ruby/object:Gem::Version
|
112
|
-
version:
|
112
|
+
version: 2.2.8
|
113
113
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
115
|
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0'
|
118
118
|
requirements: []
|
119
|
-
|
120
|
-
|
121
|
-
signing_key:
|
119
|
+
rubygems_version: 3.2.15
|
120
|
+
signing_key:
|
122
121
|
specification_version: 4
|
123
122
|
summary: Fast bitwise operations for Ruby
|
124
123
|
test_files: []
|
125
|
-
has_rdoc:
|
@@ -1 +0,0 @@
|
|
1
|
-
/* nothing special needed for MRI Ruby 1.9.3 - 2.1 */
|
data/lib/bit_twiddle.so
DELETED
Binary file
|