summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/imports.c
diff options
context:
space:
mode:
authorMatt Turner <[email protected]>2015-03-10 17:55:21 -0700
committerMatt Turner <[email protected]>2015-03-18 21:06:26 -0700
commitdd0d3a2c0fb388745519c8a3be800720541eccfe (patch)
treec825d6c3580455cee47ce55610767ed2eda39bac /src/mesa/main/imports.c
parentbb22aa08e4b08c9688c5d5c6558ac01663d0163a (diff)
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 <[email protected]>
Diffstat (limited to 'src/mesa/main/imports.c')
-rw-r--r--src/mesa/main/imports.c25
1 files changed, 3 insertions, 22 deletions
diff --git a/src/mesa/main/imports.c b/src/mesa/main/imports.c
index ac8deeb97c9..68c7316575c 100644
--- a/src/mesa/main/imports.c
+++ b/src/mesa/main/imports.c
@@ -45,6 +45,7 @@
#include <stdio.h>
#include <stdarg.h>
#include "c99_math.h"
+#include "util/rounding.h" /* for _mesa_roundeven */
#include "imports.h"
#include "context.h"
#include "mtypes.h"
@@ -307,26 +308,6 @@ _mesa_bitcount_64(uint64_t n)
#endif
-/* Using C99 rounding functions for roundToEven() implementation is
- * difficult, because round(), rint, and nearbyint() are affected by
- * fesetenv(), which the application may have done for its own
- * purposes. Mesa's IROUND macro is close to what we want, but it
- * rounds away from 0 on n + 0.5.
- */
-int
-_mesa_round_to_even(float val)
-{
- int rounded = IROUND(val);
-
- if (val - floor(val) == 0.5) {
- if (rounded % 2 != 0)
- rounded += val > 0 ? -1 : 1;
- }
-
- return rounded;
-}
-
-
/**
* Convert a 4-byte float to a 2-byte half float.
*
@@ -388,7 +369,7 @@ _mesa_float_to_half(float val)
* or normal.
*/
e = 0;
- m = _mesa_round_to_even((1 << 24) * fabsf(fi.f));
+ m = (int) _mesa_roundevenf((1 << 24) * fabsf(fi.f));
}
else if (new_exp > 15) {
/* map this value to infinity */
@@ -402,7 +383,7 @@ _mesa_float_to_half(float val)
* either normal or infinite.
*/
e = new_exp + 15;
- m = _mesa_round_to_even(flt_m / (float) (1 << 13));
+ m = (int) _mesa_roundevenf(flt_m / (float) (1 << 13));
}
}