From 51c4ffa45b03262ddc183b815dba07462ef0a29f Mon Sep 17 00:00:00 2001 From: naruse Date: Mon, 21 Mar 2016 13:36:03 +0000 Subject: * internal.h (rb_fix_divmod_fix): like r54213, use FIX2NUM only if x == FIXNUM_MIN && y == -1. This must be a rare case and it is expected compiler to handle well. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54216 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- internal.h | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'internal.h') diff --git a/internal.h b/internal.h index 48acd5bb0c..38175d776e 100644 --- a/internal.h +++ b/internal.h @@ -298,39 +298,46 @@ rb_fix_mul_fix(VALUE x, VALUE y) * Note that div may overflow fixnum. */ static inline void -rb_divmod(long x, long y, long *divp, long *modp) { +rb_fix_divmod_fix(VALUE a, VALUE b, VALUE *divp, VALUE *modp) { /* assume / and % comply C99. * ldiv(3) won't be inlined by GCC and clang. * I expect / and % are compiled as single idiv. */ - long div = x / y; - long mod = x % y; + long x = FIX2LONG(a); + long y = FIX2LONG(b); + long div, mod; + if (x == FIXNUM_MIN && y == -1) { + if (divp) *divp = LONG2NUM(-FIXNUM_MIN); + if (modp) *modp = LONG2FIX(0); + return; + } + div = x / y; + mod = x % y; if (y > 0 ? mod < 0 : mod > 0) { mod += y; div -= 1; } - if (divp) *divp = div; - if (modp) *modp = mod; + if (divp) *divp = LONG2FIX(div); + if (modp) *modp = LONG2FIX(mod); } /* div() for Ruby * This behaves different from C99 for negative arguments. - * Note that div may overflow fixnum */ -static inline long -rb_div(long x, long y) { - long div; - rb_divmod(x, y, &div, NULL); +static inline VALUE +rb_fix_div_fix(VALUE x, VALUE y) { + VALUE div; + rb_fix_divmod_fix(x, y, &div, NULL); return div; } /* mod() for Ruby * This behaves different from C99 for negative arguments. */ -static inline long -rb_mod(long x, long y) { - long mod; - rb_divmod(x, y, NULL, &mod); +static inline VALUE +rb_fix_mod_fix(VALUE x, VALUE y) { + VALUE mod; + rb_fix_divmod_fix(x, y, NULL, &mod); return mod; } -- cgit v1.2.3