Mesa (main): nir/lower_bit_size: Support add_sat and sub_sat

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Aug 24 20:19:20 UTC 2021


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

Author: Ian Romanick <ian.d.romanick at intel.com>
Date:   Wed Jul 28 18:16:24 2021 -0700

nir/lower_bit_size: Support add_sat and sub_sat

Without this, lowered saturating ALU instructions would only clamp to
the range of the new type instead of the range of the old type.

v2: Use nir_iclamp.  Suggested by Jason. Use new
u_{int,uint}N_{min,max}() helpers.

Fixes: 090e2824079 ("nir: Add a saturated unsigned integer add opcode")
Reviewed-by: Jason Ekstrand <jason at jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12142>

---

 src/compiler/nir/nir_lower_bit_size.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/src/compiler/nir/nir_lower_bit_size.c b/src/compiler/nir/nir_lower_bit_size.c
index e31d9c86000..0bfe723575f 100644
--- a/src/compiler/nir/nir_lower_bit_size.c
+++ b/src/compiler/nir/nir_lower_bit_size.c
@@ -81,6 +81,23 @@ lower_alu_instr(nir_builder *bld, nir_alu_instr *alu, unsigned bit_size)
          lowered_dst = nir_ishr_imm(bld, lowered_dst, dst_bit_size);
    } else {
       lowered_dst = nir_build_alu_src_arr(bld, op, srcs);
+
+      /* The add_sat and sub_sat instructions need to clamp the result to the
+       * range of the original type.
+       */
+      if (op == nir_op_iadd_sat || op == nir_op_isub_sat) {
+         const int64_t int_max = u_intN_max(dst_bit_size);
+         const int64_t int_min = u_intN_min(dst_bit_size);
+
+         lowered_dst = nir_iclamp(bld, lowered_dst,
+                                  nir_imm_intN_t(bld, int_min, bit_size),
+                                  nir_imm_intN_t(bld, int_max, bit_size));
+      } else if (op == nir_op_uadd_sat || op == nir_op_usub_sat) {
+         const uint64_t uint_max = u_uintN_max(dst_bit_size);
+
+         lowered_dst = nir_umin(bld, lowered_dst,
+                                nir_imm_intN_t(bld, uint_max, bit_size));
+      }
    }
 
 



More information about the mesa-commit mailing list