diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-26 01:54:16 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-03-26 01:54:16 +0000 |
commit | d635da2087996e03140448c3676a34feab538032 (patch) | |
tree | a42a105fa07e5ea1e4dc29ae9c5d754435b2e26d | |
parent | f7cdac1f30117b2e3923c7f654c50677d21e4ed0 (diff) | |
download | ruby-d635da2087996e03140448c3676a34feab538032.tar.gz |
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
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | internal.h | 6 | ||||
-rw-r--r-- | numeric.c | 73 |
3 files changed, 83 insertions, 1 deletions
@@ -1,4 +1,7 @@ -Sat Mar 26 10:51:58 2016 Nobuyoshi Nakada <nobu@ruby-lang.org> +Sat Mar 26 10:54:14 2016 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * 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); @@ -2926,6 +2926,18 @@ fix_uminus(VALUE 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) { char buf[SIZEOF_VALUE*CHAR_BIT + 1], *const e = buf + sizeof buf, *b = e; @@ -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)&&((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 |