From 0bbaa95c10324c4ea95ab7c145d26767b0634cdc Mon Sep 17 00:00:00 2001
From: naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
Date: Tue, 8 Mar 2016 09:15:18 +0000
Subject: * intern.h (rb_divmod): assume compilers `/` and `%` comply C99   and
 reduce branching. If a compiler doesn't comply, add #ifdefs.

* intern.h (rb_div): added for Ruby's behavior.

* intern.h (rb_mod): added for Ruby's behavior.

* insns.def (opt_div): use rb_div.

* insns.def (opt_mod): use rb_mod.

* numeric.c (fixdivmod): removed.

* numeric.c (fix_divide): use rb_div.

* numeric.c (fix_mod): use rb_mod.

* numeric.c (fix_divmod): use rb_divmod.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
---
 insns.def | 66 ++++++---------------------------------------------------------
 1 file changed, 6 insertions(+), 60 deletions(-)

(limited to 'insns.def')

diff --git a/insns.def b/insns.def
index 20140b30c1..98871bf9a6 100644
--- a/insns.def
+++ b/insns.def
@@ -1473,34 +1473,9 @@ opt_div
 {
     if (FIXNUM_2_P(recv, obj) &&
 	BASIC_OP_UNREDEFINED_P(BOP_DIV, FIXNUM_REDEFINED_OP_FLAG)) {
-	long x, y, div;
-
-	x = FIX2LONG(recv);
-	y = FIX2LONG(obj);
-	{
-	    /* copied from numeric.c#fixdivmod */
-	    long mod;
-	    if (y == 0)
-		goto INSN_LABEL(normal_dispatch);
-	    if (y < 0) {
-		if (x < 0)
-		    div = -x / -y;
-		else
-		    div = -(x / -y);
-	    }
-	    else {
-		if (x < 0)
-		    div = -(-x / y);
-		else
-		    div = x / y;
-	    }
-	    mod = x - div * y;
-	    if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) {
-		mod += y;
-		div -= 1;
-	    }
-	}
-	val = LONG2NUM(div);
+	long y = FIX2LONG(obj);
+	if (y == 0) goto INSN_LABEL(normal_dispatch);
+	val = LONG2NUM(rb_div(FIX2LONG(recv), y));
     }
     else if (FLONUM_2_P(recv, obj) &&
 	     BASIC_OP_UNREDEFINED_P(BOP_DIV, FLOAT_REDEFINED_OP_FLAG)) {
@@ -1536,38 +1511,9 @@ opt_mod
 {
     if (FIXNUM_2_P(recv, obj) &&
 	BASIC_OP_UNREDEFINED_P(BOP_MOD, FIXNUM_REDEFINED_OP_FLAG )) {
-	long x, y;
-
-	x = FIX2LONG(recv);
-	y = FIX2LONG(obj);
-	if (x > 0 && y > 0) {
-	    val = LONG2FIX(x % y);
-	}
-	else {
-	    /* copied from numeric.c#fixdivmod */
-	    long div, mod;
-
-	    if (y == 0)
-		goto INSN_LABEL(normal_dispatch);
-	    if (y < 0) {
-		if (x < 0)
-		    div = -x / -y;
-		else
-		    div = -(x / -y);
-	    }
-	    else {
-		if (x < 0)
-		    div = -(-x / y);
-		else
-		    div = x / y;
-	    }
-	    mod = x - div * y;
-	    if ((mod < 0 && y > 0) || (mod > 0 && y < 0)) {
-		mod += y;
-		div -= 1;
-	    }
-	    val = LONG2FIX(mod);
-	}
+	long y = FIX2LONG(obj);
+	if (y == 0) goto INSN_LABEL(normal_dispatch);
+	val = LONG2FIX(rb_mod(FIX2LONG(recv), y));
     }
     else if (FLONUM_2_P(recv, obj) &&
 	     BASIC_OP_UNREDEFINED_P(BOP_MOD, FLOAT_REDEFINED_OP_FLAG)) {
-- 
cgit v1.2.3

