overflow 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +79 -35
- data/Rakefile +1 -1
- data/example/murmurhash.rb +44 -0
- data/ext/overflow/extconf.rb +2 -0
- data/ext/overflow/overflow.c +455 -0
- data/lib/overflow.rb +0 -1
- data/overflow.gemspec +2 -2
- data/spec/numeric_spec.rb +167 -0
- data/spec/overflow_spec.rb +150 -101
- data/spec/spec_helper.rb +2 -0
- metadata +20 -4
- data/ext/overflow/init.c +0 -323
- data/lib/overflow/version.rb +0 -3
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: overflow
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 0.8.3
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: limits
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: Overflow is a class to overflow calculated as C language in Ruby.
|
70
84
|
email: co000ri@gmail.com
|
71
85
|
executables: []
|
@@ -79,12 +93,13 @@ files:
|
|
79
93
|
- LICENSE.txt
|
80
94
|
- README.md
|
81
95
|
- Rakefile
|
96
|
+
- example/murmurhash.rb
|
82
97
|
- ext/overflow/extconf.rb
|
83
|
-
- ext/overflow/
|
98
|
+
- ext/overflow/overflow.c
|
84
99
|
- lib/overflow.rb
|
85
|
-
- lib/overflow/version.rb
|
86
100
|
- overflow.gemspec
|
87
101
|
- spec/mem_spec.rb
|
102
|
+
- spec/numeric_spec.rb
|
88
103
|
- spec/overflow_spec.rb
|
89
104
|
- spec/spec_helper.rb
|
90
105
|
homepage: ''
|
@@ -113,6 +128,7 @@ specification_version: 4
|
|
113
128
|
summary: Overflow is a class to overflow calculated as C language in Ruby.
|
114
129
|
test_files:
|
115
130
|
- spec/mem_spec.rb
|
131
|
+
- spec/numeric_spec.rb
|
116
132
|
- spec/overflow_spec.rb
|
117
133
|
- spec/spec_helper.rb
|
118
134
|
has_rdoc:
|
data/ext/overflow/init.c
DELETED
@@ -1,323 +0,0 @@
|
|
1
|
-
#include "ruby.h"
|
2
|
-
|
3
|
-
typedef enum {
|
4
|
-
i8,
|
5
|
-
ui8,
|
6
|
-
i16,
|
7
|
-
ui16,
|
8
|
-
i32,
|
9
|
-
ui32,
|
10
|
-
i64,
|
11
|
-
ui64
|
12
|
-
} types;
|
13
|
-
|
14
|
-
typedef uint64_t value_t;
|
15
|
-
|
16
|
-
typedef struct {
|
17
|
-
value_t value;
|
18
|
-
types type;
|
19
|
-
} overflow_t;
|
20
|
-
|
21
|
-
types char2type (char c)
|
22
|
-
{
|
23
|
-
switch (c) {
|
24
|
-
case 'c': return i8;
|
25
|
-
case 'C': return ui8;
|
26
|
-
case 's': return i16;
|
27
|
-
case 'S': return ui16;
|
28
|
-
case 'i': return i32;
|
29
|
-
case 'I': return ui32;
|
30
|
-
case 'l': return i32;
|
31
|
-
case 'L': return ui32;
|
32
|
-
case 'q': return i64;
|
33
|
-
case 'Q': return ui64;
|
34
|
-
default:
|
35
|
-
rb_raise(rb_eArgError, "type %c is not support", c);
|
36
|
-
}
|
37
|
-
}
|
38
|
-
|
39
|
-
static void
|
40
|
-
overflow_free(overflow_t* ptr)
|
41
|
-
{
|
42
|
-
xfree(ptr);
|
43
|
-
}
|
44
|
-
|
45
|
-
static VALUE
|
46
|
-
overflow_alloc(VALUE self)
|
47
|
-
{
|
48
|
-
overflow_t* ptr = ALLOC(overflow_t);
|
49
|
-
return Data_Wrap_Struct(self, 0, overflow_free, ptr);
|
50
|
-
}
|
51
|
-
|
52
|
-
static VALUE
|
53
|
-
overflow_initialize(VALUE self, VALUE obj)
|
54
|
-
{
|
55
|
-
overflow_t *ptr;
|
56
|
-
char *p;
|
57
|
-
char c;
|
58
|
-
|
59
|
-
if (rb_type(obj) != T_STRING) {
|
60
|
-
rb_raise(rb_eArgError, "set a type char for `pack' template");
|
61
|
-
}
|
62
|
-
|
63
|
-
Data_Get_Struct(self, overflow_t, ptr);
|
64
|
-
p = RSTRING_PTR(obj);
|
65
|
-
|
66
|
-
ptr->type = char2type(*p);
|
67
|
-
return self;
|
68
|
-
}
|
69
|
-
|
70
|
-
static VALUE
|
71
|
-
overflow_set(VALUE self, VALUE obj)
|
72
|
-
{
|
73
|
-
overflow_t *ptr;
|
74
|
-
Data_Get_Struct(self, overflow_t, ptr);
|
75
|
-
VALUE other;
|
76
|
-
|
77
|
-
switch (rb_type(obj)) {
|
78
|
-
case T_FIXNUM:
|
79
|
-
switch (ptr->type) {
|
80
|
-
case i8: ptr->value = (int8_t) NUM2LL(obj); break;
|
81
|
-
case ui8: ptr->value = (uint8_t) NUM2LL(obj); break;
|
82
|
-
case i16: ptr->value = (int16_t) NUM2LL(obj); break;
|
83
|
-
case ui16: ptr->value = (uint16_t) NUM2LL(obj); break;
|
84
|
-
case i32: ptr->value = (int32_t) NUM2LL(obj); break;
|
85
|
-
case ui32: ptr->value = (uint32_t) NUM2LL(obj); break;
|
86
|
-
case i64: ptr->value = (int64_t) NUM2LL(obj); break;
|
87
|
-
case ui64: ptr->value = (uint64_t) NUM2LL(obj); break;
|
88
|
-
}
|
89
|
-
break;
|
90
|
-
case T_BIGNUM:
|
91
|
-
if (RBIGNUM_POSITIVE_P(obj)) {
|
92
|
-
other = rb_funcall(obj, rb_intern("&"), 1, ULL2NUM(0xffffffffffffffffLL));
|
93
|
-
ptr->value = (uint64_t) NUM2ULL(other);
|
94
|
-
} else {
|
95
|
-
ptr->value = (int64_t) NUM2LL(obj);
|
96
|
-
}
|
97
|
-
break;
|
98
|
-
}
|
99
|
-
return self;
|
100
|
-
}
|
101
|
-
|
102
|
-
static VALUE
|
103
|
-
overflow_to_i(VALUE self)
|
104
|
-
{
|
105
|
-
overflow_t *ptr;
|
106
|
-
Data_Get_Struct(self, overflow_t, ptr);
|
107
|
-
|
108
|
-
switch (ptr->type) {
|
109
|
-
case i8:
|
110
|
-
return INT2NUM((int8_t)ptr->value);
|
111
|
-
case ui8:
|
112
|
-
return UINT2NUM((uint8_t)ptr->value);
|
113
|
-
case i16:
|
114
|
-
return INT2NUM((int16_t)ptr->value);
|
115
|
-
case ui16:
|
116
|
-
return UINT2NUM((uint16_t)ptr->value);
|
117
|
-
case i32:
|
118
|
-
return LONG2NUM((int32_t)ptr->value);
|
119
|
-
case ui32:
|
120
|
-
return ULONG2NUM((uint32_t)ptr->value);
|
121
|
-
case i64:
|
122
|
-
return LL2NUM((int64_t)ptr->value);
|
123
|
-
case ui64:
|
124
|
-
return ULL2NUM((uint64_t)ptr->value);
|
125
|
-
}
|
126
|
-
}
|
127
|
-
|
128
|
-
#define TYPE_PLUS(type, value, other) ((type)((type)(value) + (type)(other)))
|
129
|
-
|
130
|
-
static uint64_t
|
131
|
-
plus(types type, uint64_t a, uint64_t b)
|
132
|
-
{
|
133
|
-
switch (type) {
|
134
|
-
case i8: return TYPE_PLUS(int8_t, a, b);
|
135
|
-
case ui8: return TYPE_PLUS(uint8_t, a, b);
|
136
|
-
case i16: return TYPE_PLUS(int16_t, a, b);
|
137
|
-
case ui16: return TYPE_PLUS(uint16_t, a, b);
|
138
|
-
case i32: return TYPE_PLUS(int32_t, a, b);
|
139
|
-
case ui32: return TYPE_PLUS(uint32_t, a, b);
|
140
|
-
case i64: return TYPE_PLUS(int64_t, a, b);
|
141
|
-
case ui64: return TYPE_PLUS(uint64_t, a, b);
|
142
|
-
}
|
143
|
-
}
|
144
|
-
|
145
|
-
static VALUE
|
146
|
-
overflow_plus(VALUE self, VALUE num)
|
147
|
-
{
|
148
|
-
overflow_t *ptr;
|
149
|
-
Data_Get_Struct(self, overflow_t, ptr);
|
150
|
-
|
151
|
-
uint64_t a;
|
152
|
-
uint64_t b;
|
153
|
-
|
154
|
-
if (RB_TYPE_P(num, T_BIGNUM)) {
|
155
|
-
num = rb_funcall(num, rb_intern("&"), 1, ULL2NUM(0xffffffffffffffffLL));
|
156
|
-
}
|
157
|
-
a = ptr->value;
|
158
|
-
b = NUM2ULL(num);
|
159
|
-
|
160
|
-
ptr->value = plus(ptr->type, a, b);
|
161
|
-
return self;
|
162
|
-
}
|
163
|
-
|
164
|
-
#define TYPE_MINUS(type, value, other) ((type)((type)(value) - (type)(other)))
|
165
|
-
|
166
|
-
static uint64_t
|
167
|
-
minus(types type, uint64_t a, uint64_t b)
|
168
|
-
{
|
169
|
-
switch (type) {
|
170
|
-
case i8: return TYPE_MINUS(int8_t, a, b);
|
171
|
-
case ui8: return TYPE_MINUS(uint8_t, a, b);
|
172
|
-
case i16: return TYPE_MINUS(int16_t, a, b);
|
173
|
-
case ui16: return TYPE_MINUS(uint16_t, a, b);
|
174
|
-
case i32: return TYPE_MINUS(int32_t, a, b);
|
175
|
-
case ui32: return TYPE_MINUS(uint32_t, a, b);
|
176
|
-
case i64: return TYPE_MINUS(int64_t, a, b);
|
177
|
-
case ui64: return TYPE_MINUS(uint64_t, a, b);
|
178
|
-
}
|
179
|
-
}
|
180
|
-
|
181
|
-
static VALUE
|
182
|
-
overflow_minus(VALUE self, VALUE num)
|
183
|
-
{
|
184
|
-
overflow_t *ptr;
|
185
|
-
Data_Get_Struct(self, overflow_t, ptr);
|
186
|
-
|
187
|
-
uint64_t a;
|
188
|
-
uint64_t b;
|
189
|
-
|
190
|
-
if (RB_TYPE_P(num, T_BIGNUM)) {
|
191
|
-
num = rb_funcall(num, rb_intern("&"), 1, ULL2NUM(0xffffffffffffffffLL));
|
192
|
-
}
|
193
|
-
a = ptr->value;
|
194
|
-
b = NUM2ULL(num);
|
195
|
-
|
196
|
-
ptr->value = minus(ptr->type, a, b);
|
197
|
-
return self;
|
198
|
-
}
|
199
|
-
|
200
|
-
#define TYPE_MUL(type, value, other) ((type)((type)(value) * (type)(other)))
|
201
|
-
|
202
|
-
static uint64_t
|
203
|
-
mul(types type, uint64_t a, uint64_t b)
|
204
|
-
{
|
205
|
-
switch (type) {
|
206
|
-
case i8: return TYPE_MUL(int8_t, a, b);
|
207
|
-
case ui8: return TYPE_MUL(uint8_t, a, b);
|
208
|
-
case i16: return TYPE_MUL(int16_t, a, b);
|
209
|
-
case ui16: return TYPE_MUL(uint16_t, a, b);
|
210
|
-
case i32: return TYPE_MUL(int32_t, a, b);
|
211
|
-
case ui32: return TYPE_MUL(uint32_t, a, b);
|
212
|
-
case i64: return TYPE_MUL(int64_t, a, b);
|
213
|
-
case ui64: return TYPE_MUL(uint64_t, a, b);
|
214
|
-
}
|
215
|
-
}
|
216
|
-
|
217
|
-
static VALUE
|
218
|
-
overflow_mul(VALUE self, VALUE num)
|
219
|
-
{
|
220
|
-
overflow_t *ptr;
|
221
|
-
Data_Get_Struct(self, overflow_t, ptr);
|
222
|
-
|
223
|
-
uint64_t a;
|
224
|
-
uint64_t b;
|
225
|
-
|
226
|
-
if (RB_TYPE_P(num, T_BIGNUM)) {
|
227
|
-
num = rb_funcall(num, rb_intern("&"), 1, ULL2NUM(0xffffffffffffffffLL));
|
228
|
-
}
|
229
|
-
|
230
|
-
a = ptr->value;
|
231
|
-
b = NUM2ULL(num);
|
232
|
-
|
233
|
-
ptr->value = mul(ptr->type, a, b);
|
234
|
-
return self;
|
235
|
-
}
|
236
|
-
|
237
|
-
static VALUE
|
238
|
-
lshift(overflow_t *ptr, long width)
|
239
|
-
{
|
240
|
-
switch (ptr->type) {
|
241
|
-
case i8: return INT2NUM((int8_t)(ptr->value << width));
|
242
|
-
case ui8: return UINT2NUM((uint8_t)(ptr->value << width));
|
243
|
-
case i16: return INT2NUM((int16_t)(ptr->value << width));
|
244
|
-
case ui16: return UINT2NUM((uint16_t)(ptr->value << width));
|
245
|
-
case i32: return LONG2NUM((int32_t)(ptr->value << width));
|
246
|
-
case ui32: return ULONG2NUM((uint32_t)(ptr->value << width));
|
247
|
-
case i64: return LONG2NUM((int64_t)(ptr->value << width));
|
248
|
-
case ui64: return ULONG2NUM((uint64_t)(ptr->value << width));
|
249
|
-
}
|
250
|
-
}
|
251
|
-
|
252
|
-
static VALUE
|
253
|
-
rshift(overflow_t *ptr, long width)
|
254
|
-
{
|
255
|
-
switch (ptr->type) {
|
256
|
-
case i8: return INT2NUM((int8_t)(ptr->value >> width));
|
257
|
-
case ui8: return UINT2NUM((uint8_t)(ptr->value >> width));
|
258
|
-
case i16: return INT2NUM((int16_t)(ptr->value >> width));
|
259
|
-
case ui16: return UINT2NUM((uint16_t)(ptr->value >> width));
|
260
|
-
case i32: return LONG2NUM((int32_t)(ptr->value >> width));
|
261
|
-
case ui32: return ULONG2NUM((uint32_t)(ptr->value >> width));
|
262
|
-
case i64: return LONG2NUM((int64_t)(ptr->value >> width));
|
263
|
-
case ui64: return ULONG2NUM((uint64_t)(ptr->value >> width));
|
264
|
-
}
|
265
|
-
}
|
266
|
-
|
267
|
-
static VALUE
|
268
|
-
overflow_lshift(VALUE self, VALUE obj)
|
269
|
-
{
|
270
|
-
long width;
|
271
|
-
overflow_t *ptr;
|
272
|
-
Data_Get_Struct(self, overflow_t, ptr);
|
273
|
-
|
274
|
-
if (!FIXNUM_P(obj))
|
275
|
-
rb_raise(rb_eArgError, "over 64 left shift not support");
|
276
|
-
|
277
|
-
width = FIX2LONG(obj);
|
278
|
-
if (64 <= width)
|
279
|
-
rb_raise(rb_eArgError, "over 64 left shift not support");
|
280
|
-
|
281
|
-
if (width < 0) {
|
282
|
-
return rshift(ptr, -width);
|
283
|
-
} else {
|
284
|
-
return lshift(ptr, width);
|
285
|
-
}
|
286
|
-
}
|
287
|
-
|
288
|
-
static VALUE
|
289
|
-
overflow_rshift(VALUE self, VALUE obj)
|
290
|
-
{
|
291
|
-
long width;
|
292
|
-
overflow_t *ptr;
|
293
|
-
Data_Get_Struct(self, overflow_t, ptr);
|
294
|
-
|
295
|
-
if (!FIXNUM_P(obj))
|
296
|
-
rb_raise(rb_eArgError, "over 64 right shift not support");
|
297
|
-
|
298
|
-
width = FIX2LONG(obj);
|
299
|
-
if (64 <= width)
|
300
|
-
rb_raise(rb_eArgError, "over 64 right shift not support");
|
301
|
-
|
302
|
-
if (width < 0) {
|
303
|
-
return lshift(ptr, -width);
|
304
|
-
} else {
|
305
|
-
return rshift(ptr, width);
|
306
|
-
}
|
307
|
-
}
|
308
|
-
|
309
|
-
void
|
310
|
-
Init_overflow(void)
|
311
|
-
{
|
312
|
-
VALUE cOverflow;
|
313
|
-
cOverflow = rb_define_class("Overflow", rb_cObject);
|
314
|
-
rb_define_alloc_func(cOverflow, overflow_alloc);
|
315
|
-
rb_define_method(cOverflow, "initialize", overflow_initialize, 1);
|
316
|
-
rb_define_method(cOverflow, "set", overflow_set, 1);
|
317
|
-
rb_define_method(cOverflow, "to_i", overflow_to_i, 0);
|
318
|
-
rb_define_method(cOverflow, "+", overflow_plus, 1);
|
319
|
-
rb_define_method(cOverflow, "-", overflow_minus, 1);
|
320
|
-
rb_define_method(cOverflow, "*", overflow_mul, 1);
|
321
|
-
rb_define_method(cOverflow, "<<", overflow_lshift, 1);
|
322
|
-
rb_define_method(cOverflow, ">>", overflow_rshift, 1);
|
323
|
-
}
|
data/lib/overflow/version.rb
DELETED