diff options
author | Matt Turner <mattst88@gmail.com> | 2015-03-10 17:55:21 -0700 |
---|---|---|
committer | Matt Turner <mattst88@gmail.com> | 2015-03-18 21:06:26 -0700 |
commit | dd0d3a2c0fb388745519c8a3be800720541eccfe (patch) | |
tree | c825d6c3580455cee47ce55610767ed2eda39bac /src/glsl/ir_constant_expression.cpp | |
parent | bb22aa08e4b08c9688c5d5c6558ac01663d0163a (diff) | |
download | external_mesa3d-dd0d3a2c0fb388745519c8a3be800720541eccfe.zip external_mesa3d-dd0d3a2c0fb388745519c8a3be800720541eccfe.tar.gz external_mesa3d-dd0d3a2c0fb388745519c8a3be800720541eccfe.tar.bz2 |
mesa: Replace _mesa_round_to_even() with _mesa_roundeven().
Eric's initial patch adding constant expression evaluation for
ir_unop_round_even used nearbyint. The open-coded _mesa_round_to_even
implementation came about without much explanation after a reviewer
asked whether nearbyint depended on the application not modifying the
rounding mode. Of course (as Eric commented) we rely on the application
not changing the rounding mode from its default (round-to-nearest) in
many other places, including the IROUND function used by
_mesa_round_to_even!
Worse, IROUND() is implemented using the trunc(x + 0.5) trick which
fails for x = nextafterf(0.5, 0.0).
Still worse, _mesa_round_to_even unexpectedly returns an int. I suspect
that could cause problems when rounding large integral values not
representable as an int in ir_constant_expression.cpp's
ir_unop_round_even evaluation. Its use of _mesa_round_to_even is clearly
broken for doubles (as noted during review).
The constant expression evaluation code for the packing built-in
functions also mistakenly assumed that _mesa_round_to_even returned a
float, as can be seen by the cast through a signed integer type to an
unsigned (since negative float -> unsigned conversions are undefined).
rint() and nearbyint() implement the round-half-to-even behavior we want
when the rounding mode is set to the default round-to-nearest. The only
difference between them is that nearbyint() raises the inexact
exception.
This patch implements _mesa_roundeven{f,}, a function similar to the
roundeven function added by a yet unimplemented technical specification
(ISO/IEC TS 18661-1:2014), with a small difference in behavior -- we
don't bother raising the inexact exception, which I don't think we care
about anyway.
At least recent Intel CPUs can quickly change a subset of the bits in
the x87 floating-point control register, but the exception mask bits are
not included. rint() does not need to change these bits, but nearbyint()
does (twice: save old, set new, and restore old) in order to raise the
inexact exception, which would incur some penalty.
Reviewed-by: Carl Worth <cworth@cworth.org>
Diffstat (limited to 'src/glsl/ir_constant_expression.cpp')
-rw-r--r-- | src/glsl/ir_constant_expression.cpp | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/src/glsl/ir_constant_expression.cpp b/src/glsl/ir_constant_expression.cpp index 388c4c2..ecebc3c 100644 --- a/src/glsl/ir_constant_expression.cpp +++ b/src/glsl/ir_constant_expression.cpp @@ -35,6 +35,7 @@ #include <math.h> #include "main/core.h" /* for MAX2, MIN2, CLAMP */ +#include "util/rounding.h" /* for _mesa_roundeven */ #include "ir.h" #include "glsl_types.h" #include "program/hash_table.h" @@ -245,8 +246,8 @@ pack_snorm_1x8(float x) * We must first cast the float to an int, because casting a negative * float to a uint is undefined. */ - return (uint8_t) (int8_t) - _mesa_round_to_even(CLAMP(x, -1.0f, +1.0f) * 127.0f); + return (uint8_t) (int) + _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f); } /** @@ -267,8 +268,8 @@ pack_snorm_1x16(float x) * We must first cast the float to an int, because casting a negative * float to a uint is undefined. */ - return (uint16_t) (int16_t) - _mesa_round_to_even(CLAMP(x, -1.0f, +1.0f) * 32767.0f); + return (uint16_t) (int) + _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f); } /** @@ -322,7 +323,7 @@ pack_unorm_1x8(float x) * * packUnorm4x8: round(clamp(c, 0, +1) * 255.0) */ - return (uint8_t) _mesa_round_to_even(CLAMP(x, 0.0f, 1.0f) * 255.0f); + return (uint8_t) (int) _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f); } /** @@ -340,7 +341,8 @@ pack_unorm_1x16(float x) * * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0) */ - return (uint16_t) _mesa_round_to_even(CLAMP(x, 0.0f, 1.0f) * 65535.0f); + return (uint16_t) (int) + _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f); } /** @@ -733,9 +735,9 @@ ir_expression::constant_expression_value(struct hash_table *variable_context) case ir_unop_round_even: for (unsigned c = 0; c < op[0]->type->components(); c++) { if (op[0]->type->base_type == GLSL_TYPE_DOUBLE) - data.d[c] = _mesa_round_to_even(op[0]->value.d[c]); + data.d[c] = _mesa_roundeven(op[0]->value.d[c]); else - data.f[c] = _mesa_round_to_even(op[0]->value.f[c]); + data.f[c] = _mesa_roundevenf(op[0]->value.f[c]); } break; |