bit-twiddle 0.0.7 → 0.1.2
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/ext/bit_twiddle/extconf.rb +5 -1
- data/ext/bit_twiddle/ruby30/bt_bignum.h +83 -0
- data/ext/bit_twiddle/ruby31/bt_bignum.h +83 -0
- metadata +5 -5
- data/lib/bit_twiddle.so +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fb5bc1ef21de0ce254366175d3e55593136fcab5cc5401ff11c1bae85c4c3241
|
4
|
+
data.tar.gz: 5f61aa034bb23c84037a18f36ddf2755282407b3820c9db1e6b0953eb7e9ed76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30b2b074082ed3e3a79f6cbd0df048e1317bd9b17b8fac0875068b3fb2e04c2b143ef3df93f8cf0e780785bf73705f478a9a82d7d5ef88ac30fce99cef10713a
|
7
|
+
data.tar.gz: ef489d27df2ea645ec997e37561414f37d19d9a04a0bc0c6a04cc87d770c413ea53a5c309732a46a38d7b257a50ee662535ffd71133e859b08e6cce4fe4d9cdc
|
data/LICENSE
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
The program code in the 'bit-twiddle' repository (currently available at https://github.com/alexdowad/bit-twiddle) has been placed in the public domain by its author. You can do anything you want with it, anything at all. You can copy it, sell it, buy it, lease it, rent it out, put it in pawn, or use it as a down payment on a new truck. You can sing it out in the shower, or on a crowded bus. You can even inscribe it on a silk flag, and plant it on Kilimanjaro's snow-covered slopes. The possibilities are endless!
|
2
2
|
|
3
|
-
If
|
3
|
+
If 'public domain' isn't what you like, then you can use this code under a CC0 (creative commons) license. Still no good? Drop the author a note and we'll see how to fix you up.
|
data/ext/bit_twiddle/extconf.rb
CHANGED
@@ -20,8 +20,12 @@ if RUBY_ENGINE == 'rbx'
|
|
20
20
|
raise "bit-twiddle does not support Rubinius. Sorry!"
|
21
21
|
elsif RUBY_VERSION < '2.3.0'
|
22
22
|
$CFLAGS << " -I#{File.join(dir, 'ruby22')} "
|
23
|
-
|
23
|
+
elsif RUBY_VERSION < '3.0.0'
|
24
24
|
$CFLAGS << " -I#{File.join(dir, 'ruby23')} "
|
25
|
+
elsif RUBY_VERSION < '3.1.0'
|
26
|
+
$CFLAGS << " -I#{File.join(dir, 'ruby30')} "
|
27
|
+
else
|
28
|
+
$CFLAGS << " -I#{File.join(dir, 'ruby31')} "
|
25
29
|
end
|
26
30
|
|
27
31
|
check_sizeof 'short'
|
@@ -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
|
@@ -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*RBIMPL_RVALUE_EMBED_LEN_MAX/SIZEOF_ACTUAL_BDIGIT) < (1 << BIGNUM_EMBED_LEN_NUMBITS)-1
|
29
|
+
# define BIGNUM_EMBED_LEN_MAX (SIZEOF_VALUE*RBIMPL_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.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:
|
11
|
+
date: 2022-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -94,9 +94,10 @@ files:
|
|
94
94
|
- ext/bit_twiddle/extconf.rb
|
95
95
|
- ext/bit_twiddle/ruby22/bt_bignum.h
|
96
96
|
- ext/bit_twiddle/ruby23/bt_bignum.h
|
97
|
+
- ext/bit_twiddle/ruby30/bt_bignum.h
|
98
|
+
- ext/bit_twiddle/ruby31/bt_bignum.h
|
97
99
|
- lib/bit-twiddle.rb
|
98
100
|
- lib/bit-twiddle/core_ext.rb
|
99
|
-
- lib/bit_twiddle.so
|
100
101
|
homepage: http://github.com/alexdowad/bit-twiddle
|
101
102
|
licenses:
|
102
103
|
- None (Public Domain)
|
@@ -116,8 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
117
|
- !ruby/object:Gem::Version
|
117
118
|
version: '0'
|
118
119
|
requirements: []
|
119
|
-
|
120
|
-
rubygems_version: 2.6.13
|
120
|
+
rubygems_version: 3.1.2
|
121
121
|
signing_key:
|
122
122
|
specification_version: 4
|
123
123
|
summary: Fast bitwise operations for Ruby
|
data/lib/bit_twiddle.so
DELETED
Binary file
|