Skip to content

lagoon: fix %mod jet to C-fmod truncation + NaN on divide-by-zero#55

Merged
sigilante merged 1 commit into
mainfrom
sigilante/fix-mod-jet
Jun 9, 2026
Merged

lagoon: fix %mod jet to C-fmod truncation + NaN on divide-by-zero#55
sigilante merged 1 commit into
mainfrom
sigilante/fix-mod-jet

Conversation

@sigilante

Copy link
Copy Markdown
Collaborator

The vendored %mod jets diverged from the Hoon +mod (fixed Hoon-side in #52 / sigilante/fix-i754-mod): they rounded the quotient to nearest (IEEE remainder) and returned the dividend on a zero divisor, where +mod does C fmod — truncate the quotient toward zero, and return NaN on a zero divisor or non-finite operand.

This fixes both vendored jet variants:

  • lagoon/vere/noun/jets/i/lagoon.c (standard; maroon/... symlinks here)
  • lagoon/vere64/noun/jets/lagoon.c (64-bit)

In both u3qi_la_mod_i754 (ray-mod) and u3qi_la_mods_i754 (mod-scalar), all four widths (f16/f32/f64/f128):

  1. Truncate the quotient with softfloat_round_minMag instead of the active mode.
  2. When the quotient is non-finite (exponent bits all 1), overwrite the result with the width's qNaN.

Purely SoftFloat-level (f*_to_i64's mode is an explicit arg), so the stateless SoftBLAS interface is untouched.

Verification

The 64-bit variant of this fix is Vere PR urbit/vere#1022, verified on a live ship (~tex):

probe result (was)
ray-mod 5 mod 3 (f64) 2.0 -1.0
ray-mod 5 mod 0 (f64) NaN 5.0
mod-scalar 5 mod 3 2.0
mod-scalar 5 mod 0 NaN 5.0
ray-mod 5 mod 3 (f32) 2.0 -1.0

🤖 Generated with Claude Code

Both vendored jet variants (lagoon/vere standard + lagoon/vere64 64-bit)
had the %mod ray-op and mod-scalar jets rounding the quotient to nearest
(IEEE remainder) and returning the dividend on a zero divisor, diverging
from the Hoon +mod (C fmod: truncate toward zero; NaN on /0 or a
non-finite operand).

Truncate the quotient with softfloat_round_minMag and return the width's
qNaN on a non-finite quotient, in both u3qi_la_mod_i754 and
u3qi_la_mods_i754, all four widths.  Mirrors Vere PR #1022, verified on
the live ~tex ship (5 mod 3 -> 2.0; 5 mod 0 -> NaN; f32 and f64).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@sigilante sigilante merged commit 975120c into main Jun 9, 2026
@sigilante sigilante deleted the sigilante/fix-mod-jet branch June 9, 2026 01:46
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