summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorcremno <[email protected]>2016-05-09 16:31:57 +0200
committercremno <[email protected]>2016-05-09 16:52:57 +0200
commit7276e84637f0abb9f4f1e709b13388aff3499663 (patch)
treed5cb6881b351e5c70e6ef8b16d0d4ebe35781c15 /include
parent39b6a9dafc942396167f905da8f4e7de3ac22229 (diff)
downloadmruby-7276e84637f0abb9f4f1e709b13388aff3499663.tar.gz
mruby-7276e84637f0abb9f4f1e709b13388aff3499663.zip
use type-generic checked arithmetic builtins
Version checking is not reliable - especially with Clang. E.g. Apple's Clang (Xcode) uses different version numbers. A feature check (__has_builtin) is the recommened way. Add the MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS macro which may be used in other files.
Diffstat (limited to 'include')
-rw-r--r--include/mruby/numeric.h48
1 files changed, 21 insertions, 27 deletions
diff --git a/include/mruby/numeric.h b/include/mruby/numeric.h
index cbfeea2b1..b16c299a4 100644
--- a/include/mruby/numeric.h
+++ b/include/mruby/numeric.h
@@ -31,45 +31,39 @@ mrb_value mrb_fixnum_minus(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_fixnum_mul(mrb_state *mrb, mrb_value x, mrb_value y);
mrb_value mrb_num_div(mrb_state *mrb, mrb_value x, mrb_value y);
-/* Idea from Potion: https://github.com/perl11/potion (MIT) */
-#if (defined(__clang__) && ((__clang_major__ > 3) || (__clang_major__ == 3 && __clang_minor__ >= 4))) \
- || (defined(__GNUC__) && __GNUC__ >= 5)
+#ifndef __has_builtin
+ #define __has_builtin(x) 0
+#endif
-static inline mrb_bool
-mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
-{
- mrb_bool of;
+#if (defined(__GNUC__) && __GNUC__ >= 5) || \
+ (__has_builtin(__builtin_add_overflow) && \
+ __has_builtin(__builtin_sub_overflow) && \
+ __has_builtin(__builtin_mul_overflow))
+# define MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
+#endif
+
+#ifdef MRB_HAVE_TYPE_GENERIC_CHECKED_ARITHMETIC_BUILTINS
-#ifdef MRB_INT64
- long long val;
- of = __builtin_saddll_overflow(augend, addend, &val) ||
+#ifndef MRB_WORD_BOXING
+# define WBCHK(x) 0
#else
- int val;
- of = __builtin_sadd_overflow(augend, addend, &val) ||
+# define WBCHK(x) !FIXABLE(x)
#endif
- (val > MRB_INT_MAX) || (val < MRB_INT_MIN);
- *sum = (mrb_int) val;
- return of;
+static inline mrb_bool
+mrb_int_add_overflow(mrb_int augend, mrb_int addend, mrb_int *sum)
+{
+ return __builtin_add_overflow(augend, addend, sum) || WBCHK(*sum);
}
static inline mrb_bool
mrb_int_sub_overflow(mrb_int minuend, mrb_int subtrahend, mrb_int *difference)
{
- mrb_bool of;
+ return __builtin_sub_overflow(minuend, subtrahend, difference) || WBCHK(*difference);
+}
-#ifdef MRB_INT64
- long long val;
- of = __builtin_ssubll_overflow(minuend, subtrahend, &val) ||
-#else
- int val;
- of = __builtin_ssub_overflow(minuend, subtrahend, &val) ||
-#endif
- (val > MRB_INT_MAX) || (val < MRB_INT_MIN);
+#undef WBCHK
- *difference = (mrb_int) val;
- return of;
-}
#else
#define MRB_UINT_MAKE2(n) uint ## n ## _t