From f225bbca5067f092118ca3eeabb46106eefeb6c1 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Fri, 27 Dec 2024 22:07:01 +0100 Subject: [PATCH] Fix `bounded_int_trim` for signed types. --- src/libfuncs/bounded_int.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libfuncs/bounded_int.rs b/src/libfuncs/bounded_int.rs index 99e7c5a7b..c87f306be 100644 --- a/src/libfuncs/bounded_int.rs +++ b/src/libfuncs/bounded_int.rs @@ -734,9 +734,14 @@ fn build_trim<'ctx, 'this>( // and constraining their range a single value from either the lower or upper limit. However, // since we're returning a `BoundedInt` we need to offset its internal representation // accordingly. - let value = if info.trimmed_value == BigInt::ZERO { - let k1 = entry.const_int_from_type(context, location, 1, value.r#type())?; - entry.append_op_result(arith::subi(value, k1, location))? + let value = if info.trimmed_value <= BigInt::ZERO { + let offset = if info.trimmed_value == BigInt::ZERO { + entry.const_int_from_type(context, location, 1, value.r#type())? + } else { + entry.const_int_from_type(context, location, &info.trimmed_value + 1, value.r#type())? + }; + + entry.append_op_result(arith::subi(value, offset, location))? } else { value };