fix(shader): compute V_FRACT_F32 from the exact fraction of |x| #1250
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1250"
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 boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-07T19:47:06Z.
What
EmitFPFract32emitsGLSLstd450Fract. Vulkan specifiesFractas correctly rounded in the implementation's rounding mode, which may round down. Intel'sfrcdoes, on the Windows driver and on Mesa ANV:The input is
0xba85a805. Its exact fraction is 0.99898028315…, so rounding to nearest gives0x3f7fbd2c. On the Windows driverFractstays at0x3f7fbd2beven when the module declaresRoundingModeRTE 32.Emitting
x - floor(x)(the first version of this PR) is not enough. Whenfloor(x)has a single use, Mesa folds the subtraction back intoffracteven withNoContraction(nir_opt_algebraic.py, a rule without~), and ANV emitsfrcagain. See @hashaam1217's comment below.V_FRACT_F32now emitsfract(|x|), which is exact: the fraction of a non-negative float is representable, so there is nothing to round. For a negative non-integerxit returns1 - fract(|x|), a single correctly rounded subtraction with the same value asx - floor(x). Edge cases:-0.0and negative integers give+0.0.unaryFloatSpecials.1 - frounds to 1.0 is clamped to0x3f7fffffthere.V_FRACT_F16takes the same path. The formula is @hashaam1217's.Source of the semantics:
ffractassrc0 - floorf(src0)(src/compiler/nir/nir_opcodes.py), and ACO emitsv_fract_f32for it (src/amd/compiler/instruction_selection/aco_select_nir_alu.cpp,nir_op_ffract).fsub(x, floor(x)), clamped below 1.0 with the infinity check kept, tov_fract_*(llvm/test/CodeGen/AMDGPU/fract-match.ll).Not measured with the hardware oracle. The result follows the f32 rounding mode like any other f32 op (#981).
Tested
Windows 11 (26200), WinLibs GCC 15.2.0 r7, CI configure flags, main at
4764e59c,ctest --output-on-failure --timeout 120 --repeat until-fail:3:ANYPS5_GPU=Intel:agc_driver_vop1_float_unaryandagc_driver_f32_result_clamppass;vop1_float_unaryfails on main.shaderFloat64, noshaderDeviceClock, compute subgroup width #1185, 64-bit atomics #1213, f16 denormals #981,OpSMulExtended#1246). None are new.vop1_float_unary(comment below).Checklist
main; no other open PR implements the same thing🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by hashaam1217 on 2026-10-08T01:41:54Z.
Checked this on Linux: Intel Iris Xe (Alder Lake GT2), Mesa 25.2.8 ANV, at
2cc5735d.vop1_float_unaryandf32_result_clamppass with this change (main fails with the samelane 13 fract_f32 v5 is 0x3f7fbd2b, expected 0x3f7fbd2c), and NVIDIA is unaffected.On Mesa, though, the test passes only because
Vop1FloatUnaryalso runsv_floor_f32on the same registers. NIR merges the twoffloors, so the floor has two uses. Whenfloor(x)has a single use, Mesa foldsx - floor(x)back intoffracteven withNoContraction(nir_opt_algebraic.pyL480, which has no~). ANV then emitsfrc, the instruction that rounds down.Standalone check: one SPIR-V module per row, compiled with glslang and dispatched 1×1×1 on input
0xba85a805(correctly rounded result0x3f7fbd2c). The NIR and ISA columns are fromINTEL_DEBUG=cs:fract(x)ffract→frc0x3f7fbd2b0x3f7fbd2cprecise float r = x - floor(x);(the SPIR-V this PR emits:OpFSub+NoContraction)ffract!→frc0x3f7fbd2b0x3f7fbd2cfloat r = x - floor(x);ffract→frc0x3f7fbd2b0x3f7fbd2cfloat f = fract(abs(x)); float r = (x < 0.0 && f != 0.0) ? 1.0 - f : f;ffract(fabs),fadd,bcsel→frc (abs),add,sel0x3f7fbd2c0x3f7fbd2c-1.5,-0.99999994and0.75give the same results in every row on both GPUs.The last form holds up because the fraction of a non-negative float is exactly representable, so
frchas nothing to round. For a negative non-integerx,fract(x) = 1 - fract(|x|), which is then a single correctly rounded subtraction. AsEmitFPFract32:NaN fails both ordered compares and passes through.
-0.0and negative integers give+0.0.-infgives NaN, whichunaryFloatSpecialsalready marks invalid. A tiny negative input where1 - frounds to 1.0 is still clamped to0x3f7fffffby the translation.With this body on
2cc5735d: NVIDIA full suite 402/402; on Intel,vop1_float_unaryandf32_result_clamppass, and the full suite (withoutflat_store, which runs out of memory there on main too) has 33 failures: main's 34 minusvop1_float_unary.This is your fix, so feel free to take it into this PR; I won't open a competing one.
Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-08T02:07:31Z.
Thanks, that's a real hole. With a single-use floor this PR's first version only moved the problem into Mesa's algebraic pass. I've taken your
fract(|x|)/1 - fract(|x|)body into the commit, with you as co-author, and updated the description.Re-run on Windows with that body (Raptor Lake-S, driver 32.0.101.6790, and RTX 4070, driver 32.0.15.9597):
vop1_float_unaryandf32_result_clamppass on both.vop1_float_unary, and none are new.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T09:36:06Z.
Heads-up: this PR is on hold, not rejected.
v_fract_f32/v_fract_f16results depend on the shader float mode (IEEE_MODE, DX10_CLAMP, the denormal bits of FLOAT_MODE in RSRC1). Real titles set that mode per shader, and the recompiler currently assumes a single one; see #981. Until that is settled, no float-mode dependent shader PR gets reviewed or merged. Nothing to do on your side for now. Once #981 lands, this will probably need tests for each mode value it relies on.For when it resumes: on our HW oracle (gfx1035, IEEE=0, f32 denormals flushed and kept) this PR matched bit for bit on every finite input (4034
v_fract_f32rows, all 65536v_fract_f16inputs); the only differences were sNaN inputs, frommain's NaN handling, which is exactly the mode-dependent part.Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-10T14:30:49Z.
Independent validation on real hardware (no-regression; I have no Intel GPU to see the fix's effect)
Built and ran the PR head
480b0480on my own box:Release,BUILD_TESTING=ON.Results:
agc_driver_vop1_float_unaryPASSES on all three of my GPUs (5090, 4090, AMD iGPU) with the fix.Honest rigor finding — I could NOT reproduce the target bug: the regression this fixes is Intel-specific (Intel
frcroundsFractdown; the failing lane0xba85a805 → expected 0x3f7fbd2c). I do not have an Intel GPU, so I checked what the test does on my hardware with the fix reverted to the baseGLSLstd450Fract: the test still passes on my 5090 (exit 0). In other words, on NVIDIA and AMDFractis already correct, so base and this fix are indistinguishable on my machines — I can only confirm no-regression, not that the fix actually changes the Intel result. I did not run on Intel Raptor Lake / Iris Xe (ANV), so the actual rounding fix is unverified on my end.Code-level read (agreeing with the approach): emitting
fract(|x|)(with1 − fract(|x|)for negative non-integers) is a reasonable way to sidestep thex − floor(x)form, since a non-negative fraction is exactly representable and Mesa/ANV can't fold it back intofrc. I did not independently verify the driver-level folding claim (that's the crux and is hardware/driver dependent).Also noted: the PR's
mergeable_stateshows as unstable (basedf16c4cis behind current main) — I tested the head as-is, so I can't speak to a clean merge onto the latest main.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-10T16:24:05Z.
Adding the Intel half that @klosixgaming-arch couldn't check: on Arrow Lake the fix does what it says.
Windows 11 Home (26200), WinLibs GCC 15.2.0 r7, Release. Intel Graphics in a Core Ultra 9 275HX (Arrow Lake, device 0x7d67, driver 32.0.101.8991) and an RTX 5060 Laptop GPU (32.0.16.1742) in the same machine.
480b0480cherry-picked onto currentmain57f96fd2, which applies cleanly.agc_driver_vop1_float_unaryon the iGPU: fails onmainevery run (lane 13 fract_f32 v5 is 0x3f7fbd2b, expected 0x3f7fbd2c, the same value as the UHD 770 and 630 in #2114). With this PR it passes 3/3. It passes 3/3 on the RTX 5060 too.APS5_NO_PIPELINE_LTO=1),vop1_float_unarydrops out of the failures and the 5 left are the ones with their own issues (#2815, #2822, #2805, and the two Float64 skips in #2114).I understand it's on hold for #981, so this is just so the Intel number exists when that's settled.
AI-assisted (Claude Code); every number above is from runs on this machine.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:23:26Z.
Measured
v_fract_f32on the HW oracle (gfx1035, IEEE=0, f32 denormals flushed and kept): 4034 rows (0, -0, ±1, negative integers, tiny negatives,0xba85a805, ±max, denormals, random) match this PR bit for bit on every finite input, andv_fract_f16matches on every non-NaN input (all 65536 tested). Build + ctest green with the PR merged onto current main. We can't run the Intel fix here; it was confirmed by you, @hashaam1217 (ANV) and @Silverash-i8 (Arrow Lake), and @klosixgaming-arch saw no regression on NVIDIA/AMD. Merging.