Skip to content

IntMul

Description

Generic multiplication convenience macro.

Parameters

Name Direction Description
result out Destination for the product
a in Left operand
b in Right operand selected through generic dispatch

Usage example (from documentation)

  IntMul(&product, &value, 10u);

Success

Returns true; *result holds a * b.

Failure

Returns false if an intermediate allocation fails; *result is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    
    bool test_int_mul(void) {
        WriteFmt("Testing IntMul\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        IntMul(&result_value, &a, &b);
    
        bool result = IntToU64(&result_value) == 126;
    
    bool test_int_mul_scalar(void) {
        WriteFmt("Testing IntMul generic dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str text         = StrInit(&alloc.base);
    
        IntMul(&result_value, &value, 25u);
        text = IntToStr(&result_value);
    
    bool test_int_mul_zero(void) {
        WriteFmt("Testing IntMul with zero\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        IntMul(&result_value, &a, &b);
    
        bool result = IntIsZero(&result_value);
        Int product = IntInit(&alloc.base);
    
        bool ok = IntMul(&product, &a, &b);
    
        bool result = ok && (IntToU64(&product) == (u64)121932631112635269ull);
        Int product = IntInit(&alloc.base);
    
        bool ok = IntMul(&product, &a, &zero);
    
        bool result = ok && IntIsZero(&product);
        Int b = IntFrom((u64)3u, &alloc.base);
    
        IntMul(NULL, &a, &b);
    
        DefaultAllocatorDeinit(&alloc);
    
        /* Signed literal forces int_mul_i64 dispatch. */
        bool ok     = IntMul(&result_value, &value, (i64)13);
        bool result = ok && (IntToU64(&result_value) == 91);
        Int result_value = IntFrom(99, &alloc.base);
    
        bool ok     = IntMul(&result_value, &value, (i64)0);
        bool result = ok && (IntToU64(&result_value) == 0);
        Int r = IntInit(a);
    
        bool ok = IntMul(&r, &x, &y);
        ok      = ok && IntToU64(&r) == 123456ull * 7891011ull;
        Int r = IntFrom(42u, a); // r starts non-empty so the internal deinit matters
    
        bool ok = IntMul(&r, &x, &y);
        ok      = ok && IntIsZero(&r);
        Int r = IntFrom(7u, a);
    
        bool ok = IntMul(&r, &x, 13u);
        ok      = ok && IntToU64(&r) == 6500u;
        Int r = IntFrom(0xDEADBEEFu, a); // pre-populated: holds a live buffer
    
        bool ok = IntMul(&r, &x, &y);
        ok      = ok && IntToU64(&r) == 123456ull * 7891011ull;
        Int product = IntInit(&alloc.base);
    
        bool ok = IntMul(&product, &a, &b);
    
        bool result = ok && (IntToU64(&product) == (u64)273u);
Last updated on