SkScalar.h revision 187d5595901d1120d9425851e5afdd773f574502
18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Copyright (C) 2006 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Licensed under the Apache License, Version 2.0 (the "License");
58a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * you may not use this file except in compliance with the License.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * You may obtain a copy of the License at
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *      http://www.apache.org/licenses/LICENSE-2.0
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Unless required by applicable law or agreed to in writing, software
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * distributed under the License is distributed on an "AS IS" BASIS,
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * See the License for the specific language governing permissions and
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * limitations under the License.
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkScalar_DEFINED
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkScalar_DEFINED
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkFixed.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkScalar.h
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Types and macros for the data type SkScalar. This is the fractional numeric type
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    that, depending on the compile-time flag SK_SCALAR_IS_FLOAT, may be implemented
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    either as an IEEE float, or as a 16.16 SkFixed. The macros in this file are written
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    to allow the calling code to manipulate SkScalar values without knowing which representation
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    is in effect.
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_SCALAR_IS_FLOAT
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #include "SkFloatingPoint.h"
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkScalar is our type for fractional values and coordinates. Depending on
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        compile configurations, it is either represented as an IEEE float, or
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        as a 16.16 fixed point integer.
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef float   SkScalar;
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    extern const uint32_t gIEEENotANumber;
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    extern const uint32_t gIEEEInfinity;
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_Scalar1              (1.0f)
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_Scalar1 is defined to be 1/2 represented as an SkScalar
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarHalf           (0.5f)
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_ScalarInfinity is defined to be infinity as an SkScalar
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarInfinity           (*(const float*)&gIEEEInfinity)
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_ScalarMax is defined to be the largest value representable as an SkScalar
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarMax            (3.4028235e+38f)
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_ScalarMin is defined to be the smallest value representable as an SkScalar
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarMin            (1.1754944e-38f)
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarNaN      (*(const float*)(const void*)&gIEEENotANumber)
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkScalarIsNaN(n) returns true if argument is not a number
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static inline bool SkScalarIsNaN(float x) { return x != x; }
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkIntToScalar(n) returns its integer argument as an SkScalar
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkIntToScalar(n)        ((float)(n))
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkFixedToScalar(x)      SkFixedToFloat(x)
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarToFixed(x)      SkFloatToFixed(x)
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarToFloat(n)      (n)
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkFloatToScalar(n)      (n)
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarToDouble(n)      (double)(n)
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDoubleToScalar(n)      (float)(n)
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkScalarFraction(x) returns the signed fractional part of the argument
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarFraction(x)     sk_float_mod(x, 1.0f)
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Rounds the SkScalar to the nearest integer value
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarRound(x)        sk_float_round2int(x)
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the smallest integer that is >= the specified SkScalar
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarCeil(x)         sk_float_ceil2int(x)
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the largest integer that is <= the specified SkScalar
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarFloor(x)        sk_float_floor2int(x)
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the absolute value of the specified SkScalar
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarAbs(x)          sk_float_abs(x)
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the value pinned between 0 and max inclusive
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return x < 0 ? 0 : x > max ? max : x;
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the value pinned between min and max inclusive
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return x < min ? min : x > max ? max : x;
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the specified SkScalar squared (x*x)
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of two SkScalars
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMul(a, b)       ((float)(a) * (b))
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of two SkScalars plus a third SkScalar
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of a SkScalar and an int rounded to the nearest integer value
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b))
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of a SkScalar and an int promoted to the next larger int
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b))
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of a SkScalar and an int truncated to the next smaller int
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b))
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the quotient of two SkScalars (a/b)
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarDiv(a, b)       ((float)(a) / (b))
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the mod of two SkScalars (a mod b)
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMod(x,y)        sk_float_mod(x,y)
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of the first two arguments, divided by the third argument
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the multiplicative inverse of the SkScalar (1/x)
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarInvert(x)       (SK_Scalar1 / (x))
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarFastInvert(x)   (SK_Scalar1 / (x))
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the square root of the SkScalar
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSqrt(x)         sk_float_sqrt(x)
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the average of two SkScalars (a+b)/2
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarAve(a, b)       (((a) + (b)) * 0.5f)
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the geometric mean of two SkScalars
1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMean(a, b)      sk_float_sqrt((float)(a) * (b))
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns one half of the specified SkScalar
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarHalf(a)         ((a) * 0.5f)
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarSqrt2          1.41421356f
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarPI             3.14159265f
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarTanPIOver8     0.414213562f
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarRoot2Over2     0.707106781f
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSin(radians)    (float)sk_float_sin(radians)
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarCos(radians)    (float)sk_float_cos(radians)
1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarTan(radians)    (float)sk_float_tan(radians)
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarASin(val)   (float)sk_float_asin(val)
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarACos(val)   (float)sk_float_acos(val)
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarExp(x)  (float)sk_float_exp(x)
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarLog(x)  (float)sk_float_log(x)
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkFixed SkScalar;
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_Scalar1              SK_Fixed1
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarHalf           SK_FixedHalf
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarInfinity   SK_FixedMax
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarMax            SK_FixedMax
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarMin            SK_FixedMin
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarNaN            SK_FixedNaN
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarIsNaN(x)        ((x) == SK_FixedNaN)
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkIntToScalar(n)        SkIntToFixed(n)
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkFixedToScalar(x)      (x)
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarToFixed(x)      (x)
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #ifdef SK_CAN_USE_FLOAT
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        #define SkScalarToFloat(n)  SkFixedToFloat(n)
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        #define SkFloatToScalar(n)  SkFloatToFixed(n)
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        #define SkScalarToDouble(n) SkFixedToDouble(n)
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        #define SkDoubleToScalar(n) SkDoubleToFixed(n)
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #endif
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarFraction(x)     SkFixedFraction(x)
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarRound(x)        SkFixedRound(x)
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarCeil(x)         SkFixedCeil(x)
1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarFloor(x)        SkFixedFloor(x)
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarAbs(x)          SkFixedAbs(x)
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarClampMax(x, max) SkClampMax(x, max)
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarPin(x, min, max) SkPin32(x, min, max)
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSquare(x)       SkFixedSquare(x)
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMul(a, b)       SkFixedMul(a, b)
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulAdd(a, b, c) SkFixedMulAdd(a, b, c)
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulRound(a, b)  SkFixedMulCommon(a, b, SK_FixedHalf)
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulCeil(a, b)   SkFixedMulCommon(a, b, SK_Fixed1 - 1)
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulFloor(a, b)  SkFixedMulCommon(a, b, 0)
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarDiv(a, b)       SkFixedDiv(a, b)
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMod(a, b)       SkFixedMod(a, b)
2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulDiv(a, b, c) SkMulDiv(a, b, c)
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarInvert(x)       SkFixedInvert(x)
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarFastInvert(x)   SkFixedFastInvert(x)
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSqrt(x)         SkFixedSqrt(x)
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarAve(a, b)       SkFixedAve(a, b)
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMean(a, b)      SkFixedMean(a, b)
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarHalf(a)         ((a) >> 1)
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarSqrt2          SK_FixedSqrt2
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarPI             SK_FixedPI
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarTanPIOver8     SK_FixedTanPIOver8
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarRoot2Over2     SK_FixedRoot2Over2
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDegreesToRadians(degrees)     SkFractMul(degrees, SK_FractPIOver180)
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSinCos(radians, cosPtr) SkFixedSinCos(radians, cosPtr)
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSin(radians)    SkFixedSin(radians)
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarCos(radians)    SkFixedCos(radians)
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarTan(val)        SkFixedTan(val)
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarASin(val)       SkFixedASin(val)
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarACos(val)       SkFixedACos(val)
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarATan2(y, x)     SkFixedATan2(y,x)
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarExp(x)          SkFixedExp(x)
2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarLog(x)          SkFixedLog(x)
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkMaxScalar(a, b)       SkMax32(a, b)
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkMinScalar(a, b)       SkMin32(a, b)
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*  <= is slower than < for floats, so we use < for our tolerance test
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
235187d5595901d1120d9425851e5afdd773f574502reed@android.comstatic inline bool SkScalarNearlyZero(SkScalar x,
236187d5595901d1120d9425851e5afdd773f574502reed@android.com                                  SkScalar tolerance = SK_ScalarNearlyZero) {
2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(tolerance > 0);
2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkScalarAbs(x) < tolerance;
2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Linearly interpolate between A and B, based on t.
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    If t is 0, return A
2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    If t is 1, return B
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    else interpolate.
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    t must be [0..SK_Scalar1]
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
247187d5595901d1120d9425851e5afdd773f574502reed@android.comstatic inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(t >= 0 && t <= SK_Scalar1);
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return A + SkScalarMul(B - A, t);
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
254