michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef jslibmath_h michael@0: #define jslibmath_h michael@0: michael@0: #include "mozilla/FloatingPoint.h" michael@0: michael@0: #include michael@0: michael@0: #include "jsnum.h" michael@0: michael@0: /* michael@0: * Use system provided math routines. michael@0: */ michael@0: michael@0: /* The right copysign function is not always named the same thing. */ michael@0: #ifdef __GNUC__ michael@0: #define js_copysign __builtin_copysign michael@0: #elif defined _WIN32 michael@0: #define js_copysign _copysign michael@0: #else michael@0: #define js_copysign copysign michael@0: #endif michael@0: michael@0: /* Consistency wrapper for platform deviations in fmod() */ michael@0: static inline double michael@0: js_fmod(double d, double d2) michael@0: { michael@0: #ifdef XP_WIN michael@0: /* michael@0: * Workaround MS fmod bug where 42 % (1/0) => NaN, not 42. michael@0: * Workaround MS fmod bug where -0 % -N => 0, not -0. michael@0: */ michael@0: if ((mozilla::IsFinite(d) && mozilla::IsInfinite(d2)) || michael@0: (d == 0 && mozilla::IsFinite(d2))) { michael@0: return d; michael@0: } michael@0: #endif michael@0: return fmod(d, d2); michael@0: } michael@0: michael@0: namespace js { michael@0: michael@0: inline double michael@0: NumberDiv(double a, double b) michael@0: { michael@0: if (b == 0) { michael@0: if (a == 0 || mozilla::IsNaN(a) michael@0: #ifdef XP_WIN michael@0: || mozilla::IsNaN(b) /* XXX MSVC miscompiles such that (NaN == 0) */ michael@0: #endif michael@0: ) michael@0: return JS::GenericNaN(); michael@0: michael@0: if (mozilla::IsNegative(a) != mozilla::IsNegative(b)) michael@0: return mozilla::NegativeInfinity(); michael@0: return mozilla::PositiveInfinity(); michael@0: } michael@0: michael@0: return a / b; michael@0: } michael@0: michael@0: inline double michael@0: NumberMod(double a, double b) { michael@0: if (b == 0) michael@0: return JS::GenericNaN(); michael@0: return js_fmod(a, b); michael@0: } michael@0: michael@0: } michael@0: michael@0: #endif /* jslibmath_h */