]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/MathExtras.h
JavaScriptCore-721.26.tar.gz
[apple/javascriptcore.git] / wtf / MathExtras.h
CommitLineData
b37bf2e1 1/*
4e4e5a6f 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 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
4e4e5a6f 29#include <cmath>
f9bf01c6 30#include <float.h>
b37bf2e1 31#include <stdlib.h>
b37bf2e1 32
f9bf01c6 33#if OS(SOLARIS)
b37bf2e1
A
34#include <ieeefp.h>
35#endif
36
f9bf01c6 37#if OS(OPENBSD)
9dae56ea
A
38#include <sys/types.h>
39#include <machine/ieee.h>
40#endif
b37bf2e1 41
9dae56ea 42#if COMPILER(MSVC)
f9bf01c6 43#if OS(WINCE)
9dae56ea 44#include <stdlib.h>
9dae56ea 45#endif
b37bf2e1 46#include <limits>
b37bf2e1
A
47#endif
48
49#ifndef M_PI
50const double piDouble = 3.14159265358979323846;
51const float piFloat = 3.14159265358979323846f;
52#else
53const double piDouble = M_PI;
54const float piFloat = static_cast<float>(M_PI);
55#endif
56
57#ifndef M_PI_4
58const double piOverFourDouble = 0.785398163397448309616;
59const float piOverFourFloat = 0.785398163397448309616f;
60#else
61const double piOverFourDouble = M_PI_4;
62const float piOverFourFloat = static_cast<float>(M_PI_4);
63#endif
64
f9bf01c6 65#if OS(DARWIN)
9dae56ea
A
66
67// Work around a bug in the Mac OS X libc where ceil(-0.1) return +0.
68inline double wtf_ceil(double x) { return copysign(ceil(x), x); }
69
70#define ceil(x) wtf_ceil(x)
71
72#endif
73
f9bf01c6 74#if OS(SOLARIS)
b37bf2e1
A
75
76#ifndef isfinite
77inline bool isfinite(double x) { return finite(x) && !isnand(x); }
78#endif
79#ifndef isinf
80inline bool isinf(double x) { return !finite(x) && !isnand(x); }
81#endif
82#ifndef signbit
83inline bool signbit(double x) { return x < 0.0; } // FIXME: Wrong for negative 0.
84#endif
85
86#endif
87
f9bf01c6 88#if OS(OPENBSD)
9dae56ea
A
89
90#ifndef isfinite
91inline bool isfinite(double x) { return finite(x); }
92#endif
93#ifndef signbit
94inline bool signbit(double x) { struct ieee_double *p = (struct ieee_double *)&x; return p->dbl_sign; }
95#endif
96
97#endif
98
99#if COMPILER(MSVC) || COMPILER(RVCT)
b37bf2e1 100
4e4e5a6f
A
101// We must not do 'num + 0.5' or 'num - 0.5' because they can cause precision loss.
102static double round(double num)
103{
104 double integer = ceil(num);
105 if (num > 0)
106 return integer - num > 0.5 ? integer - 1.0 : integer;
107 return integer - num >= 0.5 ? integer - 1.0 : integer;
108}
109static float roundf(float num)
110{
111 float integer = ceilf(num);
112 if (num > 0)
113 return integer - num > 0.5f ? integer - 1.0f : integer;
114 return integer - num >= 0.5f ? integer - 1.0f : integer;
115}
116inline long long llround(double num) { return static_cast<long long>(round(num)); }
117inline long long llroundf(float num) { return static_cast<long long>(roundf(num)); }
118inline long lround(double num) { return static_cast<long>(round(num)); }
119inline long lroundf(float num) { return static_cast<long>(roundf(num)); }
b37bf2e1
A
120inline double trunc(double num) { return num > 0 ? floor(num) : ceil(num); }
121
9dae56ea
A
122#endif
123
124#if COMPILER(MSVC)
4e4e5a6f
A
125// The 64bit version of abs() is already defined in stdlib.h which comes with VC10
126#if COMPILER(MSVC9_OR_LOWER)
127inline long long abs(long long num) { return _abs64(num); }
128#endif
9dae56ea
A
129
130inline bool isinf(double num) { return !_finite(num) && !_isnan(num); }
131inline bool isnan(double num) { return !!_isnan(num); }
132inline bool signbit(double num) { return _copysign(1.0, num) < 0; }
133
b37bf2e1
A
134inline double nextafter(double x, double y) { return _nextafter(x, y); }
135inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x + FLT_EPSILON; }
136
137inline double copysign(double x, double y) { return _copysign(x, y); }
138inline int isfinite(double x) { return _finite(x); }
139
140// Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values.
141inline double wtf_atan2(double x, double y)
142{
9dae56ea
A
143 double posInf = std::numeric_limits<double>::infinity();
144 double negInf = -std::numeric_limits<double>::infinity();
145 double nan = std::numeric_limits<double>::quiet_NaN();
b37bf2e1
A
146
147 double result = nan;
148
149 if (x == posInf && y == posInf)
150 result = piOverFourDouble;
151 else if (x == posInf && y == negInf)
152 result = 3 * piOverFourDouble;
153 else if (x == negInf && y == posInf)
154 result = -piOverFourDouble;
155 else if (x == negInf && y == negInf)
156 result = -3 * piOverFourDouble;
157 else
158 result = ::atan2(x, y);
159
160 return result;
161}
162
163// Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
164inline double wtf_fmod(double x, double y) { return (!isinf(x) && isinf(y)) ? x : fmod(x, y); }
165
166// Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead of 1.
167inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); }
168
169#define atan2(x, y) wtf_atan2(x, y)
170#define fmod(x, y) wtf_fmod(x, y)
171#define pow(x, y) wtf_pow(x, y)
172
b37bf2e1
A
173#endif // COMPILER(MSVC)
174
b37bf2e1
A
175inline double deg2rad(double d) { return d * piDouble / 180.0; }
176inline double rad2deg(double r) { return r * 180.0 / piDouble; }
177inline double deg2grad(double d) { return d * 400.0 / 360.0; }
178inline double grad2deg(double g) { return g * 360.0 / 400.0; }
9dae56ea
A
179inline double turn2deg(double t) { return t * 360.0; }
180inline double deg2turn(double d) { return d / 360.0; }
b37bf2e1
A
181inline double rad2grad(double r) { return r * 200.0 / piDouble; }
182inline double grad2rad(double g) { return g * piDouble / 200.0; }
183
184inline float deg2rad(float d) { return d * piFloat / 180.0f; }
185inline float rad2deg(float r) { return r * 180.0f / piFloat; }
186inline float deg2grad(float d) { return d * 400.0f / 360.0f; }
187inline float grad2deg(float g) { return g * 360.0f / 400.0f; }
9dae56ea
A
188inline float turn2deg(float t) { return t * 360.0f; }
189inline float deg2turn(float d) { return d / 360.0f; }
b37bf2e1
A
190inline float rad2grad(float r) { return r * 200.0f / piFloat; }
191inline float grad2rad(float g) { return g * piFloat / 200.0f; }
192
4e4e5a6f
A
193#if !COMPILER(MSVC) && !COMPILER(WINSCW) && !(COMPILER(RVCT) && OS(SYMBIAN))
194using std::isfinite;
195using std::isinf;
196using std::isnan;
197using std::signbit;
198#endif
199
b37bf2e1 200#endif // #ifndef WTF_MathExtras_h