fn less_than()

in checker/src/abstract_value.rs [3574:3665]


    fn less_than(&self, other: Rc<AbstractValue>) -> Rc<AbstractValue> {
        // [x < c] -> c > x
        if !self.is_compile_time_constant() && other.is_compile_time_constant() {
            // Normalize binary expressions so that if only one of the operands is a constant, it is
            // always the left operand.
            return other.greater_than(self.clone());
        }
        if let Some(result) = self
            .get_cached_interval()
            .less_than(other.get_cached_interval().as_ref())
        {
            return Rc::new(result.into());
        }
        match (&self.expression, &other.expression) {
            // [(c ? v1 : v2) < c3] -> c ? (v1 < c3) : (v2 < c3)
            (
                Expression::ConditionalExpression {
                    condition: c,
                    consequent: v1,
                    alternate: v2,
                    ..
                },
                Expression::CompileTimeConstant(..),
            ) if !v1.is_top() && !v2.is_top() => {
                return c.conditional_expression(
                    v1.less_than(other.clone()),
                    v2.less_than(other.clone()),
                );
            }
            // [c3 < (c ? v1 : v2)] -> c ? (c3 < v1) : (c3 < v2 )
            (
                Expression::CompileTimeConstant(..),
                Expression::ConditionalExpression {
                    condition: c,
                    consequent: v1,
                    alternate: v2,
                    ..
                },
            ) if !v1.is_top() && !v2.is_top() => {
                return c.conditional_expression(
                    self.less_than(v1.clone()),
                    self.less_than(v2.clone()),
                );
            }
            // [(c1 * x) < c2] -> x < c2 / c1
            (Expression::Mul { left: c1, right: x }, _)
                if c1.is_compile_time_constant()
                    && other.is_compile_time_constant()
                    && other.expression.infer_type().is_integer() =>
            {
                //todo: debug_checked_assume!(!c1.is_zero()); // otherwise constant folding would have reduced the Mul
                return x.less_than(c1.divide(other.clone()));
            }
            // [c1 < (c2 * x)] -> x >= c1 / c2
            (_, Expression::Mul { left: c2, right: x })
                if self.is_compile_time_constant()
                    && c2.is_compile_time_constant()
                    && self.expression.infer_type().is_integer() =>
            {
                debug_checked_assume!(!c2.is_zero()); // otherwise constant folding would have reduced the Mul
                return x.greater_or_equal(self.divide(c2.clone()));
            }
            // [(x & c1) < c2] -> true if c1 == c2 - 1 and c2 is a power of two
            (
                Expression::BitAnd {
                    left: _x,
                    right: c1,
                },
                Expression::CompileTimeConstant(ConstantDomain::U128(c2)),
            ) if c1.is_compile_time_constant() && c2.is_power_of_two() => {
                if let Expression::CompileTimeConstant(ConstantDomain::U128(c1)) = &c1.expression {
                    if *c1 == *c2 - 1 {
                        return Rc::new(TRUE);
                    }
                }
            }
            // [x < x] -> false
            _ => {
                if self.eq(&other) {
                    return Rc::new(FALSE);
                }
            }
        }
        self.try_to_constant_fold_and_distribute_binary_op(
            other,
            ConstantDomain::less_than,
            Self::less_than,
            |l, r| {
                AbstractValue::make_binary(l, r, |left, right| Expression::LessThan { left, right })
            },
        )
    }