nir: Fix nir_fmax_abs_vec_comp
authorAlyssa Rosenzweig <alyssa@collabora.com>
Thu, 13 Oct 2022 21:18:09 +0000 (17:18 -0400)
committerMarge Bot <emma+marge@anholt.net>
Mon, 17 Oct 2022 20:46:24 +0000 (20:46 +0000)
This failed to take fabs of the first component, implementing an unintended
formula that would return the right results in some common cases but is wrong in
general:

   max { x, |y|, |z| }

instead of the intended

   max { |x|, |y|, |z| }

Reexpress the implementation to make correctness obvious.

Fixes: 272e927d0e9 ("nir/spirv: initial handling of OpenCL.std extension opcodes")
Signed-off-by: Alyssa Rosenzweig <alyssa@collabora.com>
Reviewed-by: Karol Herbst <kherbst@redhat.com>
Reviewed-by: Jason Ekstrand <jason.ekstrand@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18754>

src/compiler/nir/nir_builtin_builder.h

index 40983a2..97a4e4d 100644 (file)
@@ -62,9 +62,10 @@ nir_nan_check2(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y, nir_ssa_def *res)
 static inline nir_ssa_def *
 nir_fmax_abs_vec_comp(nir_builder *b, nir_ssa_def *vec)
 {
-   nir_ssa_def *res = nir_channel(b, vec, 0);
+   nir_ssa_def *abs = nir_fabs(b, vec);
+   nir_ssa_def *res = nir_channel(b, abs, 0);
    for (unsigned i = 1; i < vec->num_components; ++i)
-      res = nir_fmax(b, res, nir_fabs(b, nir_channel(b, vec, i)));
+      res = nir_fmax(b, res, nir_channel(b, abs, i));
    return res;
 }