Skip to content

Conversation

@stevenfontanella
Copy link
Member

@stevenfontanella stevenfontanella commented Jan 25, 2026

  • Use Literal::add and Literal::mul to avoid overflow of signed arithmetic. These do the right thing by casting to unsigned first.
  • Use bit_cast instead of a C-style cast to convey the intent better when converting between signed and unsigned.
  • Also add a unit test for i8x16.avgr_u to demonstrate that it doesn't overflow.

From reading the code, most other arithmetic in Literal.cpp shouldn't invoke UB. There are some spec tests that are disabled due to float/nan behavior that I'll look at in future PRs.

@stevenfontanella stevenfontanella changed the title Try fixing arithmetic ub Fix UB in SIMD dot product arithmetic Jan 25, 2026
@stevenfontanella stevenfontanella force-pushed the arithmetic branch 2 times, most recently from 8b0c607 to 447c007 Compare January 27, 2026 01:07
@stevenfontanella stevenfontanella marked this pull request as ready for review January 27, 2026 17:15
switch (type.getBasic()) {
case Type::i32:
return Literal(uint32_t(i32) + uint32_t(other.i32));
return Literal(bit_cast<uint32_t>(i32) + bit_cast<uint32_t>(other.i32));
Copy link
Member

Choose a reason for hiding this comment

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

I am getting en.cppreference.com took too long to respond so I can't read the docs on this, but is bit_cast not from a pretty recent C++ version? Is it not too new?

Copy link
Member Author

Choose a reason for hiding this comment

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

bit_cast is from C++20 but we polyfill it with our own implementation: link. utilities.h is included in this file and bit_cast is already used in other places in this file.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good, thanks.

@stevenfontanella stevenfontanella merged commit af46d19 into main Jan 27, 2026
17 checks passed
@stevenfontanella stevenfontanella deleted the arithmetic branch January 27, 2026 18:57
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.

4 participants