diff options
author | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-02-02 15:54:51 +0000 |
---|---|---|
committer | naruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-02-02 15:54:51 +0000 |
commit | 038ccbd112d26d15b7433b86bf48b1be0fea6e2f (patch) | |
tree | 1a1526b8206577a2b172836d875fc904d3b33f7f /insns.def | |
parent | b36f5ba197ebf5b871044c037bebfffc2675780c (diff) |
Use carry flag to reduce instructions
NOTE:
(1) Fixnum's LSB is always 1.
It means you can always run `x - 1` without overflow.
(2) Of course `z = x + (y-1)` may overflow.
Now z's LSB is always 1, and the MSB of true result is also 1.
You can get true result in long as `(1<<63)|(z>>1)`,
and it equals to `(z<<63)|(z>>1)` == `ror(z)`.
GCC and Clang have __builtin_add_ovewflow:
* https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html
* https://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57506 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'insns.def')
-rw-r--r-- | insns.def | 18 |
1 files changed, 2 insertions, 16 deletions
@@ -1373,16 +1373,7 @@ opt_plus { if (FIXNUM_2_P(recv, obj) && BASIC_OP_UNREDEFINED_P(BOP_PLUS,INTEGER_REDEFINED_OP_FLAG)) { - /* fixnum + fixnum */ -#ifndef LONG_LONG_VALUE - VALUE msb = (VALUE)1 << ((sizeof(VALUE) * CHAR_BIT) - 1); - val = recv - 1 + obj; - if ((~(recv ^ obj) & (recv ^ val)) & msb) { - val = rb_int2big((SIGNED_VALUE)((val>>1) | (recv & msb))); - } -#else - val = LONG2NUM(FIX2LONG(recv) + FIX2LONG(obj)); -#endif + val = rb_fix_plus_fix(recv, obj); } else if (FLONUM_2_P(recv, obj) && BASIC_OP_UNREDEFINED_P(BOP_PLUS, FLOAT_REDEFINED_OP_FLAG)) { @@ -1426,12 +1417,7 @@ opt_minus { if (FIXNUM_2_P(recv, obj) && BASIC_OP_UNREDEFINED_P(BOP_MINUS, INTEGER_REDEFINED_OP_FLAG)) { - long a, b, c; - - a = FIX2LONG(recv); - b = FIX2LONG(obj); - c = a - b; - val = LONG2NUM(c); + val = rb_fix_minus_fix(recv, obj); } else if (FLONUM_2_P(recv, obj) && BASIC_OP_UNREDEFINED_P(BOP_MINUS, FLOAT_REDEFINED_OP_FLAG)) { |