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