Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: sky/engine/wtf/MathExtras.h

Issue 709603006: Remove a bunch of OS(MACOSX) code (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Even more Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 12 matching lines...) Expand all
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #ifndef WTF_MathExtras_h 26 #ifndef WTF_MathExtras_h
27 #define WTF_MathExtras_h 27 #define WTF_MathExtras_h
28 28
29 #include "wtf/CPU.h" 29 #include "wtf/CPU.h"
30 #include <cmath> 30 #include <cmath>
31 #include <limits> 31 #include <limits>
32 32
33 #if COMPILER(MSVC)
34 #include "wtf/Assertions.h"
35 #include <stdint.h>
36 #endif
37
38 #if OS(OPENBSD)
39 #include <sys/types.h>
40 #include <machine/ieee.h>
41 #endif
42
43 const double piDouble = M_PI; 33 const double piDouble = M_PI;
44 const float piFloat = static_cast<float>(M_PI); 34 const float piFloat = static_cast<float>(M_PI);
45 35
46 const double piOverTwoDouble = M_PI_2; 36 const double piOverTwoDouble = M_PI_2;
47 const float piOverTwoFloat = static_cast<float>(M_PI_2); 37 const float piOverTwoFloat = static_cast<float>(M_PI_2);
48 38
49 const double piOverFourDouble = M_PI_4; 39 const double piOverFourDouble = M_PI_4;
50 const float piOverFourFloat = static_cast<float>(M_PI_4); 40 const float piOverFourFloat = static_cast<float>(M_PI_4);
51 41
52 const double twoPiDouble = piDouble * 2.0; 42 const double twoPiDouble = piDouble * 2.0;
53 const float twoPiFloat = piFloat * 2.0f; 43 const float twoPiFloat = piFloat * 2.0f;
54 44
55 #if OS(MACOSX) 45 #if OS(ANDROID)
56
57 // Work around a bug in the Mac OS X libc where ceil(-0.1) return +0.
58 inline double wtf_ceil(double x) { return copysign(ceil(x), x); }
59
60 #define ceil(x) wtf_ceil(x)
61
62 #endif
63
64 #if OS(OPENBSD)
65
66 namespace std {
67
68 #ifndef isfinite
69 inline bool isfinite(double x) { return finite(x); }
70 #endif
71 #ifndef signbit
72 inline bool signbit(double x) { struct ieee_double *p = (struct ieee_double *)&x ; return p->dbl_sign; }
73 #endif
74
75 } // namespace std
76
77 #endif
78
79 #if COMPILER(MSVC) && (_MSC_VER < 1800)
80
81 // We must not do 'num + 0.5' or 'num - 0.5' because they can cause precision lo ss.
82 static double round(double num)
83 {
84 double integer = ceil(num);
85 if (num > 0)
86 return integer - num > 0.5 ? integer - 1.0 : integer;
87 return integer - num >= 0.5 ? integer - 1.0 : integer;
88 }
89 static float roundf(float num)
90 {
91 float integer = ceilf(num);
92 if (num > 0)
93 return integer - num > 0.5f ? integer - 1.0f : integer;
94 return integer - num >= 0.5f ? integer - 1.0f : integer;
95 }
96 inline long long llround(double num) { return static_cast<long long>(round(num)) ; }
97 inline long long llroundf(float num) { return static_cast<long long>(roundf(num) ); }
98 inline long lround(double num) { return static_cast<long>(round(num)); }
99 inline long lroundf(float num) { return static_cast<long>(roundf(num)); }
100 inline double trunc(double num) { return num > 0 ? floor(num) : ceil(num); }
101
102 #endif
103
104 #if OS(ANDROID) || COMPILER(MSVC)
105 // ANDROID and MSVC's math.h does not currently supply log2 or log2f. 46 // ANDROID and MSVC's math.h does not currently supply log2 or log2f.
106 inline double log2(double num) 47 inline double log2(double num)
107 { 48 {
108 // This constant is roughly M_LN2, which is not provided by default on Windo ws and Android. 49 // This constant is roughly M_LN2, which is not provided by default on Windo ws and Android.
109 return log(num) / 0.693147180559945309417232121458176568; 50 return log(num) / 0.693147180559945309417232121458176568;
110 } 51 }
111 52
112 inline float log2f(float num) 53 inline float log2f(float num)
113 { 54 {
114 // This constant is roughly M_LN2, which is not provided by default on Windo ws and Android. 55 // This constant is roughly M_LN2, which is not provided by default on Windo ws and Android.
115 return logf(num) / 0.693147180559945309417232121458176568f; 56 return logf(num) / 0.693147180559945309417232121458176568f;
116 } 57 }
117 #endif 58 #endif
118 59
119 #if COMPILER(MSVC)
120
121 // VS2013 has most of the math functions now, but we still need to work
122 // around various differences in behavior of Inf.
123
124 #if _MSC_VER < 1800
125
126 namespace std {
127
128 inline bool isinf(double num) { return !_finite(num) && !_isnan(num); }
129 inline bool isnan(double num) { return !!_isnan(num); }
130 inline bool isfinite(double x) { return _finite(x); }
131 inline bool signbit(double num) { return _copysign(1.0, num) < 0; }
132
133 } // namespace std
134
135 inline double nextafter(double x, double y) { return _nextafter(x, y); }
136 inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x + FLT_EPSILON; }
137
138 inline double copysign(double x, double y) { return _copysign(x, y); }
139
140 #endif // _MSC_VER
141
142 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN inst ead of specific values.
143 inline double wtf_atan2(double x, double y)
144 {
145 double posInf = std::numeric_limits<double>::infinity();
146 double negInf = -std::numeric_limits<double>::infinity();
147 double nan = std::numeric_limits<double>::quiet_NaN();
148
149 double result = nan;
150
151 if (x == posInf && y == posInf)
152 result = piOverFourDouble;
153 else if (x == posInf && y == negInf)
154 result = 3 * piOverFourDouble;
155 else if (x == negInf && y == posInf)
156 result = -piOverFourDouble;
157 else if (x == negInf && y == negInf)
158 result = -3 * piOverFourDouble;
159 else
160 result = ::atan2(x, y);
161
162 return result;
163 }
164
165 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
166 inline double wtf_fmod(double x, double y) { return (!std::isinf(x) && std::isin f(y)) ? x : fmod(x, y); }
167
168 // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead of 1.
169 inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); }
170
171 #define atan2(x, y) wtf_atan2(x, y)
172 #define fmod(x, y) wtf_fmod(x, y)
173 #define pow(x, y) wtf_pow(x, y)
174
175 #if _MSC_VER < 1800
176
177 // MSVC's math functions do not bring lrint.
178 inline long int lrint(double flt)
179 {
180 int64_t intgr;
181 #if CPU(X86)
182 __asm {
183 fld flt
184 fistp intgr
185 };
186 #else
187 ASSERT(std::isfinite(flt));
188 double rounded = round(flt);
189 intgr = static_cast<int64_t>(rounded);
190 // If the fractional part is exactly 0.5, we need to check whether
191 // the rounded result is even. If it is not we need to add 1 to
192 // negative values and subtract one from positive values.
193 if ((fabs(intgr - flt) == 0.5) & intgr)
194 intgr -= ((intgr >> 62) | 1); // 1 with the sign of result, i.e. -1 or 1 .
195 #endif
196 return static_cast<long int>(intgr);
197 }
198
199 #endif // _MSC_VER
200
201 #endif // COMPILER(MSVC)
202
203 inline double deg2rad(double d) { return d * piDouble / 180.0; } 60 inline double deg2rad(double d) { return d * piDouble / 180.0; }
204 inline double rad2deg(double r) { return r * 180.0 / piDouble; } 61 inline double rad2deg(double r) { return r * 180.0 / piDouble; }
205 inline double deg2grad(double d) { return d * 400.0 / 360.0; } 62 inline double deg2grad(double d) { return d * 400.0 / 360.0; }
206 inline double grad2deg(double g) { return g * 360.0 / 400.0; } 63 inline double grad2deg(double g) { return g * 360.0 / 400.0; }
207 inline double turn2deg(double t) { return t * 360.0; } 64 inline double turn2deg(double t) { return t * 360.0; }
208 inline double deg2turn(double d) { return d / 360.0; } 65 inline double deg2turn(double d) { return d / 360.0; }
209 inline double rad2grad(double r) { return r * 200.0 / piDouble; } 66 inline double rad2grad(double r) { return r * 200.0 / piDouble; }
210 inline double grad2rad(double g) { return g * piDouble / 200.0; } 67 inline double grad2rad(double g) { return g * piDouble / 200.0; }
211 inline double turn2grad(double t) { return t * 400; } 68 inline double turn2grad(double t) { return t * 400; }
212 inline double grad2turn(double g) { return g / 400; } 69 inline double grad2turn(double g) { return g / 400; }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 { 138 {
282 return b ? greatestCommonDivisor(b, a % b) : a; 139 return b ? greatestCommonDivisor(b, a % b) : a;
283 } 140 }
284 141
285 inline size_t lowestCommonMultiple(size_t a, size_t b) 142 inline size_t lowestCommonMultiple(size_t a, size_t b)
286 { 143 {
287 return a && b ? a / greatestCommonDivisor(a, b) * b : 0; 144 return a && b ? a / greatestCommonDivisor(a, b) * b : 0;
288 } 145 }
289 146
290 #ifndef UINT64_C 147 #ifndef UINT64_C
291 #if COMPILER(MSVC)
292 #define UINT64_C(c) c ## ui64
293 #else
294 #define UINT64_C(c) c ## ull 148 #define UINT64_C(c) c ## ull
295 #endif 149 #endif
296 #endif
297 150
298 // Calculate d % 2^{64}. 151 // Calculate d % 2^{64}.
299 inline void doubleToInteger(double d, unsigned long long& value) 152 inline void doubleToInteger(double d, unsigned long long& value)
300 { 153 {
301 if (std::isnan(d) || std::isinf(d)) 154 if (std::isnan(d) || std::isinf(d))
302 value = 0; 155 value = 0;
303 else { 156 else {
304 // -2^{64} < fmodValue < 2^{64}. 157 // -2^{64} < fmodValue < 2^{64}.
305 double fmodValue = fmod(trunc(d), std::numeric_limits<unsigned long long >::max() + 1.0); 158 double fmodValue = fmod(trunc(d), std::numeric_limits<unsigned long long >::max() + 1.0);
306 if (fmodValue >= 0) { 159 if (fmodValue >= 0) {
(...skipping 27 matching lines...) Expand all
334 if (i >> 2) 187 if (i >> 2)
335 log2 += 2, i >>= 2; 188 log2 += 2, i >>= 2;
336 if (i >> 1) 189 if (i >> 1)
337 log2 += 1; 190 log2 += 1;
338 return log2; 191 return log2;
339 } 192 }
340 193
341 } // namespace WTF 194 } // namespace WTF
342 195
343 #endif // #ifndef WTF_MathExtras_h 196 #endif // #ifndef WTF_MathExtras_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698