Skip to content

Conversation

sagudev
Copy link
Collaborator

@sagudev sagudev commented Sep 13, 2025

Connections
Continuation of #6213.
Implements some functions in const #4507.

Description
Existing macros work for component-wise cases where return type is equal to input type. But it's hard to generalize macros further. So I present LiteralVector:

/// Vector for each [`Literal`] type
///
/// This type ensures that all elements have same type
enum LiteralVector {
    F64(ArrayVec<f64, { crate::VectorSize::MAX }>),
    F32(ArrayVec<f32, { crate::VectorSize::MAX }>),
    U32(ArrayVec<u32, { crate::VectorSize::MAX }>),
    I32(ArrayVec<i32, { crate::VectorSize::MAX }>),
    U64(ArrayVec<u64, { crate::VectorSize::MAX }>),
    I64(ArrayVec<i64, { crate::VectorSize::MAX }>),
    Bool(ArrayVec<bool, { crate::VectorSize::MAX }>),
    AbstractInt(ArrayVec<i64, { crate::VectorSize::MAX }>),
    AbstractFloat(ArrayVec<f64, { crate::VectorSize::MAX }>),
}

that abstracts away at least vector obtaining logic. This makes it easier to implement some builtins that needs access to all components like dot, length, distance (folds LiteralVector to Literal) and normalize (maps LiteralVector).

Testing
There are some snapshot tests.

Squash or Rebase?
Reviewable per commit.
Squash

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.

@sagudev

This comment was marked as resolved.

@sagudev sagudev marked this pull request as draft September 16, 2025 10:31
@sagudev sagudev force-pushed the const_rel branch 2 times, most recently from a3e2ac1 to c5081a4 Compare September 18, 2025 08:43
@sagudev sagudev marked this pull request as ready for review September 18, 2025 08:48
Comment on lines +1908 to +1909
let (a, ty) = self.extract_vec_with_size::<3>(a)?;
let (b, _) = self.extract_vec_with_size::<3>(b)?;
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: IMO it'd have been better to have this refactor separated for the purpose of review, but 🤷🏻‍♂️ it's not too hard to follow.

Comment on lines +1808 to +1809
crate::MathFunction::Normalize => {
// https://www.w3.org/TR/WGSL/#normalize-builtin
Copy link
Member

Choose a reason for hiding this comment

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

Review TODO for myself: figure out what the "zero vector" domain exception listed in the standard is, and see if we need to validate it out.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Relevant spec parts https://www.w3.org/TR/WGSL/#normalize-builtin:

The domain is all vectors except the zero vector.

and

When evaluated outside its domain, the default exception handling rules of IEEE-754 require an implementation to generate an exception and yield a NaN value. In contrast, WGSL does not mandate floating point exceptions, and may instead yield an indeterminate value. See § 15.7.2 Differences from IEEE-754.

So we can do anything we want, current code will return NaN.

@ErichDonGubler
Copy link
Member

Just wanted to acknowledge that I'm taking a long time to finish this review. I haven't had bandwidth to spare, with some Mozilla tasks overriding my other priorities. I'm hoping to get to this early next week! ❤️💦

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