Skip to content

Nat.div and Nat.mod are one native division each: Base drops the other half as a Nat - #2173

Merged
nicolas-abril merged 3 commits into
bendlang:mainfrom
nicolas-abril:opt/nat-divmod
Nov 18, 2026
Merged

nicolas-abril merged 3 commits into
bendlang:mainfrom
nicolas-abril:opt/nat-divmod

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Nat.div and Nat.mod were Base's Pair.fst and Pair.snd of Nat.divmod. A generic def doesn't know the half it drops is a Nat, so in C the drop was a term_sink test on that half. Every Nat.div therefore also computed the remainder, and every Nat.mod the quotient.

They now go through two Base helpers, Nat.div.go and Nat.mod.go, typed Nat & Nat -> Nat. Dropping a Nat costs nothing, so clang deletes the unused half and leaves one division. The first version of this PR made both names C operations in comp.ts (+31 tokens). That no longer fits under the 66k cap, and this version adds 0 tokens to comp.ts.

Results

One M4, one thread, korulang/koru-benchmarks osprey-compute-kernels. Each figure is the least of 7 runs, as instructions / cycles.

kernel main C operations (first version) this
isqrt 304.8M / 84.0M 164.5M / 58.6M 164.5M / 58.5M
powmod 6902M / 5030M 4728M / 3589M 4728M / 3586M
digitsum 436.7M / 62.6M 336.7M / 54.4M 327.8M / 53.9M
collatz 278.9M / 98.8M 235.6M / 91.5M 263.8M / 90.5M
coprime 338.9M / 266.0M 183.5M / 253.6M 183.5M / 253.6M
gcdsum 320.1M / 314.6M 160.0M / 305.0M 160.0M / 305.6M
primes 120.2M / 26.3M 84.5M / 23.2M 84.5M / 23.2M
josephus 92.2M / 127.1M 102.2M / 127.3M 92.2M / 127.2M
factorial 3565M / 2121M 2825M / 2171M 2825M / 2172M

All 17 bench/runtime programs emit byte-identical C and JS.

Checks

  • tests/run/nat_div_native.bend covers b = 0, a < b, a = b, the largest Nat on either side, both names as function values and partly applied, and a = b * (a / b) + a % b over a countdown.
  • Gates:
    • repo: 56/56
    • test: 1866/1866
    • ping: 84/84
    • perf: 119–123/124, with the same kind of misses on main (118/124). One mini (node 16) is out of disk, so some builds fail with clang failed to build main.

🤖 Generated with Claude Code

Base spells them Pair.fst and Pair.snd of Nat.divmod. Nat.divmod was already
native, but the generic Pair.fst / Pair.snd then dropped the unused half, and
a drop of a word of unknown type is a test (term_sink) that clang cannot
remove when it cannot bound the word: a remainder computed for nothing, two
compares and a branch per division. Both names are now C operations, the two
halves of Nat.divmod's own text. The JS lane and the interpreter still run
Base's defs.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@nicolas-abril nicolas-abril changed the title Nat.div and Nat.mod are one native division each, without the drop test of Pair.fst and Pair.snd Nat.div and Nat.mod are one native division each: Base drops the other half as a Nat Nov 18, 2026
@nicolas-abril
nicolas-abril merged commit 0e2f38f into bendlang:main Nov 18, 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.

1 participant