Skip to content

IntCompare

IntCompare

Description

Compare an integer against another integer-like value. Dispatches on the type of rhs to the matching internal handler.

Parameters

Name Direction Description
lhs in Left-hand integer
rhs in Right-hand operand (Int, pointer, u64, or i64 compatible type)

Usage example (from documentation)

  int cmp = IntCompare(&value, 42);

Returns

-1 if lhs < rhs, 0 if equal, 1 if lhs > rhs.

Usage example (Cross-references)

Usage examples (Cross-references)
            float_scale_to_exponent(&lhs_scaled, target_exponent);
            float_scale_to_exponent(&rhs_scaled, target_exponent);
            cmp = IntCompare(&lhs_scaled.significand, &rhs_scaled.significand);
    
            FloatDeinit(&lhs_scaled);
            temp.negative = lhs.negative;
        } else {
            int cmp = IntCompare(&lhs.significand, &rhs.significand);
    
            if (cmp > 0) {
            temp.negative = a->negative;
        } else {
            int cmp = IntCompare(&a->magnitude, &b->magnitude);
    
            if (cmp == 0) {
    }
    
    int(IntCompare)(Int *lhs, Int *rhs) {
        ValidateInt(lhs);
        ValidateInt(rhs);
        ValidateInt(b);
    
        if (IntCompare(a, b) < 0) {
            return false;
        }
        Int r                   = IntClone(&normalized_dividend);
    
        if (IntCompare(&normalized_dividend, &normalized_divisor) >= 0) {
            u64 dividend_bits = IntBitLength(&normalized_dividend);
            u64 divisor_bits  = IntBitLength(&normalized_divisor);
    
            MISRA_PRIV_IntPowU64(&mid_pow, &mid, degree);
            cmp = IntCompare(&mid_pow, value);
    
            if (cmp <= 0) {
                Int x    = IntInit();
    
                if (IntCompare(&base, value) >= 0) {
                    IntDeinit(&base);
                    IntDeinit(&x);
    
    bool test_int_compare_ignores_leading_zeros(void) {
        WriteFmt("Testing IntCompare leading-zero normalization\n");
    
        Int lhs = IntFromBinary("0001011");
        Int rhs = IntFrom(11);
    
        bool result = IntCompare(&lhs, &rhs) == 0;
        result      = result && IntEQ(&lhs, &rhs);
    
    bool test_int_compare(void) {
        WriteFmt("Testing IntCompare\n");
    
        Int a = IntFrom(41);
        Int c = IntFromBinary("000101010");
    
        bool result = IntCompare(&a, &b) < 0;
        result      = result && (IntCompare(&b, &a) > 0);
        result      = result && (IntCompare(&b, &c) == 0);
    
        bool result = IntCompare(&a, &b) < 0;
        result      = result && (IntCompare(&b, &a) > 0);
        result      = result && (IntCompare(&b, &c) == 0);
        bool result = IntCompare(&a, &b) < 0;
        result      = result && (IntCompare(&b, &a) > 0);
        result      = result && (IntCompare(&b, &c) == 0);
    
        IntDeinit(&a);
    
    bool test_int_compare_generic(void) {
        WriteFmt("Testing IntCompare generic dispatch\n");
    
        Int value = IntFrom(42);
        IntShiftLeft(&big, 80);
    
        bool result = (IntCompare(&value, &same) == 0);
        result      = result && (IntCompare(&value, same) == 0);
        result      = result && (IntCompare(&value, 42) == 0);
    
        bool result = (IntCompare(&value, &same) == 0);
        result      = result && (IntCompare(&value, same) == 0);
        result      = result && (IntCompare(&value, 42) == 0);
        result      = result && (IntCompare(&value, 100ULL) < 0);
        bool result = (IntCompare(&value, &same) == 0);
        result      = result && (IntCompare(&value, same) == 0);
        result      = result && (IntCompare(&value, 42) == 0);
        result      = result && (IntCompare(&value, 100ULL) < 0);
        result      = result && (IntCompare(&value, -1) > 0);
        result      = result && (IntCompare(&value, same) == 0);
        result      = result && (IntCompare(&value, 42) == 0);
        result      = result && (IntCompare(&value, 100ULL) < 0);
        result      = result && (IntCompare(&value, -1) > 0);
        result      = result && (IntCompare(&big, UINT64_MAX) > 0);
        result      = result && (IntCompare(&value, 42) == 0);
        result      = result && (IntCompare(&value, 100ULL) < 0);
        result      = result && (IntCompare(&value, -1) > 0);
        result      = result && (IntCompare(&big, UINT64_MAX) > 0);
        result      = result && IntEQ(&value, 42);
        result      = result && (IntCompare(&value, 100ULL) < 0);
        result      = result && (IntCompare(&value, -1) > 0);
        result      = result && (IntCompare(&big, UINT64_MAX) > 0);
        result      = result && IntEQ(&value, 42);
        result      = result && IntLE(&value, 42);
    
        IntModMul(&check, &result_value, &b, &m);
        result = result && (IntCompare(&check, 10) == 0);
    
        IntDeinit(&a);
        bool result = IntModSqrt(&root, &value, &mod);
        IntSquareMod(&check, &root, &mod);
        result = result && (IntCompare(&check, 10) == 0);
    
        IntDeinit(&value);
    
        bool result = !IntModSqrt(&root, &value, &mod);
        result      = result && (IntCompare(&root, 99) == 0);
    
        IntDeinit(&value);
    
        bool result = !IntModDiv(&result_value, &a, &b, &m);
        result      = result && (IntCompare(&result_value, 99) == 0);
    
        IntDeinit(&a);
    /// TAGS: Int, Compare, Ordering
    ///
    int (IntCompare)(Int *lhs, Int *rhs);
    
    #ifndef __cplusplus
                (rhs),                                                                                                     \
                Int: MISRA_PRIV_IntCompareValue,                                                                           \
                Int *: IntCompare,                                                                                         \
                const Int *: MISRA_PRIV_IntCompareConst,                                                                   \
                unsigned char: MISRA_PRIV_IntCompareU64,                                                                   \
Last updated on