@@ -809,17 +809,13 @@ fix_rev(mrb_state *mrb, mrb_value num)
809
809
return mrb_fixnum_value (~val );
810
810
}
811
811
812
- static mrb_value
813
- bit_coerce (mrb_state * mrb , mrb_value x )
814
- {
815
- while (!mrb_fixnum_p (x )) {
816
- if (mrb_float_p (x )) {
817
- mrb_raise (mrb , E_TYPE_ERROR , "can't convert Float into Integer" );
818
- }
819
- x = mrb_to_int (mrb , x );
820
- }
821
- return x ;
822
- }
812
+ static mrb_value flo_and (mrb_state * mrb , mrb_value x );
813
+ static mrb_value flo_or (mrb_state * mrb , mrb_value x );
814
+ static mrb_value flo_xor (mrb_state * mrb , mrb_value x );
815
+ #define bit_op (x ,y ,op1 ,op2 ) do {\
816
+ if (mrb_fixnum_p(y)) return mrb_fixnum_value(mrb_fixnum(x) op2 mrb_fixnum(y));\
817
+ return flo_ ## op1(mrb, mrb_float_value(mrb, mrb_fixnum(x)));\
818
+ } while(0)
823
819
824
820
/* 15.2.8.3.9 */
825
821
/*
@@ -835,9 +831,7 @@ fix_and(mrb_state *mrb, mrb_value x)
835
831
mrb_value y ;
836
832
837
833
mrb_get_args (mrb , "o" , & y );
838
-
839
- y = bit_coerce (mrb , y );
840
- return mrb_fixnum_value (mrb_fixnum (x ) & mrb_fixnum (y ));
834
+ bit_op (x , y , and , & );
841
835
}
842
836
843
837
/* 15.2.8.3.10 */
@@ -854,9 +848,7 @@ fix_or(mrb_state *mrb, mrb_value x)
854
848
mrb_value y ;
855
849
856
850
mrb_get_args (mrb , "o" , & y );
857
-
858
- y = bit_coerce (mrb , y );
859
- return mrb_fixnum_value (mrb_fixnum (x ) | mrb_fixnum (y ));
851
+ bit_op (x , y , or , |);
860
852
}
861
853
862
854
/* 15.2.8.3.11 */
@@ -873,9 +865,7 @@ fix_xor(mrb_state *mrb, mrb_value x)
873
865
mrb_value y ;
874
866
875
867
mrb_get_args (mrb , "o" , & y );
876
-
877
- y = bit_coerce (mrb , y );
878
- return mrb_fixnum_value (mrb_fixnum (x ) ^ mrb_fixnum (y ));
868
+ bit_op (x , y , or , ^);
879
869
}
880
870
881
871
#define NUMERIC_SHIFT_WIDTH_MAX (MRB_INT_BIT-1)
0 commit comments