Commit 607f8ce7 authored by Tom Lane's avatar Tom Lane

Avoid a performance regression in float overflow/underflow detection.

Commit 6bf0bc84 replaced float.c's CHECKFLOATVAL() macro with static
inline subroutines, but that wasn't too well thought out.  In the original
coding, the unlikely condition (isinf(result) or result == 0) was checked
first, and the inf_is_valid or zero_is_valid condition only afterwards.
The inline-subroutine coding caused that to be swapped around, which is
pretty horrid for performance because (a) in common cases the is_valid
condition is twice as expensive to evaluate (e.g., requiring two isinf()
calls not one) and (b) in common cases the is_valid condition is false,
requiring us to perform the unlikely-condition check anyway.  Net result
is that one isinf() call becomes two or three, resulting in visible
performance loss as reported by Keisuke Kuroda.

The original fix proposal was to revert the replacement of the macro,
but on second thought, that macro was just a bad idea from the beginning:
if anything it's a net negative for readability of the code.  So instead,
let's just open-code all the overflow/underflow tests, being careful to
test the unlikely condition first (and mark it unlikely() to help the
compiler get the point).

Also, rather than having N copies of the actual ereport() calls, collapse
those into out-of-line error subroutines to save some code space.  This
does mean that the error file/line numbers won't be very helpful for
figuring out where the issue really is --- but we'd already burned that
bridge by putting the ereports into static inlines.

In HEAD, check_float[48]_val() are gone altogether.  In v12, leave them
present in float.h but unused in the core code, just in case some
extension is depending on them.

Emre Hasegeli, with some kibitzing from me and Andres Freund

Discussion: https://postgr.es/m/CANDwggLe1Gc1OrRqvPfGE=kM9K0FSfia0hbeFCEmwabhLz95AA@mail.gmail.com
parent caba0910
This diff is collapsed.
...@@ -5545,7 +5545,10 @@ pg_hypot(float8 x, float8 y) ...@@ -5545,7 +5545,10 @@ pg_hypot(float8 x, float8 y)
yx = y / x; yx = y / x;
result = x * sqrt(1.0 + (yx * yx)); result = x * sqrt(1.0 + (yx * yx));
check_float8_val(result, false, false); if (unlikely(isinf(result)))
float_overflow_error();
if (unlikely(result == 0.0))
float_underflow_error();
return result; return result;
} }
...@@ -37,6 +37,9 @@ extern PGDLLIMPORT int extra_float_digits; ...@@ -37,6 +37,9 @@ extern PGDLLIMPORT int extra_float_digits;
/* /*
* Utility functions in float.c * Utility functions in float.c
*/ */
extern void float_overflow_error(void) pg_attribute_noreturn();
extern void float_underflow_error(void) pg_attribute_noreturn();
extern void float_zero_divide_error(void) pg_attribute_noreturn();
extern int is_infinite(float8 val); extern int is_infinite(float8 val);
extern float8 float8in_internal(char *num, char **endptr_p, extern float8 float8in_internal(char *num, char **endptr_p,
const char *type_name, const char *orig_string); const char *type_name, const char *orig_string);
...@@ -129,41 +132,7 @@ get_float8_nan(void) ...@@ -129,41 +132,7 @@ get_float8_nan(void)
} }
/* /*
* Checks to see if a float4/8 val has underflowed or overflowed * Floating-point arithmetic with overflow/underflow reported as errors
*/
static inline void
check_float4_val(const float4 val, const bool inf_is_valid,
const bool zero_is_valid)
{
if (!inf_is_valid && unlikely(isinf(val)))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value out of range: overflow")));
if (!zero_is_valid && unlikely(val == 0.0))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value out of range: underflow")));
}
static inline void
check_float8_val(const float8 val, const bool inf_is_valid,
const bool zero_is_valid)
{
if (!inf_is_valid && unlikely(isinf(val)))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value out of range: overflow")));
if (!zero_is_valid && unlikely(val == 0.0))
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value out of range: underflow")));
}
/*
* Routines for operations with the checks above
* *
* There isn't any way to check for underflow of addition/subtraction * There isn't any way to check for underflow of addition/subtraction
* because numbers near the underflow value have already been rounded to * because numbers near the underflow value have already been rounded to
...@@ -178,7 +147,8 @@ float4_pl(const float4 val1, const float4 val2) ...@@ -178,7 +147,8 @@ float4_pl(const float4 val1, const float4 val2)
float4 result; float4 result;
result = val1 + val2; result = val1 + val2;
check_float4_val(result, isinf(val1) || isinf(val2), true); if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
float_overflow_error();
return result; return result;
} }
...@@ -189,7 +159,8 @@ float8_pl(const float8 val1, const float8 val2) ...@@ -189,7 +159,8 @@ float8_pl(const float8 val1, const float8 val2)
float8 result; float8 result;
result = val1 + val2; result = val1 + val2;
check_float8_val(result, isinf(val1) || isinf(val2), true); if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
float_overflow_error();
return result; return result;
} }
...@@ -200,7 +171,8 @@ float4_mi(const float4 val1, const float4 val2) ...@@ -200,7 +171,8 @@ float4_mi(const float4 val1, const float4 val2)
float4 result; float4 result;
result = val1 - val2; result = val1 - val2;
check_float4_val(result, isinf(val1) || isinf(val2), true); if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
float_overflow_error();
return result; return result;
} }
...@@ -211,7 +183,8 @@ float8_mi(const float8 val1, const float8 val2) ...@@ -211,7 +183,8 @@ float8_mi(const float8 val1, const float8 val2)
float8 result; float8 result;
result = val1 - val2; result = val1 - val2;
check_float8_val(result, isinf(val1) || isinf(val2), true); if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
float_overflow_error();
return result; return result;
} }
...@@ -222,8 +195,10 @@ float4_mul(const float4 val1, const float4 val2) ...@@ -222,8 +195,10 @@ float4_mul(const float4 val1, const float4 val2)
float4 result; float4 result;
result = val1 * val2; result = val1 * val2;
check_float4_val(result, isinf(val1) || isinf(val2), if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
val1 == 0.0f || val2 == 0.0f); float_overflow_error();
if (unlikely(result == 0.0f) && val1 != 0.0f && val2 != 0.0f)
float_underflow_error();
return result; return result;
} }
...@@ -234,8 +209,10 @@ float8_mul(const float8 val1, const float8 val2) ...@@ -234,8 +209,10 @@ float8_mul(const float8 val1, const float8 val2)
float8 result; float8 result;
result = val1 * val2; result = val1 * val2;
check_float8_val(result, isinf(val1) || isinf(val2), if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
val1 == 0.0 || val2 == 0.0); float_overflow_error();
if (unlikely(result == 0.0) && val1 != 0.0 && val2 != 0.0)
float_underflow_error();
return result; return result;
} }
...@@ -245,13 +222,13 @@ float4_div(const float4 val1, const float4 val2) ...@@ -245,13 +222,13 @@ float4_div(const float4 val1, const float4 val2)
{ {
float4 result; float4 result;
if (val2 == 0.0f) if (unlikely(val2 == 0.0f))
ereport(ERROR, float_zero_divide_error();
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero")));
result = val1 / val2; result = val1 / val2;
check_float4_val(result, isinf(val1) || isinf(val2), val1 == 0.0f); if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
float_overflow_error();
if (unlikely(result == 0.0f) && val1 != 0.0f)
float_underflow_error();
return result; return result;
} }
...@@ -261,13 +238,13 @@ float8_div(const float8 val1, const float8 val2) ...@@ -261,13 +238,13 @@ float8_div(const float8 val1, const float8 val2)
{ {
float8 result; float8 result;
if (val2 == 0.0) if (unlikely(val2 == 0.0))
ereport(ERROR, float_zero_divide_error();
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero")));
result = val1 / val2; result = val1 / val2;
check_float8_val(result, isinf(val1) || isinf(val2), val1 == 0.0); if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
float_overflow_error();
if (unlikely(result == 0.0) && val1 != 0.0)
float_underflow_error();
return result; return result;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment