Mesa (master): nir: Add more modulus opcodes

Jason Ekstrand jekstrand at kemper.freedesktop.org
Wed Apr 13 22:45:35 UTC 2016


Module: Mesa
Branch: master
Commit: 745b3d295e0c432b95d43f979c74f9e91baaf6de
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=745b3d295e0c432b95d43f979c74f9e91baaf6de

Author: Jason Ekstrand <jason.ekstrand at intel.com>
Date:   Fri Mar 25 11:13:40 2016 -0700

nir: Add more modulus opcodes

These are all needed for SPIR-V

Reviewed-by: Rob Clark <robdclark at gmail.com>
Reviewed-by: Matt Turner <mattst88 at gmail.com>

---

 src/compiler/nir/nir_opcodes.py       | 16 +++++++++++++++-
 src/compiler/nir/nir_opt_algebraic.py |  1 +
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/compiler/nir/nir_opcodes.py b/src/compiler/nir/nir_opcodes.py
index 9f62e08..e75ca28 100644
--- a/src/compiler/nir/nir_opcodes.py
+++ b/src/compiler/nir/nir_opcodes.py
@@ -443,9 +443,23 @@ binop_convert("uadd_carry", tuint, tuint, commutative, "src0 + src1 < src0")
 
 binop_convert("usub_borrow", tuint, tuint, "", "src0 < src1")
 
-binop("fmod", tfloat, "", "src0 - src1 * floorf(src0 / src1)")
 binop("umod", tuint, "", "src1 == 0 ? 0 : src0 % src1")
 
+# For signed integers, there are several different possible definitions of
+# "modulus" or "remainder".  We follow the conventions used by LLVM and
+# SPIR-V.  The irem opcode implements the standard C/C++ signed "%"
+# operation while the imod opcode implements the more mathematical
+# "modulus" operation.  For details on the difference, see
+#
+# http://mathforum.org/library/drmath/view/52343.html
+
+binop("irem", tint, "", "src1 == 0 ? 0 : src0 % src1")
+binop("imod", tint, "",
+      "src1 == 0 ? 0 : ((src0 % src1 == 0 || (src0 >= 0) == (src1 >= 0)) ?"
+      "                 src0 % src1 : src0 % src1 + src1)")
+binop("fmod", tfloat, "", "src0 - src1 * floorf(src0 / src1)")
+binop("frem", tfloat, "", "src0 - src1 * truncf(src0 / src1)")
+
 #
 # Comparisons
 #
diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py
index ec8929a..2749b06 100644
--- a/src/compiler/nir/nir_opt_algebraic.py
+++ b/src/compiler/nir/nir_opt_algebraic.py
@@ -287,6 +287,7 @@ optimizations = [
 
    # Misc. lowering
    (('fmod', a, b), ('fsub', a, ('fmul', b, ('ffloor', ('fdiv', a, b)))), 'options->lower_fmod'),
+   (('frem', a, b), ('fsub', a, ('fmul', b, ('ftrunc', ('fdiv', a, b)))), 'options->lower_fmod'),
    (('uadd_carry', a, b), ('b2i', ('ult', ('iadd', a, b), a)), 'options->lower_uadd_carry'),
    (('usub_borrow', a, b), ('b2i', ('ult', a, b)), 'options->lower_usub_borrow'),
 




More information about the mesa-commit mailing list