Module: Mesa
Branch: main
Commit: 741dbadae09ef376c1b6176f195b73930d1294db
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=741dbadae09ef376c1b6176f195b73930d1294db

Author: Georg Lehmann <[email protected]>
Date:   Mon Oct 10 13:56:51 2022 +0200

nir: Fix ifind_msb_rev constant folding.

For example if src0 is 0x80000000 we should return 1, not 0.

Signed-off-by: Georg Lehmann <[email protected]>
Reviewed-by: Ian Romanick <[email protected]>
Reviewed-by: Rhys Perry <[email protected]>

Fixes: a5747f8ab35 ("nir: add opcodes for *find_msb_rev and lowering")
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18951>

---

 src/compiler/nir/nir_opcodes.py | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/src/compiler/nir/nir_opcodes.py b/src/compiler/nir/nir_opcodes.py
index dded4a6a635..b49efdbcc19 100644
--- a/src/compiler/nir/nir_opcodes.py
+++ b/src/compiler/nir/nir_opcodes.py
@@ -500,16 +500,12 @@ for (int bit = bit_size - 1; bit >= 0; bit--) {
 
 unop_convert("ifind_msb_rev", tint32, tint, """
 dst = -1;
-if (src0 != 0 && src0 != -1) {
-   for (int bit = 0; bit < 31; bit++) {
-      /* If src0 < 0, we're looking for the first 0 bit.
-       * if src0 >= 0, we're looking for the first 1 bit.
-       */
-      if ((((src0 << bit) & 0x40000000) && (src0 >= 0)) ||
-          ((!((src0 << bit) & 0x40000000)) && (src0 < 0))) {
-         dst = bit;
-         break;
-      }
+/* We are looking for the highest bit that's not the same as the sign bit. */
+uint32_t sign = src0 & 0x80000000u;
+for (int bit = 0; bit < 32; bit++) {
+   if (((src0 << bit) & 0x80000000u) != sign) {
+      dst = bit;
+      break;
    }
 }
 """)

Reply via email to