Skip to content

IntSub

IntSub

Description

Generic subtraction convenience macro. Dispatches on the type of b to the matching IntSub* overload.

Parameters

Name Direction Description
result out Destination for the difference
a in Left operand
b in Right operand (Int, pointer, u64, or i64 compatible type)

Usage example (from documentation)

  bool ok = IntSub(&diff, &value, 1u);

Returns

Result of the selected subtraction overload.

Usage example (Cross-references)

Usage examples (Cross-references)
    
            if (cmp > 0) {
                (void)IntSub(&temp.significand, &lhs.significand, &rhs.significand);
                temp.negative = lhs.negative;
            } else if (cmp < 0) {
                temp.negative = lhs.negative;
            } else if (cmp < 0) {
                (void)IntSub(&temp.significand, &rhs.significand, &lhs.significand);
                temp.negative = rhs.negative;
            } else {
                temp.negative = false;
            } else if (cmp > 0) {
                (void)IntSub(&temp.magnitude, &a->magnitude, &b->magnitude);
                temp.negative = a->negative;
            } else {
                temp.negative = a->negative;
            } else {
                (void)IntSub(&temp.magnitude, &b->magnitude, &a->magnitude);
                temp.negative = b->negative;
            }
    }
    
    bool(IntSub)(Int *result, Int *a, Int *b) {
        ValidateInt(result);
        ValidateInt(a);
    
        Int rhs = MISRA_PRIV_IntFromU64(subtrahend);
        bool ok = IntSub(result, value, &rhs);
    
        IntDeinit(&rhs);
                    Int next = IntInit();
    
                    (void)IntSub(&next, &r, &shifted);
                    IntDeinit(&r);
                    r = next;
                    high = IntInit();
                } else {
                    (void)IntSub(&next, &mid, &one);
                    IntDeinit(&high);
                    high = next;
    
            MISRA_PRIV_IntPowU64(&power, &best, degree);
            (void)IntSub(&rem, value, &power);
    
            IntDeinit(&power);
    
        if (IntGE(&ar, &br)) {
            (void)IntSub(result, &ar, &br);
        } else {
            Int diff = IntInit();
            Int diff = IntInit();
    
            (void)IntSub(&diff, &br, &ar);
            if (IntIsZero(&diff)) {
                Int zero = IntInit();
                int_replace(result, &zero);
            } else {
                (void)IntSub(result, modulus, &diff);
            }
            IntMod(&mag_mod, &t.magnitude, modulus);
            if (t.negative && !IntIsZero(&mag_mod)) {
                (void)IntSub(&positive, modulus, &mag_mod);
            } else {
                IntMod(&positive, &t.magnitude, modulus);
    
    bool test_int_sub(void) {
        WriteFmt("Testing IntSub\n");
    
        Int a      = IntFrom(256);
        Int result_value = IntInit();
    
        bool result = IntSub(&result_value, &a, &b);
        result      = result && (IntToU64(&result_value) == 255);
    
    bool test_int_sub_generic(void) {
        WriteFmt("Testing IntSub generic dispatch\n");
    
        Int base         = IntFrom(40);
        Str text         = StrInit();
    
        bool result = IntSub(&result_value, &base, rhs);
        result      = result && (IntToU64(&result_value) == 38);
        result      = result && (IntToU64(&result_value) == 38);
    
        result = result && IntSub(&result_value, &base, 2u);
        result = result && (IntToU64(&result_value) == 38);
        result = result && (IntToU64(&result_value) == 38);
    
        result = result && IntSub(&result_value, &base, -2);
        result = result && (IntToU64(&result_value) == 42);
        result = result && (IntToU64(&result_value) == 42);
    
        result = result && IntSub(&result_value, &huge, 90);
        text   = IntToStr(&result_value);
        result = result && (strcmp(text.data, "12345678901234567800") == 0);
        result = result && (strcmp(text.data, "12345678901234567800") == 0);
    
        result = result && !IntSub(&preserved, &base, 50);
        result = result && (IntToU64(&preserved) == 99);
    
    bool test_int_sub_underflow_preserves_result(void) {
        WriteFmt("Testing IntSub underflow handling\n");
    
        Int a      = IntFrom(3);
        Int result_value = IntFrom(99);
    
        bool result = !IntSub(&result_value, &a, &b);
        result      = result && (IntToU64(&result_value) == 99);
    /// TAGS: Int, Math, Subtract
    ///
    bool (IntSub)(Int *result, Int *a, Int *b);
    ///
    /// Multiply two integers.
                (rhs),                                                                                                     \
                Int: MISRA_PRIV_IntSubValue,                                                                               \
                Int *: IntSub,                                                                                             \
                const Int *: MISRA_PRIV_IntSubConst,                                                                       \
                unsigned char: MISRA_PRIV_IntSubU64,                                                                       \
Last updated on