Skip to content

Tests: float-to-int conversions clamp and take NaN as 0 on the GPU - #639

Open
acaracargo wants to merge 1 commit into
xcl-dev:mainfrom
acaracargo:fork/cvt-int-gpu-tests
Open

acaracargo wants to merge 1 commit into
xcl-dev:mainfrom
acaracargo:fork/cvt-int-gpu-tests

Conversation

@acaracargo

Copy link
Copy Markdown

HLSL leaves an out-of-range or NaN float-to-int cast undefined. The ISA clamps and maps NaN to 0, which is what the cvt_i32 / cvt_u32 helpers in emit.rs are for.

This adds GPU coverage for v_cvt_i32_f32, v_cvt_u32_f32, v_cvt_flr_i32_f32, v_cvt_rpi_i32_f32, v_cvt_u16_f16 and v_cvt_i16_f16.

The inputs cover NaN of both signs, infinities, integer range boundaries, negative halves, negative zero and a denormal. The reference uses Rust's as conversion, which saturates and maps NaN to zero. The f16 sources carry another value in their high half.

#515 covers the opposite conversion direction and does not overlap with this test.

What is in it

  • crates/xcl/src/gpu/recomp/tests.rs: gpu_float_to_int_conversions_clamp_and_take_nan_as_zero.

Tested

  • NVIDIA GeForce RTX 4060 (32-lane waves only), driver 610.88; Intel Core i5-13400F; Windows 11 Pro 26300; Rust 1.100.0 x86_64-pc-windows-msvc; DXC 1.9.2602.17; debug.
  • cargo test --workspace -- --test-threads=4 on ac0b845: 685 passed, 0 failed, 41 ignored (main 9ce2070: 684 / 0 / 41).
  • SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64).
  • Changing the NaN or overflow result of cvt_i32 causes the test to fail (checked locally, reverted).
  • On this driver, a plain (int) cast already saturates and maps NaN to 0, so removing the helper altogether is not caught here.

Regression run

  • Test-only change, no runtime regression run.

Not tested

  • AMD GPUs and native wave64; console hardware.
  • v_cvt_rpi_i32_f32 of 0.49999997, where floor(x + 0.5) in f32 rounds up. The ISA text does not specify whether the addition is rounded.

AI use

  • Claude Code (Claude Opus 5.5) wrote the test and the original description and ran the tests and mutation checks. ChatGPT assisted with editing the PR description.

v_cvt_i32_f32, v_cvt_u32_f32, v_cvt_flr_i32_f32, v_cvt_rpi_i32_f32,
v_cvt_u16_f16 and v_cvt_i16_f16 on the edges of their range: NaN of
both signs, +-inf, +-2^31 and the f32 values next to it, the largest
f32 below 2^32, negative halves for floor / round, -0 and a denormal
(0 whether or not it is flushed). The f16 sources carry another value
in the high half. The reference is Rust's `as` (saturating, NaN -> 0),
which is the ISA's rule. These conversions had no edge-value GPU test;
HLSL leaves an out-of-range or NaN cast undefined, which is what the
cvt_i32 / cvt_u32 helpers in emit.rs guard against.

Measured: RTX 4060 (wave32), Windows 11 Pro 26300, Rust 1.100, debug.
cargo test --workspace -- --test-threads=4: 678 passed, 0 failed,
41 ignored; SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask,
gpu_wave32_program_in_wave64_hardware.
Changing cvt_i32's NaN or overflow result fails the test. On this
NVIDIA driver a plain (int) cast already saturates and maps NaN to 0,
so removing the helper is not caught here; it may be on other drivers.

Assisted-by: Claude Code (Claude Opus 5.5)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of ac0b845698d82b765b5436cd0151cef6feb0127c on AMD RDNA4 (native wave64). I merged it onto current main 63cdc25 locally (clean merge) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace + cargo test --workspace -- --test-threads=4 on the merge: 693 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 653 passed, 0 failed, 21 ignored
  • 0 SKIPPED; gpu_float_to_int_conversions_clamp_and_take_nan_as_zero passes.

Review: NaN converts to 0 and out-of-range values (±inf included) clamp; v_cvt_flr_i32_f32 is floor(x) and v_cvt_rpi_i32_f32 floor(x + 0.5) before the conversion, and the f16 sources carry another value in the high half, as the ISA describes. This is the first AMD run.

Not tested: no retail title (not needed for a test-only change), release build; the PR's head on its old base was not run, only its merge onto main.

Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

@marquezinii

Copy link
Copy Markdown
Contributor

Test run of ac0b845698d82b765b5436cd0151cef6feb0127c on NVIDIA Blackwell with a newer driver than the one in the description. I merged it onto current main f4ce27c locally (clean merge, 66 lines in tests.rs) and tested that tree.

Tested

  • NVIDIA GeForce RTX 5070 12 GB (32-lane waves only), driver 621.24; Windows 11 Pro 26300; rustc 1.99.1 stable-x86_64-pc-windows-msvc, debug; DXC 1.9.2602.17 (Windows SDK 10.0.26100.0)
  • cargo test --workspace -- --test-threads=4: 702 passed, 0 failed, 41 ignored; --test-threads=1: the same
  • SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64)
  • gpu_float_to_int_conversions_clamp_and_take_nan_as_zero passes
  • Your note holds on this driver too: with cvt_u32 reduced to return (uint)f;, and separately cvt_i32 to return (int)f; (by hand, reverted), the test still passes. So on RTX 4060 / 610.88 and RTX 5070 / 621.24 a plain cast already saturates and maps NaN to 0, and only AMD (or another vendor) can show whether the helpers are needed.

Not tested

  • AMD / Intel, native wave64

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and the plain-cast checks and wrote this report.

@4usphs

4usphs commented Nov 24, 2026 •

Copy link
Copy Markdown

Measured on RDNA2 hardware: this answers the v_cvt_rpi_i32_f32 question under Not tested, and shows a second difference. For NaN, v_cvt_flr_i32_f32 and v_cvt_rpi_i32_f32 do not give 0.

Method. The Radeon iGPU of a Ryzen 7 9800X3D (gfx1036, RDNA2), Windows 11 Pro 26200, display driver 32.0.21045.29003, AMD OpenCL 3652.0 (PAL,LC). Each instruction runs as inline RDNA2 assembly in an OpenCL C kernel (the #518 / #772 method), one input per work-item, VDST preset to 0xCAFEBABE, in wave32 and in wave64 (-Wf,-mwavefrontsize64). There are 3 runs with identical output. The NaN and rounding cases were also run with MODE.DX10_CLAMP / MODE.IEEE set to each of the 4 combinations with s_setreg_imm32_b32 (read back with s_getreg_b32: 0xf0 to 0x3f0), and they give the same results in all 4.

1. v_cvt_rpi_i32_f32 rounds exactly. The hardware computes floor(x + 0.5) without rounding x + 0.5 to f32.

x hardware floor(x + 0.5f) in f32 (the test's reference, and emit.rs today) exact floor(x + 0.5)
0.49999997 (0x3EFFFFFF) 0 1 0
8388609.0 (0x4B000001) 8388609 8388610 8388609
8388611.0 (0x4B000003) 8388611 8388612 8388611
-8388609.0 (0xCB000001) -8388609 -8388608 -8388609
-0.49999997, -0.50000006, 1.4999999, 2.5, -2.5 0, -1, 1, 3, -2 same same

emit.rs translates it as cvt_i32(floor(x + 0.5f)) (line 3281 on main 077ce40). So it gives 1 for 0.49999997 and is off by one for odd integers from 2^23 up.

2. NaN in v_cvt_flr_i32_f32 / v_cvt_rpi_i32_f32 takes the sign of the NaN.

source v_cvt_i32_f32 v_cvt_u32_f32 v_cvt_flr_i32_f32 v_cvt_rpi_i32_f32
+qNaN 0x7FC00000, +sNaN 0x7F800001, 0x7FFFFFFF 0 0 0x7FFFFFFF 0x7FFFFFFF
-qNaN 0xFFC00000, -sNaN 0xFF800001 0 0 0x80000000 0x80000000

v_cvt_i32_f32 and v_cvt_u32_f32 give 0 as the test expects. For flr and rpi, the test's first two inputs (NAN, -NAN) expect 0, which is the opposite of the hardware. emit.rs gives 0 there too, through cvt_i32's isnan select.

Everything else in this test matches the hardware. These are the other 18 f32 inputs for all four f32 instructions, the 12 f16 inputs for v_cvt_u16_f16 / v_cvt_i16_f16 (low 16 bits), and wave32 = wave64 everywhere. Side note for #638's open point: both f16 conversions left D[31:16] at the preset 0xCAFE, so the high half is kept, not zeroed.

What would match the hardware, as far as I can tell (not tried in emit.rs): for flr / rpi, NaN gives select(asuint(x) >> 31, 0x80000000, 0x7FFFFFFF). rpi rounds without the f32 addition, e.g. r = floor(x); r + (x - r >= 0.5f) (x - floor(x) is exact in f32), then clamps.

For comparison, an RX 9070 XT (gfx1201, RDNA4, OpenCL 3686.0) gives the same results for everything above: exact rpi rounding, the NaN signs for flr / rpi, and D[31:16] kept.

Not tested: the console itself (gfx1036 is a desktop RDNA2 APU of the same generation), RDNA1, this PR's test on this GPU (it does not run D3D12 tests in my setup; RDNA4 runs are above).

Kernel (one of the cases)
__kernel void k(__global uint* o, __global const uint* A) {
  uint i = get_global_id(0), a = A[i], r = 0xCAFEBABEu, m0, saved;
  __asm__ volatile(
    "s_getreg_b32 %1, hwreg(HW_REG_MODE)\n"
    "s_setreg_imm32_b32 hwreg(HW_REG_MODE, 8, 2), 0\n"   // DX10_CLAMP, IEEE: 0..3
    "s_getreg_b32 %2, hwreg(HW_REG_MODE)\n"
    "v_cvt_rpi_i32_f32 %0, %3\n"
    "s_setreg_b32 hwreg(HW_REG_MODE), %1\n"
    : "+v"(r), "=&s"(saved), "=&s"(m0) : "v"(a));
  o[2*i] = r; o[2*i+1] = m0;
}

AI use: Claude Code (Claude Opus 5.5) wrote the OpenCL probes, ran them on my PC and wrote this comment. I have not reviewed the code.

@ivall ivall added the off-goal Does not bear on the current goal (world loading); waits until it is reached label Nov 24, 2026
@JJAutoCompare

Copy link
Copy Markdown

Empirical test report (ac0b845)

Hardware / environment

  • PR Tests: float-to-int conversions clamp and take NaN as 0 on the GPU #639 head ac0b845698d82b765b5436cd0151cef6feb0127c, verified on a clean detached HEAD.
  • Intel Arc A770 Graphics, driver 32.0.101.9175. AMD Ryzen 7 3800X 8-Core Processor. Windows 11 Enterprise Evaluation, build 26200.
  • Rust 1.100.0 (0ef1bc386, x86_64-pc-windows-msvc), unoptimized test profile. The runner reported DXC 1.9.2609.5 (01b62ad4) and DXIL 1.9.2609.5.

Commands and results

  • cargo test -p xcl --lib --no-run exited 0 outside the Arc lock. Cargo built the test executable in 30.59 s and emitted one fetch_update deprecation warning.
  • $testExe is the executable printed by Cargo and $arcLock is the shared Invoke-ArcTest.ps1 script. The focused command ran with XCL_ADAPTER=A770 and RUST_TEST_NOCAPTURE=1.
& $arcLock -Test { & $testExe gpu::recomp::tests::gpu_float_to_int_conversions_clamp_and_take_nan_as_zero --exact --nocapture --test-threads=1 }

The focused command exited 0. The log shows 1 passed, 0 failed, 0 ignored, 0 skipped, with 665 tests filtered out. The skip count is the number of case-sensitive log lines beginning with SKIPPED . The test harness reported 0.19 s.

Observed behavior

The translated compute shader matched the Rust expectations for all six conversion instructions across 20 checked lanes, including NaN to zero, clamping at integer bounds and for infinities, and the signed floor and nearest rounding with ties toward positive infinity. The two f16-source conversions also matched with nonzero bits in the high half of the source registers.

Not tested

Full GPU workspace suite and game smoke runs.

AI use

Verified with T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high). Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against pr639-focused.log and published.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

off-goal Does not bear on the current goal (world loading); waits until it is reached

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants