Skip to content

builtins: add clamp x lo hi#216

Merged
danieljohnmorris merged 1 commit into
mainfrom
fix/clamp-builtin
May 12, 2026
Merged

builtins: add clamp x lo hi#216
danieljohnmorris merged 1 commit into
mainfrom
fix/clamp-builtin

Conversation

@danieljohnmorris
Copy link
Copy Markdown
Collaborator

Clamping is a hot path for normalisation, UI coordinates, retry backoff caps, any bounded numeric output. min(hi, max(lo, x)) every time wastes tokens and reads poorly.

Implementation:

  • clamp x lo hi returns lo if x<lo, hi if x>hi, else x
  • Opcode 149 across tree, vm, cranelift
  • Uses a data word for the third register since the ABC slot only carries two operands

Tests: 8 cross-engine regression tests + examples/clamp.ilo with -- run: directives.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

❌ Patch coverage is 74.35897% with 20 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/vm/compile_cranelift.rs 12.50% 14 Missing ⚠️
src/interpreter/mod.rs 55.55% 4 Missing ⚠️
src/vm/mod.rs 93.75% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Clamping a value into a range is a hot path for normalisation, UI
coordinates, retry backoff caps, and any bounded numeric output.
Writing min(hi, max(lo, x)) every time costs tokens and reads worse.

clamp x lo hi returns lo when x < lo, hi when x > hi, else x. Opcode
149 across tree, vm, cranelift; uses a data word for the third
register since the standard ABC slot only carries two operands.
@danieljohnmorris danieljohnmorris merged commit 99e2c00 into main May 12, 2026
4 of 5 checks passed
@danieljohnmorris danieljohnmorris deleted the fix/clamp-builtin branch May 12, 2026 21:55
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