mirror of
https://github.com/postgres/postgres.git
synced 2025-06-23 14:01:44 +03:00
Use new overflow aware integer operations.
A previous commit added inline functions that provide fast(er) and correct overflow checks for signed integer math. Use them in a significant portion of backend code. There's more to touch in both backend and frontend code, but these were the easily identifiable cases. The old overflow checks are noticeable in integer heavy workloads. A secondary benefit is that getting rid of overflow checks that rely on signed integer overflow wrapping around, will allow us to get rid of -fwrapv in the future. Which in turn slows down other code. Author: Andres Freund Discussion: https://postgr.es/m/20171024103954.ztmatprlglz3rwke@alap3.anarazel.de
This commit is contained in:
@ -22,6 +22,7 @@
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "common/int.h"
|
||||
#include "libpq/pqformat.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/cash.h"
|
||||
@ -199,20 +200,21 @@ cash_in(PG_FUNCTION_ARGS)
|
||||
|
||||
for (; *s; s++)
|
||||
{
|
||||
/* we look for digits as long as we have found less */
|
||||
/* than the required number of decimal places */
|
||||
/*
|
||||
* We look for digits as long as we have found less than the required
|
||||
* number of decimal places.
|
||||
*/
|
||||
if (isdigit((unsigned char) *s) && (!seen_dot || dec < fpoint))
|
||||
{
|
||||
Cash newvalue = (value * 10) - (*s - '0');
|
||||
int8 digit = *s - '0';
|
||||
|
||||
if (newvalue / 10 != value)
|
||||
if (pg_mul_s64_overflow(value, 10, &value) ||
|
||||
pg_sub_s64_overflow(value, digit, &value))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||
errmsg("value \"%s\" is out of range for type %s",
|
||||
str, "money")));
|
||||
|
||||
value = newvalue;
|
||||
|
||||
if (seen_dot)
|
||||
dec++;
|
||||
}
|
||||
@ -230,26 +232,23 @@ cash_in(PG_FUNCTION_ARGS)
|
||||
|
||||
/* round off if there's another digit */
|
||||
if (isdigit((unsigned char) *s) && *s >= '5')
|
||||
value--; /* remember we build the value in the negative */
|
||||
|
||||
if (value > 0)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||
errmsg("value \"%s\" is out of range for type %s",
|
||||
str, "money")));
|
||||
|
||||
/* adjust for less than required decimal places */
|
||||
for (; dec < fpoint; dec++)
|
||||
{
|
||||
Cash newvalue = value * 10;
|
||||
|
||||
if (newvalue / 10 != value)
|
||||
/* remember we build the value in the negative */
|
||||
if (pg_sub_s64_overflow(value, 1, &value))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||
errmsg("value \"%s\" is out of range for type %s",
|
||||
str, "money")));
|
||||
}
|
||||
|
||||
value = newvalue;
|
||||
/* adjust for less than required decimal places */
|
||||
for (; dec < fpoint; dec++)
|
||||
{
|
||||
if (pg_mul_s64_overflow(value, 10, &value))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||
errmsg("value \"%s\" is out of range for type %s",
|
||||
str, "money")));
|
||||
}
|
||||
|
||||
/*
|
||||
@ -285,12 +284,12 @@ cash_in(PG_FUNCTION_ARGS)
|
||||
*/
|
||||
if (sgn > 0)
|
||||
{
|
||||
result = -value;
|
||||
if (result < 0)
|
||||
if (value == PG_INT64_MIN)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
||||
errmsg("value \"%s\" is out of range for type %s",
|
||||
str, "money")));
|
||||
result = -value;
|
||||
}
|
||||
else
|
||||
result = value;
|
||||
|
Reference in New Issue
Block a user