mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 20:45:14 +00:00
LibWasm: Make Absolute/Negate<SignedIntegral> explicitly work mod 2^N
Previously we relied on signed overflow, this commit makes the same behaviour explicit (avoiding UB in the process).
This commit is contained in:
parent
99824eae14
commit
8c8310f0bd
Notes:
sideshowbarker
2024-07-17 01:46:00 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/LadybirdBrowser/ladybird/commit/8c8310f0bd Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/665
1 changed files with 24 additions and 2 deletions
|
@ -505,14 +505,36 @@ struct PopCount {
|
|||
|
||||
struct Absolute {
|
||||
template<typename Lhs>
|
||||
auto operator()(Lhs lhs) const { return AK::abs(lhs); }
|
||||
Lhs operator()(Lhs lhs) const
|
||||
{
|
||||
if constexpr (IsFloatingPoint<Lhs>)
|
||||
return AK::abs(lhs);
|
||||
|
||||
if constexpr (IsSigned<Lhs>) {
|
||||
if (lhs == NumericLimits<Lhs>::min())
|
||||
return NumericLimits<Lhs>::min(); // Return the negation of _i_ modulo 2^N: https://www.w3.org/TR/wasm-core-2/#-hrefop-iabsmathrmiabs_n-i step 3
|
||||
}
|
||||
|
||||
return AK::abs(lhs);
|
||||
}
|
||||
|
||||
static StringView name() { return "abs"sv; }
|
||||
};
|
||||
|
||||
struct Negate {
|
||||
template<typename Lhs>
|
||||
auto operator()(Lhs lhs) const { return -lhs; }
|
||||
Lhs operator()(Lhs lhs) const
|
||||
{
|
||||
if constexpr (IsFloatingPoint<Lhs>)
|
||||
return -lhs;
|
||||
|
||||
if constexpr (IsSigned<Lhs>) {
|
||||
if (lhs == NumericLimits<Lhs>::min())
|
||||
return NumericLimits<Lhs>::min(); // Return the negation of _i_ modulo 2^N: https://www.w3.org/TR/wasm-core-2/#-hrefop-iabsmathrmiabs_n-i step 3
|
||||
}
|
||||
|
||||
return -lhs;
|
||||
}
|
||||
|
||||
static StringView name() { return "== 0"sv; }
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue