accept floats as bit operator operands; fix #3260

parent 81122950
...@@ -809,17 +809,13 @@ fix_rev(mrb_state *mrb, mrb_value num) ...@@ -809,17 +809,13 @@ fix_rev(mrb_state *mrb, mrb_value num)
return mrb_fixnum_value(~val); return mrb_fixnum_value(~val);
} }
static mrb_value static mrb_value flo_and(mrb_state *mrb, mrb_value x);
bit_coerce(mrb_state *mrb, mrb_value x) static mrb_value flo_or(mrb_state *mrb, mrb_value x);
{ static mrb_value flo_xor(mrb_state *mrb, mrb_value x);
while (!mrb_fixnum_p(x)) { #define bit_op(x,y,op1,op2) do {\
if (mrb_float_p(x)) { if (mrb_fixnum_p(y)) return mrb_fixnum_value(mrb_fixnum(x) op2 mrb_fixnum(y));\
mrb_raise(mrb, E_TYPE_ERROR, "can't convert Float into Integer"); return flo_ ## op1(mrb, mrb_float_value(mrb, mrb_fixnum(x)));\
} } while(0)
x = mrb_to_int(mrb, x);
}
return x;
}
/* 15.2.8.3.9 */ /* 15.2.8.3.9 */
/* /*
...@@ -835,9 +831,7 @@ fix_and(mrb_state *mrb, mrb_value x) ...@@ -835,9 +831,7 @@ fix_and(mrb_state *mrb, mrb_value x)
mrb_value y; mrb_value y;
mrb_get_args(mrb, "o", &y); mrb_get_args(mrb, "o", &y);
bit_op(x, y, and, &);
y = bit_coerce(mrb, y);
return mrb_fixnum_value(mrb_fixnum(x) & mrb_fixnum(y));
} }
/* 15.2.8.3.10 */ /* 15.2.8.3.10 */
...@@ -854,9 +848,7 @@ fix_or(mrb_state *mrb, mrb_value x) ...@@ -854,9 +848,7 @@ fix_or(mrb_state *mrb, mrb_value x)
mrb_value y; mrb_value y;
mrb_get_args(mrb, "o", &y); mrb_get_args(mrb, "o", &y);
bit_op(x, y, or, |);
y = bit_coerce(mrb, y);
return mrb_fixnum_value(mrb_fixnum(x) | mrb_fixnum(y));
} }
/* 15.2.8.3.11 */ /* 15.2.8.3.11 */
...@@ -873,9 +865,7 @@ fix_xor(mrb_state *mrb, mrb_value x) ...@@ -873,9 +865,7 @@ fix_xor(mrb_state *mrb, mrb_value x)
mrb_value y; mrb_value y;
mrb_get_args(mrb, "o", &y); mrb_get_args(mrb, "o", &y);
bit_op(x, y, or, ^);
y = bit_coerce(mrb, y);
return mrb_fixnum_value(mrb_fixnum(x) ^ mrb_fixnum(y));
} }
#define NUMERIC_SHIFT_WIDTH_MAX (MRB_INT_BIT-1) #define NUMERIC_SHIFT_WIDTH_MAX (MRB_INT_BIT-1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment