Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shyouhei/30badd7cf762d677d8001e8fbe65dfa5 to your computer and use it in GitHub Desktop.
Save shyouhei/30badd7cf762d677d8001e8fbe65dfa5 to your computer and use it in GitHub Desktop.
From 8ecef9e3ed8003a1cb6c1601cfa56d82f70e9900 Mon Sep 17 00:00:00 2001
From: "Urabe, Shyouhei" <[email protected]>
Date: Wed, 8 Mar 2017 13:14:04 +0900
Subject: [PATCH] re-introduce __builtin_add_overflow
r57789 (74cdd89) was gradually "improve"d by naruse through r57793 to
r57806, resulted in reverting the efect of r57789 while retaining its
complexity. I think the current situation is slightly worse than
before (same output complicated source code).
Here I introduce __builtin_add_overflow again, which (I think) is what
naruse wanted to do in r57793.
Signed-off-by: Urabe, Shyouhei <[email protected]>
---
include/ruby/ruby.h | 29 +++++++++++++----------------
internal.h | 7 +++----
numeric.c | 7 +++----
3 files changed, 19 insertions(+), 24 deletions(-)
diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h
index 212ba82dd7..3cae4c0fd5 100644
--- a/include/ruby/ruby.h
+++ b/include/ruby/ruby.h
@@ -1505,15 +1505,14 @@ rb_integer_type_p(VALUE obj)
#endif
static inline int
-rb_long2fix_overflow(long l, VALUE *ptr)
+rb_long_is_fixable_p(long v)
{
- if (RB_FIXABLE(l)) {
- *ptr = RB_LONG2FIX(l);
- return 0;
- }
- else {
- return 1;
- }
+#ifdef HAVE_BUILTIN___BUILTIN_ADD_OVERFLOW
+ SIGNED_VALUE w;
+ return! __builtin_add_overflow(v, v, &w);
+#else
+ return RB_FIXABLE(v);
+#endif
}
#if SIZEOF_INT < SIZEOF_LONG
@@ -1523,11 +1522,10 @@ rb_long2fix_overflow(long l, VALUE *ptr)
static inline VALUE
rb_int2num_inline(int v)
{
- VALUE ret;
- if (rb_long2fix_overflow(v, &ret))
- return rb_int2big(v);
+ if (rb_long_is_fixable_p(v))
+ return RB_LONG2FIX(v);
else
- return ret;
+ return rb_int2big(v);
}
#define RB_INT2NUM(x) rb_int2num_inline(x)
@@ -1547,11 +1545,10 @@ rb_uint2num_inline(unsigned int v)
static inline VALUE
rb_long2num_inline(long v)
{
- VALUE ret;
- if (rb_long2fix_overflow(v, &ret))
- return rb_int2big(v);
+ if (rb_long_is_fixable_p(v))
+ return LONG2FIX(v);
else
- return ret;
+ return rb_int2big(v);
}
#define RB_LONG2NUM(x) rb_long2num_inline(x)
diff --git a/internal.h b/internal.h
index f2e726ce6e..9c8a2c00ae 100644
--- a/internal.h
+++ b/internal.h
@@ -1386,12 +1386,11 @@ rb_float_new_inline(double d)
static inline VALUE
rb_dbl2ival(double d)
{
- VALUE val;
- if (rb_long2fix_overflow(d, &val)) {
- return rb_dbl2big(d);
+ if (RB_FIXABLE(d)) {
+ return LONG2FIX((long)d);
}
else {
- return val;
+ return rb_dbl2big(d);
}
}
diff --git a/numeric.c b/numeric.c
index 4f789e47a4..c728e309f4 100644
--- a/numeric.c
+++ b/numeric.c
@@ -3007,16 +3007,15 @@ VALUE
rb_num2fix(VALUE val)
{
long v;
- VALUE w;
if (FIXNUM_P(val)) {
return val;
}
- else if (rb_long2fix_overflow((v = rb_num2long(val)), &w)) {
- rb_raise(rb_eRangeError, "integer %ld out of range of fixnum", v);
+ else if (rb_long_is_fixable_p(v = rb_num2long(val))) {
+ return LONG2FIX(v);
}
else {
- return w;
+ rb_raise(rb_eRangeError, "integer %ld out of range of fixnum", v);
}
}
--
2.12.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment