2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef WTF_MathExtras_h
27 #define WTF_MathExtras_h
35 #include <wtf/StdLibExtras.h>
42 #include <sys/types.h>
43 #include <machine/ieee.h>
54 const double piDouble = 3.14159265358979323846;
55 const float piFloat = 3.14159265358979323846f;
57 const double piDouble = M_PI;
58 const float piFloat = static_cast<float>(M_PI);
62 const double piOverTwoDouble = 1.57079632679489661923;
63 const float piOverTwoFloat = 1.57079632679489661923f;
65 const double piOverTwoDouble = M_PI_2;
66 const float piOverTwoFloat = static_cast<float>(M_PI_2);
70 const double piOverFourDouble = 0.785398163397448309616;
71 const float piOverFourFloat = 0.785398163397448309616f;
73 const double piOverFourDouble = M_PI_4;
74 const float piOverFourFloat = static_cast<float>(M_PI_4);
79 // Work around a bug in the Mac OS X libc where ceil(-0.1) return +0.
80 inline double wtf_ceil(double x) { return copysign(ceil(x), x); }
82 #define ceil(x) wtf_ceil(x)
89 inline bool isfinite(double x) { return finite(x) && !isnand(x); }
92 inline bool isinf(double x) { return !finite(x) && !isnand(x); }
95 inline bool signbit(double x) { return copysign(1.0, x) < 0; }
103 inline bool isfinite(double x) { return finite(x); }
106 inline bool signbit(double x) { struct ieee_double *p = (struct ieee_double *)&x; return p->dbl_sign; }
111 #if COMPILER(MSVC) || (COMPILER(RVCT) && !(RVCT_VERSION_AT_LEAST(3, 0, 0, 0)))
113 // We must not do 'num + 0.5' or 'num - 0.5' because they can cause precision loss.
114 static double round(double num)
116 double integer = ceil(num);
118 return integer - num > 0.5 ? integer - 1.0 : integer;
119 return integer - num >= 0.5 ? integer - 1.0 : integer;
121 static float roundf(float num)
123 float integer = ceilf(num);
125 return integer - num > 0.5f ? integer - 1.0f : integer;
126 return integer - num >= 0.5f ? integer - 1.0f : integer;
128 inline long long llround(double num) { return static_cast<long long>(round(num)); }
129 inline long long llroundf(float num) { return static_cast<long long>(roundf(num)); }
130 inline long lround(double num) { return static_cast<long>(round(num)); }
131 inline long lroundf(float num) { return static_cast<long>(roundf(num)); }
132 inline double trunc(double num) { return num > 0 ? floor(num) : ceil(num); }
137 // The 64bit version of abs() is already defined in stdlib.h which comes with VC10
138 #if COMPILER(MSVC9_OR_LOWER)
139 inline long long abs(long long num) { return _abs64(num); }
142 inline bool isinf(double num) { return !_finite(num) && !_isnan(num); }
143 inline bool isnan(double num) { return !!_isnan(num); }
144 inline bool signbit(double num) { return _copysign(1.0, num) < 0; }
146 inline double nextafter(double x, double y) { return _nextafter(x, y); }
147 inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x + FLT_EPSILON; }
149 inline double copysign(double x, double y) { return _copysign(x, y); }
150 inline int isfinite(double x) { return _finite(x); }
152 // MSVC's math.h does not currently supply log2.
153 inline double log2(double num)
155 // This constant is roughly M_LN2, which is not provided by default on Windows.
156 return log(num) / 0.693147180559945309417232121458176568;
159 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values.
160 inline double wtf_atan2(double x, double y)
162 double posInf = std::numeric_limits<double>::infinity();
163 double negInf = -std::numeric_limits<double>::infinity();
164 double nan = std::numeric_limits<double>::quiet_NaN();
168 if (x == posInf && y == posInf)
169 result = piOverFourDouble;
170 else if (x == posInf && y == negInf)
171 result = 3 * piOverFourDouble;
172 else if (x == negInf && y == posInf)
173 result = -piOverFourDouble;
174 else if (x == negInf && y == negInf)
175 result = -3 * piOverFourDouble;
177 result = ::atan2(x, y);
182 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
183 inline double wtf_fmod(double x, double y) { return (!isinf(x) && isinf(y)) ? x : fmod(x, y); }
185 // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead of 1.
186 inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); }
188 #define atan2(x, y) wtf_atan2(x, y)
189 #define fmod(x, y) wtf_fmod(x, y)
190 #define pow(x, y) wtf_pow(x, y)
192 #endif // COMPILER(MSVC)
194 inline double deg2rad(double d) { return d * piDouble / 180.0; }
195 inline double rad2deg(double r) { return r * 180.0 / piDouble; }
196 inline double deg2grad(double d) { return d * 400.0 / 360.0; }
197 inline double grad2deg(double g) { return g * 360.0 / 400.0; }
198 inline double turn2deg(double t) { return t * 360.0; }
199 inline double deg2turn(double d) { return d / 360.0; }
200 inline double rad2grad(double r) { return r * 200.0 / piDouble; }
201 inline double grad2rad(double g) { return g * piDouble / 200.0; }
203 inline float deg2rad(float d) { return d * piFloat / 180.0f; }
204 inline float rad2deg(float r) { return r * 180.0f / piFloat; }
205 inline float deg2grad(float d) { return d * 400.0f / 360.0f; }
206 inline float grad2deg(float g) { return g * 360.0f / 400.0f; }
207 inline float turn2deg(float t) { return t * 360.0f; }
208 inline float deg2turn(float d) { return d / 360.0f; }
209 inline float rad2grad(float r) { return r * 200.0f / piFloat; }
210 inline float grad2rad(float g) { return g * piFloat / 200.0f; }
212 // std::numeric_limits<T>::min() returns the smallest positive value for floating point types
213 template<typename T> inline T defaultMinimumForClamp() { return std::numeric_limits<T>::min(); }
214 template<> inline float defaultMinimumForClamp() { return -std::numeric_limits<float>::max(); }
215 template<> inline double defaultMinimumForClamp() { return -std::numeric_limits<double>::max(); }
216 template<typename T> inline T defaultMaximumForClamp() { return std::numeric_limits<T>::max(); }
218 template<typename T> inline T clampTo(double value, T min = defaultMinimumForClamp<T>(), T max = defaultMaximumForClamp<T>())
220 if (value >= static_cast<double>(max))
222 if (value <= static_cast<double>(min))
224 return static_cast<T>(value);
226 template<> inline long long int clampTo(double, long long int, long long int); // clampTo does not support long long ints.
228 inline int clampToInteger(double value)
230 return clampTo<int>(value);
233 inline float clampToFloat(double value)
235 return clampTo<float>(value);
238 inline int clampToPositiveInteger(double value)
240 return clampTo<int>(value, 0);
243 inline int clampToInteger(float value)
245 return clampTo<int>(value);
248 inline int clampToInteger(unsigned x)
250 const unsigned intMax = static_cast<unsigned>(std::numeric_limits<int>::max());
253 return std::numeric_limits<int>::max();
254 return static_cast<int>(x);
257 inline bool isWithinIntRange(float x)
259 return x > static_cast<float>(std::numeric_limits<int>::min()) && x < static_cast<float>(std::numeric_limits<int>::max());
262 #if !COMPILER(MSVC) && !(COMPILER(RVCT) && PLATFORM(BREWMP)) && !OS(SOLARIS) && !OS(SYMBIAN)
269 // decompose 'number' to its sign, exponent, and mantissa components.
270 // The result is interpreted as:
271 // (sign ? -1 : 1) * pow(2, exponent) * (mantissa / (1 << 52))
272 inline void decomposeDouble(double number, bool& sign, int32_t& exponent, uint64_t& mantissa)
274 ASSERT(isfinite(number));
276 sign = signbit(number);
278 uint64_t bits = WTF::bitwise_cast<uint64_t>(number);
279 exponent = (static_cast<int32_t>(bits >> 52) & 0x7ff) - 0x3ff;
280 mantissa = bits & 0xFFFFFFFFFFFFFull;
282 // Check for zero/denormal values; if so, adjust the exponent,
283 // if not insert the implicit, omitted leading 1 bit.
284 if (exponent == -0x3ff)
285 exponent = mantissa ? -0x3fe : 0;
287 mantissa |= 0x10000000000000ull;
290 #endif // #ifndef WTF_MathExtras_h