shader: lower v_sin_f16 and v_cos_f16 in direct Compute DXIL #3300
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3300"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-28T06:21:02Z.
Lowers VOP1 (
0x60/0x61), VOP3 (0x1e0/0x1e1), and SDWAv_sin_f16andv_cos_f16in the direct Compute DXIL path viabinary16_unary()and a sharedDirectCompute::trig_turns()helper.Fixes #3298. Refs #2935.
Changes
src/gfx1013_compute_native.cpp(binary16_unary()): Addv_sin_f16andv_cos_f16, enabling direct binary16 unary decode/SDWA subword handling,MODEchecking, and DPP form refusal.src/gfx1013_direct_compute.inl(trig_turns(),float_alu()):trig_turns(bool cosine, const string &a)shared byv_sin_f32/v_cos_f32andv_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.0at|part| == 0.5for sine and|part| == 0.25for cosine.v_sin_f16/v_cos_f16throughtrig_turns()+packed_half_rne(), mapping±InfandNaN(|source| >= 0x7c00) to0x7e00, preserving-0.0(0x8000) onv_sin_f16, and keeping destinationD[31:16]unchanged on VOP1/VOP3 (or applying SDWAPRESERVE/PAD).tests/native_sin_cos_f16_check.py,tests/cmake/native_sin_cos_f16.cmake,docs/experiments/native-sin-cos-f16.md: Addir,controls, andhardwarechecks covering 37 cases (VOP1, VOP3abs/negincluding sin withneg, SDWA sin withABS/NEGon a negative half, SDWAWORD_0/WORD_1PRESERVE/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 ontomainff9dcef0; the conflicts with #3425v_log_f16keep 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_irusedv_sin_f16as its "direct DXIL lowering" refusal control. That control is removed because this PR lowers the op, as #3425 did forv_log_f16.python -B tests/docs_links_check.py: passed (179 Markdown files, 718 links).irmode runs on a model host. That host keeps NaN payloads (also throughlegacyF16ToF32), returns the negative default NaN0xffc00000forInf - Inf, and makes Sin of a zero angle return+0. Mutants ofnative_sin_cos_f16_ir, each rebuilt and then restored:ugetougt: fails.v_sin_f16-0.0select: fails.AI-assisted.
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-28T06:48:31Z.
I reviewed the diff (not run). Three notes:
trig_turnscomputespart = a - op26(a). DXIL op 26 isRound_ne: the repository mapsv_rndne_f32to 26 andv_floor_f32to 27. That call is inherited from thev_sin_f32/v_cos_f32path onmain. What is new here describes it as truncation: the docs, themath.truncreference model, andinterpret_trigin the IR interpreter.|part| <= 0.5, so the|part| == 0.75test for cos can never be true. The0.25test still catches -0.75 turns, which reduce to +0.25.cos_3qtr_posholds only under the trunc model. On hardware, +0.75 reduces to -0.25.irtest checks the IR against trunc semantics.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 ownsin_subnormalcase expects about 3.7e-7, so the check accepts any result up to 0.001 in magnitude, including 0.docs/experiments/f16-alu-direct.md). Saying which bound applies to DXILSin/Cos, and comparing it with the RDNA2 accuracy forV_SIN_F16/V_COS_F16, would show whether this needs the same treatment.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by JohnConnorNPC on 2026-11-28T06:56:19Z.
Thanks for the PR. I built it at
475a2325(clang-cl cross build, run under Wine, no GPU). Theirandcontrolsmodes both pass:34 cases matched; 8 refusals verifiedandcompiled 34 cases to DXIL; verified 8 refusals. A few requests, building on the comment above:v_rndne_f32tounary(26, a)andv_trunc_f32to 29, sopart = a - unary(26, a)gives|part| <= 0.5.The new
|part| == 0.75clause intrig_turns()can therefore never be true, and it changes nothing forv_cos_f32orv_cos_f16.Could you make
expected_trig_f16,interpret_trigand 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.
cos_3qtr_pos("kills drop 0.75"). Deleting the 0.75 clause leavesnative_sin_cos_f16_irgreen even under the current trunc model.cos(0.75 turn) is 1.19e-08 in binary32, which rounds to
0x0000with or without the clause. Please drop or reword that comment.|source| >= 0x7c00 -> 0x7e00select is unpinned. Removing it, or changingugetougt, still passes, becausesame_binary16accepts any NaN andInf - round(Inf)is already NaN. Either compare the NaN bits or drop "canonical" from the doc.-0.0path is unpinned on its own. Removing either thev_sin_f16override or thea == 0select intrig_turns()still passes; only removing both fails atsin_neg_zero.This comment was AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-28T18:50:44Z.
Review: CHANGES_REQUESTED
Reviewed
475a2325by its diff. The points from 06:48 and 06:56 are still open; one more on the tests:negis 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.ABScase is cos of +0.5 (cos_sdwa_w0_pad, diff:299), whereabschanges nothing; no SDWA case setsNEG.Fix: a VOP3 sin case with
neg=1(e.g. +0.125 → -0.7071), and SDWA sin cases on a negative selected half, one withABSand one withNEG. (sin_0375_absalready covers VOP3abs.)Imported from R0Y00/Xbox2Windows. Originally posted by JohnConnorNPC on 2026-11-29T12:55:31Z.
Review of
475a2325against main77d1ba4d. 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
git merge-tree --write-tree --name-only origin/main 475a2325→CONFLICT (content)insrc/gfx1013_compute_native.cppandsrc/gfx1013_direct_compute.inl. Both conflicts are against #3425 (v_log_f16,2397aad3):binary16_unary(): main has|| name == "v_log_f16", this PR has|| name == "v_sin_f16" || name == "v_cos_f16".const bool ... log = n == "v_log_f16";and its special-resultelse if (log)arm, next to this PR'strigflag and arm.Keeping both sides looks mechanical, with
logkept in thersq ? 25 : exp ? 21 : log ? 23 : 24opcode choice.sincase withneg, and SDWAsincases withABSandNEGon a negative half) is still open.SHOULD
Round_ne) model inexpected_trig_f16/interpret_trig/docs, the dead|part| == 0.75clause, and the unpinned NaN and-0selects. The opcodes themselves match the repo mapping:unary_f32(cosine ? 12 : 13, radians)is DXIL Cos/Sin,unary_f32(6, part)is FAbs, and0x401921FB60000000is 2π as f32.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-29T21:06:38Z.
Thanks @Leyukaka and @JohnConnorNPC. I rebased onto
mainff9dcef0and pushed6a48b7e2. The #3425 conflicts keep both sides:logstays in thersq ? 25 : exp ? 21 : log ? 23 : 24choice, next totrig.Change request
sin_0125_neg: +0.125 withneggives -0.7071.sin_sdwa_w1_abs(WORD_1 = -0.125 withABS) andsin_sdwa_w0_neg(WORD_0 = -0.125 withNEG). sin is odd, so dropping either modifier flips the sign.Op 26
interpret_trigand the docs now use round-to-nearest-even:|part| <= 0.5, and +0.75 turns reduces to -0.25.|part| == 0.75cos clause is removed fromtrig_turns(), and thecos_3qtr_poscomment is reworded.Unpinned selects
irnow compares every bit.interpret_trigruns on a model host that:legacyF16ToF32;0xffc00000forInf - Inf, as x86 SSE does;+0.ugetougt, or removing thev_sin_f16-0.0select each failsnative_sin_cos_f16_ir. I rebuilt and ran each mutant, then restored the source.a == 0select intrig_turns()is the sharedv_sin_f32code frommain. For binary16 the-0.0select covers the same input, so it is not pinned separately here. The doc says so.Tolerance and bound
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.sin_subnormalcannot be told from 0. The RDNA2V_SIN_F16accuracy is not compared.Other
native_f16_alu_irusedv_sin_f16as its "direct DXIL lowering" refusal control. This PR lowers that op, so I removed the control, as #3425 did forv_log_f16.^native_.*_(controls|ir)$: 138/138 passed.AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by JohnConnorNPC on 2026-11-29T21:27:19Z.
I re-reviewed
6a48b7e2against475a2325by its diff, usinggit range-diffandgit 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, andv_sin_f32/v_cos_f32now call the sharedtrig_turns(). That helper is the same code as onmain, minus the dead0.75clause.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, or0xb9a8withoutABS.sin_sdwa_w0_neg→0x39a8, or0xb9a8withoutNEG.The unselected half is 0.5 turns, which gives sin = +0, so these cases also catch a wrong word select.
Earlier points.
round, half-even), ininterpret_trig(copysign(round(x), x)) and in the doc. The clause and the comment are gone.irnow compares every bit on a model host. That host keeps NaN payloads, returns0xffc00000for Inf - Inf and gives Sin(±0) = +0. On it, the NaN select,ugeand thev_sin_f16−0 select are each observable.trig_turnscomment now says "half a turn".Still open (NIT).
native_sin_cos_f16_iris guarded only byif(EXISTS "${INTAKE_PYTHON}"). 23 sibling guards also requireTARGET native_compute_ir_dump; onlynative_global_store.cmakedoesn't.Not measured. CI does not run these tests:
grep -c native_sin_cos_f16over the run log gives 0, with100% 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.
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-29T21:31:44Z.
Thanks @JohnConnorNPC. The NIT is fixed in
3a8aaf24:native_sin_cos_f16_iris now guarded byTARGET 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.
Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T21:39:06Z.
Review: APPROVED
Reviewed
3a8aaf24by its diff; this replaces the changes request on475a2325.sin_0125_neg(VOP3neg,tests/native_sin_cos_f16_check.py:151),sin_sdwa_w1_abs(:163) andsin_sdwa_w0_neg(:164), both on a negative selected half.irmodel host use round half to even for op 26, the0.75cos 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 thev_sin_f32/v_cos_f32body moved unchanged.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_hardwareran 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_hardwarepassed on the same adapter.