Bug 1874684 - Part 24: Prevent arbitrary loops in NormalizedTimeDurationToDays. r...
[gecko.git] / js / src / jslibmath.h
blob5c6416ef36e7e55a20c82880a9b5034d1d8a578b
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef jslibmath_h
8 #define jslibmath_h
10 #include "mozilla/FloatingPoint.h"
12 #include <math.h>
14 #include "js/Value.h"
15 #include "vm/JSContext.h"
17 namespace js {
19 inline double NumberDiv(double a, double b) {
20 AutoUnsafeCallWithABI unsafe;
21 if (b == 0) {
22 if (a == 0 || std::isnan(a)) {
23 return JS::GenericNaN();
25 if (mozilla::IsNegative(a) != mozilla::IsNegative(b)) {
26 return mozilla::NegativeInfinity<double>();
28 return mozilla::PositiveInfinity<double>();
31 return a / b;
34 inline double NumberMod(double a, double b) {
35 AutoUnsafeCallWithABI unsafe;
36 if (b == 0) {
37 return JS::GenericNaN();
39 double r = fmod(a, b);
40 #if defined(XP_WIN)
41 // Some versions of Windows (Win 10 v1803, v1809) miscompute the sign of zero
42 // results from fmod. The sign should match the sign of the LHS. This bug
43 // only affects 64-bit builds. See bug 1527007.
44 if (mozilla::IsPositiveZero(r) && mozilla::IsNegative(a)) {
45 return -0.0;
47 #endif
48 return r;
51 } // namespace js
53 #endif /* jslibmath_h */