summaryrefslogtreecommitdiff
path: root/src/integer.h
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2019-01-20 13:00:53 +0000
committerEdward Thomson <ethomson@edwardthomson.com>2019-01-20 13:00:53 +0000
commitf04f1c7eedf2081c35dda71594a6072c6cc65694 (patch)
tree6e3a52c82f2993fae4ecef0715f6b36c0471171b /src/integer.h
parent2848923a2f5099b6d105b0b30212134d84377dee (diff)
downloadlibgit2-f04f1c7eedf2081c35dda71594a6072c6cc65694.tar.gz
add with overflow intrinsics: simplify tests
Use the smallest unsigned type that is equivalent to `size_t` to simplify the conditionals. Error if we're on a system that we believe offers builtins but we cannot determine which one to use.
Diffstat (limited to 'src/integer.h')
-rw-r--r--src/integer.h44
1 files changed, 18 insertions, 26 deletions
diff --git a/src/integer.h b/src/integer.h
index 98d2b73c5..f6642ca23 100644
--- a/src/integer.h
+++ b/src/integer.h
@@ -58,32 +58,24 @@ GIT_INLINE(bool) git__add_uint64_overflow(uint64_t *out, uint64_t one, uint64_t
#if (__has_builtin(__builtin_add_overflow) || \
(defined(__GNUC__) && (__GNUC__ >= 5)))
-/*
- * Even though __builtin_{add,mul}_overflow should be able to handle all
- * possible cases (since it can accept any type), under some configurations
- * clang would need a dependency on compiler-rt. In order to avoid that, we
- * attempt to choose one of the explicit unsigned long long / unsigned long
- * versions of the intrinsics if possible. Unfortunately, unsigned long long
- * and unsigned long are still different types to the compiler, so we need to
- * still do some additional sniffing to prevent MinGW 64 from choosing the
- * wrong version and triggering compiler warnings.
- */
-# if (SIZE_MAX == ULLONG_MAX) && (ULONG_MAX == ULLONG_MAX) && defined(_WIN64)
-# define git__add_sizet_overflow(out, one, two) \
- __builtin_uaddll_overflow(one, two, out)
-# define git__multiply_sizet_overflow(out, one, two) \
- __builtin_umulll_overflow(one, two, out)
-# elif (SIZE_MAX == ULONG_MAX) && (ULONG_MAX == ULLONG_MAX)
-# define git__add_sizet_overflow(out, one, two) \
- __builtin_uaddl_overflow(one, two, out)
-# define git__multiply_sizet_overflow(out, one, two) \
- __builtin_umull_overflow(one, two, out)
-# else
-# define git__add_sizet_overflow(out, one, two) \
- __builtin_add_overflow(one, two, out)
-# define git__multiply_sizet_overflow(out, one, two) \
- __builtin_mul_overflow(one, two, out)
-# endif
+# if (SIZE_MAX == UINT_MAX)
+# define git__add_sizet_overflow(out, one, two) \
+ __builtin_uadd_overflow(one, two, out)
+# define git__multiply_sizet_overflow(out, one, two) \
+ __builtin_umul_overflow(one, two, out)
+# elif (SIZE_MAX == ULONG_MAX)
+# define git__add_sizet_overflow(out, one, two) \
+ __builtin_uaddl_overflow(one, two, out)
+# define git__multiply_sizet_overflow(out, one, two) \
+ __builtin_umull_overflow(one, two, out)
+# elif (SIZE_MAX == ULLONG_MAX)
+# define git__add_sizet_overflow(out, one, two) \
+ __builtin_uaddll_overflow(one, two, out)
+# define git__multiply_sizet_overflow(out, one, two) \
+ __builtin_umulll_overflow(one, two, out)
+# else
+# error compiler has add with overflow intrinsics but SIZE_MAX is unknown
+# endif
#else