Skip to content

JS Nat.pow performs one recursive multiply per exponent step #1699

Closed as not planned

Description

@oxura

Nat.pow still lowers through its linear Base recursion on the JS lane. U32.pow already has a host path, but the Nat operation doesn't.

A small exported wrapper is enough to measure it:

import Base
def power(a: Nat, n: Nat) -> Nat:
  Nat.pow(a, n)

I compiled that to .mjs and called power(i & 1, 512 + (i & 511)) 1,000 times per round. Nine warmed alternating rounds on Linux x64/Bun 1.3.13: median 4.297 ms at PR #1682's existing 0b82c20 head, 0.199 ms with checked repeated squaring. Both sums were 500. This is a bounded workload, not an application-wide speed claim.

The new path keeps the 48-bit result limit, uses arithmetic halving for the full Nat exponent, and skips an unused final square. Regression coverage includes zero-to-zero, wide exponents for zero/one, the largest valid base to power one and an overflowing result. This is an algorithmic lowering improvement, not a request to reopen the deferred general JS stack issue #798.

Patch: oxura@aa331dfd
Included in existing PR #1682; no duplicate PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions