static double max_double(double x, double y) { // Math.max(num, NaN) => NaN, Math.max(-0, +0) => +0 if (x > y || IsNaN(x) || (x == y && IsNegative(y))) return x; return y; }
double js::math_max_impl(double x, double y) { // Math.max(num, NaN) => NaN, Math.max(-0, +0) => +0 if (x > y || IsNaN(x) || (x == y && IsNegative(y))) return x; return y; }
T js::GetBiggestNumberLessThan(T x) { MOZ_ASSERT(!IsNegative(x)); MOZ_ASSERT(IsFinite(x)); typedef typename mozilla::FloatingPoint<T>::Bits Bits; Bits bits = mozilla::BitwiseCast<Bits>(x); MOZ_ASSERT(bits > 0, "will underflow"); return mozilla::BitwiseCast<T>(bits - 1); }
bool js_math_max(JSContext *cx, unsigned argc, Value *vp) { CallArgs args = CallArgsFromVp(argc, vp); double maxval = NegativeInfinity<double>(); for (unsigned i = 0; i < args.length(); i++) { double x; if (!ToNumber(cx, args[i], &x)) return false; // Math.max(num, NaN) => NaN, Math.max(-0, +0) => +0 if (x > maxval || IsNaN(x) || (x == maxval && IsNegative(maxval))) maxval = x; } args.rval().setNumber(maxval); return true; }
static void TestPredicates() { MOZ_ASSERT(IsNaN(UnspecifiedNaN())); MOZ_ASSERT(IsNaN(SpecificNaN(1, 17)));; MOZ_ASSERT(IsNaN(SpecificNaN(0, 0xfffffffffff0fULL))); MOZ_ASSERT(!IsNaN(0)); MOZ_ASSERT(!IsNaN(-0.0)); MOZ_ASSERT(!IsNaN(1.0)); MOZ_ASSERT(!IsNaN(PositiveInfinity())); MOZ_ASSERT(!IsNaN(NegativeInfinity())); MOZ_ASSERT(IsInfinite(PositiveInfinity())); MOZ_ASSERT(IsInfinite(NegativeInfinity())); MOZ_ASSERT(!IsInfinite(UnspecifiedNaN())); MOZ_ASSERT(!IsInfinite(0)); MOZ_ASSERT(!IsInfinite(-0.0)); MOZ_ASSERT(!IsInfinite(1.0)); MOZ_ASSERT(!IsFinite(PositiveInfinity())); MOZ_ASSERT(!IsFinite(NegativeInfinity())); MOZ_ASSERT(!IsFinite(UnspecifiedNaN())); MOZ_ASSERT(IsFinite(0)); MOZ_ASSERT(IsFinite(-0.0)); MOZ_ASSERT(IsFinite(1.0)); MOZ_ASSERT(!IsNegative(PositiveInfinity())); MOZ_ASSERT(IsNegative(NegativeInfinity())); MOZ_ASSERT(IsNegative(-0.0)); MOZ_ASSERT(!IsNegative(0.0)); MOZ_ASSERT(IsNegative(-1.0)); MOZ_ASSERT(!IsNegative(1.0)); MOZ_ASSERT(!IsNegativeZero(PositiveInfinity())); MOZ_ASSERT(!IsNegativeZero(NegativeInfinity())); MOZ_ASSERT(!IsNegativeZero(SpecificNaN(1, 17)));; MOZ_ASSERT(!IsNegativeZero(SpecificNaN(1, 0xfffffffffff0fULL))); MOZ_ASSERT(!IsNegativeZero(SpecificNaN(0, 17)));; MOZ_ASSERT(!IsNegativeZero(SpecificNaN(0, 0xfffffffffff0fULL))); MOZ_ASSERT(!IsNegativeZero(UnspecifiedNaN())); MOZ_ASSERT(IsNegativeZero(-0.0)); MOZ_ASSERT(!IsNegativeZero(0.0)); MOZ_ASSERT(!IsNegativeZero(-1.0)); MOZ_ASSERT(!IsNegativeZero(1.0)); int32_t i; MOZ_ASSERT(DoubleIsInt32(0.0, &i)); MOZ_ASSERT(i == 0); MOZ_ASSERT(!DoubleIsInt32(-0.0, &i)); MOZ_ASSERT(DoubleEqualsInt32(0.0, &i)); MOZ_ASSERT(i == 0); MOZ_ASSERT(DoubleEqualsInt32(-0.0, &i)); MOZ_ASSERT(i == 0); MOZ_ASSERT(DoubleIsInt32(INT32_MIN, &i)); MOZ_ASSERT(i == INT32_MIN); MOZ_ASSERT(DoubleIsInt32(INT32_MAX, &i)); MOZ_ASSERT(i == INT32_MAX); MOZ_ASSERT(DoubleEqualsInt32(INT32_MIN, &i)); MOZ_ASSERT(i == INT32_MIN); MOZ_ASSERT(DoubleEqualsInt32(INT32_MAX, &i)); MOZ_ASSERT(i == INT32_MAX); MOZ_ASSERT(!DoubleIsInt32(0.5, &i)); MOZ_ASSERT(!DoubleIsInt32(double(INT32_MAX) + 0.1, &i)); MOZ_ASSERT(!DoubleIsInt32(double(INT32_MIN) - 0.1, &i)); MOZ_ASSERT(!DoubleIsInt32(NegativeInfinity(), &i)); MOZ_ASSERT(!DoubleIsInt32(PositiveInfinity(), &i)); MOZ_ASSERT(!DoubleIsInt32(UnspecifiedNaN(), &i)); MOZ_ASSERT(!DoubleEqualsInt32(0.5, &i)); MOZ_ASSERT(!DoubleEqualsInt32(double(INT32_MAX) + 0.1, &i)); MOZ_ASSERT(!DoubleEqualsInt32(double(INT32_MIN) - 0.1, &i)); MOZ_ASSERT(!DoubleEqualsInt32(NegativeInfinity(), &i)); MOZ_ASSERT(!DoubleEqualsInt32(PositiveInfinity(), &i)); MOZ_ASSERT(!DoubleEqualsInt32(UnspecifiedNaN(), &i)); }