Skip to content

Fix constant evaluation of sign(0.0f)#8942

Merged
andyleiserson merged 4 commits intogfx-rs:trunkfrom
mandryskowski:mandr/sign-0f
Jan 27, 2026
Merged

Fix constant evaluation of sign(0.0f)#8942
andyleiserson merged 4 commits intogfx-rs:trunkfrom
mandryskowski:mandr/sign-0f

Conversation

@mandryskowski
Copy link
Copy Markdown
Contributor

@mandryskowski mandryskowski commented Jan 27, 2026

Description
According to WGSL spec, sign(0) = 0
In constant_evaluator.rs the signum method is used to evaluate sign(e).
However, 0_f32.signum() and f16::ZERO.signum() output 1.0f, not 0.0f.
https://doc.rust-lang.org/std/primitive.f32.html#method.signum
https://docs.rs/half/latest/half/struct.f16.html#method.signum

Because of this, the following shader outputs 1.0f:

struct StorageBuffer {
    a: f32,
}

@group(0)
@binding(0)
var<storage, read_write> s_output: StorageBuffer;

@compute
@workgroup_size(1)
fn main() {
    s_output = StorageBuffer(sign(0.0));
}

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy --tests. If applicable, add:
    • --target wasm32-unknown-unknown
  • Run cargo xtask test to run tests.
  • If this contains user-facing changes, add a CHANGELOG.md entry.

Copy link
Copy Markdown
Contributor

@andyleiserson andyleiserson left a comment

Choose a reason for hiding this comment

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

Thanks!

I checked the CTS, and it does have coverage for this, in webgpu:shader,execution,expression,call,builtin,sign:*, but there are also still some failures in that test due to not having constant evaluation for frexp, so we can rely on the snapshot test for now.

Negative zero is also an interesting test case, which the CTS covers, so I don't think it's necessary to add it to the snapshot test.

@andyleiserson andyleiserson enabled auto-merge (squash) January 27, 2026 19:30
@andyleiserson andyleiserson merged commit 2ef1089 into gfx-rs:trunk Jan 27, 2026
54 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.

2 participants