diff --git a/CHANGELOG.md b/CHANGELOG.md index a39cd68f8d..bc778f561b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/naga/src/proc/constant_evaluator.rs b/naga/src/proc/constant_evaluator.rs index 2baf918118..2c4cf3a42e 100644 --- a/naga/src/proc/constant_evaluator.rs +++ b/naga/src/proc/constant_evaluator.rs @@ -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,