Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef jslibmath_h |
michael@0 | 8 | #define jslibmath_h |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/FloatingPoint.h" |
michael@0 | 11 | |
michael@0 | 12 | #include <math.h> |
michael@0 | 13 | |
michael@0 | 14 | #include "jsnum.h" |
michael@0 | 15 | |
michael@0 | 16 | /* |
michael@0 | 17 | * Use system provided math routines. |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | /* The right copysign function is not always named the same thing. */ |
michael@0 | 21 | #ifdef __GNUC__ |
michael@0 | 22 | #define js_copysign __builtin_copysign |
michael@0 | 23 | #elif defined _WIN32 |
michael@0 | 24 | #define js_copysign _copysign |
michael@0 | 25 | #else |
michael@0 | 26 | #define js_copysign copysign |
michael@0 | 27 | #endif |
michael@0 | 28 | |
michael@0 | 29 | /* Consistency wrapper for platform deviations in fmod() */ |
michael@0 | 30 | static inline double |
michael@0 | 31 | js_fmod(double d, double d2) |
michael@0 | 32 | { |
michael@0 | 33 | #ifdef XP_WIN |
michael@0 | 34 | /* |
michael@0 | 35 | * Workaround MS fmod bug where 42 % (1/0) => NaN, not 42. |
michael@0 | 36 | * Workaround MS fmod bug where -0 % -N => 0, not -0. |
michael@0 | 37 | */ |
michael@0 | 38 | if ((mozilla::IsFinite(d) && mozilla::IsInfinite(d2)) || |
michael@0 | 39 | (d == 0 && mozilla::IsFinite(d2))) { |
michael@0 | 40 | return d; |
michael@0 | 41 | } |
michael@0 | 42 | #endif |
michael@0 | 43 | return fmod(d, d2); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | namespace js { |
michael@0 | 47 | |
michael@0 | 48 | inline double |
michael@0 | 49 | NumberDiv(double a, double b) |
michael@0 | 50 | { |
michael@0 | 51 | if (b == 0) { |
michael@0 | 52 | if (a == 0 || mozilla::IsNaN(a) |
michael@0 | 53 | #ifdef XP_WIN |
michael@0 | 54 | || mozilla::IsNaN(b) /* XXX MSVC miscompiles such that (NaN == 0) */ |
michael@0 | 55 | #endif |
michael@0 | 56 | ) |
michael@0 | 57 | return JS::GenericNaN(); |
michael@0 | 58 | |
michael@0 | 59 | if (mozilla::IsNegative(a) != mozilla::IsNegative(b)) |
michael@0 | 60 | return mozilla::NegativeInfinity<double>(); |
michael@0 | 61 | return mozilla::PositiveInfinity<double>(); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | return a / b; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | inline double |
michael@0 | 68 | NumberMod(double a, double b) { |
michael@0 | 69 | if (b == 0) |
michael@0 | 70 | return JS::GenericNaN(); |
michael@0 | 71 | return js_fmod(a, b); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | #endif /* jslibmath_h */ |