Home | History | Annotate | Download | only in core
      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 #ifndef SK_SCALAR_TO_FLOAT_EXCLUDED
     82     #define SkFloatToScalar(n)      (n)
     83 #endif
     84 
     85     #define SkScalarToDouble(n)      (double)(n)
     86     #define SkDoubleToScalar(n)      (float)(n)
     87 
     88     /** SkScalarFraction(x) returns the signed fractional part of the argument
     89     */
     90     #define SkScalarFraction(x)     sk_float_mod(x, 1.0f)
     91 
     92     #define SkScalarFloorToScalar(x)    sk_float_floor(x)
     93     #define SkScalarCeilToScalar(x)     sk_float_ceil(x)
     94     #define SkScalarRoundToScalar(x)    sk_float_floor((x) + 0.5f)
     95 
     96     #define SkScalarFloorToInt(x)       sk_float_floor2int(x)
     97     #define SkScalarCeilToInt(x)        sk_float_ceil2int(x)
     98     #define SkScalarRoundToInt(x)       sk_float_round2int(x)
     99     #define SkScalarTruncToInt(x)       static_cast<int>(x)
    100 
    101     /** Returns the absolute value of the specified SkScalar
    102     */
    103     #define SkScalarAbs(x)          sk_float_abs(x)
    104     /** Return x with the sign of y
    105      */
    106     #define SkScalarCopySign(x, y)  sk_float_copysign(x, y)
    107     /** Returns the value pinned between 0 and max inclusive
    108     */
    109     inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
    110         return x < 0 ? 0 : x > max ? max : x;
    111     }
    112     /** Returns the value pinned between min and max inclusive
    113     */
    114     inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
    115         return x < min ? min : x > max ? max : x;
    116     }
    117     /** Returns the specified SkScalar squared (x*x)
    118     */
    119     inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
    120     /** Returns the product of two SkScalars
    121     */
    122     #define SkScalarMul(a, b)       ((float)(a) * (b))
    123     /** Returns the product of two SkScalars plus a third SkScalar
    124     */
    125     #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
    126     /** Returns the product of a SkScalar and an int rounded to the nearest integer value
    127     */
    128     #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b))
    129     /** Returns the product of a SkScalar and an int promoted to the next larger int
    130     */
    131     #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b))
    132     /** Returns the product of a SkScalar and an int truncated to the next smaller int
    133     */
    134     #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b))
    135     /** Returns the quotient of two SkScalars (a/b)
    136     */
    137     #define SkScalarDiv(a, b)       ((float)(a) / (b))
    138     /** Returns the mod of two SkScalars (a mod b)
    139     */
    140     #define SkScalarMod(x,y)        sk_float_mod(x,y)
    141     /** Returns the product of the first two arguments, divided by the third argument
    142     */
    143     #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
    144     /** Returns the multiplicative inverse of the SkScalar (1/x)
    145     */
    146     #define SkScalarInvert(x)       (SK_Scalar1 / (x))
    147     #define SkScalarFastInvert(x)   (SK_Scalar1 / (x))
    148     /** Returns the square root of the SkScalar
    149     */
    150     #define SkScalarSqrt(x)         sk_float_sqrt(x)
    151     /** Returns b to the e
    152     */
    153     #define SkScalarPow(b, e)       sk_float_pow(b, e)
    154     /** Returns the average of two SkScalars (a+b)/2
    155     */
    156     #define SkScalarAve(a, b)       (((a) + (b)) * 0.5f)
    157     /** Returns the geometric mean of two SkScalars
    158     */
    159     #define SkScalarMean(a, b)      sk_float_sqrt((float)(a) * (b))
    160     /** Returns one half of the specified SkScalar
    161     */
    162     #define SkScalarHalf(a)         ((a) * 0.5f)
    163 
    164     #define SK_ScalarSqrt2          1.41421356f
    165     #define SK_ScalarPI             3.14159265f
    166     #define SK_ScalarTanPIOver8     0.414213562f
    167     #define SK_ScalarRoot2Over2     0.707106781f
    168 
    169     #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
    170     float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
    171     #define SkScalarSin(radians)    (float)sk_float_sin(radians)
    172     #define SkScalarCos(radians)    (float)sk_float_cos(radians)
    173     #define SkScalarTan(radians)    (float)sk_float_tan(radians)
    174     #define SkScalarASin(val)   (float)sk_float_asin(val)
    175     #define SkScalarACos(val)   (float)sk_float_acos(val)
    176     #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
    177     #define SkScalarExp(x)  (float)sk_float_exp(x)
    178     #define SkScalarLog(x)  (float)sk_float_log(x)
    179 
    180     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; }
    182 
    183     static inline bool SkScalarIsInt(SkScalar x) {
    184         return x == (float)(int)x;
    185     }
    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) : SkScalarFloorToInt(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_FractPIOver180)
    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 
    264 // DEPRECATED : use ToInt or ToScalar variant
    265 #define SkScalarFloor(x)    SkScalarFloorToInt(x)
    266 #define SkScalarCeil(x)     SkScalarCeilToInt(x)
    267 #define SkScalarRound(x)    SkScalarRoundToInt(x)
    268 
    269 /**
    270  *  Returns -1 || 0 || 1 depending on the sign of value:
    271  *  -1 if x < 0
    272  *   0 if x == 0
    273  *   1 if x > 0
    274  */
    275 static inline int SkScalarSignAsInt(SkScalar x) {
    276     return x < 0 ? -1 : (x > 0);
    277 }
    278 
    279 // Scalar result version of above
    280 static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
    281     return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
    282 }
    283 
    284 #define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
    285 
    286 static inline bool SkScalarNearlyZero(SkScalar x,
    287                                     SkScalar tolerance = SK_ScalarNearlyZero) {
    288     SkASSERT(tolerance >= 0);
    289     return SkScalarAbs(x) <= tolerance;
    290 }
    291 
    292 static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
    293                                      SkScalar tolerance = SK_ScalarNearlyZero) {
    294     SkASSERT(tolerance >= 0);
    295     return SkScalarAbs(x-y) <= tolerance;
    296 }
    297 
    298 /** Linearly interpolate between A and B, based on t.
    299     If t is 0, return A
    300     If t is 1, return B
    301     else interpolate.
    302     t must be [0..SK_Scalar1]
    303 */
    304 static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
    305     SkASSERT(t >= 0 && t <= SK_Scalar1);
    306     return A + SkScalarMul(B - A, t);
    307 }
    308 
    309 static inline SkScalar SkScalarLog2(SkScalar x) {
    310     static const SkScalar log2_conversion_factor = SkScalarDiv(1, SkScalarLog(2));
    311 
    312     return SkScalarMul(SkScalarLog(x), log2_conversion_factor);
    313 }
    314 
    315 /** Interpolate along the function described by (keys[length], values[length])
    316     for the passed searchKey.  SearchKeys outside the range keys[0]-keys[Length]
    317     clamp to the min or max value.  This function was inspired by a desire
    318     to change the multiplier for thickness in fakeBold; therefore it assumes
    319     the number of pairs (length) will be small, and a linear search is used.
    320     Repeated keys are allowed for discontinuous functions (so long as keys is
    321     monotonically increasing), and if key is the value of a repeated scalar in
    322     keys, the first one will be used.  However, that may change if a binary
    323     search is used.
    324 */
    325 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
    326                             const SkScalar values[], int length);
    327 
    328 /*
    329  *  Helper to compare an array of scalars.
    330  */
    331 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
    332 #ifdef SK_SCALAR_IS_FLOAT
    333     SkASSERT(n >= 0);
    334     for (int i = 0; i < n; ++i) {
    335         if (a[i] != b[i]) {
    336             return false;
    337         }
    338     }
    339     return true;
    340 #else
    341     return 0 == memcmp(a, b, n * sizeof(SkScalar));
    342 #endif
    343 }
    344 
    345 #endif
    346