Issue #2241 - Part 2: Add SameValueZero implementation to mfbt/FloatingPoint.h

Backported from Mozilla bug 1560658.

This is to prevent duplication of code while implementing DOMMatrix operations.
This commit is contained in:
Job Bautista 2023-05-10 19:22:39 +08:00 • committed by roytam1
commit 621868a340
2 changed files with 27 additions and 0 deletions

View file

@ -8,6 +8,8 @@
#ifndef js_Equality_h #ifndef js_Equality_h
#define js_Equality_h #define js_Equality_h
#include "mozilla/FloatingPoint.h"
#include "jstypes.h" // JS_PUBLIC_API #include "jstypes.h" // JS_PUBLIC_API
#include "js/RootingAPI.h" // JS::Handle #include "js/RootingAPI.h" // JS::Handle
@ -50,6 +52,17 @@ LooselyEqual(JSContext* cx, JS::Handle<JS::Value> v1, JS::Handle<JS::Value> v2,
extern JS_PUBLIC_API(bool) extern JS_PUBLIC_API(bool)
SameValue(JSContext* cx, JS::Handle<JS::Value> v1, JS::Handle<JS::Value> v2, bool* same); SameValue(JSContext* cx, JS::Handle<JS::Value> v1, JS::Handle<JS::Value> 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 } // namespace JS
#endif /* js_Equality_h */ #endif /* js_Equality_h */

View file

@ -396,6 +396,20 @@ NumbersAreIdentical(T aValue1, T aValue2)
return BitwiseCast<Bits>(aValue1) == BitwiseCast<Bits>(aValue2); return BitwiseCast<Bits>(aValue1) == BitwiseCast<Bits>(aValue2);
} }
/**
* Return true if |aValue| and |aValue2| are equal (ignoring sign if both are
* zero) or both NaN.
*/
template <typename T>
static inline bool
EqualOrBothNaN(T aValue1, T aValue2)
{
if (IsNaN(aValue1)) {
return IsNaN(aValue2);
}
return aValue1 == aValue2;
}
namespace detail { namespace detail {
template<typename T> template<typename T>