diff options
author | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-08-22 01:50:21 +0000 |
---|---|---|
committer | shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-08-22 01:50:21 +0000 |
commit | 78cc8cc1c4d2642e3a234cc1874e26cb258dec50 (patch) | |
tree | 08dc119c129d6101110d7ee89e99c145cd9ab6fb /numeric.c | |
parent | c204c6b71bad7db3b95484f519f89da26ce7bc02 (diff) |
* bignum.c (rb_big_lshift, rb_big_rshift): separated functions
to get rid of infinite recursion. fixed calculation in edge
cases. [ruby-dev:31244]
* numeric.c (rb_fix_lshift, rb_fix_rshift): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_6@13188 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 43 |
1 files changed, 31 insertions, 12 deletions
@@ -2457,7 +2457,8 @@ fix_xor(x, y) return LONG2NUM(val); } -static VALUE fix_rshift _((VALUE, VALUE)); +static VALUE fix_lshift _((long, unsigned long)); +static VALUE fix_rshift _((long, unsigned long)); /* * call-seq: @@ -2467,18 +2468,28 @@ static VALUE fix_rshift _((VALUE, VALUE)); */ static VALUE -fix_lshift(x, y) +rb_fix_lshift(x, y) VALUE x, y; { long val, width; val = NUM2LONG(x); - width = NUM2LONG(y); + if (!FIXNUM_P(y)) + return rb_big_lshift(rb_int2big(val), y); + width = FIX2LONG(y); if (width < 0) - return fix_rshift(x, LONG2FIX(-width)); + return fix_rshift(val, (unsigned long)-width); + return fix_lshift(val, width); +} + +static VALUE +fix_lshift(val, width) + long val; + unsigned long width; +{ if (width > (sizeof(VALUE)*CHAR_BIT-1) || ((unsigned long)val)>>(sizeof(VALUE)*CHAR_BIT-1-width) > 0) { - return rb_big_lshift(rb_int2big(val), y); + return rb_big_lshift(rb_int2big(val), ULONG2NUM(width)); } val = val << width; return LONG2NUM(val); @@ -2492,16 +2503,24 @@ fix_lshift(x, y) */ static VALUE -fix_rshift(x, y) +rb_fix_rshift(x, y) VALUE x, y; { long i, val; - i = NUM2LONG(y); - if (i < 0) - return fix_lshift(x, LONG2FIX(-i)); - if (i == 0) return x; val = FIX2LONG(x); + if (!FIXNUM_P(y)) + return rb_big_rshift(rb_int2big(val), y); + i = FIX2LONG(y); + if (i == 0) return x; + if (i < 0) + return fix_lshift(val, (unsigned long)-i); + return fix_rshift(val, i); +} + +static VALUE +fix_rshift(long val, unsigned long i) +{ if (i >= sizeof(long)*CHAR_BIT-1) { if (val < 0) return INT2FIX(-1); return INT2FIX(0); @@ -2902,8 +2921,8 @@ Init_Numeric() rb_define_method(rb_cFixnum, "^", fix_xor, 1); rb_define_method(rb_cFixnum, "[]", fix_aref, 1); - rb_define_method(rb_cFixnum, "<<", fix_lshift, 1); - rb_define_method(rb_cFixnum, ">>", fix_rshift, 1); + rb_define_method(rb_cFixnum, "<<", rb_fix_lshift, 1); + rb_define_method(rb_cFixnum, ">>", rb_fix_rshift, 1); rb_define_method(rb_cFixnum, "to_f", fix_to_f, 0); rb_define_method(rb_cFixnum, "size", fix_size, 0); |