1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Provide overflow safe integer math inline functions.

It's not easy to get signed integer overflow checks correct and
fast. Therefore abstract the necessary infrastructure into a common
header providing addition, subtraction and multiplication for 16, 32,
64 bit signed integers.

The new macros aren't yet used, but a followup commit will convert
several open coded overflow checks.

Author: Andres Freund, with some code stolen from Greg Stark
Reviewed-By: Robert Haas
Discussion: https://postgr.es/m/20171024103954.ztmatprlglz3rwke@alap3.anarazel.de
This commit is contained in:
Andres Freund
2017-10-29 22:13:54 -07:00
parent 95b52351fe
commit 4d6ad31257
6 changed files with 304 additions and 0 deletions

33
configure vendored
View File

@ -14472,6 +14472,39 @@ esac
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for __builtin_mul_overflow" >&5
$as_echo_n "checking for __builtin_mul_overflow... " >&6; }
if ${pgac_cv__builtin_op_overflow+:} false; then :
$as_echo_n "(cached) " >&6
else
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
int
main ()
{
PG_INT64_TYPE result;
__builtin_mul_overflow((PG_INT64_TYPE) 1, (PG_INT64_TYPE) 2, &result);
;
return 0;
}
_ACEOF
if ac_fn_c_try_compile "$LINENO"; then :
pgac_cv__builtin_op_overflow=yes
else
pgac_cv__builtin_op_overflow=no
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv__builtin_op_overflow" >&5
$as_echo "$pgac_cv__builtin_op_overflow" >&6; }
if test x"$pgac_cv__builtin_op_overflow" = xyes ; then
$as_echo "#define HAVE__BUILTIN_OP_OVERFLOW 1" >>confdefs.h
fi
# Check size of void *, size_t (enables tweaks for > 32bit address space)
# The cast to long int works around a bug in the HP C Compiler
# version HP92453-01 B.11.11.23709.GP, which incorrectly rejects