jslibmath.h (1364B)
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/. */ 6 7 #ifndef jslibmath_h 8 #define jslibmath_h 9 10 #include "mozilla/FloatingPoint.h" 11 12 #include <math.h> 13 14 #include "js/Value.h" 15 #include "vm/JSContext.h" 16 17 namespace js { 18 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(); 24 } 25 if (mozilla::IsNegative(a) != mozilla::IsNegative(b)) { 26 return mozilla::NegativeInfinity<double>(); 27 } 28 return mozilla::PositiveInfinity<double>(); 29 } 30 31 return a / b; 32 } 33 34 inline double NumberMod(double a, double b) { 35 AutoUnsafeCallWithABI unsafe; 36 if (b == 0) { 37 return JS::GenericNaN(); 38 } 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; 46 } 47 #endif 48 return r; 49 } 50 51 } // namespace js 52 53 #endif /* jslibmath_h */