1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

internal.h: fix r57507

* internal.h (rb_overflowed_fix_to_int): invert sign bit.  should
  not set LSB of fixnum value, which is always set, to MSB.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57509 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-02-03 06:11:32 +00:00
parent 0db6b623a6
commit c2a6adc5fb

View file

@ -348,10 +348,10 @@ VALUE rb_int128t2big(int128_t n);
#define ST2FIX(h) LONG2FIX((long)(h))
static inline unsigned long
rb_ulong_rotate_right(unsigned long x)
static inline long
rb_overflowed_fix_to_int(long x)
{
return (x >> 1) | (x << (SIZEOF_LONG * CHAR_BIT - 1));
return (long)((unsigned long)(x >> 1) ^ (1LU << (SIZEOF_LONG * CHAR_BIT - 1)));
}
static inline VALUE
@ -368,7 +368,7 @@ rb_fix_plus_fix(VALUE x, VALUE y)
* and it equals to `(z<<63)|(z>>1)` == `ror(z)`.
*/
if (__builtin_add_overflow((long)x, (long)y-1, &lz)) {
return rb_int2big(rb_ulong_rotate_right((unsigned long)lz));
return rb_int2big(rb_overflowed_fix_to_int(lz));
}
else {
return (VALUE)lz;
@ -385,7 +385,7 @@ rb_fix_minus_fix(VALUE x, VALUE y)
#ifdef HAVE_BUILTIN___BUILTIN_SUB_OVERFLOW
long lz;
if (__builtin_sub_overflow((long)x, (long)y-1, &lz)) {
return rb_int2big(rb_ulong_rotate_right((unsigned long)lz));
return rb_int2big(rb_overflowed_fix_to_int(lz));
}
else {
return (VALUE)lz;