Skip to content

shader: lower v_sin_f16 and v_cos_f16 in direct Compute DXIL - #3300

Merged
JohnConnorNPC merged 3 commits into
R0Y00:mainfrom
legacy-code-archaeologist:shader/direct-sin-cos-f16
Nov 29, 2026
Merged

JohnConnorNPC merged 3 commits into
R0Y00:mainfrom
legacy-code-archaeologist:shader/direct-sin-cos-f16

Conversation

@legacy-code-archaeologist

@legacy-code-archaeologist legacy-code-archaeologist commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Lowers VOP1 (0x60/0x61), VOP3 (0x1e0/0x1e1), and SDWA v_sin_f16 and v_cos_f16 in the direct Compute DXIL path via binary16_unary() and a shared DirectCompute::trig_turns() helper.

Fixes #3298. Refs #2935.

Changes

  • src/gfx1013_compute_native.cpp (binary16_unary()): Add v_sin_f16 and v_cos_f16, enabling direct binary16 unary decode/SDWA subword handling, MODE checking, and DPP form refusal.
  • src/gfx1013_direct_compute.inl (trig_turns(), float_alu()):
    • Extract trig_turns(bool cosine, const string &a) shared by v_sin_f32/v_cos_f32 and v_sin_f16/v_cos_f16: reduces in turns with round-to-nearest-even (part = a - Round_ne(a), DXIL op 26, |part| <= 0.5, signed zero preserved) before converting to radians (* 2*pi), and forces exact +0.0 at |part| == 0.5 for sine and |part| == 0.25 for cosine.
    • Lower v_sin_f16 / v_cos_f16 through trig_turns() + packed_half_rne(), mapping ±Inf and NaN (|source| >= 0x7c00) to 0x7e00, preserving -0.0 (0x8000) on v_sin_f16, and keeping destination D[31:16] unchanged on VOP1/VOP3 (or applying SDWA PRESERVE/PAD).
  • tests/native_sin_cos_f16_check.py, tests/cmake/native_sin_cos_f16.cmake, docs/experiments/native-sin-cos-f16.md: Add ir, controls, and hardware checks covering 37 cases (VOP1, VOP3 abs/neg including sin with neg, SDWA sin with ABS/NEG on a negative half, SDWA WORD_0/WORD_1 PRESERVE/PAD, inline float/int, literal, SGPR, VGPR overlap, ±0, ±0.25, ±0.5, ±0.75, -1.75, 2048.0, 65504.0, ±Inf, qNaN, sNaN, subnormals, general angles) and 8 negative refusal cases.

Verification

On 6a48b7e2 (rebased onto main ff9dcef0; the conflicts with #3425 v_log_f16 keep both sides):

  • ctest --preset dev --no-tests=error -R '^native_sin_cos_f16_' -V:
    • native_sin_cos_f16_ir: Passed, 37 cases matched bit for bit and 8 refusals verified.
    • native_sin_cos_f16_controls: Passed, 37 cases compiled to DXIL and 8 refusals verified.
    • native_sin_cos_f16_hardware: Skipped (77). The local adapter does not report SM 6.6 with wave32, so the readback is unmeasured here.
  • ctest --preset dev --no-tests=error -R '^native_.*_(controls|ir)$|^native_sin_cos' -j 2: 138/138 passed. native_f16_alu_ir used v_sin_f16 as its "direct DXIL lowering" refusal control. That control is removed because this PR lowers the op, as compute: lower binary16 VOP1 v_log_f16 in the direct compute path #3425 did for v_log_f16.
  • python -B tests/docs_links_check.py: passed (179 Markdown files, 718 links).
  • The ir mode runs on a model host. That host keeps NaN payloads (also through legacyF16ToF32), returns the negative default NaN 0xffc00000 for Inf - Inf, and makes Sin of a zero angle return +0. Mutants of native_sin_cos_f16_ir, each rebuilt and then restored:
    1. Remove the NaN select: fails.
    2. Change its uge to ugt: fails.
    3. Remove the v_sin_f16 -0.0 select: fails.
  • The hardware tolerance is 2^-20 absolute plus two binary16 ulp of the expected value (it was 0.001). NaN, ±0 and ±1 are compared exactly. No documented DXIL Sin/Cos error bound was found, so the doc states this tolerance as an assumption.

AI-assisted.

🤖 Generated with Claude Code

@Leyukaka

Copy link
Copy Markdown
Collaborator

I reviewed the diff (not run). Three notes:

  • The rounding op. trig_turns computes part = a - op26(a). DXIL op 26 is Round_ne: the repository maps v_rndne_f32 to 26 and v_floor_f32 to 27. That call is inherited from the v_sin_f32/v_cos_f32 path on main. What is new here describes it as truncation: the docs, the math.trunc reference model, and interpret_trig in the IR interpreter.
    • With round-to-nearest, |part| <= 0.5, so the |part| == 0.75 test for cos can never be true. The 0.25 test still catches -0.75 turns, which reduce to +0.25.
    • The "kills drop 0.75" comment on cos_3qtr_pos holds only under the trunc model. On hardware, +0.75 reduces to -0.25.
    • sin and cos are periodic, so both reductions are mathematically equivalent. But the radians argument differs (0.75 against -0.25 turns), so bit-identical results are not guaranteed, and the ir test checks the IR against trunc semantics.
  • The test tolerance. The hardware check accepts abs(got - want) <= 0.001. That is about 2 binary16 ULP near sin = 0.95, 16 ULP near 0.063, and 262 ULP near 0.0061 (2^-10 turns). The PR's own sin_subnormal case expects about 3.7e-7, so the check accepts any result up to 0.001 in magnitude, including 0.
  • The accuracy bound. The docs do not state an error bound for these ops. In compute: lower binary16 VOP1 rcp, sqrt, exp and rsq #3220 the maintainer asked for a documented host error bound before relying on host transcendental functions for binary16 results; for exp and rsq that bound came from the shader model 4 assembly references (docs/experiments/f16-alu-direct.md). Saying which bound applies to DXIL Sin/Cos, and comparing it with the RDNA2 accuracy for V_SIN_F16/V_COS_F16, would show whether this needs the same treatment.

AI-assisted.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Thanks for the PR. I built it at 475a232 (clang-cl cross build, run under Wine, no GPU). The ir and controls modes both pass: 34 cases matched; 8 refusals verified and compiled 34 cases to DXIL; verified 8 refusals. A few requests, building on the comment above:

  1. Op 26 reduction. I confirm the point above. The translator maps v_rndne_f32 to unary(26, a) and v_trunc_f32 to 29, so part = a - unary(26, a) gives |part| <= 0.5.
    The new |part| == 0.75 clause in trig_turns() can therefore never be true, and it changes nothing for v_cos_f32 or v_cos_f16.
    Could you make expected_trig_f16, interpret_trig and the doc use round-to-nearest-even, and drop the clause (or use op 29 if truncation was intended)?
    I recomputed all 34 expected values with a round-to-nearest-even reduction and none change, so the fix is cheap.
  2. cos_3qtr_pos ("kills drop 0.75"). Deleting the 0.75 clause leaves native_sin_cos_f16_ir green even under the current trunc model.
    cos(0.75 turn) is 1.19e-08 in binary32, which rounds to 0x0000 with or without the clause. Please drop or reword that comment.
  3. Smaller.
    • The |source| >= 0x7c00 -> 0x7e00 select is unpinned. Removing it, or changing uge to ugt, still passes, because same_binary16 accepts any NaN and Inf - round(Inf) is already NaN. Either compare the NaN bits or drop "canonical" from the doc.
    • Each -0.0 path is unpinned on its own. Removing either the v_sin_f16 override or the a == 0 select in trig_turns() still passes; only removing both fails at sin_neg_zero.

This comment was AI-assisted.

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 475a2325 by its diff. The points from 06:48 and 06:56 are still open; one more on the tests:

  • neg is only tested on cos (cos_0125_neg, diff:289). cos(-x) = cos(x), so dropping the neg modifier for these ops keeps the case green.
  • The SDWA ABS case is cos of +0.5 (cos_sdwa_w0_pad, diff:299), where abs changes nothing; no SDWA case sets NEG.

Fix: a VOP3 sin case with neg=1 (e.g. +0.125 → -0.7071), and SDWA sin cases on a negative selected half, one with ABS and one with NEG. (sin_0375_abs already covers VOP3 abs.)

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Review of 475a2325 against main 77d1ba4d. Verdict: hold. Nothing has changed since Leyukaka's change request and my 06:56 comment on this same head, and it now conflicts with main.

MUST

  1. Please rebase. git merge-tree --write-tree --name-only origin/main 475a2325 → CONFLICT (content) in src/gfx1013_compute_native.cpp and src/gfx1013_direct_compute.inl. Both conflicts are against compute: lower binary16 VOP1 v_log_f16 in the direct compute path #3425 (v_log_f16, 2397aad3):
    • binary16_unary(): main has || name == "v_log_f16", this PR has || name == "v_sin_f16" || name == "v_cos_f16".
    • The unary block: main has const bool ... log = n == "v_log_f16"; and its special-result else if (log) arm, next to this PR's trig flag and arm.
      Keeping both sides looks mechanical, with log kept in the rsq ? 25 : exp ? 21 : log ? 23 : 24 opcode choice.
  2. Leyukaka's change request (a VOP3 sin case with neg, and SDWA sin cases with ABS and NEG on a negative half) is still open.

SHOULD

  1. My 06:56 points 1-3 are still open: the op 26 (Round_ne) model in expected_trig_f16/interpret_trig/docs, the dead |part| == 0.75 clause, and the unpinned NaN and -0 selects. The opcodes themselves match the repo mapping: unary_f32(cosine ? 12 : 13, radians) is DXIL Cos/Sin, unary_f32(6, part) is FAbs, and 0x401921FB60000000 is 2π as f32.

AI-assisted.

Adds v_sin_f16 and v_cos_f16 to binary16_unary() and shares a DirectCompute::trig_turns() helper with v_sin_f32/v_cos_f32, rounding via packed_half_rne() while preserving D[31:16], -0.0 on v_sin_f16, exact quarter/half-turn zeros, and Inf/NaN -> 0x7e00.

Fixes R0Y00#3298. Refs R0Y00#2935.

AI-assisted.
- Model the reduction as DXIL Round_ne (op 26) in the reference, the IR
  interpreter and the docs, and drop the |part| == 0.75 cos clause, which
  can never hold with |part| <= 0.5.
- Compare every bit in the ir mode on a model host that keeps NaN
  payloads, answers Inf - Inf with a negative default NaN and returns +0
  from Sin of a zero angle, so the NaN select, its uge test and the sin
  -0 select are each pinned.
- Add a VOP3 sin case with neg and SDWA sin cases with ABS and NEG on a
  negative half.
- Replace the 0.001 hardware tolerance with 2^-20 absolute plus two
  binary16 ulp and document that no DXIL Sin/Cos bound was found.
- Drop native_f16_alu's v_sin_f16 refusal control, which this lowering
  replaces (as R0Y00#3425 did for v_log_f16).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

Thanks @Leyukaka and @JohnConnorNPC. I rebased onto main ff9dcef0 and pushed 6a48b7e2. The #3425 conflicts keep both sides: log stays in the rsq ? 25 : exp ? 21 : log ? 23 : 24 choice, next to trig.

Change request

  • New VOP3 case sin_0125_neg: +0.125 with neg gives -0.7071.
  • New SDWA sin cases on a negative half, sin_sdwa_w1_abs (WORD_1 = -0.125 with ABS) and sin_sdwa_w0_neg (WORD_0 = -0.125 with NEG). sin is odd, so dropping either modifier flips the sign.

Op 26

  • The reference, interpret_trig and the docs now use round-to-nearest-even: |part| <= 0.5, and +0.75 turns reduces to -0.25.
  • The |part| == 0.75 cos clause is removed from trig_turns(), and the cos_3qtr_pos comment is reworded.
  • As JohnConnorNPC computed, no expected value changed.

Unpinned selects

  • ir now compares every bit. interpret_trig runs on a model host that:
    • keeps NaN payloads, also through legacyF16ToF32;
    • returns the negative quiet NaN 0xffc00000 for Inf - Inf, as x86 SSE does;
    • has Sin of a zero angle return +0.
  • D3D fixes none of these. On that host each special select is needed: removing the NaN select, changing uge to ugt, or removing the v_sin_f16 -0.0 select each fails native_sin_cos_f16_ir. I rebuilt and ran each mutant, then restored the source.
  • The a == 0 select in trig_turns() is the shared v_sin_f32 code from main. For binary16 the -0.0 select covers the same input, so it is not pinned separately here. The doc says so.

Tolerance and bound

  • The hardware check now accepts 2^-20 absolute plus two binary16 ulp of the expected value, instead of 0.001. NaN, ±0 and ±1 stay exact.
  • I found no documented error bound for DXIL Sin/Cos. Section 3.1 of the D3D11.3 functional spec has none, and I could not read the instruction rows. The doc therefore states the tolerance as an assumption.
  • It also states that near a zero crossing only the absolute part applies, so sin_subnormal cannot be told from 0. The RDNA2 V_SIN_F16 accuracy is not compared.
  • The readback stays unmeasured on this machine (77).

Other

AI-assisted.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

I re-reviewed 6a48b7e2 against 475a2325 by its diff, using git range-diff and git diff 063d4e1a 6a48b7e2. I did not run it.

Rebase. The first commit keeps both sides of the #3425 change. The trig block has its own magnitude, and v_sin_f32/v_cos_f32 now call the shared trig_turns(). That helper is the same code as on main, minus the dead 0.75 clause.

Change request (18:50). All three requested cases are now in the file. I computed their expected values with the RNE reference:

  • sin_0125_neg → 0xb9a8.
  • sin_sdwa_w1_abs → 0x39a8, or 0xb9a8 without ABS.
  • sin_sdwa_w0_neg → 0x39a8, or 0xb9a8 without NEG.

The unselected half is 0.5 turns, which gives sin = +0, so these cases also catch a wrong word select.

Earlier points.

  • Op 26 is now modelled as Round_ne in the reference (round, half-even), in interpret_trig (copysign(round(x), x)) and in the doc. The clause and the comment are gone.
  • ir now compares every bit on a model host. That host keeps NaN payloads, returns 0xffc00000 for Inf - Inf and gives Sin(±0) = +0. On it, the NaN select, uge and the v_sin_f16 −0 select are each observable.
  • The tolerance is two binary16 ulp of the expected value plus 2^-20, and the ulp formula handles subnormals. The doc presents the bound as an assumption.
  • The trig_turns comment now says "half a turn".

Still open (NIT). native_sin_cos_f16_ir is guarded only by if(EXISTS "${INTAKE_PYTHON}"). 23 sibling guards also require TARGET native_compute_ir_dump; only native_global_store.cmake doesn't.

Not measured. CI does not run these tests: grep -c native_sin_cos_f16 over the run log gives 0, with 100% tests passed out of 82. Neither side has measured the hardware readback. Since #3449, native shaders are the default path, so a translated shader that uses these ops now runs this lowering instead of being refused.

I found no blocking issues on this head. It still needs the change request re-reviewed on 6a48b7e2.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor Author

Thanks @JohnConnorNPC. The NIT is fixed in 3a8aaf24: native_sin_cos_f16_ir is now guarded by TARGET native_compute_ir_dump AND EXISTS "${INTAKE_PYTHON}", like its siblings. After a reconfigure, ctest --preset dev --no-tests=error -R '^native_sin_cos_f16_' gives ir and controls passed and hardware skipped (77). The hardware readback is still unmeasured here: this PC's only D3D12 FL 12_0 adapter does not report SM 6.6 with wave32.

AI-assisted.

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3a8aaf24 by its diff; this replaces the changes request on 475a2325.

  • The three test points are covered: sin_0125_neg (VOP3 neg, tests/native_sin_cos_f16_check.py:151), sin_sdwa_w1_abs (:163) and sin_sdwa_w0_neg (:164), both on a negative selected half.
  • The 06:48 points are covered: the reference and the ir model host use round half to even for op 26, the 0.75 cos clause is gone, and the hardware tolerance is 2^-20 absolute plus two binary16 ulp instead of 0.001, stated in the doc as an assumption.
  • trig_turns() is the v_sin_f32/v_cos_f32 body moved unchanged.
  • Measured on 3a8aaf24, dev preset, -DX2W_WITH_OPUS=OFF: ctest --preset dev --no-tests=error -R '^native_sin_cos_f16_(ir|controls|hardware)$|^native_f16_alu' → 5/5 passed. native_sin_cos_f16_hardware ran instead of exiting 77: "74 words matched with D[31:16] preserved and guards" on NVIDIA GeForce RTX 5080 (10de:2c02), driver 32.0.16.2101. native_f16_alu_hardware passed on the same adapter.

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.

shader: lower v_sin_f16 and v_cos_f16 in direct Compute DXIL

3 participants