Skip to content

Commit

Permalink
[naga] change i32 arithmetic operations to use wrapping_ instead of…
Browse files Browse the repository at this point in the history
… `checked_` (#6835)
  • Loading branch information
matthew-wong1 authored Jan 6, 2025
1 parent 959c2db commit 826db5e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148]
- Show types of LHS and RHS in binary operation type mismatch errors. By @ErichDonGubler in [#6450](https://github.com/gfx-rs/wgpu/pull/6450).
- The GLSL parser now uses less expressions for function calls. By @magcius in [#6604](https://github.com/gfx-rs/wgpu/pull/6604).
- Add a note to help with a common syntax error case for global diagnostic filter directives. By @e-hat in [#6718](https://github.com/gfx-rs/wgpu/pull/6718)
- Change arithmetic operations between two i32 variables to wrap on overflow to match WGSL spec. By @matthew-wong1 in [#6835](https://github.com/gfx-rs/wgpu/pull/6835).

##### General

Expand Down
28 changes: 11 additions & 17 deletions naga/src/proc/constant_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1808,29 +1808,23 @@ impl<'a> ConstantEvaluator<'a> {

_ => match (left_value, right_value) {
(Literal::I32(a), Literal::I32(b)) => Literal::I32(match op {
BinaryOperator::Add => a.checked_add(b).ok_or_else(|| {
ConstantEvaluatorError::Overflow("addition".into())
})?,
BinaryOperator::Subtract => a.checked_sub(b).ok_or_else(|| {
ConstantEvaluatorError::Overflow("subtraction".into())
})?,
BinaryOperator::Multiply => a.checked_mul(b).ok_or_else(|| {
ConstantEvaluatorError::Overflow("multiplication".into())
})?,
BinaryOperator::Divide => a.checked_div(b).ok_or_else(|| {
BinaryOperator::Add => a.wrapping_add(b),
BinaryOperator::Subtract => a.wrapping_sub(b),
BinaryOperator::Multiply => a.wrapping_mul(b),
BinaryOperator::Divide => {
if b == 0 {
ConstantEvaluatorError::DivisionByZero
return Err(ConstantEvaluatorError::DivisionByZero);
} else {
ConstantEvaluatorError::Overflow("division".into())
a.wrapping_div(b)
}
})?,
BinaryOperator::Modulo => a.checked_rem(b).ok_or_else(|| {
}
BinaryOperator::Modulo => {
if b == 0 {
ConstantEvaluatorError::RemainderByZero
return Err(ConstantEvaluatorError::RemainderByZero);
} else {
ConstantEvaluatorError::Overflow("remainder".into())
a.wrapping_rem(b)
}
})?,
}
BinaryOperator::And => a & b,
BinaryOperator::ExclusiveOr => a ^ b,
BinaryOperator::InclusiveOr => a | b,
Expand Down

0 comments on commit 826db5e

Please sign in to comment.