• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006, 2007, 2008 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 <math.h>
30 #include <stdlib.h>
31 
32 #if PLATFORM(SOLARIS)
33 #include <ieeefp.h>
34 #endif
35 
36 #if PLATFORM(OPENBSD)
37 #include <sys/types.h>
38 #include <machine/ieee.h>
39 #endif
40 
41 #if COMPILER(MSVC)
42 #if PLATFORM(WINCE)
43 #include <stdlib.h>
44 #endif
45 #include <limits>
46 
47 #if HAVE(FLOAT_H)
48 #include <float.h>
49 #endif
50 
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_4
62 const double piOverFourDouble = 0.785398163397448309616;
63 const float piOverFourFloat = 0.785398163397448309616f;
64 #else
65 const double piOverFourDouble = M_PI_4;
66 const float piOverFourFloat = static_cast<float>(M_PI_4);
67 #endif
68 
69 #if PLATFORM(DARWIN)
70 
71 // Work around a bug in the Mac OS X libc where ceil(-0.1) return +0.
wtf_ceil(double x)72 inline double wtf_ceil(double x) { return copysign(ceil(x), x); }
73 
74 #define ceil(x) wtf_ceil(x)
75 
76 #endif
77 
78 #if PLATFORM(SOLARIS)
79 
80 #ifndef isfinite
isfinite(double x)81 inline bool isfinite(double x) { return finite(x) && !isnand(x); }
82 #endif
83 #ifndef isinf
isinf(double x)84 inline bool isinf(double x) { return !finite(x) && !isnand(x); }
85 #endif
86 #ifndef signbit
signbit(double x)87 inline bool signbit(double x) { return x < 0.0; } // FIXME: Wrong for negative 0.
88 #endif
89 
90 #endif
91 
92 #if PLATFORM(OPENBSD)
93 
94 #ifndef isfinite
isfinite(double x)95 inline bool isfinite(double x) { return finite(x); }
96 #endif
97 #ifndef signbit
signbit(double x)98 inline bool signbit(double x) { struct ieee_double *p = (struct ieee_double *)&x; return p->dbl_sign; }
99 #endif
100 
101 #endif
102 
103 #if COMPILER(MSVC) || COMPILER(RVCT)
104 
lround(double num)105 inline long lround(double num) { return static_cast<long>(num > 0 ? num + 0.5 : ceil(num - 0.5)); }
lroundf(float num)106 inline long lroundf(float num) { return static_cast<long>(num > 0 ? num + 0.5f : ceilf(num - 0.5f)); }
round(double num)107 inline double round(double num) { return num > 0 ? floor(num + 0.5) : ceil(num - 0.5); }
roundf(float num)108 inline float roundf(float num) { return num > 0 ? floorf(num + 0.5f) : ceilf(num - 0.5f); }
trunc(double num)109 inline double trunc(double num) { return num > 0 ? floor(num) : ceil(num); }
110 
111 #endif
112 
113 #if COMPILER(MSVC)
114 
isinf(double num)115 inline bool isinf(double num) { return !_finite(num) && !_isnan(num); }
isnan(double num)116 inline bool isnan(double num) { return !!_isnan(num); }
signbit(double num)117 inline bool signbit(double num) { return _copysign(1.0, num) < 0; }
118 
nextafter(double x,double y)119 inline double nextafter(double x, double y) { return _nextafter(x, y); }
nextafterf(float x,float y)120 inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x + FLT_EPSILON; }
121 
copysign(double x,double y)122 inline double copysign(double x, double y) { return _copysign(x, y); }
isfinite(double x)123 inline int isfinite(double x) { return _finite(x); }
124 
125 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values.
wtf_atan2(double x,double y)126 inline double wtf_atan2(double x, double y)
127 {
128     double posInf = std::numeric_limits<double>::infinity();
129     double negInf = -std::numeric_limits<double>::infinity();
130     double nan = std::numeric_limits<double>::quiet_NaN();
131 
132     double result = nan;
133 
134     if (x == posInf && y == posInf)
135         result = piOverFourDouble;
136     else if (x == posInf && y == negInf)
137         result = 3 * piOverFourDouble;
138     else if (x == negInf && y == posInf)
139         result = -piOverFourDouble;
140     else if (x == negInf && y == negInf)
141         result = -3 * piOverFourDouble;
142     else
143         result = ::atan2(x, y);
144 
145     return result;
146 }
147 
148 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
wtf_fmod(double x,double y)149 inline double wtf_fmod(double x, double y) { return (!isinf(x) && isinf(y)) ? x : fmod(x, y); }
150 
151 // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead of 1.
wtf_pow(double x,double y)152 inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); }
153 
154 #define atan2(x, y) wtf_atan2(x, y)
155 #define fmod(x, y) wtf_fmod(x, y)
156 #define pow(x, y) wtf_pow(x, y)
157 
158 #endif // COMPILER(MSVC)
159 
deg2rad(double d)160 inline double deg2rad(double d)  { return d * piDouble / 180.0; }
rad2deg(double r)161 inline double rad2deg(double r)  { return r * 180.0 / piDouble; }
deg2grad(double d)162 inline double deg2grad(double d) { return d * 400.0 / 360.0; }
grad2deg(double g)163 inline double grad2deg(double g) { return g * 360.0 / 400.0; }
turn2deg(double t)164 inline double turn2deg(double t) { return t * 360.0; }
deg2turn(double d)165 inline double deg2turn(double d) { return d / 360.0; }
rad2grad(double r)166 inline double rad2grad(double r) { return r * 200.0 / piDouble; }
grad2rad(double g)167 inline double grad2rad(double g) { return g * piDouble / 200.0; }
168 
deg2rad(float d)169 inline float deg2rad(float d)  { return d * piFloat / 180.0f; }
rad2deg(float r)170 inline float rad2deg(float r)  { return r * 180.0f / piFloat; }
deg2grad(float d)171 inline float deg2grad(float d) { return d * 400.0f / 360.0f; }
grad2deg(float g)172 inline float grad2deg(float g) { return g * 360.0f / 400.0f; }
turn2deg(float t)173 inline float turn2deg(float t) { return t * 360.0f; }
deg2turn(float d)174 inline float deg2turn(float d) { return d / 360.0f; }
rad2grad(float r)175 inline float rad2grad(float r) { return r * 200.0f / piFloat; }
grad2rad(float g)176 inline float grad2rad(float g) { return g * piFloat / 200.0f; }
177 
178 #endif // #ifndef WTF_MathExtras_h
179