Merge "Better solution for Docomo 1339 bug." into tizen_2.1
[framework/web/webkit-efl.git] / Source / WTF / wtf / MathExtras.h
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
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 <algorithm>
30 #include <cmath>
31 #include <float.h>
32 #include <limits>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <wtf/StdLibExtras.h>
36
37 #if OS(SOLARIS)
38 #include <ieeefp.h>
39 #endif
40
41 #if OS(OPENBSD)
42 #include <sys/types.h>
43 #include <machine/ieee.h>
44 #endif
45
46 #if OS(QNX)
47 // FIXME: Look into a way to have cmath import its functions into both the standard and global
48 // namespace. For now, we include math.h since the QNX cmath header only imports its functions
49 // into the standard namespace.
50 #include <math.h>
51 #endif
52
53 #ifndef M_PI
54 const double piDouble = 3.14159265358979323846;
55 const float piFloat = 3.14159265358979323846f;
56 #else
57 const double piDouble = M_PI;
58 const float piFloat = static_cast<float>(M_PI);
59 #endif
60
61 #ifndef M_PI_2
62 const double piOverTwoDouble = 1.57079632679489661923;
63 const float piOverTwoFloat = 1.57079632679489661923f;
64 #else
65 const double piOverTwoDouble = M_PI_2;
66 const float piOverTwoFloat = static_cast<float>(M_PI_2);
67 #endif
68
69 #ifndef M_PI_4
70 const double piOverFourDouble = 0.785398163397448309616;
71 const float piOverFourFloat = 0.785398163397448309616f;
72 #else
73 const double piOverFourDouble = M_PI_4;
74 const float piOverFourFloat = static_cast<float>(M_PI_4);
75 #endif
76
77 #if OS(DARWIN)
78
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); }
81
82 #define ceil(x) wtf_ceil(x)
83
84 #endif
85
86 #if OS(SOLARIS)
87
88 #ifndef isfinite
89 inline bool isfinite(double x) { return finite(x) && !isnand(x); }
90 #endif
91 #ifndef isinf
92 inline bool isinf(double x) { return !finite(x) && !isnand(x); }
93 #endif
94 #ifndef signbit
95 inline bool signbit(double x) { return copysign(1.0, x) < 0; }
96 #endif
97
98 #endif
99
100 #if OS(OPENBSD)
101
102 #ifndef isfinite
103 inline bool isfinite(double x) { return finite(x); }
104 #endif
105 #ifndef signbit
106 inline bool signbit(double x) { struct ieee_double *p = (struct ieee_double *)&x; return p->dbl_sign; }
107 #endif
108
109 #endif
110
111 #if COMPILER(MSVC) || (COMPILER(RVCT) && !(RVCT_VERSION_AT_LEAST(3, 0, 0, 0)))
112
113 // We must not do 'num + 0.5' or 'num - 0.5' because they can cause precision loss.
114 static double round(double num)
115 {
116     double integer = ceil(num);
117     if (num > 0)
118         return integer - num > 0.5 ? integer - 1.0 : integer;
119     return integer - num >= 0.5 ? integer - 1.0 : integer;
120 }
121 static float roundf(float num)
122 {
123     float integer = ceilf(num);
124     if (num > 0)
125         return integer - num > 0.5f ? integer - 1.0f : integer;
126     return integer - num >= 0.5f ? integer - 1.0f : integer;
127 }
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); }
133
134 #endif
135
136 #if COMPILER(GCC) && OS(QNX)
137 // The stdlib on QNX doesn't contain long abs(long). See PR #104666.
138 inline long long abs(long num) { return labs(num); }
139 #endif
140
141 #if OS(ANDROID) || COMPILER(MSVC)
142 // ANDROID and MSVC's math.h does not currently supply log2 or log2f.
143 inline double log2(double num)
144 {
145     // This constant is roughly M_LN2, which is not provided by default on Windows and Android.
146     return log(num) / 0.693147180559945309417232121458176568;
147 }
148
149 inline float log2f(float num)
150 {
151     // This constant is roughly M_LN2, which is not provided by default on Windows and Android.
152     return logf(num) / 0.693147180559945309417232121458176568f;
153 }
154 #endif
155
156 #if COMPILER(MSVC)
157 // The 64bit version of abs() is already defined in stdlib.h which comes with VC10
158 #if COMPILER(MSVC9_OR_LOWER)
159 inline long long abs(long long num) { return _abs64(num); }
160 #endif
161
162 inline bool isinf(double num) { return !_finite(num) && !_isnan(num); }
163 inline bool isnan(double num) { return !!_isnan(num); }
164 inline bool signbit(double num) { return _copysign(1.0, num) < 0; }
165
166 inline double nextafter(double x, double y) { return _nextafter(x, y); }
167 inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x + FLT_EPSILON; }
168
169 inline double copysign(double x, double y) { return _copysign(x, y); }
170 inline int isfinite(double x) { return _finite(x); }
171
172 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values.
173 inline double wtf_atan2(double x, double y)
174 {
175     double posInf = std::numeric_limits<double>::infinity();
176     double negInf = -std::numeric_limits<double>::infinity();
177     double nan = std::numeric_limits<double>::quiet_NaN();
178
179     double result = nan;
180
181     if (x == posInf && y == posInf)
182         result = piOverFourDouble;
183     else if (x == posInf && y == negInf)
184         result = 3 * piOverFourDouble;
185     else if (x == negInf && y == posInf)
186         result = -piOverFourDouble;
187     else if (x == negInf && y == negInf)
188         result = -3 * piOverFourDouble;
189     else
190         result = ::atan2(x, y);
191
192     return result;
193 }
194
195 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
196 inline double wtf_fmod(double x, double y) { return (!isinf(x) && isinf(y)) ? x : fmod(x, y); }
197
198 // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead of 1.
199 inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); }
200
201 #define atan2(x, y) wtf_atan2(x, y)
202 #define fmod(x, y) wtf_fmod(x, y)
203 #define pow(x, y) wtf_pow(x, y)
204
205 // MSVC's math functions do not bring lrint.
206 inline long int lrint(double flt)
207 {
208     int64_t intgr;
209 #if CPU(X86)
210     __asm {
211         fld flt
212         fistp intgr
213     };
214 #else
215     ASSERT(isfinite(flt));
216     double rounded = round(flt);
217     intgr = static_cast<int64_t>(rounded);
218     // If the fractional part is exactly 0.5, we need to check whether
219     // the rounded result is even. If it is not we need to add 1 to
220     // negative values and subtract one from positive values.
221     if (fabs(intgr - flt) == 0.5 & intgr)
222         intgr -= ((intgr >> 62) | 1); // 1 with the sign of result, i.e. -1 or 1.
223 #endif
224     return static_cast<long int>(intgr);
225 }
226
227 #endif // COMPILER(MSVC)
228
229 inline double deg2rad(double d)  { return d * piDouble / 180.0; }
230 inline double rad2deg(double r)  { return r * 180.0 / piDouble; }
231 inline double deg2grad(double d) { return d * 400.0 / 360.0; }
232 inline double grad2deg(double g) { return g * 360.0 / 400.0; }
233 inline double turn2deg(double t) { return t * 360.0; }
234 inline double deg2turn(double d) { return d / 360.0; }
235 inline double rad2grad(double r) { return r * 200.0 / piDouble; }
236 inline double grad2rad(double g) { return g * piDouble / 200.0; }
237
238 inline float deg2rad(float d)  { return d * piFloat / 180.0f; }
239 inline float rad2deg(float r)  { return r * 180.0f / piFloat; }
240 inline float deg2grad(float d) { return d * 400.0f / 360.0f; }
241 inline float grad2deg(float g) { return g * 360.0f / 400.0f; }
242 inline float turn2deg(float t) { return t * 360.0f; }
243 inline float deg2turn(float d) { return d / 360.0f; }
244 inline float rad2grad(float r) { return r * 200.0f / piFloat; }
245 inline float grad2rad(float g) { return g * piFloat / 200.0f; }
246
247 // std::numeric_limits<T>::min() returns the smallest positive value for floating point types
248 template<typename T> inline T defaultMinimumForClamp() { return std::numeric_limits<T>::min(); }
249 template<> inline float defaultMinimumForClamp() { return -std::numeric_limits<float>::max(); }
250 template<> inline double defaultMinimumForClamp() { return -std::numeric_limits<double>::max(); }
251 template<typename T> inline T defaultMaximumForClamp() { return std::numeric_limits<T>::max(); }
252
253 template<typename T> inline T clampTo(double value, T min = defaultMinimumForClamp<T>(), T max = defaultMaximumForClamp<T>())
254 {
255     if (value >= static_cast<double>(max))
256         return max;
257     if (value <= static_cast<double>(min))
258         return min;
259     return static_cast<T>(value);
260 }
261 template<> inline long long int clampTo(double, long long int, long long int); // clampTo does not support long long ints.
262
263 inline int clampToInteger(double value)
264 {
265     return clampTo<int>(value);
266 }
267
268 inline float clampToFloat(double value)
269 {
270     return clampTo<float>(value);
271 }
272
273 inline int clampToPositiveInteger(double value)
274 {
275     return clampTo<int>(value, 0);
276 }
277
278 inline int clampToInteger(float value)
279 {
280     return clampTo<int>(value);
281 }
282
283 inline int clampToInteger(unsigned x)
284 {
285     const unsigned intMax = static_cast<unsigned>(std::numeric_limits<int>::max());
286
287     if (x >= intMax)
288         return std::numeric_limits<int>::max();
289     return static_cast<int>(x);
290 }
291
292 inline bool isWithinIntRange(float x)
293 {
294     return x > static_cast<float>(std::numeric_limits<int>::min()) && x < static_cast<float>(std::numeric_limits<int>::max());
295 }
296
297 #if !COMPILER(MSVC) && !COMPILER(RVCT) && !OS(SOLARIS)
298 using std::isfinite;
299 #if !COMPILER_QUIRK(GCC11_GLOBAL_ISINF_ISNAN)
300 using std::isinf;
301 using std::isnan;
302 #endif
303 using std::signbit;
304 #endif
305
306 #if COMPILER_QUIRK(GCC11_GLOBAL_ISINF_ISNAN)
307 // A workaround to avoid conflicting declarations of isinf and isnan when compiling with GCC in C++11 mode.
308 namespace std {
309     inline bool wtf_isinf(float f) { return std::isinf(f); }
310     inline bool wtf_isinf(double d) { return std::isinf(d); }
311     inline bool wtf_isnan(float f) { return std::isnan(f); }
312     inline bool wtf_isnan(double d) { return std::isnan(d); }
313 };
314
315 using std::wtf_isinf;
316 using std::wtf_isnan;
317
318 #define isinf(x) wtf_isinf(x)
319 #define isnan(x) wtf_isnan(x)
320 #endif
321
322 #ifndef UINT64_C
323 #if COMPILER(MSVC)
324 #define UINT64_C(c) c ## ui64
325 #else
326 #define UINT64_C(c) c ## ull
327 #endif
328 #endif
329
330
331 // decompose 'number' to its sign, exponent, and mantissa components.
332 // The result is interpreted as:
333 //     (sign ? -1 : 1) * pow(2, exponent) * (mantissa / (1 << 52))
334 inline void decomposeDouble(double number, bool& sign, int32_t& exponent, uint64_t& mantissa)
335 {
336     ASSERT(isfinite(number));
337
338     sign = signbit(number);
339
340     uint64_t bits = WTF::bitwise_cast<uint64_t>(number);
341     exponent = (static_cast<int32_t>(bits >> 52) & 0x7ff) - 0x3ff;
342     mantissa = bits & 0xFFFFFFFFFFFFFull;
343
344     // Check for zero/denormal values; if so, adjust the exponent,
345     // if not insert the implicit, omitted leading 1 bit.
346     if (exponent == -0x3ff)
347         exponent = mantissa ? -0x3fe : 0;
348     else
349         mantissa |= 0x10000000000000ull;
350 }
351
352 // Calculate d % 2^{64}.
353 inline void doubleToInteger(double d, unsigned long long& value)
354 {
355     if (isnan(d) || isinf(d))
356         value = 0;
357     else {
358         // -2^{64} < fmodValue < 2^{64}.
359         double fmodValue = fmod(trunc(d), std::numeric_limits<unsigned long long>::max() + 1.0);
360         if (fmodValue >= 0) {
361             // 0 <= fmodValue < 2^{64}.
362             // 0 <= value < 2^{64}. This cast causes no loss.
363             value = static_cast<unsigned long long>(fmodValue);
364         } else {
365             // -2^{64} < fmodValue < 0.
366             // 0 < fmodValueInUnsignedLongLong < 2^{64}. This cast causes no loss.
367             unsigned long long fmodValueInUnsignedLongLong = static_cast<unsigned long long>(-fmodValue);
368             // -1 < (std::numeric_limits<unsigned long long>::max() - fmodValueInUnsignedLongLong) < 2^{64} - 1.
369             // 0 < value < 2^{64}.
370             value = std::numeric_limits<unsigned long long>::max() - fmodValueInUnsignedLongLong + 1;
371         }
372     }
373 }
374
375 #endif // #ifndef WTF_MathExtras_h