[前][次][番号順一覧][スレッド一覧]

ruby-changes:42222

From: nobu <ko1@a...>
Date: Sat, 26 Mar 2016 10:54:20 +0900 (JST)
Subject: [ruby-changes:42222] nobu:r54296 (trunk): numeric.c: basic arithmetic

nobu	2016-03-26 10:54:16 +0900 (Sat, 26 Mar 2016)

  New Revision: 54296

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54296

  Log:
    numeric.c: basic arithmetic
    
    * numeric.c (rb_int_{uminus,plus,minus,mul,idiv,modulo}): basic
      arithmetic functions for generic Integers.

  Modified files:
    trunk/ChangeLog
    trunk/internal.h
    trunk/numeric.c
Index: numeric.c
===================================================================
--- numeric.c	(revision 54295)
+++ numeric.c	(revision 54296)
@@ -2926,6 +2926,18 @@ fix_uminus(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2926
 }
 
 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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3050
     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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3096
     }
 }
 
+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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3143
     }
 }
 
+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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3245
     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) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3286
     }
 }
 
+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
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54295)
+++ ChangeLog	(revision 54296)
@@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
-Sat Mar 26 10:51:58 2016  Nobuyoshi Nakada  <nobu@r...>
+Sat Mar 26 10:54:14 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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.
Index: internal.h
===================================================================
--- internal.h	(revision 54295)
+++ internal.h	(revision 54296)
@@ -1007,6 +1007,12 @@ double ruby_float_mod(double x, double y https://github.com/ruby/ruby/blob/trunk/internal.h#L1007
 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);
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]