summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorMatt Turner <mattst88@gmail.com>2015-03-10 17:55:21 -0700
committerMatt Turner <mattst88@gmail.com>2015-03-18 21:06:26 -0700
commitdd0d3a2c0fb388745519c8a3be800720541eccfe (patch)
treec825d6c3580455cee47ce55610767ed2eda39bac /src/util
parentbb22aa08e4b08c9688c5d5c6558ac01663d0163a (diff)
downloadexternal_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/util')
-rw-r--r--src/util/Makefile.sources1
-rw-r--r--src/util/rounding.h58
2 files changed, 59 insertions, 0 deletions
diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources
index 560ea83..3e0d02b 100644
--- a/src/util/Makefile.sources
+++ b/src/util/Makefile.sources
@@ -14,6 +14,7 @@ MESA_UTIL_FILES := \
register_allocate.h \
rgtc.c \
rgtc.h \
+ rounding.h \
set.c \
set.h \
simple_list.h \
diff --git a/src/util/rounding.h b/src/util/rounding.h
new file mode 100644
index 0000000..c8be450
--- /dev/null
+++ b/src/util/rounding.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright © 2015 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <math.h>
+
+/* The C standard library has functions round()/rint()/nearbyint() that round
+ * their arguments according to the rounding mode set in the floating-point
+ * control register. While there are trunc()/ceil()/floor() functions that do
+ * a specific operation without modifying the rounding mode, there is no
+ * roundeven() in any version of C.
+ *
+ * Technical Specification 18661 (ISO/IEC TS 18661-1:2014) adds roundeven(),
+ * but it's unfortunately not implemented by glibc.
+ *
+ * This implementation differs in that it does not raise the inexact exception.
+ *
+ * We use rint() to implement these functions, with the assumption that the
+ * floating-point rounding mode has not been changed from the default Round
+ * to Nearest.
+ */
+
+/**
+ * \brief Rounds \c x to the nearest integer, with ties to the even integer.
+ */
+static inline float
+_mesa_roundevenf(float x)
+{
+ return rintf(x);
+}
+
+/**
+ * \brief Rounds \c x to the nearest integer, with ties to the even integer.
+ */
+static inline double
+_mesa_roundeven(double x)
+{
+ return rint(x);
+}