Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SkScalar_DEFINED
     18 #define SkScalar_DEFINED
     19 
     20 #include "SkFixed.h"
     21 
     22 /** \file SkScalar.h
     23 
     24     Types and macros for the data type SkScalar. This is the fractional numeric type
     25     that, depending on the compile-time flag SK_SCALAR_IS_FLOAT, may be implemented
     26     either as an IEEE float, or as a 16.16 SkFixed. The macros in this file are written
     27     to allow the calling code to manipulate SkScalar values without knowing which representation
     28     is in effect.
     29 */
     30 
     31 #ifdef SK_SCALAR_IS_FLOAT
     32     #include "SkFloatingPoint.h"
     33 
     34     /** SkScalar is our type for fractional values and coordinates. Depending on
     35         compile configurations, it is either represented as an IEEE float, or
     36         as a 16.16 fixed point integer.
     37     */
     38     typedef float   SkScalar;
     39     extern const uint32_t gIEEENotANumber;
     40     extern const uint32_t gIEEEInfinity;
     41 
     42     /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar
     43     */
     44     #define SK_Scalar1              (1.0f)
     45     /** SK_Scalar1 is defined to be 1/2 represented as an SkScalar
     46     */
     47     #define SK_ScalarHalf           (0.5f)
     48     /** SK_ScalarInfinity is defined to be infinity as an SkScalar
     49     */
     50     #define SK_ScalarInfinity           (*(const float*)&gIEEEInfinity)
     51     /** SK_ScalarMax is defined to be the largest value representable as an SkScalar
     52     */
     53     #define SK_ScalarMax            (3.4028235e+38f)
     54     /** SK_ScalarMin is defined to be the smallest value representable as an SkScalar
     55     */
     56     #define SK_ScalarMin            (1.1754944e-38f)
     57     /** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar
     58     */
     59     #define SK_ScalarNaN      (*(const float*)(const void*)&gIEEENotANumber)
     60     /** SkScalarIsNaN(n) returns true if argument is not a number
     61     */
     62     static inline bool SkScalarIsNaN(float x) { return x != x; }
     63     /** SkIntToScalar(n) returns its integer argument as an SkScalar
     64     */
     65     #define SkIntToScalar(n)        ((float)(n))
     66     /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
     67     */
     68     #define SkFixedToScalar(x)      SkFixedToFloat(x)
     69     /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed
     70     */
     71     #define SkScalarToFixed(x)      SkFloatToFixed(x)
     72 
     73     #define SkScalarToFloat(n)      (n)
     74     #define SkFloatToScalar(n)      (n)
     75 
     76     #define SkScalarToDouble(n)      (double)(n)
     77     #define SkDoubleToScalar(n)      (float)(n)
     78 
     79     /** SkScalarFraction(x) returns the signed fractional part of the argument
     80     */
     81     #define SkScalarFraction(x)     sk_float_mod(x, 1.0f)
     82     /** Rounds the SkScalar to the nearest integer value
     83     */
     84     #define SkScalarRound(x)        sk_float_round2int(x)
     85     /** Returns the smallest integer that is >= the specified SkScalar
     86     */
     87     #define SkScalarCeil(x)         sk_float_ceil2int(x)
     88     /** Returns the largest integer that is <= the specified SkScalar
     89     */
     90     #define SkScalarFloor(x)        sk_float_floor2int(x)
     91     /** Returns the absolute value of the specified SkScalar
     92     */
     93     #define SkScalarAbs(x)          sk_float_abs(x)
     94     /** Return x with the sign of y
     95      */
     96     #define SkScalarCopySign(x, y)  sk_float_copysign(x, y)
     97     /** Returns the value pinned between 0 and max inclusive
     98     */
     99     inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
    100         return x < 0 ? 0 : x > max ? max : x;
    101     }
    102     /** Returns the value pinned between min and max inclusive
    103     */
    104     inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
    105         return x < min ? min : x > max ? max : x;
    106     }
    107     /** Returns the specified SkScalar squared (x*x)
    108     */
    109     inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
    110     /** Returns the product of two SkScalars
    111     */
    112     #define SkScalarMul(a, b)       ((float)(a) * (b))
    113     /** Returns the product of two SkScalars plus a third SkScalar
    114     */
    115     #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
    116     /** Returns the product of a SkScalar and an int rounded to the nearest integer value
    117     */
    118     #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b))
    119     /** Returns the product of a SkScalar and an int promoted to the next larger int
    120     */
    121     #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b))
    122     /** Returns the product of a SkScalar and an int truncated to the next smaller int
    123     */
    124     #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b))
    125     /** Returns the quotient of two SkScalars (a/b)
    126     */
    127     #define SkScalarDiv(a, b)       ((float)(a) / (b))
    128     /** Returns the mod of two SkScalars (a mod b)
    129     */
    130     #define SkScalarMod(x,y)        sk_float_mod(x,y)
    131     /** Returns the product of the first two arguments, divided by the third argument
    132     */
    133     #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
    134     /** Returns the multiplicative inverse of the SkScalar (1/x)
    135     */
    136     #define SkScalarInvert(x)       (SK_Scalar1 / (x))
    137     #define SkScalarFastInvert(x)   (SK_Scalar1 / (x))
    138     /** Returns the square root of the SkScalar
    139     */
    140     #define SkScalarSqrt(x)         sk_float_sqrt(x)
    141     /** Returns the average of two SkScalars (a+b)/2
    142     */
    143     #define SkScalarAve(a, b)       (((a) + (b)) * 0.5f)
    144     /** Returns the geometric mean of two SkScalars
    145     */
    146     #define SkScalarMean(a, b)      sk_float_sqrt((float)(a) * (b))
    147     /** Returns one half of the specified SkScalar
    148     */
    149     #define SkScalarHalf(a)         ((a) * 0.5f)
    150 
    151     #define SK_ScalarSqrt2          1.41421356f
    152     #define SK_ScalarPI             3.14159265f
    153     #define SK_ScalarTanPIOver8     0.414213562f
    154     #define SK_ScalarRoot2Over2     0.707106781f
    155 
    156     #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
    157     float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
    158     #define SkScalarSin(radians)    (float)sk_float_sin(radians)
    159     #define SkScalarCos(radians)    (float)sk_float_cos(radians)
    160     #define SkScalarTan(radians)    (float)sk_float_tan(radians)
    161     #define SkScalarASin(val)   (float)sk_float_asin(val)
    162     #define SkScalarACos(val)   (float)sk_float_acos(val)
    163     #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
    164     #define SkScalarExp(x)  (float)sk_float_exp(x)
    165     #define SkScalarLog(x)  (float)sk_float_log(x)
    166 
    167     inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
    168     inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
    169 
    170 #else
    171     typedef SkFixed SkScalar;
    172 
    173     #define SK_Scalar1              SK_Fixed1
    174     #define SK_ScalarHalf           SK_FixedHalf
    175     #define SK_ScalarInfinity   SK_FixedMax
    176     #define SK_ScalarMax            SK_FixedMax
    177     #define SK_ScalarMin            SK_FixedMin
    178     #define SK_ScalarNaN            SK_FixedNaN
    179     #define SkScalarIsNaN(x)        ((x) == SK_FixedNaN)
    180     #define SkIntToScalar(n)        SkIntToFixed(n)
    181     #define SkFixedToScalar(x)      (x)
    182     #define SkScalarToFixed(x)      (x)
    183     #ifdef SK_CAN_USE_FLOAT
    184         #define SkScalarToFloat(n)  SkFixedToFloat(n)
    185         #define SkFloatToScalar(n)  SkFloatToFixed(n)
    186 
    187         #define SkScalarToDouble(n) SkFixedToDouble(n)
    188         #define SkDoubleToScalar(n) SkDoubleToFixed(n)
    189     #endif
    190     #define SkScalarFraction(x)     SkFixedFraction(x)
    191     #define SkScalarRound(x)        SkFixedRound(x)
    192     #define SkScalarCeil(x)         SkFixedCeil(x)
    193     #define SkScalarFloor(x)        SkFixedFloor(x)
    194     #define SkScalarAbs(x)          SkFixedAbs(x)
    195     #define SkScalarCopySign(x, y)  SkCopySign32(x, y)
    196     #define SkScalarClampMax(x, max) SkClampMax(x, max)
    197     #define SkScalarPin(x, min, max) SkPin32(x, min, max)
    198     #define SkScalarSquare(x)       SkFixedSquare(x)
    199     #define SkScalarMul(a, b)       SkFixedMul(a, b)
    200     #define SkScalarMulAdd(a, b, c) SkFixedMulAdd(a, b, c)
    201     #define SkScalarMulRound(a, b)  SkFixedMulCommon(a, b, SK_FixedHalf)
    202     #define SkScalarMulCeil(a, b)   SkFixedMulCommon(a, b, SK_Fixed1 - 1)
    203     #define SkScalarMulFloor(a, b)  SkFixedMulCommon(a, b, 0)
    204     #define SkScalarDiv(a, b)       SkFixedDiv(a, b)
    205     #define SkScalarMod(a, b)       SkFixedMod(a, b)
    206     #define SkScalarMulDiv(a, b, c) SkMulDiv(a, b, c)
    207     #define SkScalarInvert(x)       SkFixedInvert(x)
    208     #define SkScalarFastInvert(x)   SkFixedFastInvert(x)
    209     #define SkScalarSqrt(x)         SkFixedSqrt(x)
    210     #define SkScalarAve(a, b)       SkFixedAve(a, b)
    211     #define SkScalarMean(a, b)      SkFixedMean(a, b)
    212     #define SkScalarHalf(a)         ((a) >> 1)
    213 
    214     #define SK_ScalarSqrt2          SK_FixedSqrt2
    215     #define SK_ScalarPI             SK_FixedPI
    216     #define SK_ScalarTanPIOver8     SK_FixedTanPIOver8
    217     #define SK_ScalarRoot2Over2     SK_FixedRoot2Over2
    218 
    219     #define SkDegreesToRadians(degrees)     SkFractMul(degrees, SK_FractPIOver180)
    220     #define SkScalarSinCos(radians, cosPtr) SkFixedSinCos(radians, cosPtr)
    221     #define SkScalarSin(radians)    SkFixedSin(radians)
    222     #define SkScalarCos(radians)    SkFixedCos(radians)
    223     #define SkScalarTan(val)        SkFixedTan(val)
    224     #define SkScalarASin(val)       SkFixedASin(val)
    225     #define SkScalarACos(val)       SkFixedACos(val)
    226     #define SkScalarATan2(y, x)     SkFixedATan2(y,x)
    227     #define SkScalarExp(x)          SkFixedExp(x)
    228     #define SkScalarLog(x)          SkFixedLog(x)
    229 
    230     #define SkMaxScalar(a, b)       SkMax32(a, b)
    231     #define SkMinScalar(a, b)       SkMin32(a, b)
    232 #endif
    233 
    234 #define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
    235 
    236 /*  <= is slower than < for floats, so we use < for our tolerance test
    237 */
    238 
    239 static inline bool SkScalarNearlyZero(SkScalar x,
    240                                   SkScalar tolerance = SK_ScalarNearlyZero) {
    241     SkASSERT(tolerance > 0);
    242     return SkScalarAbs(x) < tolerance;
    243 }
    244 
    245 /** Linearly interpolate between A and B, based on t.
    246     If t is 0, return A
    247     If t is 1, return B
    248     else interpolate.
    249     t must be [0..SK_Scalar1]
    250 */
    251 static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
    252     SkASSERT(t >= 0 && t <= SK_Scalar1);
    253     return A + SkScalarMul(B - A, t);
    254 }
    255 
    256 #endif
    257 
    258