* 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
This commit is contained in:
parent
2118eccc77
commit
51c4ffa45b
@ -1,3 +1,9 @@
|
|||||||
|
Mon Mar 21 22:32:50 2016 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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.
|
||||||
|
|
||||||
Mon Mar 21 22:15:11 2016 NARUSE, Yui <naruse@ruby-lang.org>
|
Mon Mar 21 22:15:11 2016 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* time.c (mod): Add Fixnum case.
|
* time.c (mod): Add Fixnum case.
|
||||||
|
10
insns.def
10
insns.def
@ -1523,9 +1523,8 @@ opt_div
|
|||||||
{
|
{
|
||||||
if (FIXNUM_2_P(recv, obj) &&
|
if (FIXNUM_2_P(recv, obj) &&
|
||||||
BASIC_OP_UNREDEFINED_P(BOP_DIV, FIXNUM_REDEFINED_OP_FLAG)) {
|
BASIC_OP_UNREDEFINED_P(BOP_DIV, FIXNUM_REDEFINED_OP_FLAG)) {
|
||||||
long y = FIX2LONG(obj);
|
if (FIX2LONG(obj) == 0) goto INSN_LABEL(normal_dispatch);
|
||||||
if (y == 0) goto INSN_LABEL(normal_dispatch);
|
val = rb_fix_div_fix(recv, obj);
|
||||||
val = LONG2NUM(rb_div(FIX2LONG(recv), y));
|
|
||||||
}
|
}
|
||||||
else if (FLONUM_2_P(recv, obj) &&
|
else if (FLONUM_2_P(recv, obj) &&
|
||||||
BASIC_OP_UNREDEFINED_P(BOP_DIV, FLOAT_REDEFINED_OP_FLAG)) {
|
BASIC_OP_UNREDEFINED_P(BOP_DIV, FLOAT_REDEFINED_OP_FLAG)) {
|
||||||
@ -1561,9 +1560,8 @@ opt_mod
|
|||||||
{
|
{
|
||||||
if (FIXNUM_2_P(recv, obj) &&
|
if (FIXNUM_2_P(recv, obj) &&
|
||||||
BASIC_OP_UNREDEFINED_P(BOP_MOD, FIXNUM_REDEFINED_OP_FLAG )) {
|
BASIC_OP_UNREDEFINED_P(BOP_MOD, FIXNUM_REDEFINED_OP_FLAG )) {
|
||||||
long y = FIX2LONG(obj);
|
if (FIX2LONG(obj) == 0) goto INSN_LABEL(normal_dispatch);
|
||||||
if (y == 0) goto INSN_LABEL(normal_dispatch);
|
val = rb_fix_mod_fix(recv, obj);
|
||||||
val = LONG2FIX(rb_mod(FIX2LONG(recv), y));
|
|
||||||
}
|
}
|
||||||
else if (FLONUM_2_P(recv, obj) &&
|
else if (FLONUM_2_P(recv, obj) &&
|
||||||
BASIC_OP_UNREDEFINED_P(BOP_MOD, FLOAT_REDEFINED_OP_FLAG)) {
|
BASIC_OP_UNREDEFINED_P(BOP_MOD, FLOAT_REDEFINED_OP_FLAG)) {
|
||||||
|
35
internal.h
35
internal.h
@ -298,39 +298,46 @@ rb_fix_mul_fix(VALUE x, VALUE y)
|
|||||||
* Note that div may overflow fixnum.
|
* Note that div may overflow fixnum.
|
||||||
*/
|
*/
|
||||||
static inline void
|
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.
|
/* assume / and % comply C99.
|
||||||
* ldiv(3) won't be inlined by GCC and clang.
|
* ldiv(3) won't be inlined by GCC and clang.
|
||||||
* I expect / and % are compiled as single idiv.
|
* I expect / and % are compiled as single idiv.
|
||||||
*/
|
*/
|
||||||
long div = x / y;
|
long x = FIX2LONG(a);
|
||||||
long mod = x % y;
|
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) {
|
if (y > 0 ? mod < 0 : mod > 0) {
|
||||||
mod += y;
|
mod += y;
|
||||||
div -= 1;
|
div -= 1;
|
||||||
}
|
}
|
||||||
if (divp) *divp = div;
|
if (divp) *divp = LONG2FIX(div);
|
||||||
if (modp) *modp = mod;
|
if (modp) *modp = LONG2FIX(mod);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* div() for Ruby
|
/* div() for Ruby
|
||||||
* This behaves different from C99 for negative arguments.
|
* This behaves different from C99 for negative arguments.
|
||||||
* Note that div may overflow fixnum
|
|
||||||
*/
|
*/
|
||||||
static inline long
|
static inline VALUE
|
||||||
rb_div(long x, long y) {
|
rb_fix_div_fix(VALUE x, VALUE y) {
|
||||||
long div;
|
VALUE div;
|
||||||
rb_divmod(x, y, &div, NULL);
|
rb_fix_divmod_fix(x, y, &div, NULL);
|
||||||
return div;
|
return div;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* mod() for Ruby
|
/* mod() for Ruby
|
||||||
* This behaves different from C99 for negative arguments.
|
* This behaves different from C99 for negative arguments.
|
||||||
*/
|
*/
|
||||||
static inline long
|
static inline VALUE
|
||||||
rb_mod(long x, long y) {
|
rb_fix_mod_fix(VALUE x, VALUE y) {
|
||||||
long mod;
|
VALUE mod;
|
||||||
rb_divmod(x, y, NULL, &mod);
|
rb_fix_divmod_fix(x, y, NULL, &mod);
|
||||||
return mod;
|
return mod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
numeric.c
10
numeric.c
@ -3135,7 +3135,7 @@ fix_divide(VALUE x, VALUE y, ID op)
|
|||||||
{
|
{
|
||||||
if (FIXNUM_P(y)) {
|
if (FIXNUM_P(y)) {
|
||||||
if (FIX2LONG(y) == 0) rb_num_zerodiv();
|
if (FIX2LONG(y) == 0) rb_num_zerodiv();
|
||||||
return LONG2NUM(rb_div(FIX2LONG(x), FIX2LONG(y)));
|
return rb_fix_div_fix(x, y);
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
||||||
x = rb_int2big(FIX2LONG(x));
|
x = rb_int2big(FIX2LONG(x));
|
||||||
@ -3207,7 +3207,7 @@ fix_mod(VALUE x, VALUE y)
|
|||||||
{
|
{
|
||||||
if (FIXNUM_P(y)) {
|
if (FIXNUM_P(y)) {
|
||||||
if (FIX2LONG(y) == 0) rb_num_zerodiv();
|
if (FIX2LONG(y) == 0) rb_num_zerodiv();
|
||||||
return LONG2FIX(rb_mod(FIX2LONG(x), FIX2LONG(y)));
|
return rb_fix_mod_fix(x, y);
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
||||||
x = rb_int2big(FIX2LONG(x));
|
x = rb_int2big(FIX2LONG(x));
|
||||||
@ -3231,10 +3231,10 @@ static VALUE
|
|||||||
fix_divmod(VALUE x, VALUE y)
|
fix_divmod(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
if (FIXNUM_P(y)) {
|
if (FIXNUM_P(y)) {
|
||||||
long div, mod;
|
VALUE div, mod;
|
||||||
if (FIX2LONG(y) == 0) rb_num_zerodiv();
|
if (FIX2LONG(y) == 0) rb_num_zerodiv();
|
||||||
rb_divmod(FIX2LONG(x), FIX2LONG(y), &div, &mod);
|
rb_fix_divmod_fix(x, y, &div, &mod);
|
||||||
return rb_assoc_new(LONG2NUM(div), LONG2FIX(mod));
|
return rb_assoc_new(div, mod);
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
else if (RB_TYPE_P(y, T_BIGNUM)) {
|
||||||
x = rb_int2big(FIX2LONG(x));
|
x = rb_int2big(FIX2LONG(x));
|
||||||
|
2
time.c
2
time.c
@ -109,7 +109,7 @@ mod(VALUE x, VALUE y)
|
|||||||
{
|
{
|
||||||
if (FIXNUM_P(y)) {
|
if (FIXNUM_P(y)) {
|
||||||
if (FIX2LONG(y) == 0) rb_num_zerodiv();
|
if (FIX2LONG(y) == 0) rb_num_zerodiv();
|
||||||
if (FIXNUM_P(x)) return LONG2FIX(rb_mod(FIX2LONG(x), FIX2LONG(y)));
|
if (FIXNUM_P(x)) return rb_fix_mod_fix(x, y);
|
||||||
}
|
}
|
||||||
if (RB_TYPE_P(x, T_BIGNUM)) return rb_big_modulo(x, y);
|
if (RB_TYPE_P(x, T_BIGNUM)) return rb_big_modulo(x, y);
|
||||||
return rb_funcall(x, '%', 1, y);
|
return rb_funcall(x, '%', 1, y);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user