Skip to content

Commit

Permalink
Treat wasmtime::component::Val::Float{32,64} zero and negative zero…
Browse files Browse the repository at this point in the history
… as inequal (#5562)

Following up on #5535, treat positive and negative zero as inequal in
wasmtime::component::Val::Float{32,64}'s `PartialEq` logic. IEEE 754
equality considers these values equal, but they are semantically
distinct values, and testing and fuzzing should be aware of the
difference.
  • Loading branch information
sunfishcode authored Jan 12, 2023
1 parent 6cb68f3 commit 6a20ca5
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions crates/wasmtime/src/component/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,22 @@ impl Val {
impl PartialEq for Val {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
// This breaks conformance with IEEE-754 equality to simplify testing logic.
(Self::Float32(l), Self::Float32(r)) => l == r || (l.is_nan() && r.is_nan()),
// IEEE 754 equality considers NaN inequal to NaN and negative zero
// equal to positive zero, however we do the opposite here, because
// this logic is used by testing and fuzzing, which want to know
// whether two values are semantically the same, rather than
// numerically equal.
(Self::Float32(l), Self::Float32(r)) => {
(*l != 0.0 && l == r)
|| (*l == 0.0 && l.to_bits() == r.to_bits())
|| (l.is_nan() && r.is_nan())
}
(Self::Float32(_), _) => false,
(Self::Float64(l), Self::Float64(r)) => l == r || (l.is_nan() && r.is_nan()),
(Self::Float64(l), Self::Float64(r)) => {
(*l != 0.0 && l == r)
|| (*l == 0.0 && l.to_bits() == r.to_bits())
|| (l.is_nan() && r.is_nan())
}
(Self::Float64(_), _) => false,

(Self::Bool(l), Self::Bool(r)) => l == r,
Expand Down

0 comments on commit 6a20ca5

Please sign in to comment.