Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
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 "jsnum.h"
15 :
16 : /*
17 : * Use system provided math routines.
18 : */
19 :
20 : /* The right copysign function is not always named the same thing. */
21 : #ifdef __GNUC__
22 : #define js_copysign __builtin_copysign
23 : #elif defined _WIN32
24 : #define js_copysign _copysign
25 : #else
26 : #define js_copysign copysign
27 : #endif
28 :
29 : /* Consistency wrapper for platform deviations in fmod() */
30 : static inline double
31 0 : js_fmod(double d, double d2)
32 : {
33 : #ifdef XP_WIN
34 : /*
35 : * Workaround MS fmod bug where 42 % (1/0) => NaN, not 42.
36 : * Workaround MS fmod bug where -0 % -N => 0, not -0.
37 : */
38 : if ((mozilla::IsFinite(d) && mozilla::IsInfinite(d2)) ||
39 : (d == 0 && mozilla::IsFinite(d2))) {
40 : return d;
41 : }
42 : #endif
43 0 : return fmod(d, d2);
44 : }
45 :
46 : namespace js {
47 :
48 : inline double
49 42 : NumberDiv(double a, double b)
50 : {
51 42 : if (b == 0) {
52 0 : if (a == 0 || mozilla::IsNaN(a)
53 : #ifdef XP_WIN
54 : || mozilla::IsNaN(b) /* XXX MSVC miscompiles such that (NaN == 0) */
55 : #endif
56 : )
57 0 : return JS::GenericNaN();
58 :
59 0 : if (mozilla::IsNegative(a) != mozilla::IsNegative(b))
60 0 : return mozilla::NegativeInfinity<double>();
61 0 : return mozilla::PositiveInfinity<double>();
62 : }
63 :
64 42 : return a / b;
65 : }
66 :
67 : inline double
68 0 : NumberMod(double a, double b) {
69 0 : if (b == 0)
70 0 : return JS::GenericNaN();
71 0 : return js_fmod(a, b);
72 : }
73 :
74 : } // namespace js
75 :
76 : #endif /* jslibmath_h */
|