Issue #1240 - Part 3b - Implement WrappingOperations.h for wraparound math operations & conversions. https://bugzilla.mozilla.org/show_bug.cgi?id=1441657 Implement mozilla::WrappingMultiply. Prerequisite for our V8 fast forward.

This commit is contained in:
Brian Smith 2023-07-13 03:27:12 -05:00 committed by roytam1
commit d42d5ce138
7 changed files with 328 additions and 10 deletions

View file

@ -13,6 +13,7 @@
#include "mozilla/MathAlgorithms.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Unused.h"
#include "mozilla/WrappingOperations.h"
#include <algorithm> // for std::max
#include <fcntl.h>
@ -103,6 +104,7 @@ using mozilla::IsNegative;
using mozilla::IsNegativeZero;
using mozilla::PositiveInfinity;
using mozilla::NegativeInfinity;
using mozilla::WrappingMultiply;
using JS::ToNumber;
using JS::GenericNaN;
@ -458,16 +460,13 @@ js::math_floor(JSContext* cx, unsigned argc, Value* vp)
bool
js::math_imul_handle(JSContext* cx, HandleValue lhs, HandleValue rhs, MutableHandleValue res)
{
uint32_t a = 0, b = 0;
if (!lhs.isUndefined() && !ToUint32(cx, lhs, &a))
int32_t a = 0, b = 0;
if (!lhs.isUndefined() && !ToInt32(cx, lhs, &a))
return false;
if (!rhs.isUndefined() && !ToUint32(cx, rhs, &b))
if (!rhs.isUndefined() && !ToInt32(cx, rhs, &b))
return false;
uint32_t product = a * b;
res.setInt32(product > INT32_MAX
? int32_t(INT32_MIN + (product - INT32_MAX - 1))
: int32_t(product));
res.setInt32(WrappingMultiply(a, b));
return true;
}