Skip to content

fix(sharc): correct sat/rnd mr decode and keep 64-bit denormals exact - #982

Open
m-dwyer wants to merge 5 commits into
mainfrom
fix/sharc-fz-double-ops
Open

m-dwyer wants to merge 5 commits into
mainfrom
fix/sharc-fz-double-ops

Conversation

@m-dwyer

@m-dwyer m-dwyer commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

Two bugs in the generated SHARC core

1. SAT and RND multiplier rows decoded to the wrong accumulator. PGR Table 12-5 has bit 2 = destination is an accumulator and bit 1 = source is MRB; sharc_core/compute_mult.py had them reversed. Twelve opcodes were wrong (0x02-05, 0x0a-0d, 0x1a-1d). No stock code runs them (the DN2 hits are lookup-table data), so stock audio and state do not change. The fast tier lowers only Rn = SAT MRF (SF), which was right.

2. AArch64 FPCR.FZ flushed 64-bit denormals. fadd, fsub and fmul serve the 32-bit float emulation (hot) and the 64-bit float unit. The first version of this PR put a result check on the shared helpers. Now the 64-bit paths (_double_binary, mult_double_multiply_single) call their own checked dadd, dsub, dmul (sharc-rt/src/rt/fz/double.rs) through the boundary functions floats._d_add, _d_sub, _d_mul; the shared helpers are origin/main's code again. 32-bit operands are zero or at least 2^-126 and every chain is one op deep, so no 32-bit result can reach 2^-1022. The black_box and opaque-bit-read lessons stay in the checked helpers.

Regeneration

out/ of a lane is a symlink to the main tree's, so the cores were regenerated into dist/regen/ of the lane (main tree's gens untouched), CORE_FINGERPRINT 3f80d4b0..., from the PROVENANCE.txt recipes: DN2 sharc_rsgen dn2-1.11 (send-FX coverage, 2789 blocks), DT2 dt2-1.16 (r4 coverage, 1241 blocks), shared core by sharc_transpile --strict. Blocks identical to the 2026-11-15 gens; only core_g.rs, core_i.rs, tables and fingerprint stamps differ. The main tree's gens still need promoting by whoever owns out/ (finding 188).

Tests

  • tests/sharc/semantics/test_sharc_compute_mr_decode.py: all eight SAT/RND forms in every format, in and out of range, positive and negative, against values worked out from the manual; a decoy in the other accumulator; the named destination only. Two existing oracle tests used the wrong bits and are corrected.
  • sat_rows_use_the_named_source_and_destination (sharc-native, generated core): R5 = SAT MRB (SI), MRF = SAT MRF (SF).
  • checked_double_ops_keep_denormals_under_fz (debug and --release) and shared_helpers_on_f32_origin_operands_ignore_fz (pins the range argument).

Measurement (sharc_cells, one host binary, A = main gens, B = regenerated, 20/24 s)

Device A B B/A PCM, DSP state, CF state
DN2 trig1-hold 33.811 G 33.791 G 0.9994 equal
DT2 dt2-sample-trig 25.043 G 25.024 G 0.9992 equal

Gate

  • mise run test:changed: 19 unit and static tasks passed; rig preflight 17 halves, 0 problems.
  • Device proof (once, with the regenerated gens via SHARC_GEN_DIR_DN2/_DT2): pnpm nx:pilot run-many -t smoke,pins -p gate-smoke,gate-pins: 13 ok, 0 failed, 800 s.
  • test:all not run yet; at merge the gate adds the smoke set.

Finding: docs/findings/188-sharc-sat-rnd-decode-and-f64-fz.md [V].

🤖 Generated with Claude Code

m-dwyer and others added 5 commits December 6, 2026 19:19
AArch64 FPCR.FZ flushed denormal operands and results of the core's 64-bit
fadd/fsub/fmul/fdiv/sqrt. They now detect a denormal operand or a flushed
result and recompute on a cold path with FZ suspended. Zero operands stay
on the fast path, and the denormal test uses integer compares because a
float compare reads a denormal as zero under FZ. The f32 paths are
unchanged. A differential test (1408 mismatches before) covers it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Test the native result with one multiply and one compare (its square is
zero, denormal or NaN) and recompute exactly on the cold path. A flushed
denormal operand moves a sum or difference only when the result is tiny;
products, quotients and roots with one give zero or NaN, and a denormal
divisor is tested in fdiv. An exact zero result (an operand zero, or
x - x) returns inline instead of calling the cold path: it is the common
case. Operand bits are read with fmov, and the recomputation fences its
arithmetic with black_box: in an optimized build LLVM turned the bit
tests into float compares and moved the division before the FPCR write.
The differential test gains infinity, NaN and values around the
thresholds, and passes optimized as well as unoptimized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hecked helpers

The multiplier SAT and RND rows had the destination and source bits swapped:
PGR Table 12-5 has bit 2 = destination is MR and bit 1 = source is MRB, the
oracle had the reverse, so 12 opcodes (0x02-05, 0x0a-0d, 0x1a-1d) decoded to the
wrong accumulator or destination. Oracle tests now cover all eight forms in
every format against values worked out from the manual, and a generated-core
test checks R5 = SAT MRB (SI) and MRF = SAT MRF (SF).

The FZ check on the shared f64 helpers is removed again: they serve the 32-bit
emulation, where no operand or result can reach the double denormal range. The
64-bit ops (_double_binary, double multiply by single) call their own checked
dadd, dsub and dmul (rt/fz/double.rs), through the boundary functions
floats._d_add, _d_sub and _d_mul. A test pins the range argument: f32-origin
operands give the same results with and without FZ.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…gap on 64-bit floats

Finding 188: the 12 wrong multiplier opcodes (PGR Table 12-5), the 64-bit float
helpers split from the shared 32-bit ones, the regeneration (recipes, fingerprint)
and the paired counter measurement (B/A 0.999, PCM and state equal).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@m-dwyer
m-dwyer force-pushed the fix/sharc-fz-double-ops branch from c7b1c9c to 8fae3fc Compare December 6, 2026 10:05
@m-dwyer m-dwyer changed the title fix(sharc): keep denormal doubles exact under flush-to-zero fix(sharc): correct sat/rnd mr decode and keep 64-bit denormals exact Dec 6, 2026

This branch has not been deployed

No deployments
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