SkScalar.h revision 50c79d886bf435d3a9cad056885370e2c3f526ad
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#ifdef SK_DEBUG
71    /** SkIntToScalar(n) returns its integer argument as an SkScalar
72     *
73     * If we're compiling in DEBUG mode, and can thus afford some extra runtime
74     * cycles, check to make sure that the parameter passed in has not already
75     * been converted to SkScalar.  (A double conversion like this is harmless
76     * for SK_SCALAR_IS_FLOAT, but for SK_SCALAR_IS_FIXED this causes trouble.)
77     *
78     * Note that we need all of these method signatures to properly handle the
79     * various types that we pass into SkIntToScalar() to date:
80     * int, size_t, U8CPU, etc., even though what we really mean is "anything
81     * but a float".
82     */
83    static inline float SkIntToScalar(signed int param) {
84        return (float)param;
85    }
86    static inline float SkIntToScalar(unsigned int param) {
87        return (float)param;
88    }
89    static inline float SkIntToScalar(signed long param) {
90        return (float)param;
91    }
92    static inline float SkIntToScalar(unsigned long param) {
93        return (float)param;
94    }
95    static inline float SkIntToScalar(float /* param */) {
96        /* If the parameter passed into SkIntToScalar is a float,
97         * one of two things has happened:
98         * 1. the parameter was an SkScalar (which is typedef'd to float)
99         * 2. the parameter was a float instead of an int
100         *
101         * Either way, it's not good.
102         */
103        SkDEBUGFAIL("looks like you passed an SkScalar into SkIntToScalar");
104        return (float)0;
105    }
106#else  // not SK_DEBUG
107    /** SkIntToScalar(n) returns its integer argument as an SkScalar
108    */
109    #define SkIntToScalar(n)        ((float)(n))
110#endif // not SK_DEBUG
111    /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
112    */
113    #define SkFixedToScalar(x)      SkFixedToFloat(x)
114    /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed
115    */
116    #define SkScalarToFixed(x)      SkFloatToFixed(x)
117
118    #define SkScalarToFloat(n)      (n)
119    #define SkFloatToScalar(n)      (n)
120
121    #define SkScalarToDouble(n)      (double)(n)
122    #define SkDoubleToScalar(n)      (float)(n)
123
124    /** SkScalarFraction(x) returns the signed fractional part of the argument
125    */
126    #define SkScalarFraction(x)     sk_float_mod(x, 1.0f)
127
128    #define SkScalarFloorToScalar(x)    sk_float_floor(x)
129    #define SkScalarCeilToScalar(x)     sk_float_ceil(x)
130    #define SkScalarRoundToScalar(x)    sk_float_floor((x) + 0.5f)
131
132    #define SkScalarFloorToInt(x)       sk_float_floor2int(x)
133    #define SkScalarCeilToInt(x)        sk_float_ceil2int(x)
134    #define SkScalarRoundToInt(x)       sk_float_round2int(x)
135
136    /** Returns the absolute value of the specified SkScalar
137    */
138    #define SkScalarAbs(x)          sk_float_abs(x)
139    /** Return x with the sign of y
140     */
141    #define SkScalarCopySign(x, y)  sk_float_copysign(x, y)
142    /** Returns the value pinned between 0 and max inclusive
143    */
144    inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
145        return x < 0 ? 0 : x > max ? max : x;
146    }
147    /** Returns the value pinned between min and max inclusive
148    */
149    inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
150        return x < min ? min : x > max ? max : x;
151    }
152    /** Returns the specified SkScalar squared (x*x)
153    */
154    inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
155    /** Returns the product of two SkScalars
156    */
157    #define SkScalarMul(a, b)       ((float)(a) * (b))
158    /** Returns the product of two SkScalars plus a third SkScalar
159    */
160    #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
161    /** Returns the product of a SkScalar and an int rounded to the nearest integer value
162    */
163    #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b))
164    /** Returns the product of a SkScalar and an int promoted to the next larger int
165    */
166    #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b))
167    /** Returns the product of a SkScalar and an int truncated to the next smaller int
168    */
169    #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b))
170    /** Returns the quotient of two SkScalars (a/b)
171    */
172    #define SkScalarDiv(a, b)       ((float)(a) / (b))
173    /** Returns the mod of two SkScalars (a mod b)
174    */
175    #define SkScalarMod(x,y)        sk_float_mod(x,y)
176    /** Returns the product of the first two arguments, divided by the third argument
177    */
178    #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
179    /** Returns the multiplicative inverse of the SkScalar (1/x)
180    */
181    #define SkScalarInvert(x)       (SK_Scalar1 / (x))
182    #define SkScalarFastInvert(x)   (SK_Scalar1 / (x))
183    /** Returns the square root of the SkScalar
184    */
185    #define SkScalarSqrt(x)         sk_float_sqrt(x)
186    /** Returns b to the e
187    */
188    #define SkScalarPow(b, e)       sk_float_pow(b, e)
189    /** Returns the average of two SkScalars (a+b)/2
190    */
191    #define SkScalarAve(a, b)       (((a) + (b)) * 0.5f)
192    /** Returns the geometric mean of two SkScalars
193    */
194    #define SkScalarMean(a, b)      sk_float_sqrt((float)(a) * (b))
195    /** Returns one half of the specified SkScalar
196    */
197    #define SkScalarHalf(a)         ((a) * 0.5f)
198
199    #define SK_ScalarSqrt2          1.41421356f
200    #define SK_ScalarPI             3.14159265f
201    #define SK_ScalarTanPIOver8     0.414213562f
202    #define SK_ScalarRoot2Over2     0.707106781f
203
204    #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
205    float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
206    #define SkScalarSin(radians)    (float)sk_float_sin(radians)
207    #define SkScalarCos(radians)    (float)sk_float_cos(radians)
208    #define SkScalarTan(radians)    (float)sk_float_tan(radians)
209    #define SkScalarASin(val)   (float)sk_float_asin(val)
210    #define SkScalarACos(val)   (float)sk_float_acos(val)
211    #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
212    #define SkScalarExp(x)  (float)sk_float_exp(x)
213    #define SkScalarLog(x)  (float)sk_float_log(x)
214
215    inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
216    inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
217
218    static inline bool SkScalarIsInt(SkScalar x) {
219        return x == (float)(int)x;
220    }
221#else
222    typedef SkFixed SkScalar;
223
224    #define SK_Scalar1              SK_Fixed1
225    #define SK_ScalarHalf           SK_FixedHalf
226    #define SK_ScalarInfinity           SK_FixedMax
227    #define SK_ScalarNegativeInfinity   SK_FixedMin
228    #define SK_ScalarMax            SK_FixedMax
229    #define SK_ScalarMin            SK_FixedMin
230    #define SK_ScalarNaN            SK_FixedNaN
231    #define SkScalarIsNaN(x)        ((x) == SK_FixedNaN)
232    #define SkScalarIsFinite(x)     ((x) != SK_FixedNaN)
233
234    #define SkIntToScalar(n)        SkIntToFixed(n)
235    #define SkFixedToScalar(x)      (x)
236    #define SkScalarToFixed(x)      (x)
237    #define SkScalarToFloat(n)  SkFixedToFloat(n)
238    #define SkFloatToScalar(n)  SkFloatToFixed(n)
239
240    #define SkScalarToDouble(n) SkFixedToDouble(n)
241    #define SkDoubleToScalar(n) SkDoubleToFixed(n)
242    #define SkScalarFraction(x)     SkFixedFraction(x)
243
244    #define SkScalarFloorToScalar(x)    SkFixedFloorToFixed(x)
245    #define SkScalarCeilToScalar(x)     SkFixedCeilToFixed(x)
246    #define SkScalarRoundToScalar(x)    SkFixedRoundToFixed(x)
247
248    #define SkScalarFloorToInt(x)       SkFixedFloorToInt(x)
249    #define SkScalarCeilToInt(x)        SkFixedCeilToInt(x)
250    #define SkScalarRoundToInt(x)       SkFixedRoundToInt(x)
251
252    #define SkScalarAbs(x)          SkFixedAbs(x)
253    #define SkScalarCopySign(x, y)  SkCopySign32(x, y)
254    #define SkScalarClampMax(x, max) SkClampMax(x, max)
255    #define SkScalarPin(x, min, max) SkPin32(x, min, max)
256    #define SkScalarSquare(x)       SkFixedSquare(x)
257    #define SkScalarMul(a, b)       SkFixedMul(a, b)
258    #define SkScalarMulAdd(a, b, c) SkFixedMulAdd(a, b, c)
259    #define SkScalarMulRound(a, b)  SkFixedMulCommon(a, b, SK_FixedHalf)
260    #define SkScalarMulCeil(a, b)   SkFixedMulCommon(a, b, SK_Fixed1 - 1)
261    #define SkScalarMulFloor(a, b)  SkFixedMulCommon(a, b, 0)
262    #define SkScalarDiv(a, b)       SkFixedDiv(a, b)
263    #define SkScalarMod(a, b)       SkFixedMod(a, b)
264    #define SkScalarMulDiv(a, b, c) SkMulDiv(a, b, c)
265    #define SkScalarInvert(x)       SkFixedInvert(x)
266    #define SkScalarFastInvert(x)   SkFixedFastInvert(x)
267    #define SkScalarSqrt(x)         SkFixedSqrt(x)
268    #define SkScalarAve(a, b)       SkFixedAve(a, b)
269    #define SkScalarMean(a, b)      SkFixedMean(a, b)
270    #define SkScalarHalf(a)         ((a) >> 1)
271
272    #define SK_ScalarSqrt2          SK_FixedSqrt2
273    #define SK_ScalarPI             SK_FixedPI
274    #define SK_ScalarTanPIOver8     SK_FixedTanPIOver8
275    #define SK_ScalarRoot2Over2     SK_FixedRoot2Over2
276
277    #define SkDegreesToRadians(degrees)     SkFractMul(degrees, SK_FractPIOver180)
278    #define SkScalarSinCos(radians, cosPtr) SkFixedSinCos(radians, cosPtr)
279    #define SkScalarSin(radians)    SkFixedSin(radians)
280    #define SkScalarCos(radians)    SkFixedCos(radians)
281    #define SkScalarTan(val)        SkFixedTan(val)
282    #define SkScalarASin(val)       SkFixedASin(val)
283    #define SkScalarACos(val)       SkFixedACos(val)
284    #define SkScalarATan2(y, x)     SkFixedATan2(y,x)
285    #define SkScalarExp(x)          SkFixedExp(x)
286    #define SkScalarLog(x)          SkFixedLog(x)
287
288    #define SkMaxScalar(a, b)       SkMax32(a, b)
289    #define SkMinScalar(a, b)       SkMin32(a, b)
290
291    static inline bool SkScalarIsInt(SkFixed x) {
292        return 0 == (x & 0xffff);
293    }
294#endif
295
296// DEPRECATED : use ToInt or ToScalar variant
297#define SkScalarFloor(x)    SkScalarFloorToInt(x)
298#define SkScalarCeil(x)     SkScalarCeilToInt(x)
299#define SkScalarRound(x)    SkScalarRoundToInt(x)
300
301/**
302 *  Returns -1 || 0 || 1 depending on the sign of value:
303 *  -1 if x < 0
304 *   0 if x == 0
305 *   1 if x > 0
306 */
307static inline int SkScalarSignAsInt(SkScalar x) {
308    return x < 0 ? -1 : (x > 0);
309}
310
311// Scalar result version of above
312static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
313    return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
314}
315
316#define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
317
318static inline bool SkScalarNearlyZero(SkScalar x,
319                                    SkScalar tolerance = SK_ScalarNearlyZero) {
320    SkASSERT(tolerance >= 0);
321    return SkScalarAbs(x) <= tolerance;
322}
323
324static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
325                                     SkScalar tolerance = SK_ScalarNearlyZero) {
326    SkASSERT(tolerance >= 0);
327    return SkScalarAbs(x-y) <= tolerance;
328}
329
330/** Linearly interpolate between A and B, based on t.
331    If t is 0, return A
332    If t is 1, return B
333    else interpolate.
334    t must be [0..SK_Scalar1]
335*/
336static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
337    SkASSERT(t >= 0 && t <= SK_Scalar1);
338    return A + SkScalarMul(B - A, t);
339}
340
341static inline SkScalar SkScalarLog2(SkScalar x) {
342    static const SkScalar log2_conversion_factor = SkScalarDiv(1, SkScalarLog(2));
343
344    return SkScalarMul(SkScalarLog(x), log2_conversion_factor);
345}
346
347/** Interpolate along the function described by (keys[length], values[length])
348    for the passed searchKey.  SearchKeys outside the range keys[0]-keys[Length]
349    clamp to the min or max value.  This function was inspired by a desire
350    to change the multiplier for thickness in fakeBold; therefore it assumes
351    the number of pairs (length) will be small, and a linear search is used.
352    Repeated keys are allowed for discontinuous functions (so long as keys is
353    monotonically increasing), and if key is the value of a repeated scalar in
354    keys, the first one will be used.  However, that may change if a binary
355    search is used.
356*/
357SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
358                            const SkScalar values[], int length);
359
360/*
361 *  Helper to compare an array of scalars.
362 */
363static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
364#ifdef SK_SCALAR_IS_FLOAT
365    SkASSERT(n >= 0);
366    for (int i = 0; i < n; ++i) {
367        if (a[i] != b[i]) {
368            return false;
369        }
370    }
371    return true;
372#else
373    return 0 == memcmp(a, b, n * sizeof(SkScalar));
374#endif
375}
376
377#endif
378