[Mesa-dev] [PATCH] ac/nir: Correctly handle imod with different signs.

Bas Nieuwenhuizen bas at basnieuwenhuizen.nl
Fri Jan 26 00:09:10 UTC 2018


Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=102032
---
 src/amd/common/ac_nir_to_llvm.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/amd/common/ac_nir_to_llvm.c b/src/amd/common/ac_nir_to_llvm.c
index 8ae8650a7b..4f1e4af37b 100644
--- a/src/amd/common/ac_nir_to_llvm.c
+++ b/src/amd/common/ac_nir_to_llvm.c
@@ -1671,6 +1671,23 @@ static LLVMValueRef emit_ddxy_interp(
 	return ac_build_gather_values(&ctx->ac, result, 4);
 }
 
+static LLVMValueRef emit_imod(struct ac_llvm_context *ctx, LLVMValueRef src0, LLVMValueRef src1)
+{
+	/* The imod result should have the same sign as src1 when not 0. */
+
+	LLVMValueRef result = LLVMBuildSRem(ctx->builder, src0, src1, "");
+
+	LLVMValueRef diff_sign = LLVMBuildXor(ctx->builder, result, src1, "");
+	diff_sign = LLVMBuildICmp(ctx->builder, LLVMIntSLT, diff_sign, ctx->i32_0, "");
+
+	LLVMValueRef nonzero = LLVMBuildICmp(ctx->builder, LLVMIntNE, result, ctx->i32_0, "");
+
+	LLVMValueRef cond = LLVMBuildAnd(ctx->builder, diff_sign, nonzero, "");
+	LLVMValueRef offset = LLVMBuildSelect(ctx->builder, cond, src1, ctx->i32_0, "");
+
+	return LLVMBuildAdd(ctx->builder, result, offset, "");
+}
+
 static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
 {
 	LLVMValueRef src[4], result = NULL;
@@ -1733,7 +1750,7 @@ static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
 		result = LLVMBuildMul(ctx->ac.builder, src[0], src[1], "");
 		break;
 	case nir_op_imod:
-		result = LLVMBuildSRem(ctx->ac.builder, src[0], src[1], "");
+		result = emit_imod(&ctx->ac, src[0], src[1]);
 		break;
 	case nir_op_umod:
 		result = LLVMBuildURem(ctx->ac.builder, src[0], src[1], "");
-- 
2.16.1



More information about the mesa-dev mailing list