Skip to content

Commit

Permalink
https://github.com/gfx-rs/wgpu/issues/6023
Browse files Browse the repository at this point in the history
I don't think this is actually about casting of abstract types. we
appear to handle that correctly. it's actually about overflow handling
for non-abstract types during const evaluation
  • Loading branch information
jamienicol committed Dec 19, 2024
1 parent c71483b commit d7c8227
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions naga/src/proc/constant_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1808,29 +1808,17 @@ 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(|| {
if b == 0 {
ConstantEvaluatorError::DivisionByZero
} else {
ConstantEvaluatorError::Overflow("division".into())
}
})?,
BinaryOperator::Modulo => a.checked_rem(b).ok_or_else(|| {
if b == 0 {
ConstantEvaluatorError::RemainderByZero
} else {
ConstantEvaluatorError::Overflow("remainder".into())
}
})?,
BinaryOperator::Add => a.wrapping_add(b),
BinaryOperator::Subtract => a.wrapping_sub(b),
BinaryOperator::Multiply => a.wrapping_mul(b),
BinaryOperator::Divide => match b {
0 => return Err(ConstantEvaluatorError::DivisionByZero),
_ => a.wrapping_div(b),
},
BinaryOperator::Modulo => match b {
0 => return Err(ConstantEvaluatorError::DivisionByZero),
_ => a.wrapping_rem(b),
},
BinaryOperator::And => a & b,
BinaryOperator::ExclusiveOr => a ^ b,
BinaryOperator::InclusiveOr => a | b,
Expand All @@ -1850,21 +1838,17 @@ impl<'a> ConstantEvaluator<'a> {
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs),
}),
(Literal::U32(a), Literal::U32(b)) => Literal::U32(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(ConstantEvaluatorError::DivisionByZero)?,
BinaryOperator::Modulo => a
.checked_rem(b)
.ok_or(ConstantEvaluatorError::RemainderByZero)?,
BinaryOperator::Add => a.wrapping_add(b),
BinaryOperator::Subtract => a.wrapping_sub(b),
BinaryOperator::Multiply => a.wrapping_mul(b),
BinaryOperator::Divide => match b {
0 => return Err(ConstantEvaluatorError::DivisionByZero),
_ => a.wrapping_div(b),
},
BinaryOperator::Modulo => match b {
0 => return Err(ConstantEvaluatorError::DivisionByZero),
_ => a.wrapping_rem(b),
},
BinaryOperator::And => a & b,
BinaryOperator::ExclusiveOr => a ^ b,
BinaryOperator::InclusiveOr => a | b,
Expand Down

0 comments on commit d7c8227

Please sign in to comment.