]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/MathExtras.h
JavaScriptCore-521.tar.gz
[apple/javascriptcore.git] / wtf / MathExtras.h
CommitLineData
b37bf2e1 1/*
9dae56ea 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
b37bf2e1
A
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef WTF_MathExtras_h
27#define WTF_MathExtras_h
28
29#include <math.h>
30#include <stdlib.h>
b37bf2e1 31
9dae56ea 32#if PLATFORM(SOLARIS)
b37bf2e1
A
33#include <ieeefp.h>
34#endif
35
9dae56ea
A
36#if PLATFORM(OPENBSD)
37#include <sys/types.h>
38#include <machine/ieee.h>
39#endif
b37bf2e1 40
9dae56ea
A
41#if COMPILER(MSVC)
42#if PLATFORM(WIN_CE)
43#include <stdlib.h>
44#else
b37bf2e1 45#include <xmath.h>
9dae56ea 46#endif
b37bf2e1
A
47#include <limits>
48
49#if HAVE(FLOAT_H)
50#include <float.h>
51#endif
52
53#endif
54
55#ifndef M_PI
56const double piDouble = 3.14159265358979323846;
57const float piFloat = 3.14159265358979323846f;
58#else
59const double piDouble = M_PI;
60const float piFloat = static_cast<float>(M_PI);
61#endif
62
63#ifndef M_PI_4
64const double piOverFourDouble = 0.785398163397448309616;
65const float piOverFourFloat = 0.785398163397448309616f;
66#else
67const double piOverFourDouble = M_PI_4;
68const float piOverFourFloat = static_cast<float>(M_PI_4);
69#endif
70
9dae56ea
A
71#if PLATFORM(DARWIN)
72
73// Work around a bug in the Mac OS X libc where ceil(-0.1) return +0.
74inline double wtf_ceil(double x) { return copysign(ceil(x), x); }
75
76#define ceil(x) wtf_ceil(x)
77
78#endif
79
80#if PLATFORM(SOLARIS)
b37bf2e1
A
81
82#ifndef isfinite
83inline bool isfinite(double x) { return finite(x) && !isnand(x); }
84#endif
85#ifndef isinf
86inline bool isinf(double x) { return !finite(x) && !isnand(x); }
87#endif
88#ifndef signbit
89inline bool signbit(double x) { return x < 0.0; } // FIXME: Wrong for negative 0.
90#endif
91
92#endif
93
9dae56ea
A
94#if PLATFORM(OPENBSD)
95
96#ifndef isfinite
97inline bool isfinite(double x) { return finite(x); }
98#endif
99#ifndef signbit
100inline bool signbit(double x) { struct ieee_double *p = (struct ieee_double *)&x; return p->dbl_sign; }
101#endif
102
103#endif
104
105#if COMPILER(MSVC) || COMPILER(RVCT)
b37bf2e1 106
b37bf2e1
A
107inline long lround(double num) { return static_cast<long>(num > 0 ? num + 0.5 : ceil(num - 0.5)); }
108inline long lroundf(float num) { return static_cast<long>(num > 0 ? num + 0.5f : ceilf(num - 0.5f)); }
109inline double round(double num) { return num > 0 ? floor(num + 0.5) : ceil(num - 0.5); }
110inline float roundf(float num) { return num > 0 ? floorf(num + 0.5f) : ceilf(num - 0.5f); }
b37bf2e1
A
111inline double trunc(double num) { return num > 0 ? floor(num) : ceil(num); }
112
9dae56ea
A
113#endif
114
115#if COMPILER(MSVC)
116
117inline bool isinf(double num) { return !_finite(num) && !_isnan(num); }
118inline bool isnan(double num) { return !!_isnan(num); }
119inline bool signbit(double num) { return _copysign(1.0, num) < 0; }
120
b37bf2e1
A
121inline double nextafter(double x, double y) { return _nextafter(x, y); }
122inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x + FLT_EPSILON; }
123
124inline double copysign(double x, double y) { return _copysign(x, y); }
125inline int isfinite(double x) { return _finite(x); }
126
127// Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values.
128inline double wtf_atan2(double x, double y)
129{
9dae56ea
A
130 double posInf = std::numeric_limits<double>::infinity();
131 double negInf = -std::numeric_limits<double>::infinity();
132 double nan = std::numeric_limits<double>::quiet_NaN();
b37bf2e1
A
133
134 double result = nan;
135
136 if (x == posInf && y == posInf)
137 result = piOverFourDouble;
138 else if (x == posInf && y == negInf)
139 result = 3 * piOverFourDouble;
140 else if (x == negInf && y == posInf)
141 result = -piOverFourDouble;
142 else if (x == negInf && y == negInf)
143 result = -3 * piOverFourDouble;
144 else
145 result = ::atan2(x, y);
146
147 return result;
148}
149
150// Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
151inline double wtf_fmod(double x, double y) { return (!isinf(x) && isinf(y)) ? x : fmod(x, y); }
152
153// Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead of 1.
154inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); }
155
156#define atan2(x, y) wtf_atan2(x, y)
157#define fmod(x, y) wtf_fmod(x, y)
158#define pow(x, y) wtf_pow(x, y)
159
b37bf2e1
A
160#endif // COMPILER(MSVC)
161
b37bf2e1
A
162inline double deg2rad(double d) { return d * piDouble / 180.0; }
163inline double rad2deg(double r) { return r * 180.0 / piDouble; }
164inline double deg2grad(double d) { return d * 400.0 / 360.0; }
165inline double grad2deg(double g) { return g * 360.0 / 400.0; }
9dae56ea
A
166inline double turn2deg(double t) { return t * 360.0; }
167inline double deg2turn(double d) { return d / 360.0; }
b37bf2e1
A
168inline double rad2grad(double r) { return r * 200.0 / piDouble; }
169inline double grad2rad(double g) { return g * piDouble / 200.0; }
170
171inline float deg2rad(float d) { return d * piFloat / 180.0f; }
172inline float rad2deg(float r) { return r * 180.0f / piFloat; }
173inline float deg2grad(float d) { return d * 400.0f / 360.0f; }
174inline float grad2deg(float g) { return g * 360.0f / 400.0f; }
9dae56ea
A
175inline float turn2deg(float t) { return t * 360.0f; }
176inline float deg2turn(float d) { return d / 360.0f; }
b37bf2e1
A
177inline float rad2grad(float r) { return r * 200.0f / piFloat; }
178inline float grad2rad(float g) { return g * piFloat / 200.0f; }
179
180#endif // #ifndef WTF_MathExtras_h