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

Side by Side Diff: include/core/SkScalar.h

Issue 117053002: remove SK_SCALAR_IS_[FLOAT,FIXED] and assume floats (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years 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 | Annotate | Revision Log
OLDNEW
1
2 /* 1 /*
3 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
4 * 3 *
5 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 5 * found in the LICENSE file.
7 */ 6 */
8 7
9
10 #ifndef SkScalar_DEFINED 8 #ifndef SkScalar_DEFINED
11 #define SkScalar_DEFINED 9 #define SkScalar_DEFINED
12 10
13 #include "SkFixed.h" 11 #include "SkFixed.h"
14 #include "SkFloatingPoint.h" 12 #include "SkFloatingPoint.h"
15 13
16 /** \file SkScalar.h 14 typedef float SkScalar;
17 15
18 Types and macros for the data type SkScalar. This is the fractional numeric type 16 /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar
19 that, depending on the compile-time flag SK_SCALAR_IS_FLOAT, may be implemen ted
20 either as an IEEE float, or as a 16.16 SkFixed. The macros in this file are written
21 to allow the calling code to manipulate SkScalar values without knowing whic h representation
22 is in effect.
23 */ 17 */
18 #define SK_Scalar1 (1.0f)
19 /** SK_Scalar1 is defined to be 1/2 represented as an SkScalar
20 */
21 #define SK_ScalarHalf (0.5f)
22 /** SK_ScalarInfinity is defined to be infinity as an SkScalar
23 */
24 #define SK_ScalarInfinity SK_FloatInfinity
25 /** SK_ScalarNegativeInfinity is defined to be negative infinity as an SkScalar
26 */
27 #define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity
28 /** SK_ScalarMax is defined to be the largest value representable as an SkScalar
29 */
30 #define SK_ScalarMax (3.402823466e+38f)
31 /** SK_ScalarMin is defined to be the smallest value representable as an SkScala r
32 */
33 #define SK_ScalarMin (-SK_ScalarMax)
34 /** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar
35 */
36 #define SK_ScalarNaN SK_FloatNaN
37 /** SkScalarIsNaN(n) returns true if argument is not a number
38 */
39 static inline bool SkScalarIsNaN(float x) { return x != x; }
24 40
25 #ifdef SK_SCALAR_IS_FLOAT 41 /** Returns true if x is not NaN and not infinite */
42 static inline bool SkScalarIsFinite(float x) {
43 // We rely on the following behavior of infinities and nans
44 // 0 * finite --> 0
45 // 0 * infinity --> NaN
46 // 0 * NaN --> NaN
47 float prod = x * 0;
48 // At this point, prod will either be NaN or 0
49 // Therefore we can return (prod == prod) or (0 == prod).
50 return prod == prod;
51 }
26 52
27 /** SkScalar is our type for fractional values and coordinates. Depending on 53 /** SkIntToScalar(n) returns its integer argument as an SkScalar
28 compile configurations, it is either represented as an IEEE float, or 54 */
29 as a 16.16 fixed point integer. 55 #define SkIntToScalar(n) ((float)(n))
30 */ 56 /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
31 typedef float SkScalar; 57 */
58 #define SkFixedToScalar(x) SkFixedToFloat(x)
59 /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed
60 */
61 #define SkScalarToFixed(x) SkFloatToFixed(x)
32 62
33 /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar 63 #define SkScalarToFloat(n) (n)
34 */
35 #define SK_Scalar1 (1.0f)
36 /** SK_Scalar1 is defined to be 1/2 represented as an SkScalar
37 */
38 #define SK_ScalarHalf (0.5f)
39 /** SK_ScalarInfinity is defined to be infinity as an SkScalar
40 */
41 #define SK_ScalarInfinity SK_FloatInfinity
42 /** SK_ScalarNegativeInfinity is defined to be negative infinity as an SkSca lar
43 */
44 #define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity
45 /** SK_ScalarMax is defined to be the largest value representable as an SkSc alar
46 */
47 #define SK_ScalarMax (3.402823466e+38f)
48 /** SK_ScalarMin is defined to be the smallest value representable as an SkS calar
49 */
50 #define SK_ScalarMin (-SK_ScalarMax)
51 /** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar
52 */
53 #define SK_ScalarNaN SK_FloatNaN
54 /** SkScalarIsNaN(n) returns true if argument is not a number
55 */
56 static inline bool SkScalarIsNaN(float x) { return x != x; }
57
58 /** Returns true if x is not NaN and not infinite */
59 static inline bool SkScalarIsFinite(float x) {
60 // We rely on the following behavior of infinities and nans
61 // 0 * finite --> 0
62 // 0 * infinity --> NaN
63 // 0 * NaN --> NaN
64 float prod = x * 0;
65 // At this point, prod will either be NaN or 0
66 // Therefore we can return (prod == prod) or (0 == prod).
67 return prod == prod;
68 }
69
70 /** SkIntToScalar(n) returns its integer argument as an SkScalar
71 */
72 #define SkIntToScalar(n) ((float)(n))
73 /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
74 */
75 #define SkFixedToScalar(x) SkFixedToFloat(x)
76 /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed
77 */
78 #define SkScalarToFixed(x) SkFloatToFixed(x)
79
80 #define SkScalarToFloat(n) (n)
81 #ifndef SK_SCALAR_TO_FLOAT_EXCLUDED 64 #ifndef SK_SCALAR_TO_FLOAT_EXCLUDED
82 #define SkFloatToScalar(n) (n) 65 #define SkFloatToScalar(n) (n)
83 #endif 66 #endif
84 67
85 #define SkScalarToDouble(n) (double)(n) 68 #define SkScalarToDouble(n) (double)(n)
86 #define SkDoubleToScalar(n) (float)(n) 69 #define SkDoubleToScalar(n) (float)(n)
87 70
88 /** SkScalarFraction(x) returns the signed fractional part of the argument 71 /** SkScalarFraction(x) returns the signed fractional part of the argument
89 */ 72 */
90 #define SkScalarFraction(x) sk_float_mod(x, 1.0f) 73 #define SkScalarFraction(x) sk_float_mod(x, 1.0f)
91 74
92 #define SkScalarFloorToScalar(x) sk_float_floor(x) 75 #define SkScalarFloorToScalar(x) sk_float_floor(x)
93 #define SkScalarCeilToScalar(x) sk_float_ceil(x) 76 #define SkScalarCeilToScalar(x) sk_float_ceil(x)
94 #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f) 77 #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f)
95 78
96 #define SkScalarFloorToInt(x) sk_float_floor2int(x) 79 #define SkScalarFloorToInt(x) sk_float_floor2int(x)
97 #define SkScalarCeilToInt(x) sk_float_ceil2int(x) 80 #define SkScalarCeilToInt(x) sk_float_ceil2int(x)
98 #define SkScalarRoundToInt(x) sk_float_round2int(x) 81 #define SkScalarRoundToInt(x) sk_float_round2int(x)
99 #define SkScalarTruncToInt(x) static_cast<int>(x) 82 #define SkScalarTruncToInt(x) static_cast<int>(x)
100 83
101 /** Returns the absolute value of the specified SkScalar 84 /** Returns the absolute value of the specified SkScalar
102 */ 85 */
103 #define SkScalarAbs(x) sk_float_abs(x) 86 #define SkScalarAbs(x) sk_float_abs(x)
104 /** Return x with the sign of y 87 /** Return x with the sign of y
105 */ 88 */
106 #define SkScalarCopySign(x, y) sk_float_copysign(x, y) 89 #define SkScalarCopySign(x, y) sk_float_copysign(x, y)
107 /** Returns the value pinned between 0 and max inclusive 90 /** Returns the value pinned between 0 and max inclusive
108 */ 91 */
109 inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { 92 inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
110 return x < 0 ? 0 : x > max ? max : x; 93 return x < 0 ? 0 : x > max ? max : x;
111 } 94 }
112 /** Returns the value pinned between min and max inclusive 95 /** Returns the value pinned between min and max inclusive
113 */ 96 */
114 inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { 97 inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
115 return x < min ? min : x > max ? max : x; 98 return x < min ? min : x > max ? max : x;
116 } 99 }
117 /** Returns the specified SkScalar squared (x*x) 100 /** Returns the specified SkScalar squared (x*x)
118 */ 101 */
119 inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } 102 inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
120 /** Returns the product of two SkScalars 103 /** Returns the product of two SkScalars
121 */ 104 */
122 #define SkScalarMul(a, b) ((float)(a) * (b)) 105 #define SkScalarMul(a, b) ((float)(a) * (b))
123 /** Returns the product of two SkScalars plus a third SkScalar 106 /** Returns the product of two SkScalars plus a third SkScalar
124 */ 107 */
125 #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c)) 108 #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
126 /** Returns the product of a SkScalar and an int rounded to the nearest inte ger value 109 /** Returns the product of a SkScalar and an int rounded to the nearest integer value
127 */ 110 */
128 #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b)) 111 #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b))
129 /** Returns the product of a SkScalar and an int promoted to the next larger int 112 /** Returns the product of a SkScalar and an int promoted to the next larger int
130 */ 113 */
131 #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b)) 114 #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b))
132 /** Returns the product of a SkScalar and an int truncated to the next small er int 115 /** Returns the product of a SkScalar and an int truncated to the next smaller i nt
133 */ 116 */
134 #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b)) 117 #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b))
135 /** Returns the quotient of two SkScalars (a/b) 118 /** Returns the quotient of two SkScalars (a/b)
136 */ 119 */
137 #define SkScalarDiv(a, b) ((float)(a) / (b)) 120 #define SkScalarDiv(a, b) ((float)(a) / (b))
138 /** Returns the mod of two SkScalars (a mod b) 121 /** Returns the mod of two SkScalars (a mod b)
139 */ 122 */
140 #define SkScalarMod(x,y) sk_float_mod(x,y) 123 #define SkScalarMod(x,y) sk_float_mod(x,y)
141 /** Returns the product of the first two arguments, divided by the third arg ument 124 /** Returns the product of the first two arguments, divided by the third argumen t
142 */ 125 */
143 #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c)) 126 #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
144 /** Returns the multiplicative inverse of the SkScalar (1/x) 127 /** Returns the multiplicative inverse of the SkScalar (1/x)
145 */ 128 */
146 #define SkScalarInvert(x) (SK_Scalar1 / (x)) 129 #define SkScalarInvert(x) (SK_Scalar1 / (x))
147 #define SkScalarFastInvert(x) (SK_Scalar1 / (x)) 130 #define SkScalarFastInvert(x) (SK_Scalar1 / (x))
148 /** Returns the square root of the SkScalar 131 /** Returns the square root of the SkScalar
149 */ 132 */
150 #define SkScalarSqrt(x) sk_float_sqrt(x) 133 #define SkScalarSqrt(x) sk_float_sqrt(x)
151 /** Returns b to the e 134 /** Returns b to the e
152 */ 135 */
153 #define SkScalarPow(b, e) sk_float_pow(b, e) 136 #define SkScalarPow(b, e) sk_float_pow(b, e)
154 /** Returns the average of two SkScalars (a+b)/2 137 /** Returns the average of two SkScalars (a+b)/2
155 */ 138 */
156 #define SkScalarAve(a, b) (((a) + (b)) * 0.5f) 139 #define SkScalarAve(a, b) (((a) + (b)) * 0.5f)
157 /** Returns the geometric mean of two SkScalars 140 /** Returns the geometric mean of two SkScalars
158 */ 141 */
159 #define SkScalarMean(a, b) sk_float_sqrt((float)(a) * (b)) 142 #define SkScalarMean(a, b) sk_float_sqrt((float)(a) * (b))
160 /** Returns one half of the specified SkScalar 143 /** Returns one half of the specified SkScalar
161 */ 144 */
162 #define SkScalarHalf(a) ((a) * 0.5f) 145 #define SkScalarHalf(a) ((a) * 0.5f)
163 146
164 #define SK_ScalarSqrt2 1.41421356f 147 #define SK_ScalarSqrt2 1.41421356f
165 #define SK_ScalarPI 3.14159265f 148 #define SK_ScalarPI 3.14159265f
166 #define SK_ScalarTanPIOver8 0.414213562f 149 #define SK_ScalarTanPIOver8 0.414213562f
167 #define SK_ScalarRoot2Over2 0.707106781f 150 #define SK_ScalarRoot2Over2 0.707106781f
168 151
169 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) 152 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
170 float SkScalarSinCos(SkScalar radians, SkScalar* cosValue); 153 float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
171 #define SkScalarSin(radians) (float)sk_float_sin(radians) 154 #define SkScalarSin(radians) (float)sk_float_sin(radians)
172 #define SkScalarCos(radians) (float)sk_float_cos(radians) 155 #define SkScalarCos(radians) (float)sk_float_cos(radians)
173 #define SkScalarTan(radians) (float)sk_float_tan(radians) 156 #define SkScalarTan(radians) (float)sk_float_tan(radians)
174 #define SkScalarASin(val) (float)sk_float_asin(val) 157 #define SkScalarASin(val) (float)sk_float_asin(val)
175 #define SkScalarACos(val) (float)sk_float_acos(val) 158 #define SkScalarACos(val) (float)sk_float_acos(val)
176 #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) 159 #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
177 #define SkScalarExp(x) (float)sk_float_exp(x) 160 #define SkScalarExp(x) (float)sk_float_exp(x)
178 #define SkScalarLog(x) (float)sk_float_log(x) 161 #define SkScalarLog(x) (float)sk_float_log(x)
179 162
180 inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } 163 inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
181 inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } 164 inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
182 165
183 static inline bool SkScalarIsInt(SkScalar x) { 166 static inline bool SkScalarIsInt(SkScalar x) {
184 return x == (float)(int)x; 167 return x == (float)(int)x;
185 } 168 }
186 #else
187 typedef SkFixed SkScalar;
188
189 #define SK_Scalar1 SK_Fixed1
190 #define SK_ScalarHalf SK_FixedHalf
191 #define SK_ScalarInfinity SK_FixedMax
192 #define SK_ScalarNegativeInfinity SK_FixedMin
193 #define SK_ScalarMax SK_FixedMax
194 #define SK_ScalarMin SK_FixedMin
195 #define SK_ScalarNaN SK_FixedNaN
196 #define SkScalarIsNaN(x) ((x) == SK_FixedNaN)
197 #define SkScalarIsFinite(x) ((x) != SK_FixedNaN)
198
199 #define SkIntToScalar(n) SkIntToFixed(n)
200 #define SkFixedToScalar(x) (x)
201 #define SkScalarToFixed(x) (x)
202 #define SkScalarToFloat(n) SkFixedToFloat(n)
203 #ifndef SK_SCALAR_TO_FLOAT_EXCLUDED
204 #define SkFloatToScalar(n) SkFloatToFixed(n)
205 #endif
206
207 #define SkScalarToDouble(n) SkFixedToDouble(n)
208 #define SkDoubleToScalar(n) SkDoubleToFixed(n)
209 #define SkScalarFraction(x) SkFixedFraction(x)
210
211 #define SkScalarFloorToScalar(x) SkFixedFloorToFixed(x)
212 #define SkScalarCeilToScalar(x) SkFixedCeilToFixed(x)
213 #define SkScalarRoundToScalar(x) SkFixedRoundToFixed(x)
214
215 #define SkScalarFloorToInt(x) SkFixedFloorToInt(x)
216 #define SkScalarCeilToInt(x) SkFixedCeilToInt(x)
217 #define SkScalarRoundToInt(x) SkFixedRoundToInt(x)
218 #define SkScalarTruncToInt(x) (((x) < 0) ? SkScalarCeilToInt(x) : SkSc alarFloorToInt(x))
219
220 #define SkScalarAbs(x) SkFixedAbs(x)
221 #define SkScalarCopySign(x, y) SkCopySign32(x, y)
222 #define SkScalarClampMax(x, max) SkClampMax(x, max)
223 #define SkScalarPin(x, min, max) SkPin32(x, min, max)
224 #define SkScalarSquare(x) SkFixedSquare(x)
225 #define SkScalarMul(a, b) SkFixedMul(a, b)
226 #define SkScalarMulAdd(a, b, c) SkFixedMulAdd(a, b, c)
227 #define SkScalarMulRound(a, b) SkFixedMulCommon(a, b, SK_FixedHalf)
228 #define SkScalarMulCeil(a, b) SkFixedMulCommon(a, b, SK_Fixed1 - 1)
229 #define SkScalarMulFloor(a, b) SkFixedMulCommon(a, b, 0)
230 #define SkScalarDiv(a, b) SkFixedDiv(a, b)
231 #define SkScalarMod(a, b) SkFixedMod(a, b)
232 #define SkScalarMulDiv(a, b, c) SkMulDiv(a, b, c)
233 #define SkScalarInvert(x) SkFixedInvert(x)
234 #define SkScalarFastInvert(x) SkFixedFastInvert(x)
235 #define SkScalarSqrt(x) SkFixedSqrt(x)
236 #define SkScalarAve(a, b) SkFixedAve(a, b)
237 #define SkScalarMean(a, b) SkFixedMean(a, b)
238 #define SkScalarHalf(a) ((a) >> 1)
239
240 #define SK_ScalarSqrt2 SK_FixedSqrt2
241 #define SK_ScalarPI SK_FixedPI
242 #define SK_ScalarTanPIOver8 SK_FixedTanPIOver8
243 #define SK_ScalarRoot2Over2 SK_FixedRoot2Over2
244
245 #define SkDegreesToRadians(degrees) SkFractMul(degrees, SK_FractPIOver18 0)
246 #define SkScalarSinCos(radians, cosPtr) SkFixedSinCos(radians, cosPtr)
247 #define SkScalarSin(radians) SkFixedSin(radians)
248 #define SkScalarCos(radians) SkFixedCos(radians)
249 #define SkScalarTan(val) SkFixedTan(val)
250 #define SkScalarASin(val) SkFixedASin(val)
251 #define SkScalarACos(val) SkFixedACos(val)
252 #define SkScalarATan2(y, x) SkFixedATan2(y,x)
253 #define SkScalarExp(x) SkFixedExp(x)
254 #define SkScalarLog(x) SkFixedLog(x)
255
256 #define SkMaxScalar(a, b) SkMax32(a, b)
257 #define SkMinScalar(a, b) SkMin32(a, b)
258
259 static inline bool SkScalarIsInt(SkFixed x) {
260 return 0 == (x & 0xffff);
261 }
262 #endif
263 169
264 // DEPRECATED : use ToInt or ToScalar variant 170 // DEPRECATED : use ToInt or ToScalar variant
265 #define SkScalarFloor(x) SkScalarFloorToInt(x) 171 #define SkScalarFloor(x) SkScalarFloorToInt(x)
266 #define SkScalarCeil(x) SkScalarCeilToInt(x) 172 #define SkScalarCeil(x) SkScalarCeilToInt(x)
267 #define SkScalarRound(x) SkScalarRoundToInt(x) 173 #define SkScalarRound(x) SkScalarRoundToInt(x)
268 174
269 /** 175 /**
270 * Returns -1 || 0 || 1 depending on the sign of value: 176 * Returns -1 || 0 || 1 depending on the sign of value:
271 * -1 if x < 0 177 * -1 if x < 0
272 * 0 if x == 0 178 * 0 if x == 0
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 keys, the first one will be used. However, that may change if a binary 228 keys, the first one will be used. However, that may change if a binary
323 search is used. 229 search is used.
324 */ 230 */
325 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[], 231 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
326 const SkScalar values[], int length); 232 const SkScalar values[], int length);
327 233
328 /* 234 /*
329 * Helper to compare an array of scalars. 235 * Helper to compare an array of scalars.
330 */ 236 */
331 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) { 237 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
332 #ifdef SK_SCALAR_IS_FLOAT
333 SkASSERT(n >= 0); 238 SkASSERT(n >= 0);
334 for (int i = 0; i < n; ++i) { 239 for (int i = 0; i < n; ++i) {
335 if (a[i] != b[i]) { 240 if (a[i] != b[i]) {
336 return false; 241 return false;
337 } 242 }
338 } 243 }
339 return true; 244 return true;
340 #else
341 return 0 == memcmp(a, b, n * sizeof(SkScalar));
342 #endif
343 } 245 }
344 246
345 #endif 247 #endif
OLDNEW
« no previous file with comments | « include/core/SkRect.h ('k') | include/core/SkString.h » ('j') | src/core/SkCanvas.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698