Skip to content

noun: /lib/twoc two's-complement jets (scalar + Lagoon %int2 arrays)#1047

Open
sigilante wants to merge 1 commit into
developfrom
sigilante/twoc-jets
Open

noun: /lib/twoc two's-complement jets (scalar + Lagoon %int2 arrays)#1047
sigilante wants to merge 1 commit into
developfrom
sigilante/twoc-jets

Conversation

@sigilante

Copy link
Copy Markdown
Collaborator

Bit-exact C jets for the numerics /lib/twoc two's-complement integer library, under the non chapter alongside math/lagoon/unum. Hoon side: urbit/numerics#66.

No vendored library — the arithmetic is plain integer C plus GNU MP (already linked into pkg/noun).

Changes

  • jets/i/twoc.c — one %twid core (the width-keyed door where /lib/twoc keeps its logic; the bloq-keyed facade re-exports its gates, so this fires for both call paths). Reads wid from the door sample (gate axis 30) and dispatches: native c3_d for wid<=64, __int128 for <=128, GMP (u3r_mp/u3i_mp) for >128. Arms: add sub neg mul div rem pow abs gth lth lte gte. Declines to Hoon on out-of-width operands and zero divisors.
  • jets/i/twoc.h — shared two's-complement kernels (used by twoc.c and lagoon.c).
  • jets/i/lagoon.c — the element-wise array jets (add/sub/mul/div/mod-rays) gain a %int2 case beside %i754, looping native two's-complement per lane over the ray byte buffer (one C pass vs the super-linear Hoon bit-packing loop).
  • jets/w.h, q.hu3wi_twid_* / u3qi_twid_* declarations.
  • build.zig — add jets/i/twoc.c (no new linkLibrary; GMP already linked).
  • jets/{135,136,137}/tree.c — register twid under non (sibling of lagoon).

Validation

  • Standalone vs an independent reference: 8280 native + 5692 GMP checks pass.
  • Fires bit-exact on a hoon-135 fakezod. Scalar proven two ways (a planted sentinel confirmed the C path executes via both twid-direct and the twoc facade; timing toggle ~3.3×). %int2 array ops verified jet == Hoon for all five; ~310,000× over interpreted at 200k lanes.

Built and fired on the 32-bit runtime.

🤖 Generated with Claude Code

Bit-exact C jets for the numerics /lib/twoc two's-complement integer library,
under the non chapter alongside math/lagoon/unum.  No vendored library: the
arithmetic is plain integer C plus GNU MP (already linked into pkg/noun).

jets/i/twoc.c -- one %twid core (the width-keyed door where /lib/twoc keeps its
logic; the bloq-keyed facade re-exports its gates, so this fires for both).
Reads wid from the door sample (gate axis 30) and dispatches: native c3_d for
wid<=64, __int128 for <=128, GMP (u3r_mp/u3i_mp) for >128.  Arms: add sub neg
mul div rem pow abs gth lth lte gte.  Declines to Hoon on out-of-width operands
and zero divisors.  Kernels shared via jets/i/twoc.h.

jets/i/lagoon.c -- Lagoon element-wise array jets (add/sub/mul/div/mod-rays)
gain a %int2 case beside %i754, looping native two's-complement per lane over
the ray byte buffer (one C pass vs the super-linear Hoon bit-packing loop).

Registered twid under non in the 135/136/137 dashboards.  Fires bit-exact on a
hoon-135 fakezod (scalar verified two ways: sentinel + timing; %int2 array add
~310,000x over interpreted at 200k lanes).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@sigilante
sigilante force-pushed the sigilante/twoc-jets branch from fe0cd7d to 4375784 Compare June 29, 2026 15:44
@sigilante

Copy link
Copy Markdown
Collaborator Author

Coordination note: #1057 rewrites the lagoon.c validation layer and several kernels on develop, and every wrapper this PR's %int2 additions hook into changes shape there (_check takes [meta data] borrowed — the u3nc call sites leak — plus added consistency checks and NaN-correct comparisons). After #1057 lands, this branch's lagoon.c should be rebuilt on top of it; the numerics repo's lagoon/vere/noun/jets/i/lagoon.c (urbit/numerics#75) is exactly that combination (int2 + the #1057 fix set), compile-verified — modulo its desk-specific %mod semantics, which upstream should not take.

🤖 Generated with Claude Code

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