From 79b209f19ac47776e7c90b220a05775e48b43c24 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 26 Mar 2016 01:54:16 +0000 Subject: numeric.c: basic arithmetic * numeric.c (rb_int_{uminus,plus,minus,mul,idiv,modulo}): basic arithmetic functions for generic Integers. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54296 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 ++++- internal.h | 6 ++++++ numeric.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index f06378e08e..99c3f5775f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,7 @@ -Sat Mar 26 10:51:58 2016 Nobuyoshi Nakada +Sat Mar 26 10:54:14 2016 Nobuyoshi Nakada + + * numeric.c (rb_int_{uminus,plus,minus,mul,idiv,modulo}): basic + arithmetic functions for generic Integers. * numeric.c (FIXNUM_{POSITIVE,NEGATIVE,ZERO}_P): predict macros only for Fixnum. diff --git a/internal.h b/internal.h index e5a098db5f..3ceca669c9 100644 --- a/internal.h +++ b/internal.h @@ -1007,6 +1007,12 @@ double ruby_float_mod(double x, double y); int rb_num_negative_p(VALUE); VALUE rb_int_succ(VALUE num); VALUE rb_int_pred(VALUE num); +VALUE rb_int_uminus(VALUE num); +VALUE rb_int_plus(VALUE x, VALUE y); +VALUE rb_int_minus(VALUE x, VALUE y); +VALUE rb_int_mul(VALUE x, VALUE y); +VALUE rb_int_idiv(VALUE x, VALUE y); +VALUE rb_int_modulo(VALUE x, VALUE y); VALUE rb_dbl_hash(double d); VALUE rb_fix_plus(VALUE x, VALUE y); diff --git a/numeric.c b/numeric.c index 844c0d3bc1..fe0ae08bbc 100644 --- a/numeric.c +++ b/numeric.c @@ -2925,6 +2925,18 @@ fix_uminus(VALUE num) return LONG2NUM(-FIX2LONG(num)); } +VALUE +rb_int_uminus(VALUE num) +{ + if (FIXNUM_P(num)) { + return fix_uminus(num); + } + else if (RB_TYPE_P(num, T_BIGNUM)) { + return rb_big_uminus(num); + } + return rb_funcall(num, idUMinus, 0, 0); +} + VALUE rb_fix2str(VALUE x, int base) { @@ -3038,6 +3050,18 @@ rb_fix_plus(VALUE x, VALUE y) return fix_plus(x, y); } +VALUE +rb_int_plus(VALUE x, VALUE y) +{ + if (FIXNUM_P(x)) { + return fix_plus(x, y); + } + else if (RB_TYPE_P(x, T_BIGNUM)) { + return rb_big_plus(x, y); + } + return rb_num_coerce_bin(x, y, '+'); +} + /* * call-seq: * fix - numeric -> numeric_result @@ -3072,6 +3096,19 @@ fix_minus(VALUE x, VALUE y) } } +VALUE +rb_int_minus(VALUE x, VALUE y) +{ + if (FIXNUM_P(x)) { + return fix_minus(x, y); + } + else if (RB_TYPE_P(x, T_BIGNUM)) { + return rb_big_minus(x, y); + } + return rb_num_coerce_bin(x, y, '-'); +} + + #define SQRT_LONG_MAX ((SIGNED_VALUE)1<<((SIZEOF_LONG*CHAR_BIT-1)/2)) /*tests if N*N would overflow*/ #define FIT_SQRT_LONG(n) (((n)=-SQRT_LONG_MAX)) @@ -3106,6 +3143,18 @@ fix_mul(VALUE x, VALUE y) } } +VALUE +rb_int_mul(VALUE x, VALUE y) +{ + if (FIXNUM_P(x)) { + return fix_mul(x, y); + } + else if (RB_TYPE_P(x, T_BIGNUM)) { + return rb_big_mul(x, y); + } + return rb_num_coerce_bin(x, y, '*'); +} + /* * call-seq: * fix.fdiv(numeric) -> float @@ -3196,6 +3245,18 @@ fix_idiv(VALUE x, VALUE y) return fix_divide(x, y, id_div); } +VALUE +rb_int_idiv(VALUE x, VALUE y) +{ + if (FIXNUM_P(x)) { + return fix_idiv(x, y); + } + else if (RB_TYPE_P(x, T_BIGNUM)) { + return rb_big_idiv(x, y); + } + return num_div(x, y); +} + /* * call-seq: * fix % other -> real @@ -3225,6 +3286,18 @@ fix_mod(VALUE x, VALUE y) } } +VALUE +rb_int_modulo(VALUE x, VALUE y) +{ + if (FIXNUM_P(x)) { + return fix_mod(x, y); + } + else if (RB_TYPE_P(x, T_BIGNUM)) { + return rb_big_modulo(x, y); + } + return num_modulo(x, y); +} + /* * call-seq: * fix.divmod(numeric) -> array -- cgit v1.2.3