Skip to content

Halt early in a n_by_n multiplication loop if numbers are small enough #362

@maksymbevza

Description

@maksymbevza

Hello,
I like the library very much, thanks a lot for building and actively supporting it.

I faced the issue that when multiplying numbers of small size, that are stored in big types, the computation is still executed in full-loop. Luckily not O(n^2) but rather O(n) due to the outer loop checking for the 0-value of a limb.

Say for example we compute 11111 * 22222 using 512-bit unsigned type (32-bit limb). We'll have just the very first limbs non-zero for both values. The outer loop will ensure that if the limb value is 0 we don't run the inner loop. However, if it's not 0, the second loop will execute to the very end (to count-i to be more precise, which is helpful).
More precisely, this line continues to execute: https://github.com/ckormanyos/wide-integer/blob/master/math/wide_integer/uintwide_t.h#L3785

The suggestion is to compute the number of non-zero limbs for the second number and run the loop based on that limit.

Another example of when this is useful is when the first number is big (taking all of the limbs) but the second one is small and takes just a single limb. Another way to address this very case is to swap numbers on multiplication to multiply small by large.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions