Skip to content

twoc: two's-complement jets (scalar + Lagoon %int2 arrays)#66

Merged
sigilante merged 1 commit into
mainfrom
sigilante/twoc-jets
Jun 29, 2026
Merged

twoc: two's-complement jets (scalar + Lagoon %int2 arrays)#66
sigilante merged 1 commit into
mainfrom
sigilante/twoc-jets

Conversation

@sigilante

Copy link
Copy Markdown
Collaborator

Jets for the /lib/twoc two's-complement integer library — scalar ops plus Lagoon %int2 element-wise array ops. The runtime counterpart is urbit/vere (companion PR, linked below).

What's here

  • Hoon hints on /lib/twoc ++twid (12 hot-path arms: add sub neg mul div rem pow abs gth lth lte gte). All logic lives in the width-keyed ++twid door; the bloq-keyed ++twoc facade re-exports its gates, so a single %twid registration fires for both twid-direct (/lib/fixed) and facade (/lib/unum, Lagoon) callers.
  • Master C jets (mirrored by hand into vere): twoc.c dispatches on width — native c3 (c3_d ≤64, __int128 ≤128) for the bloq widths, GNU MP (already vendored) for arbitrary precision >128; declines to Hoon on out-of-width operands and zero divisors. Shared kernels in twoc.h.
  • Lagoon %int2 array jets (add/sub/mul/div/mod) in lagoon.c, dispatched on ray kind exactly like %i754, reusing the twoc.h kernels — one linear C pass per array op instead of the super-linear Hoon bit-packing loop.
  • Validation harnesses (libmath/tools/twoc) and a vere reference (libmath/vere/README-twoc.md).

Design notes

  • No vendored library (unlike SoftBLAS/SoftUnum): integer arithmetic is exact natively, so there's no reproducibility forcing-function; the kernels are tiny and shared via a header.
  • %int2 array lanes are always machine widths (8/16/32/64/128), so the array path is entirely native (no GMP).

Validation

  • Standalone vs an independent two's-complement reference: 8280 native + 5692 GMP checks, all sign edges, pass (libmath/tools/twoc).
  • Bit-exact on a hoon-135 fakezod. Scalar firing proven two ways (sentinel + timing). %int2 array ops verified jet == Hoon for all five ops; ~25,600× (20k lanes) to ~310,000× (200k) over interpreted (the Hoon array path is super-linear, the jet is O(N)).

🤖 Generated with Claude Code

Jet-hint /lib/twoc ++twid (12 hot-path arms: add sub neg mul div rem pow abs
gth lth lte gte).  All logic lives in the width-keyed ++twid door; the
bloq-keyed ++twoc facade re-exports its gates, so a single %twid registration
fires for both twid-direct (/lib/fixed) and facade (/lib/unum, Lagoon) callers.

Master C jet twoc.c dispatches on width: native c3 types (c3_d <=64,
__int128 <=128) for the bloq widths, GNU MP (already vendored) for arbitrary
precision >128; declines to Hoon on out-of-width operands and zero divisors.
Shared kernels factored into twoc.h.

Lagoon %int2 element-wise array jets (add/sub/mul/div/mod) added to lagoon.c,
dispatched on ray kind like %i754 and reusing the twoc.h kernels -- one linear
C pass per array op instead of the super-linear Hoon bit-packing loop.

Validated standalone (libmath/tools/twoc, 8280 native + 5692 GMP checks) and
bit-exact on a hoon-135 fakezod; %int2 array add ~25,600x (20k lanes) to
~310,000x (200k) over interpreted.  Vere reference: libmath/vere/README-twoc.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@sigilante

Copy link
Copy Markdown
Collaborator Author

Runtime counterpart: urbit/vere#1047

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