From 7276e84637f0abb9f4f1e709b13388aff3499663 Mon Sep 17 00:00:00 2001 From: cremno Date: Mon, 9 May 2016 16:31:57 +0200 Subject: 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. --- include/mruby/numeric.h | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) (limited to 'include') 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 -- cgit v1.2.3