diff --git a/src/compat/assumptions.h b/src/compat/assumptions.h --- a/src/compat/assumptions.h +++ b/src/compat/assumptions.h @@ -99,4 +99,12 @@ */ static_assert((int64_t(-10) & 0xffff) == 0xfff6, "2-complement assumed"); +/** + * Assume int64_t and long long int are the same type. + */ +static_assert(std::numeric_limits::max() == + std::numeric_limits::max()); +static_assert(std::numeric_limits::min() == + std::numeric_limits::min()); + #endif // BITCOIN_COMPAT_ASSUMPTIONS_H diff --git a/src/config/CMakeLists.txt b/src/config/CMakeLists.txt --- a/src/config/CMakeLists.txt +++ b/src/config/CMakeLists.txt @@ -55,15 +55,15 @@ check_include_files("sys/select.h" HAVE_SYS_SELECT_H) check_include_files("sys/prctl.h" HAVE_SYS_PRCTL_H) -# Bitmanip intrinsics -function(check_builtin_exist SYMBOL VARIABLE) +# Built-in compiler intrinsics +function(check_builtin_exist_with_code SYMBOL VARIABLE CODE) set( SOURCE_FILE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckBuiltinExists.c" ) set( CMAKE_CONFIGURABLE_FILE_CONTENT - "int main(int argc, char** argv) { (void)argv; return ${SYMBOL}(argc); }\n" + "int main(int argc, char** argv) { (void)argv; return ${CODE}; }\n" ) configure_file( "${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in" @@ -101,10 +101,26 @@ endif() endfunction() +function(check_builtin_exist SYMBOL VARIABLE) + check_builtin_exist_with_code( + ${SYMBOL} + ${VARIABLE} + "${SYMBOL}(argc)" + ) +endfunction() + +# Bitmanip intrinsics check_builtin_exist(__builtin_clz HAVE_DECL___BUILTIN_CLZ) check_builtin_exist(__builtin_clzl HAVE_DECL___BUILTIN_CLZL) check_builtin_exist(__builtin_clzll HAVE_DECL___BUILTIN_CLZLL) check_builtin_exist(__builtin_popcount HAVE_DECL___BUILTIN_POPCOUNT) +# Overflow math +check_builtin_exist_with_code( + __builtin_saddll_overflow HAVE_DECL___BUILTIN_SADDLL_OVERFLOW + "__builtin_saddll_overflow(argc, argc, (long long int*)&argc)") +check_builtin_exist_with_code( + __builtin_ssubll_overflow HAVE_DECL___BUILTIN_SSUBLL_OVERFLOW + "__builtin_ssubll_overflow(argc, argc, (long long int*)&argc)") # Memory management capabilities check_symbol_exists(M_ARENA_MAX "malloc.h" HAVE_MALLOPT_ARENA_MAX) diff --git a/src/config/bitcoin-config.h.cmake.in b/src/config/bitcoin-config.h.cmake.in --- a/src/config/bitcoin-config.h.cmake.in +++ b/src/config/bitcoin-config.h.cmake.in @@ -41,6 +41,8 @@ #cmakedefine HAVE_DECL___BUILTIN_CLZL 1 #cmakedefine HAVE_DECL___BUILTIN_CLZLL 1 #cmakedefine HAVE_DECL___BUILTIN_POPCOUNT 1 +#cmakedefine HAVE_DECL___BUILTIN_SADDLL_OVERFLOW 1 +#cmakedefine HAVE_DECL___BUILTIN_SSUBLL_OVERFLOW 1 #cmakedefine HAVE_MALLOPT_ARENA_MAX 1 #cmakedefine HAVE_MALLOC_INFO 1 diff --git a/src/script/intmath.h b/src/script/intmath.h --- a/src/script/intmath.h +++ b/src/script/intmath.h @@ -25,6 +25,23 @@ return result == std::numeric_limits::min(); } +/** + * Computes a + b and stores it in result. If the sum overflows or results in an + * invalid 63+sign-bit integer, return true, otherwise false. + * Uses built-in overflow functions if available, and emulated overflow math + * otherwise. + */ +static bool AddInt63Overflow(int64_t a, int64_t b, int64_t &result) { +#if HAVE_DECL___BUILTIN_SADDLL_OVERFLOW + if (__builtin_saddll_overflow(a, b, (long long int *)&result)) { + return true; + } + return result == std::numeric_limits::min(); +#else + return AddInt63OverflowEmulated(a, b, result); +#endif +} + /** * Computes a - b and stores it in result. If the difference overflows or * results in an invalid 63+sign-bit integer, return true, otherwise false. @@ -43,4 +60,21 @@ return result == std::numeric_limits::min(); } +/** + * Computes a - b and stores it in result. If the sum difference or results in + * an invalid 63+sign-bit integer, return true, otherwise false. + * Uses built-in overflow functions if available, and emulated overflow math + * otherwise. + */ +static bool SubInt63Overflow(int64_t a, int64_t b, int64_t &result) { +#if HAVE_DECL___BUILTIN_SSUBLL_OVERFLOW + if (__builtin_ssubll_overflow(a, b, (long long int *)&result)) { + return true; + } + return result == std::numeric_limits::min(); +#else + return SubInt63OverflowEmulated(a, b, result); +#endif +} + #endif // BITCOIN_SCRIPT_INTMATH_H diff --git a/src/test/intmath_tests.cpp b/src/test/intmath_tests.cpp --- a/src/test/intmath_tests.cpp +++ b/src/test/intmath_tests.cpp @@ -79,36 +79,57 @@ return num_int >= MIN_SCRIPT_63_BIT_INT && num_int <= MAX_SCRIPT_63_BIT_INT; } +void CheckArithmeticResult(std::string operation_str, bool expect_overflow, + bool had_overflow, int64_t result, + i128_t expected_result) { + if (expect_overflow) { + BOOST_CHECK_MESSAGE(had_overflow, + strprintf("%s didn't overflow", operation_str)); + } else { + BOOST_CHECK_MESSAGE(!had_overflow, + strprintf("%s overflowed", operation_str)); + BOOST_CHECK_EQUAL(result, expected_result); + } +} + void CheckArithmetic(const int64_t a_64, const int64_t b_64) { const i128_t a = a_64; const i128_t b = b_64; { + bool expect_overflow = !IsInScriptBounds(a + b); // Test AddInt63OverflowEmulated + int64_t result_emulated; + bool had_overflow_emulated = + AddInt63OverflowEmulated(a_64, b_64, result_emulated); + CheckArithmeticResult(strprintf("%d + %d", a, b), expect_overflow, + had_overflow_emulated, result_emulated, a + b); + // Test AddInt63Overflow int64_t result; - bool expect_overflow = !IsInScriptBounds(a + b); - bool had_overflow = AddInt63OverflowEmulated(a_64, b_64, result); - if (expect_overflow) { - BOOST_CHECK_MESSAGE( - had_overflow, strprintf("%d + %d didn't overflow", a_64, b_64)); - } else { - BOOST_CHECK_MESSAGE(!had_overflow, - strprintf("%d + %d overflowed", a_64, b_64)); - BOOST_CHECK_EQUAL(result, a + b); + bool had_overflow = AddInt63Overflow(a_64, b_64, result); + CheckArithmeticResult(strprintf("%d + %d", a, b), expect_overflow, + had_overflow_emulated, result_emulated, a + b); + if (!expect_overflow) { + BOOST_CHECK_EQUAL(result, result_emulated); } + BOOST_CHECK_EQUAL(had_overflow, had_overflow_emulated); } { - // Test SubInt63OverflowEmulated - int64_t result; bool expect_overflow = !IsInScriptBounds(a - b); - bool had_overflow = SubInt63OverflowEmulated(a_64, b_64, result); - if (expect_overflow) { - BOOST_CHECK_MESSAGE( - had_overflow, strprintf("%d - %d didn't overflow", a_64, b_64)); - } else { - BOOST_CHECK_MESSAGE(!had_overflow, - strprintf("%d - %d overflowed", a_64, b_64)); - BOOST_CHECK_EQUAL(result, a - b); + // Test AddInt63OverflowEmulated + int64_t result_emulated; + bool had_overflow_emulated = + SubInt63OverflowEmulated(a_64, b_64, result_emulated); + CheckArithmeticResult(strprintf("%d - %d", a, b), expect_overflow, + had_overflow_emulated, result_emulated, a - b); + // Test AddInt63Overflow + int64_t result; + bool had_overflow = SubInt63Overflow(a_64, b_64, result); + CheckArithmeticResult(strprintf("%d - %d", a, b), expect_overflow, + had_overflow_emulated, result_emulated, a - b); + if (!expect_overflow) { + BOOST_CHECK_EQUAL(result, result_emulated); } + BOOST_CHECK_EQUAL(had_overflow, had_overflow_emulated); } }