Rust-GPU / Rust-GPU/rust-gpu

u64 with `trailing_zeros` returns incorrect values

Open
#501 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Rust
Stars
3.4k
Forks
125
PR merge metrics
No merged PRs in 30d

Description

Expected Behaviour

We expect the trailing_zeros() function on u64 values to match the behavior of Rust's standard library implementation, returning the count of trailing zero bits.

For example:

  • 0x0000000000000001_u64.trailing_zeros() should return 0
  • 0x0000000100000000_u64.trailing_zeros() should return 32
  • 0x8000000000000000_u64.trailing_zeros() should return 63
Example & Steps To Reproduce
  • 0x0000000000000001 Expected: 0, Result: 32
  • 0x0000000100000000 Expected: 32, Result: 0
  • 0x8000000000000000 Expected: 63, Result: 31

Run a shader that calculates trailing zeros on a u64. I am creating a pull request that contains test to validate the function and a fix for this issue.

#![no_std]
use spirv_std::spirv;

#[spirv(compute(threads(1)))]
pub fn main_cs(
    #[spirv(storage_buffer, descriptor_set = 0, binding = 0)] output: &mut [u32],
) {
    // Expected: 0, Actual: 32
    output[0] = 0x0000000000000001_u64.trailing_zeros();
    
    // Expected: 32, Actual: 0  
    output[1] = 0x0000000100000000_u64.trailing_zeros();
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the u64 trailing_zeros() entry point used when compiling the shader, comparing its behavior with Rust's standard library implementation. Run the shader reproduction with the three listed u64 values and add or use tests covering results 0, 32, and 63. Done means all examples return the expected count of trailing zero bits.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, computer-graphics
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.