Skip to content

Reciprocal pre-compute operations for fast wide divisions #49

Description

@MaxGraey

I know the specification already rejects div/rem operations which is perfectly makes sense.

However, I'd like to discuss the possibility of adding not the high-level division operations themselves, but rather some of the underlying building blocks, specifically reciprocal_2by1 and possibly reciprocal_3by2.

The idea is similar to SSE's RCPSS/RCPPS, but targeted at wide integer arithmetic. Division by inversion is already used in a number of libraries that provide efficient division algorithms, and it was described in the following paper:
https://gmplib.org/~tege/division-paper.pdf

And how reciprocal_2by1 and reciprocal_3by2 looks like on practive.

btw not sure about Clang but GCC do this for loop inv divisors during compile time

reciprocal_2by1 is more important because it requires a lookup table, which costs size and performance on wasm side.

Furthermore, the reciprocal precomputation has optimization opportunities of its own. If the divisor is loop-invariant, the reciprocal step can be hoisted by LICM. Likewise, if multiple expressions use the same divisor, apply CSE. In that sense, representing the reciprocal explicitly as an IR-level or specific wasm op seems genuinely useful

WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions