[Mesa-dev] [PATCH] gallivm: Use nextafterf(0.5, 0.0) as rounding constant

Matt Turner mattst88 at gmail.com
Wed Nov 28 06:37:12 UTC 2018


The common truncf(x + 0.5) fails for the floating-point value just less
than 0.5 (nextafterf(0.5, 0.0)). nextafterf(0.5, 0.0) + 0.5, after
rounding is 1.0, thus truncf does not produce the desired value.

The solution is to add nextafterf(0.5, 0.0) instead of 0.5 before
truncating. This works for all values.
---
I noticed this while investigating https://bugs.gentoo.org/665570 but it
does not fix it.

Roland, do you have a suggestion for how to make lp_build_iround() work
on non-SSE/non-Altivec platforms? I notice that if I unconditionally
return TRUE from arch_rounding_available() and make
lp_build_round_arch() take the SSE4.1 path (that emits llvm.nearbyint)
it passes on ARM64.

I noticed there's some hack in lp_test_arit.c:test_unary:

   if (test->ref == &nearbyintf && length == 2 &&
       ref != roundf(testval)) {
      /* FIXME: The generic (non SSE) path in lp_build_iround, which is
       * always taken for length==2 regardless of native round support,
       * does not round to even. */
      expected_pass = FALSE;
   }

It'd be nice to get rid of that.. but maybe we can somehow use it to
just mark all the round tests as expected fail on other platforms if no
real fix is forthcoming?

 src/gallium/auxiliary/gallivm/lp_bld_arit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/gallium/auxiliary/gallivm/lp_bld_arit.c b/src/gallium/auxiliary/gallivm/lp_bld_arit.c
index f348833206b..c050bfdb936 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_arit.c
+++ b/src/gallium/auxiliary/gallivm/lp_bld_arit.c
@@ -2477,7 +2477,7 @@ lp_build_iround(struct lp_build_context *bld,
    else {
       LLVMValueRef half;
 
-      half = lp_build_const_vec(bld->gallivm, type, 0.5);
+      half = lp_build_const_vec(bld->gallivm, type, nextafterf(0.5, 0.0));
 
       if (type.sign) {
          LLVMTypeRef vec_type = bld->vec_type;
-- 
2.18.1



More information about the mesa-dev mailing list