Skip to content

perf(c): compute U32.pow by exponentiation by squaring - #1567

Merged
Lorenzobattistela merged 2 commits into
bendlang:mainfrom
sebastian-griego:perf/u32-pow
Oct 15, 2026
Merged

Lorenzobattistela merged 2 commits into
bendlang:mainfrom
sebastian-griego:perf/u32-pow

Conversation

@sebastian-griego

Copy link
Copy Markdown
Contributor

U32.pow performs one multiplication per exponent step on the C backend. Use exponentiation by squaring to reduce that work to the exponent's bit length. Keep multiplication modulo 2^32, return one for exponent zero, and retain all 48 bits of the compiled Nat exponent.

Keep the Base definition and generated JavaScript unchanged. This preserves the existing proof reductions and reference backend. Select the backend's intrinsic table once per file so C-only entries do not suppress JavaScript definitions. Add tests for overflow, zero exponents, partial applications, larger exponents, and the symbolic successor equation.

On an Apple M5 Max, 500,000 calls with exponent 4,096 took 1.827 s on main and 1.810 ms with this change. The base changes on every call. For exponent 2, 5,000,000 calls took 9.728 ms and 2.514 ms. All measured exponents from 0 through 4,096 improved on macOS Clang 21 and Linux ARM64 Clang 19. These are focused benchmark results, not application speedups.

Validation:

  • Checker and BendTT outcomes match main for all 1,646 test files, including expected rejections.
  • All 791 successful JavaScript emissions are byte-identical. Unrelated C emissions add only an unused inline helper. All 17 runtime benchmarks have identical CPU assembly.
  • 20,000 native cases match an independent modular-power reference on each OS, including exponents above 2^32. Both new tests pass through the source CLI, C, Bun, and Node.
  • Parallel validation passes on CPU and Metal. CUDA compilation passes for sm_80 and sm_90; CUDA hardware execution was not available.
  • 320 repeated emissions match. The final compiler-time comparisons show no material regression in the tested programs.
  • AddressSanitizer and UndefinedBehaviorSanitizer pass at -O0. The existing optimized-runtime sanitizer failure also occurs on main.
  • The repository gate passes. The compiler remains below its 64,000-token cap. Strict TypeScript checking reports the same 11 existing errors.

The maintainer cluster gates have not run. Linux ran in an ARM64 VM on the same physical CPU.

sebastian-griego and others added 2 commits October 15, 2026 07:59
…JS text in intr_of

Drops JS_OPERATIONS and File.ops: intr_of skips an op with no JS text in a
JS file, so JS still runs Base's U32.pow. Restores the two comments the
first commit cut to fit the cap; on current main comp.ts fits with them.
@Lorenzobattistela
Lorenzobattistela merged commit b54763b into bendlang:main Oct 15, 2026
1 check passed
@Lorenzobattistela Lorenzobattistela mentioned this pull request Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants