Skip to content

Fix the leading_zeros constant for 8, 16 and 64-bit integers - #702

Merged
Firestar99 merged 2 commits into
Rust-GPU:mainfrom
greenhat:fix-leading-zeros
Nov 16, 2026
Merged

Firestar99 merged 2 commits into
Rust-GPU:mainfrom
greenhat:fix-leading-zeros

Conversation

@greenhat

Copy link
Copy Markdown
Contributor

Close #701

leading_zeros returns a wrong value in a shader for every non-zero u8, u16 and u64:

let x: u64 = input[0];     // 1
(x as u8).leading_zeros()  // 55, expected 7
(x as u16).leading_zeros() // 47, expected 15
x.leading_zeros()          // 31, expected 63

leading_ones, checked_ilog2 and checked_next_power_of_two call it, so they are wrong as well. u32 is right.

This fixes the constant that the FindUMsb result is subtracted from (find_xsb in builder/intrinsics.rs). The code emitted for u32 does not change.

The new difftest lang/core/intrinsics/leading_zeros runs leading_zeros, leading_ones, checked_ilog2 and checked_next_power_of_two on 8, 16, 32 and 64-bit values in a shader and compares the results with the CPU. It fails without the fix.

The fix and the test were written with an AI coding agent. I reviewed and checked it.

Details

The code emitted before the fix for a u8 and for a u64 (lower half, then higher half):

%504 = OpUConvert %uint %444
%505 = OpExtInst %uint %1 FindUMsb %504
%506 = OpISub %uint %int_55 %505
%453 = OpExtInst %uint %1 FindUMsb %449
%454 = OpISub %uint %int_31 %453
%455 = OpExtInst %uint %1 FindUMsb %451
%456 = OpISub %uint %int_n1 %455
%457 = OpSelect %uint %452 %454 %456

The constants are now 7 for u8, 15 for u16, and 63 and 31 for the two halves of a u64.

For u8 and u16 the constant became 31 - offset in 05042d1. For u64 the 32 was on the higher half since 95d2902. 7ad4c4d fixed the same offsets for trailing_zeros.

The compiletest ui/lang/core/intrinsics/leading_zeros.rs has the right asserts for all four widths, but it is build-pass only, so they never run.

The test reads the values from a buffer and narrows them in the shader. It follows trailing_zeros_64: Ash backend (it needs Int8, Int16 and Int64), skipped on macOS. I ran it on lavapipe only.

@Firestar99
Firestar99 force-pushed the fix-leading-zeros branch 2 times, most recently from 0071a3d to 7df8d33 Compare November 16, 2026 13:08

@Firestar99 Firestar99 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  • cleaned up AI-isms
  • merged your new tests with the existing trailing_zeros test into bitops, since it was an almost 1:1 copy
  • in a second commit, cleaned up the leading/trailing zeros impl to make it simpler to read

As the difftests pass, I'll just self-approve this one

greenhat and others added 2 commits November 16, 2026 14:54
`find_xsb` subtracted `offset` from the leading zeros of the 32-bit word
instead of adding it. `leading_zeros` returned 48 too many for a non-zero
`u8`, 32 too many for a non-zero `u16` (both since 05042d1) and 32 too
few for a non-zero `u64`. Add `offset`, and pass 32 for the lower half of
a 64-bit value and 0 for the higher half.

Add the difftest `lang/core/intrinsics/leading_zeros` that runs
`leading_zeros`, `leading_ones`, `checked_ilog2` and
`checked_next_power_of_two` on 8, 16, 32 and 64-bit values in a shader
and compares the results with the CPU.
@Firestar99
Firestar99 enabled auto-merge November 16, 2026 15:13
@Firestar99
Firestar99 disabled auto-merge November 16, 2026 16:05
@Firestar99
Firestar99 added this pull request to the merge queue Nov 16, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 16, 2026
@Firestar99
Firestar99 added this pull request to the merge queue Nov 16, 2026
@Firestar99
Firestar99 removed this pull request from the merge queue due to a manual request Nov 16, 2026
@Firestar99
Firestar99 merged commit 7a76a77 into Rust-GPU:main Nov 16, 2026
24 checks passed
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.

Fix the leading_zeros constant for 8, 16 and 64-bit integers

2 participants