Checked SIMD vector arithmetic?

I’m prototyping implemeting Vector2/3/4 vector types and i realized if the SIMD storage is an integer vector, +, -, * aren’t available as checked operators.

error: '+' is unavailable: integer vector types do not support checked arithmetic; use the wrapping operator '&+' instead

Even though vectors could never conform to Numeric, it would very much make sense for a higher-level vector type to support +, -, and *. is it possible to have reasonably performant SIMD checked arithmetic on vector types?

I’ve also considered just implementing +, -, and * as calling into &+, &-, and &* on their underlying storage, but then it’d be weird where + on scalars can overflow but + on vectors wraps around.


Our SIMD types are kind of bare-bones at the moment (by design, we wanted them to exist in 5.0 before ABI lockdown), and usability could definitely be improved in lots of areas. I would suggest making a proposal if you want this for 5.1.

Overflow checking with SIMD is certainly a thing. There might even be a compiler intrinsic for sum-with-overflow.

(Mathematical) vectors of integers are kind of a weird thing, which should probably be a different type. In particular, you’re more likely to want to have vectors over finite fields, not one of the standard library integer types.

We certainly could add checked arithmetic to integer SIMD types, but I don’t think that’s an especially useful building block for mathematical vector types.

1 Like

they’re good for screen coordinates and text shaping

Those sorts of uses usually involve points or displacements, rather than mathematical vectors.

I would expect a mathematical vector type to e.g. have API to project onto another vector (doesn’t work with integer coordinates), or take the length (either doesn’t work or has the wrong signature).

that API is conditionally available when Scalar:FloatingPoint. i haven’t found much reason to split the types entirely.

That's certainly a design direction that you can take, but I would tend to question how much it actually buys you vs. having a separate type for integer vectors. If the answer is only "we get to reuse a little bit of code", it's probably not worth the hassle of conditional generics.

Terms of Service

Privacy Policy

Cookie Policy