From 31c7e98a4a46c2d0ef93c5fca47d64d05b96449f Mon Sep 17 00:00:00 2001 From: Diego <96022404+dzfrias@users.noreply.github.com> Date: Sat, 6 Jul 2024 13:54:56 -0700 Subject: [PATCH] LibWasm: Fix comparisons between 0.0 and -0.0 According to the spec, -0.0 < 0.0. --- .../LibWasm/AbstractMachine/Operators.h | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Operators.h b/Userland/Libraries/LibWasm/AbstractMachine/Operators.h index 4364bee6537..d3bfb90a5bd 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Operators.h +++ b/Userland/Libraries/LibWasm/AbstractMachine/Operators.h @@ -329,14 +329,12 @@ struct Minimum { auto operator()(Lhs lhs, Rhs rhs) const { if constexpr (IsFloatingPoint || IsFloatingPoint) { - if (isnan(lhs)) - return lhs; - if (isnan(rhs)) - return rhs; - if (isinf(lhs)) - return lhs > 0 ? rhs : lhs; - if (isinf(rhs)) - return rhs > 0 ? lhs : rhs; + if (isnan(lhs) || isnan(rhs)) { + return isnan(lhs) ? lhs : rhs; + } + if (lhs == 0 && rhs == 0) { + return signbit(lhs) ? lhs : rhs; + } } return min(lhs, rhs); } @@ -349,14 +347,12 @@ struct Maximum { auto operator()(Lhs lhs, Rhs rhs) const { if constexpr (IsFloatingPoint || IsFloatingPoint) { - if (isnan(lhs)) - return lhs; - if (isnan(rhs)) - return rhs; - if (isinf(lhs)) - return lhs > 0 ? lhs : rhs; - if (isinf(rhs)) - return rhs > 0 ? rhs : lhs; + if (isnan(lhs) || isnan(rhs)) { + return isnan(lhs) ? lhs : rhs; + } + if (lhs == 0 && rhs == 0) { + return signbit(lhs) ? rhs : lhs; + } } return max(lhs, rhs); }