diff options
author | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-12-22 15:06:22 +0000 |
---|---|---|
committer | mrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-12-22 15:06:22 +0000 |
commit | a756488c6d3a9a0d0fab76cd4c67e655f00c399b (patch) | |
tree | 965ecb45325ddd880378f20c06d0c52e335c9e80 /bignum.c | |
parent | 0f16820fc0484697c0535961162d5f53b9ee0a42 (diff) | |
download | ruby-a756488c6d3a9a0d0fab76cd4c67e655f00c399b.tar.gz |
* include/ruby/intern.h: add the prototype declaration of
rb_num_coerce_bit.
* numeric.c (rb_num_coerce_bit): the new coerce function for bitwise
binary operation.
* bignum.c (rb_big_and): use coerce to convert the argument, which isn't
a Fixnum nor a Bignum, to the corresponding Integer object so that
bitwise operations can support Integer-mimic objects.
[Bug #1792] [ruby-core:39491]
* bignum.c (rb_big_or): ditto.
* bignum.c (rb_big_xor): ditto.
* numeric.c (bit_coerce): ditto.
* numeric.c (fix_and): ditto.
* numeric.c (fix_or): ditto.
* numeric.c (fix_xor): ditto.
* test/ruby/test_integer.rb: add tests for the above changes.
* test/ruby/test_bignum.rb: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38560 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'bignum.c')
-rw-r--r-- | bignum.c | 31 |
1 files changed, 16 insertions, 15 deletions
@@ -3201,18 +3201,6 @@ rb_big_pow(VALUE x, VALUE y) return DBL2NUM(pow(rb_big2dbl(x), d)); } -static inline VALUE -bit_coerce(VALUE x) -{ - while (!FIXNUM_P(x) && !RB_TYPE_P(x, T_BIGNUM)) { - rb_raise(rb_eTypeError, - "can't convert %s into Integer for bitwise arithmetic", - rb_obj_classname(x)); - x = rb_to_int(x); - } - return x; -} - static VALUE bigand_int(VALUE x, long y) { @@ -3272,8 +3260,13 @@ rb_big_and(VALUE xx, VALUE yy) long i, l1, l2; char sign; + if (!FIXNUM_P(yy) && !RB_TYPE_P(yy, T_BIGNUM)) { + return rb_num_coerce_bit(xx, yy, '&'); + } + x = xx; - y = bit_coerce(yy); + y = yy; + if (!RBIGNUM_SIGN(x)) { x = rb_big_clone(x); get2comp(x); @@ -3363,8 +3356,12 @@ rb_big_or(VALUE xx, VALUE yy) long i, l1, l2; char sign; + if (!FIXNUM_P(yy) && !RB_TYPE_P(yy, T_BIGNUM)) { + return rb_num_coerce_bit(xx, yy, '|'); + } + x = xx; - y = bit_coerce(yy); + y = yy; if (!RBIGNUM_SIGN(x)) { x = rb_big_clone(x); @@ -3455,8 +3452,12 @@ rb_big_xor(VALUE xx, VALUE yy) long i, l1, l2; char sign; + if (!FIXNUM_P(yy) && !RB_TYPE_P(yy, T_BIGNUM)) { + return rb_num_coerce_bit(xx, yy, '^'); + } + x = xx; - y = bit_coerce(yy); + y = yy; if (!RBIGNUM_SIGN(x)) { x = rb_big_clone(x); |