source: webkit/trunk/JavaScriptCore/wtf/MathExtras.h@ 38975

Last change on this file since 38975 was 38959, checked in by [email protected], 16 years ago

2008-12-03 Dean Jackson <[email protected]>

Reviewed by Dan Bernstein.

JavaScriptCore:

Helper functions for turn -> degrees.
https://bugs.webkit.org/show_bug.cgi?id=22497

  • wtf/MathExtras.h: (turn2deg): (deg2turn):

WebCore:

Implement CSS 3 <angle> turn unit and support it
in rotations.
https://bugs.webkit.org/show_bug.cgi?id=22497

  • css/CSSGrammar.y:
  • css/CSSParser.cpp: (WebCore::CSSParser::validUnit): (WebCore::unitFromString): (WebCore::CSSParser::lex):
  • css/CSSPrimitiveValue.cpp: (WebCore::CSSPrimitiveValue::cssText): (WebCore::CSSPrimitiveValue::parserValue):
  • css/CSSPrimitiveValue.h: (WebCore::CSSPrimitiveValue::):
  • css/CSSStyleSelector.cpp: (WebCore::CSSStyleSelector::createTransformOperations):
  • css/tokenizer.flex:

LayoutTests:

Tests for CSS 3 <angle> turn unit
https://bugs.webkit.org/show_bug.cgi?id=22497

  • transforms/2d/transform-2d-expected.txt:
  • transforms/2d/transform-2d.html:
  • transforms/transform-value-types-expected.txt:
  • transforms/transform-value-types.html:
  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
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#include <time.h>
32
33#if PLATFORM(SOLARIS)
34#include <ieeefp.h>
35#endif
36
37#if PLATFORM(OPENBSD)
38#include <sys/types.h>
39#include <machine/ieee.h>
40#endif
41
42#if COMPILER(MSVC)
43#if PLATFORM(WIN_CE)
44#include <stdlib.h>
45#else
46#include <xmath.h>
47#endif
48#include <limits>
49
50#if HAVE(FLOAT_H)
51#include <float.h>
52#endif
53
54#endif
55
56#ifndef M_PI
57const double piDouble = 3.14159265358979323846;
58const float piFloat = 3.14159265358979323846f;
59#else
60const double piDouble = M_PI;
61const float piFloat = static_cast<float>(M_PI);
62#endif
63
64#ifndef M_PI_4
65const double piOverFourDouble = 0.785398163397448309616;
66const float piOverFourFloat = 0.785398163397448309616f;
67#else
68const double piOverFourDouble = M_PI_4;
69const float piOverFourFloat = static_cast<float>(M_PI_4);
70#endif
71
72#if PLATFORM(DARWIN)
73
74// Work around a bug in the Mac OS X libc where ceil(-0.1) return +0.
75inline double wtf_ceil(double x) { return copysign(ceil(x), x); }
76
77#define ceil(x) wtf_ceil(x)
78
79#endif
80
81#if PLATFORM(SOLARIS)
82
83#ifndef isfinite
84inline bool isfinite(double x) { return finite(x) && !isnand(x); }
85#endif
86#ifndef isinf
87inline bool isinf(double x) { return !finite(x) && !isnand(x); }
88#endif
89#ifndef signbit
90inline bool signbit(double x) { return x < 0.0; } // FIXME: Wrong for negative 0.
91#endif
92
93#endif
94
95#if PLATFORM(OPENBSD)
96
97#ifndef isfinite
98inline bool isfinite(double x) { return finite(x); }
99#endif
100#ifndef signbit
101inline bool signbit(double x) { struct ieee_double *p = (struct ieee_double *)&x; return p->dbl_sign; }
102#endif
103
104#endif
105
106#if COMPILER(MSVC) || COMPILER(RVCT)
107
108inline long lround(double num) { return static_cast<long>(num > 0 ? num + 0.5 : ceil(num - 0.5)); }
109inline long lroundf(float num) { return static_cast<long>(num > 0 ? num + 0.5f : ceilf(num - 0.5f)); }
110inline double round(double num) { return num > 0 ? floor(num + 0.5) : ceil(num - 0.5); }
111inline float roundf(float num) { return num > 0 ? floorf(num + 0.5f) : ceilf(num - 0.5f); }
112inline double trunc(double num) { return num > 0 ? floor(num) : ceil(num); }
113
114#endif
115
116#if COMPILER(MSVC)
117
118inline bool isinf(double num) { return !_finite(num) && !_isnan(num); }
119inline bool isnan(double num) { return !!_isnan(num); }
120inline bool signbit(double num) { return _copysign(1.0, num) < 0; }
121
122inline double nextafter(double x, double y) { return _nextafter(x, y); }
123inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x + FLT_EPSILON; }
124
125inline double copysign(double x, double y) { return _copysign(x, y); }
126inline int isfinite(double x) { return _finite(x); }
127
128// Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values.
129inline double wtf_atan2(double x, double y)
130{
131 double posInf = std::numeric_limits<double>::infinity();
132 double negInf = -std::numeric_limits<double>::infinity();
133 double nan = std::numeric_limits<double>::quiet_NaN();
134
135 double result = nan;
136
137 if (x == posInf && y == posInf)
138 result = piOverFourDouble;
139 else if (x == posInf && y == negInf)
140 result = 3 * piOverFourDouble;
141 else if (x == negInf && y == posInf)
142 result = -piOverFourDouble;
143 else if (x == negInf && y == negInf)
144 result = -3 * piOverFourDouble;
145 else
146 result = ::atan2(x, y);
147
148 return result;
149}
150
151// Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
152inline double wtf_fmod(double x, double y) { return (!isinf(x) && isinf(y)) ? x : fmod(x, y); }
153
154// Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead of 1.
155inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); }
156
157#define atan2(x, y) wtf_atan2(x, y)
158#define fmod(x, y) wtf_fmod(x, y)
159#define pow(x, y) wtf_pow(x, y)
160
161#if defined(_CRT_RAND_S)
162// Initializes the random number generator.
163inline void wtf_random_init()
164{
165 // No need to initialize for rand_s.
166}
167
168// Returns a pseudo-random number in the range [0, 1).
169inline double wtf_random()
170{
171 unsigned u;
172 rand_s(&u);
173
174 return static_cast<double>(u) / (static_cast<double>(UINT_MAX) + 1.0);
175}
176#endif // _CRT_RAND_S
177
178#endif // COMPILER(MSVC)
179
180#if !COMPILER(MSVC) || !defined(_CRT_RAND_S)
181
182// Initializes the random number generator.
183inline void wtf_random_init()
184{
185 srand(static_cast<unsigned>(time(0)));
186}
187
188// Returns a pseudo-random number in the range [0, 1).
189inline double wtf_random()
190{
191 return static_cast<double>(rand()) / (static_cast<double>(RAND_MAX) + 1.0);
192}
193
194#endif // #if COMPILER(MSVC)
195
196inline double deg2rad(double d) { return d * piDouble / 180.0; }
197inline double rad2deg(double r) { return r * 180.0 / piDouble; }
198inline double deg2grad(double d) { return d * 400.0 / 360.0; }
199inline double grad2deg(double g) { return g * 360.0 / 400.0; }
200inline double turn2deg(double t) { return t * 360.0; }
201inline double deg2turn(double d) { return d / 360.0; }
202inline double rad2grad(double r) { return r * 200.0 / piDouble; }
203inline double grad2rad(double g) { return g * piDouble / 200.0; }
204
205inline float deg2rad(float d) { return d * piFloat / 180.0f; }
206inline float rad2deg(float r) { return r * 180.0f / piFloat; }
207inline float deg2grad(float d) { return d * 400.0f / 360.0f; }
208inline float grad2deg(float g) { return g * 360.0f / 400.0f; }
209inline float turn2deg(float t) { return t * 360.0f; }
210inline float deg2turn(float d) { return d / 360.0f; }
211inline float rad2grad(float r) { return r * 200.0f / piFloat; }
212inline float grad2rad(float g) { return g * piFloat / 200.0f; }
213
214#endif // #ifndef WTF_MathExtras_h
Note: See TracBrowser for help on using the repository browser.