SkScalar.h revision abcec6db9b1efa28a6f4d902cb1fba01e6b38d56
1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkScalar_DEFINED
11#define SkScalar_DEFINED
12
13#include "SkFixed.h"
14#include "SkFloatingPoint.h"
15
16/** \file SkScalar.h
17
18    Types and macros for the data type SkScalar. This is the fractional numeric type
19    that, depending on the compile-time flag SK_SCALAR_IS_FLOAT, may be implemented
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 which representation
22    is in effect.
23*/
24
25#ifdef SK_SCALAR_IS_FLOAT
26
27    /** SkScalar is our type for fractional values and coordinates. Depending on
28        compile configurations, it is either represented as an IEEE float, or
29        as a 16.16 fixed point integer.
30    */
31    typedef float   SkScalar;
32
33    /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar
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 SkScalar
43    */
44    #define SK_ScalarNegativeInfinity       SK_FloatNegativeInfinity
45    /** SK_ScalarMax is defined to be the largest value representable as an SkScalar
46    */
47    #define SK_ScalarMax            (3.402823466e+38f)
48    /** SK_ScalarMin is defined to be the smallest value representable as an SkScalar
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    #define SkFloatToScalar(n)      (n)
82
83    #define SkScalarToDouble(n)      (double)(n)
84    #define SkDoubleToScalar(n)      (float)(n)
85
86    /** SkScalarFraction(x) returns the signed fractional part of the argument
87    */
88    #define SkScalarFraction(x)     sk_float_mod(x, 1.0f)
89
90    #define SkScalarFloorToScalar(x)    sk_float_floor(x)
91    #define SkScalarCeilToScalar(x)     sk_float_ceil(x)
92    #define SkScalarRoundToScalar(x)    sk_float_floor((x) + 0.5f)
93
94    #define SkScalarFloorToInt(x)       sk_float_floor2int(x)
95    #define SkScalarCeilToInt(x)        sk_float_ceil2int(x)
96    #define SkScalarRoundToInt(x)       sk_float_round2int(x)
97    #define SkScalarTruncToInt(x)       static_cast<int>(x)
98
99    /** Returns the absolute value of the specified SkScalar
100    */
101    #define SkScalarAbs(x)          sk_float_abs(x)
102    /** Return x with the sign of y
103     */
104    #define SkScalarCopySign(x, y)  sk_float_copysign(x, y)
105    /** Returns the value pinned between 0 and max inclusive
106    */
107    inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
108        return x < 0 ? 0 : x > max ? max : x;
109    }
110    /** Returns the value pinned between min and max inclusive
111    */
112    inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
113        return x < min ? min : x > max ? max : x;
114    }
115    /** Returns the specified SkScalar squared (x*x)
116    */
117    inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
118    /** Returns the product of two SkScalars
119    */
120    #define SkScalarMul(a, b)       ((float)(a) * (b))
121    /** Returns the product of two SkScalars plus a third SkScalar
122    */
123    #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
124    /** Returns the product of a SkScalar and an int rounded to the nearest integer value
125    */
126    #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b))
127    /** Returns the product of a SkScalar and an int promoted to the next larger int
128    */
129    #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b))
130    /** Returns the product of a SkScalar and an int truncated to the next smaller int
131    */
132    #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b))
133    /** Returns the quotient of two SkScalars (a/b)
134    */
135    #define SkScalarDiv(a, b)       ((float)(a) / (b))
136    /** Returns the mod of two SkScalars (a mod b)
137    */
138    #define SkScalarMod(x,y)        sk_float_mod(x,y)
139    /** Returns the product of the first two arguments, divided by the third argument
140    */
141    #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
142    /** Returns the multiplicative inverse of the SkScalar (1/x)
143    */
144    #define SkScalarInvert(x)       (SK_Scalar1 / (x))
145    #define SkScalarFastInvert(x)   (SK_Scalar1 / (x))
146    /** Returns the square root of the SkScalar
147    */
148    #define SkScalarSqrt(x)         sk_float_sqrt(x)
149    /** Returns b to the e
150    */
151    #define SkScalarPow(b, e)       sk_float_pow(b, e)
152    /** Returns the average of two SkScalars (a+b)/2
153    */
154    #define SkScalarAve(a, b)       (((a) + (b)) * 0.5f)
155    /** Returns the geometric mean of two SkScalars
156    */
157    #define SkScalarMean(a, b)      sk_float_sqrt((float)(a) * (b))
158    /** Returns one half of the specified SkScalar
159    */
160    #define SkScalarHalf(a)         ((a) * 0.5f)
161
162    #define SK_ScalarSqrt2          1.41421356f
163    #define SK_ScalarPI             3.14159265f
164    #define SK_ScalarTanPIOver8     0.414213562f
165    #define SK_ScalarRoot2Over2     0.707106781f
166
167    #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
168    float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
169    #define SkScalarSin(radians)    (float)sk_float_sin(radians)
170    #define SkScalarCos(radians)    (float)sk_float_cos(radians)
171    #define SkScalarTan(radians)    (float)sk_float_tan(radians)
172    #define SkScalarASin(val)   (float)sk_float_asin(val)
173    #define SkScalarACos(val)   (float)sk_float_acos(val)
174    #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
175    #define SkScalarExp(x)  (float)sk_float_exp(x)
176    #define SkScalarLog(x)  (float)sk_float_log(x)
177
178    inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
179    inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
180
181    static inline bool SkScalarIsInt(SkScalar x) {
182        return x == (float)(int)x;
183    }
184#else
185    typedef SkFixed SkScalar;
186
187    #define SK_Scalar1              SK_Fixed1
188    #define SK_ScalarHalf           SK_FixedHalf
189    #define SK_ScalarInfinity           SK_FixedMax
190    #define SK_ScalarNegativeInfinity   SK_FixedMin
191    #define SK_ScalarMax            SK_FixedMax
192    #define SK_ScalarMin            SK_FixedMin
193    #define SK_ScalarNaN            SK_FixedNaN
194    #define SkScalarIsNaN(x)        ((x) == SK_FixedNaN)
195    #define SkScalarIsFinite(x)     ((x) != SK_FixedNaN)
196
197    #define SkIntToScalar(n)        SkIntToFixed(n)
198    #define SkFixedToScalar(x)      (x)
199    #define SkScalarToFixed(x)      (x)
200    #define SkScalarToFloat(n)  SkFixedToFloat(n)
201    #define SkFloatToScalar(n)  SkFloatToFixed(n)
202
203    #define SkScalarToDouble(n) SkFixedToDouble(n)
204    #define SkDoubleToScalar(n) SkDoubleToFixed(n)
205    #define SkScalarFraction(x)     SkFixedFraction(x)
206
207    #define SkScalarFloorToScalar(x)    SkFixedFloorToFixed(x)
208    #define SkScalarCeilToScalar(x)     SkFixedCeilToFixed(x)
209    #define SkScalarRoundToScalar(x)    SkFixedRoundToFixed(x)
210
211    #define SkScalarFloorToInt(x)       SkFixedFloorToInt(x)
212    #define SkScalarCeilToInt(x)        SkFixedCeilToInt(x)
213    #define SkScalarRoundToInt(x)       SkFixedRoundToInt(x)
214    #define SkScalarTruncToInt(x)       (((x) < 0) ? SkScalarCeilToInt(x) : SkScalarFloorToInt(x))
215
216    #define SkScalarAbs(x)          SkFixedAbs(x)
217    #define SkScalarCopySign(x, y)  SkCopySign32(x, y)
218    #define SkScalarClampMax(x, max) SkClampMax(x, max)
219    #define SkScalarPin(x, min, max) SkPin32(x, min, max)
220    #define SkScalarSquare(x)       SkFixedSquare(x)
221    #define SkScalarMul(a, b)       SkFixedMul(a, b)
222    #define SkScalarMulAdd(a, b, c) SkFixedMulAdd(a, b, c)
223    #define SkScalarMulRound(a, b)  SkFixedMulCommon(a, b, SK_FixedHalf)
224    #define SkScalarMulCeil(a, b)   SkFixedMulCommon(a, b, SK_Fixed1 - 1)
225    #define SkScalarMulFloor(a, b)  SkFixedMulCommon(a, b, 0)
226    #define SkScalarDiv(a, b)       SkFixedDiv(a, b)
227    #define SkScalarMod(a, b)       SkFixedMod(a, b)
228    #define SkScalarMulDiv(a, b, c) SkMulDiv(a, b, c)
229    #define SkScalarInvert(x)       SkFixedInvert(x)
230    #define SkScalarFastInvert(x)   SkFixedFastInvert(x)
231    #define SkScalarSqrt(x)         SkFixedSqrt(x)
232    #define SkScalarAve(a, b)       SkFixedAve(a, b)
233    #define SkScalarMean(a, b)      SkFixedMean(a, b)
234    #define SkScalarHalf(a)         ((a) >> 1)
235
236    #define SK_ScalarSqrt2          SK_FixedSqrt2
237    #define SK_ScalarPI             SK_FixedPI
238    #define SK_ScalarTanPIOver8     SK_FixedTanPIOver8
239    #define SK_ScalarRoot2Over2     SK_FixedRoot2Over2
240
241    #define SkDegreesToRadians(degrees)     SkFractMul(degrees, SK_FractPIOver180)
242    #define SkScalarSinCos(radians, cosPtr) SkFixedSinCos(radians, cosPtr)
243    #define SkScalarSin(radians)    SkFixedSin(radians)
244    #define SkScalarCos(radians)    SkFixedCos(radians)
245    #define SkScalarTan(val)        SkFixedTan(val)
246    #define SkScalarASin(val)       SkFixedASin(val)
247    #define SkScalarACos(val)       SkFixedACos(val)
248    #define SkScalarATan2(y, x)     SkFixedATan2(y,x)
249    #define SkScalarExp(x)          SkFixedExp(x)
250    #define SkScalarLog(x)          SkFixedLog(x)
251
252    #define SkMaxScalar(a, b)       SkMax32(a, b)
253    #define SkMinScalar(a, b)       SkMin32(a, b)
254
255    static inline bool SkScalarIsInt(SkFixed x) {
256        return 0 == (x & 0xffff);
257    }
258#endif
259
260// DEPRECATED : use ToInt or ToScalar variant
261#define SkScalarFloor(x)    SkScalarFloorToInt(x)
262#define SkScalarCeil(x)     SkScalarCeilToInt(x)
263#define SkScalarRound(x)    SkScalarRoundToInt(x)
264
265/**
266 *  Returns -1 || 0 || 1 depending on the sign of value:
267 *  -1 if x < 0
268 *   0 if x == 0
269 *   1 if x > 0
270 */
271static inline int SkScalarSignAsInt(SkScalar x) {
272    return x < 0 ? -1 : (x > 0);
273}
274
275// Scalar result version of above
276static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
277    return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
278}
279
280#define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
281
282static inline bool SkScalarNearlyZero(SkScalar x,
283                                    SkScalar tolerance = SK_ScalarNearlyZero) {
284    SkASSERT(tolerance >= 0);
285    return SkScalarAbs(x) <= tolerance;
286}
287
288static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
289                                     SkScalar tolerance = SK_ScalarNearlyZero) {
290    SkASSERT(tolerance >= 0);
291    return SkScalarAbs(x-y) <= tolerance;
292}
293
294/** Linearly interpolate between A and B, based on t.
295    If t is 0, return A
296    If t is 1, return B
297    else interpolate.
298    t must be [0..SK_Scalar1]
299*/
300static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
301    SkASSERT(t >= 0 && t <= SK_Scalar1);
302    return A + SkScalarMul(B - A, t);
303}
304
305static inline SkScalar SkScalarLog2(SkScalar x) {
306    static const SkScalar log2_conversion_factor = SkScalarDiv(1, SkScalarLog(2));
307
308    return SkScalarMul(SkScalarLog(x), log2_conversion_factor);
309}
310
311/** Interpolate along the function described by (keys[length], values[length])
312    for the passed searchKey.  SearchKeys outside the range keys[0]-keys[Length]
313    clamp to the min or max value.  This function was inspired by a desire
314    to change the multiplier for thickness in fakeBold; therefore it assumes
315    the number of pairs (length) will be small, and a linear search is used.
316    Repeated keys are allowed for discontinuous functions (so long as keys is
317    monotonically increasing), and if key is the value of a repeated scalar in
318    keys, the first one will be used.  However, that may change if a binary
319    search is used.
320*/
321SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
322                            const SkScalar values[], int length);
323
324/*
325 *  Helper to compare an array of scalars.
326 */
327static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
328#ifdef SK_SCALAR_IS_FLOAT
329    SkASSERT(n >= 0);
330    for (int i = 0; i < n; ++i) {
331        if (a[i] != b[i]) {
332            return false;
333        }
334    }
335    return true;
336#else
337    return 0 == memcmp(a, b, n * sizeof(SkScalar));
338#endif
339}
340
341#endif
342