Skip to content

Commit 826db5e

Browse files
[naga] change i32 arithmetic operations to use wrapping_ instead of checked_ (#6835)
1 parent 959c2db commit 826db5e

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148]
157157
- Show types of LHS and RHS in binary operation type mismatch errors. By @ErichDonGubler in [#6450](https://github.com/gfx-rs/wgpu/pull/6450).
158158
- The GLSL parser now uses less expressions for function calls. By @magcius in [#6604](https://github.com/gfx-rs/wgpu/pull/6604).
159159
- 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)
160+
- 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).
160161

161162
##### General
162163

naga/src/proc/constant_evaluator.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,29 +1808,23 @@ impl<'a> ConstantEvaluator<'a> {
18081808

18091809
_ => match (left_value, right_value) {
18101810
(Literal::I32(a), Literal::I32(b)) => Literal::I32(match op {
1811-
BinaryOperator::Add => a.checked_add(b).ok_or_else(|| {
1812-
ConstantEvaluatorError::Overflow("addition".into())
1813-
})?,
1814-
BinaryOperator::Subtract => a.checked_sub(b).ok_or_else(|| {
1815-
ConstantEvaluatorError::Overflow("subtraction".into())
1816-
})?,
1817-
BinaryOperator::Multiply => a.checked_mul(b).ok_or_else(|| {
1818-
ConstantEvaluatorError::Overflow("multiplication".into())
1819-
})?,
1820-
BinaryOperator::Divide => a.checked_div(b).ok_or_else(|| {
1811+
BinaryOperator::Add => a.wrapping_add(b),
1812+
BinaryOperator::Subtract => a.wrapping_sub(b),
1813+
BinaryOperator::Multiply => a.wrapping_mul(b),
1814+
BinaryOperator::Divide => {
18211815
if b == 0 {
1822-
ConstantEvaluatorError::DivisionByZero
1816+
return Err(ConstantEvaluatorError::DivisionByZero);
18231817
} else {
1824-
ConstantEvaluatorError::Overflow("division".into())
1818+
a.wrapping_div(b)
18251819
}
1826-
})?,
1827-
BinaryOperator::Modulo => a.checked_rem(b).ok_or_else(|| {
1820+
}
1821+
BinaryOperator::Modulo => {
18281822
if b == 0 {
1829-
ConstantEvaluatorError::RemainderByZero
1823+
return Err(ConstantEvaluatorError::RemainderByZero);
18301824
} else {
1831-
ConstantEvaluatorError::Overflow("remainder".into())
1825+
a.wrapping_rem(b)
18321826
}
1833-
})?,
1827+
}
18341828
BinaryOperator::And => a & b,
18351829
BinaryOperator::ExclusiveOr => a ^ b,
18361830
BinaryOperator::InclusiveOr => a | b,

0 commit comments

Comments
 (0)