summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/gallivm/lp_bld_tgsi_action.c
diff options
context:
space:
mode:
authorrconde <rconde01@outlook.com>2014-09-17 18:30:23 +0200
committerRoland Scheidegger <sroland@vmware.com>2014-09-17 18:31:54 +0200
commitffeb77c7b0552a8624e46e65d6347240ac5ae84d (patch)
tree38ab1f9fe23e1310e018d7cc36ce7b84872cd31c /src/gallium/auxiliary/gallivm/lp_bld_tgsi_action.c
parent4d996877ca8495dd9c683fd89f90c445781faf3b (diff)
downloadexternal_mesa3d-ffeb77c7b0552a8624e46e65d6347240ac5ae84d.zip
external_mesa3d-ffeb77c7b0552a8624e46e65d6347240ac5ae84d.tar.gz
external_mesa3d-ffeb77c7b0552a8624e46e65d6347240ac5ae84d.tar.bz2
gallivm,tgsi: fix idiv by zero crash
While the result of signed integer division by zero is undefined by glsl (and doesn't exist with d3d10), we must not crash, so need to make sure we don't get sigfpe much like udiv already does. Unlike udiv where we return 0xffffffff (as required by d3d10) there is no requirement right now to return anything specific so we use zero.
Diffstat (limited to 'src/gallium/auxiliary/gallivm/lp_bld_tgsi_action.c')
-rw-r--r--src/gallium/auxiliary/gallivm/lp_bld_tgsi_action.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/gallium/auxiliary/gallivm/lp_bld_tgsi_action.c b/src/gallium/auxiliary/gallivm/lp_bld_tgsi_action.c
index b9546db..4a9bc1f 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_tgsi_action.c
+++ b/src/gallium/auxiliary/gallivm/lp_bld_tgsi_action.c
@@ -1248,8 +1248,26 @@ idiv_emit_cpu(
struct lp_build_tgsi_context * bld_base,
struct lp_build_emit_data * emit_data)
{
- emit_data->output[emit_data->chan] = lp_build_div(&bld_base->int_bld,
- emit_data->args[0], emit_data->args[1]);
+ LLVMBuilderRef builder = bld_base->base.gallivm->builder;
+ LLVMValueRef div_mask = lp_build_cmp(&bld_base->uint_bld,
+ PIPE_FUNC_EQUAL, emit_data->args[1],
+ bld_base->uint_bld.zero);
+ /* We want to make sure that we never divide/mod by zero to not
+ * generate sigfpe. We don't want to crash just because the
+ * shader is doing something weird. */
+ LLVMValueRef divisor = LLVMBuildOr(builder,
+ div_mask,
+ emit_data->args[1], "");
+ LLVMValueRef result = lp_build_div(&bld_base->uint_bld,
+ emit_data->args[0], divisor);
+
+ LLVMValueRef not_div_mask = LLVMBuildNot(builder,
+ div_mask,"");
+
+ /* idiv by zero doesn't have a guaranteed return value chose 0 for now. */
+ emit_data->output[emit_data->chan] = LLVMBuildAnd(builder,
+ not_div_mask,
+ result, "");
}
/* TGSI_OPCODE_INEG (CPU Only) */
@@ -1683,7 +1701,7 @@ udiv_emit_cpu(
emit_data->args[1], "");
LLVMValueRef result = lp_build_div(&bld_base->uint_bld,
emit_data->args[0], divisor);
- /* udiv by zero is guaranteed to return 0xffffffff */
+ /* udiv by zero is guaranteed to return 0xffffffff at least with d3d10 */
emit_data->output[emit_data->chan] = LLVMBuildOr(builder,
div_mask,
result, "");