From 41593f3c3711cd61156d91ad12bb8aabf91a9556 Mon Sep 17 00:00:00 2001 From: "Kristian H. Kristensen" Date: Thu, 18 Apr 2019 11:23:13 -0700 Subject: [PATCH] nir_opcodes.py: Saturate to expression that doesn't overflow Compiler warns about overflow when assigning UINT64_MAX to something smaller than a uin64_t: src/compiler/nir/nir_constant_expressions.c:16909:50: warning: implicit conversion from 'unsigned long long' to 'uint1_t' (aka 'unsigned char') changes value from 18446744073709551615 to 255 [-Wconstant-conversion] uint1_t dst = (src0 + src1) < src0 ? UINT64_MAX : (src0 + src1); ~~~ ^~~~~~~~~~ Shift UINT64_MAX down to the appropriate maximum value for the type being assigned to. Signed-off-by: Kristian H. Kristensen Reviewed-by: Jason Ekstrand --- src/compiler/nir/nir_constant_expressions.py | 2 ++ src/compiler/nir/nir_opcodes.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/compiler/nir/nir_constant_expressions.py b/src/compiler/nir/nir_constant_expressions.py index 0f89e90d7b7..f26fd0a3ea2 100644 --- a/src/compiler/nir/nir_constant_expressions.py +++ b/src/compiler/nir/nir_constant_expressions.py @@ -66,6 +66,8 @@ template = """\ #include "util/bigmath.h" #include "nir_constant_expressions.h" +#define MAX_UINT_FOR_SIZE(bits) (UINT64_MAX >> (64 - (bits))) + /** * Evaluate one component of packSnorm4x8. */ diff --git a/src/compiler/nir/nir_opcodes.py b/src/compiler/nir/nir_opcodes.py index 0f56dd9596c..d35d820aa5b 100644 --- a/src/compiler/nir/nir_opcodes.py +++ b/src/compiler/nir/nir_opcodes.py @@ -488,7 +488,7 @@ binop("iadd_sat", tint, commutative + associative, """ (src0 < src0 + src1 ? (1ull << (bit_size - 1)) : src0 + src1) """) binop("uadd_sat", tuint, commutative, - "(src0 + src1) < src0 ? UINT64_MAX : (src0 + src1)") + "(src0 + src1) < src0 ? MAX_UINT_FOR_SIZE(sizeof(src0) * 8) : (src0 + src1)") binop("isub_sat", tint, "", """ src1 < 0 ? (src0 - src1 < src0 ? (1ull << (bit_size - 1)) - 1 : src0 - src1) : -- 2.30.2