diff --git a/configure.ac b/configure.ac index e495a56619..0a3de368ac 100644 --- a/configure.ac +++ b/configure.ac @@ -1095,6 +1095,25 @@ AC_RUN_IFELSE([AC_LANG_SOURCE([[main () { AC_MSG_RESULT($ac_cv_c_double_align) AC_DEFINE_UNQUOTED(FB_DOUBLE_ALIGN, $ac_cv_c_double_align, [Alignment of double]) +AC_MSG_CHECKING(correctness of comparing 64-bit integers) +ac_cv_compare_failed=1 +ac_cv_compare_result=failed +savedflags="$CFLAGS" +CFLAGS="$CFLAGS -O3" +AC_RUN_IFELSE([AC_LANG_SOURCE([[typedef long long int SINT64; +static int abs64Compare(SINT64 n1, SINT64 n2) { + n1 = n1 > 0 ? -n1 : n1; + n2 = n2 > 0 ? -n2 : n2; + return n1 == n2 ? 0 : n1 < n2 ? 1 : -1; +} +int main() { + exit (abs64Compare(-9223372036854775808, 3652058) == 1 ? 0 : 1); +}]])],[ac_cv_compare_failed=0 +ac_cv_compare_result=success]) +CFLAGS="$savedflags" +AC_MSG_RESULT($ac_cv_compare_result) +AC_DEFINE_UNQUOTED(FB_INT64_COMPARE_FAILED, $ac_cv_compare_failed, [Error when comparing 64-bit integers]) + dnl EKU: Add any platform specific tests below case "$PLATFORM" in LINUX) diff --git a/src/common/utils_proto.h b/src/common/utils_proto.h index 554128288b..f258841ca4 100644 --- a/src/common/utils_proto.h +++ b/src/common/utils_proto.h @@ -108,6 +108,15 @@ namespace fb_utils // Return 0 if they are equal, <0 if n1 < n2 and >0 if n1 > n2. inline int abs64Compare(SINT64 n1, SINT64 n2) { +#if FB_INT64_COMPARE_FAILED + // avoid compiler bug when comparing minimum INT64 + const SINT64 MININT64 = 0x8000000000000000; + if (n1 == MININT64) + return n2 == MININT64 ? 0 : 2; + if (n2 == MININT64) + return -2; +#endif + n1 = n1 > 0 ? -n1 : n1; n2 = n2 > 0 ? -n2 : n2; return n1 == n2 ? 0 : n1 < n2 ? 1 : -1;