cranelift: Fix fmin/fmax when dealing with zeroes (#4373)

`fmin`/`fmax` are defined as returning -0.0 as smaller than 0.0.
This is not how the IEEE754 views these values and the interpreter was
returning the wrong value in these operations since it was just using the
standard IEEE754 comparisons.

This also tries to preserve NaN information by avoiding passing NaN's
through any operation that could canonicalize it.
This commit is contained in:
Afonso Bordado
2022-07-05 20:59:23 +01:00
committed by GitHub
parent 41ba851a95
commit 925891245d
5 changed files with 263 additions and 10 deletions

View File

@@ -698,17 +698,21 @@ where
Opcode::Fneg => assign(Value::neg(arg(0)?)?),
Opcode::Fabs => assign(Value::abs(arg(0)?)?),
Opcode::Fcopysign => binary(Value::copysign, arg(0)?, arg(1)?)?,
Opcode::Fmin => choose(
Value::is_nan(&arg(0)?)? || Value::lt(&arg(0)?, &arg(1)?)?,
arg(0)?,
arg(1)?,
),
Opcode::Fmin => assign(match (arg(0)?, arg(1)?) {
(a, _) if a.is_nan()? => a,
(_, b) if b.is_nan()? => b,
(a, b) if a.is_zero()? && b.is_zero()? && a.is_negative()? => a,
(a, b) if a.is_zero()? && b.is_zero()? && b.is_negative()? => b,
(a, b) => a.min(b)?,
}),
Opcode::FminPseudo => unimplemented!("FminPseudo"),
Opcode::Fmax => choose(
Value::is_nan(&arg(0)?)? || Value::gt(&arg(0)?, &arg(1)?)?,
arg(0)?,
arg(1)?,
),
Opcode::Fmax => assign(match (arg(0)?, arg(1)?) {
(a, _) if a.is_nan()? => a,
(_, b) if b.is_nan()? => b,
(a, b) if a.is_zero()? && b.is_zero()? && a.is_negative()? => b,
(a, b) if a.is_zero()? && b.is_zero()? && b.is_negative()? => a,
(a, b) => a.max(b)?,
}),
Opcode::FmaxPseudo => unimplemented!("FmaxPseudo"),
Opcode::Ceil => unimplemented!("Ceil"),
Opcode::Floor => unimplemented!("Floor"),