diff --git a/js/public/Equality.h b/js/public/Equality.h index 03181074de..6d2db50fa9 100644 --- a/js/public/Equality.h +++ b/js/public/Equality.h @@ -8,6 +8,8 @@ #ifndef js_Equality_h #define js_Equality_h +#include "mozilla/FloatingPoint.h" + #include "jstypes.h" // JS_PUBLIC_API #include "js/RootingAPI.h" // JS::Handle @@ -50,6 +52,17 @@ LooselyEqual(JSContext* cx, JS::Handle v1, JS::Handle v2, extern JS_PUBLIC_API(bool) SameValue(JSContext* cx, JS::Handle v1, JS::Handle v2, bool* same); +/** + * Implements |SameValueZero(v1, v2)| for Number values |v1| and |v2|. + * SameValueZero equates NaNs, equal nonzero values, and zeroes without respect + * to their signs. + */ +static inline bool +SameValueZero(double v1, double v2) +{ + return mozilla::EqualOrBothNaN(v1, v2); +} + } // namespace JS #endif /* js_Equality_h */ diff --git a/mfbt/FloatingPoint.h b/mfbt/FloatingPoint.h index 6a0e454ae7..7d73d7e848 100644 --- a/mfbt/FloatingPoint.h +++ b/mfbt/FloatingPoint.h @@ -396,6 +396,20 @@ NumbersAreIdentical(T aValue1, T aValue2) return BitwiseCast(aValue1) == BitwiseCast(aValue2); } +/** + * Return true if |aValue| and |aValue2| are equal (ignoring sign if both are + * zero) or both NaN. + */ +template +static inline bool +EqualOrBothNaN(T aValue1, T aValue2) +{ + if (IsNaN(aValue1)) { + return IsNaN(aValue2); + } + return aValue1 == aValue2; +} + namespace detail { template