SkScalar.h revision 9b5ca29af81b93faea040daa25a620bee99d6c38
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkScalar_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkScalar_DEFINED
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkFixed.h"
1480afbf08eb82a99f8a2eeeb1741eb7ca1ccf957depoger@google.com#include "SkFloatingPoint.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkScalar.h
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Types and macros for the data type SkScalar. This is the fractional numeric type
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    that, depending on the compile-time flag SK_SCALAR_IS_FLOAT, may be implemented
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    either as an IEEE float, or as a 16.16 SkFixed. The macros in this file are written
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    to allow the calling code to manipulate SkScalar values without knowing which representation
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    is in effect.
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_SCALAR_IS_FLOAT
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkScalar is our type for fractional values and coordinates. Depending on
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        compile configurations, it is either represented as an IEEE float, or
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        as a 16.16 fixed point integer.
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef float   SkScalar;
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    extern const uint32_t gIEEENotANumber;
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    extern const uint32_t gIEEEInfinity;
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_Scalar1              (1.0f)
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_Scalar1 is defined to be 1/2 represented as an SkScalar
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarHalf           (0.5f)
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_ScalarInfinity is defined to be infinity as an SkScalar
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarInfinity           (*(const float*)&gIEEEInfinity)
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_ScalarMax is defined to be the largest value representable as an SkScalar
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
46c8ad63e5e42637c65c6b4e0adfacce55730b722dbsalomon@google.com    #define SK_ScalarMax            (3.402823466e+38f)
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_ScalarMin is defined to be the smallest value representable as an SkScalar
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
49ee9aa304579b3d5314519372728187879456d49dbsalomon@google.com    #define SK_ScalarMin            (-SK_ScalarMax)
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarNaN      (*(const float*)(const void*)&gIEEENotANumber)
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkScalarIsNaN(n) returns true if argument is not a number
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static inline bool SkScalarIsNaN(float x) { return x != x; }
56077910e20cda41d7981084fbd047a108894bc8dfreed@google.com    /** Returns true if x is not NaN and not infinite */
57077910e20cda41d7981084fbd047a108894bc8dfreed@google.com    static inline bool SkScalarIsFinite(float x) {
58077910e20cda41d7981084fbd047a108894bc8dfreed@google.com        uint32_t bits = SkFloat2Bits(x);    // need unsigned for our shifts
59077910e20cda41d7981084fbd047a108894bc8dfreed@google.com        int exponent = bits << 1 >> 24;
60077910e20cda41d7981084fbd047a108894bc8dfreed@google.com        return exponent != 0xFF;
61077910e20cda41d7981084fbd047a108894bc8dfreed@google.com    }
62b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com#ifdef SK_DEBUG
63b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com    /** SkIntToScalar(n) returns its integer argument as an SkScalar
64b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com     *
65b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com     * If we're compiling in DEBUG mode, and can thus afford some extra runtime
66b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com     * cycles, check to make sure that the parameter passed in has not already
67b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com     * been converted to SkScalar.  (A double conversion like this is harmless
68b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com     * for SK_SCALAR_IS_FLOAT, but for SK_SCALAR_IS_FIXED this causes trouble.)
69b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com     *
70b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com     * Note that we need all of these method signatures to properly handle the
71b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com     * various types that we pass into SkIntToScalar() to date:
72b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com     * int, size_t, U8CPU, etc., even though what we really mean is "anything
73b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com     * but a float".
74b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com     */
75b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com    static inline float SkIntToScalar(signed int param) {
76b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com        return (float)param;
77b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com    }
78b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com    static inline float SkIntToScalar(unsigned int param) {
79b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com        return (float)param;
80b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com    }
81b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com    static inline float SkIntToScalar(signed long param) {
82b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com        return (float)param;
83b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com    }
84b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com    static inline float SkIntToScalar(unsigned long param) {
85b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com        return (float)param;
86b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com    }
87b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com    static inline float SkIntToScalar(float param) {
88b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com        /* If the parameter passed into SkIntToScalar is a float,
89b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com         * one of two things has happened:
90b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com         * 1. the parameter was an SkScalar (which is typedef'd to float)
91b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com         * 2. the parameter was a float instead of an int
92b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com         *
93b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com         * Either way, it's not good.
94b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com         */
950c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com        SkDEBUGFAIL("looks like you passed an SkScalar into SkIntToScalar");
96b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com        return (float)0;
97b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com    }
98b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com#else  // not SK_DEBUG
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkIntToScalar(n) returns its integer argument as an SkScalar
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkIntToScalar(n)        ((float)(n))
102b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com#endif // not SK_DEBUG
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkFixedToScalar(x)      SkFixedToFloat(x)
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarToFixed(x)      SkFloatToFixed(x)
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarToFloat(n)      (n)
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkFloatToScalar(n)      (n)
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarToDouble(n)      (double)(n)
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDoubleToScalar(n)      (float)(n)
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** SkScalarFraction(x) returns the signed fractional part of the argument
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarFraction(x)     sk_float_mod(x, 1.0f)
1191b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com
1201b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com    #define SkScalarFloorToScalar(x)    sk_float_floor(x)
1211b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com    #define SkScalarCeilToScalar(x)     sk_float_ceil(x)
12224569b5bad4156a1eb95da92c96fc96b41e11622reed@google.com    #define SkScalarRoundToScalar(x)    sk_float_floor((x) + 0.5f)
1231b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com
1241b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com    #define SkScalarFloorToInt(x)       sk_float_floor2int(x)
1251b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com    #define SkScalarCeilToInt(x)        sk_float_ceil2int(x)
1261b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com    #define SkScalarRoundToInt(x)       sk_float_round2int(x)
1271b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com
1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the absolute value of the specified SkScalar
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarAbs(x)          sk_float_abs(x)
131eebf5cb6c0f5ed2630de2e7712d61b4ec1d49015reed@android.com    /** Return x with the sign of y
132eebf5cb6c0f5ed2630de2e7712d61b4ec1d49015reed@android.com     */
133eebf5cb6c0f5ed2630de2e7712d61b4ec1d49015reed@android.com    #define SkScalarCopySign(x, y)  sk_float_copysign(x, y)
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the value pinned between 0 and max inclusive
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return x < 0 ? 0 : x > max ? max : x;
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the value pinned between min and max inclusive
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return x < min ? min : x > max ? max : x;
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the specified SkScalar squared (x*x)
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of two SkScalars
1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMul(a, b)       ((float)(a) * (b))
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of two SkScalars plus a third SkScalar
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of a SkScalar and an int rounded to the nearest integer value
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b))
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of a SkScalar and an int promoted to the next larger int
1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b))
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of a SkScalar and an int truncated to the next smaller int
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b))
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the quotient of two SkScalars (a/b)
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarDiv(a, b)       ((float)(a) / (b))
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the mod of two SkScalars (a mod b)
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMod(x,y)        sk_float_mod(x,y)
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the product of the first two arguments, divided by the third argument
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the multiplicative inverse of the SkScalar (1/x)
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarInvert(x)       (SK_Scalar1 / (x))
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarFastInvert(x)   (SK_Scalar1 / (x))
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the square root of the SkScalar
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSqrt(x)         sk_float_sqrt(x)
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the average of two SkScalars (a+b)/2
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarAve(a, b)       (((a) + (b)) * 0.5f)
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns the geometric mean of two SkScalars
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMean(a, b)      sk_float_sqrt((float)(a) * (b))
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Returns one half of the specified SkScalar
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarHalf(a)         ((a) * 0.5f)
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarSqrt2          1.41421356f
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarPI             3.14159265f
1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarTanPIOver8     0.414213562f
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarRoot2Over2     0.707106781f
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSin(radians)    (float)sk_float_sin(radians)
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarCos(radians)    (float)sk_float_cos(radians)
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarTan(radians)    (float)sk_float_tan(radians)
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarASin(val)   (float)sk_float_asin(val)
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarACos(val)   (float)sk_float_acos(val)
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarExp(x)  (float)sk_float_exp(x)
2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarLog(x)  (float)sk_float_log(x)
2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
207185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com    static inline bool SkScalarIsInt(SkScalar x) {
208185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com        return x == (float)(int)x;
209185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com    }
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkFixed SkScalar;
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_Scalar1              SK_Fixed1
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarHalf           SK_FixedHalf
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarInfinity   SK_FixedMax
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarMax            SK_FixedMax
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarMin            SK_FixedMin
2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarNaN            SK_FixedNaN
2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarIsNaN(x)        ((x) == SK_FixedNaN)
220077910e20cda41d7981084fbd047a108894bc8dfreed@google.com    #define SkScalarIsFinite(x)     ((x) != SK_FixedNaN)
221077910e20cda41d7981084fbd047a108894bc8dfreed@google.com
2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkIntToScalar(n)        SkIntToFixed(n)
2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkFixedToScalar(x)      (x)
2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarToFixed(x)      (x)
2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #ifdef SK_CAN_USE_FLOAT
2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        #define SkScalarToFloat(n)  SkFixedToFloat(n)
2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        #define SkFloatToScalar(n)  SkFloatToFixed(n)
2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        #define SkScalarToDouble(n) SkFixedToDouble(n)
2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        #define SkDoubleToScalar(n) SkDoubleToFixed(n)
2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #endif
2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarFraction(x)     SkFixedFraction(x)
2331b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com
2341b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com    #define SkScalarFloorToScalar(x)    SkFixedFloorToFixed(x)
2351b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com    #define SkScalarCeilToScalar(x)     SkFixedCeilToFixed(x)
2361b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com    #define SkScalarRoundToScalar(x)    SkFixedRoundToFixed(x)
2371b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com
2381b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com    #define SkScalarFloorToInt(x)       SkFixedFloorToInt(x)
2391b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com    #define SkScalarCeilToInt(x)        SkFixedCeilToInt(x)
2401b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com    #define SkScalarRoundToInt(x)       SkFixedRoundToInt(x)
2411b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com
2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarAbs(x)          SkFixedAbs(x)
243eebf5cb6c0f5ed2630de2e7712d61b4ec1d49015reed@android.com    #define SkScalarCopySign(x, y)  SkCopySign32(x, y)
2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarClampMax(x, max) SkClampMax(x, max)
2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarPin(x, min, max) SkPin32(x, min, max)
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSquare(x)       SkFixedSquare(x)
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMul(a, b)       SkFixedMul(a, b)
2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulAdd(a, b, c) SkFixedMulAdd(a, b, c)
2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulRound(a, b)  SkFixedMulCommon(a, b, SK_FixedHalf)
2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulCeil(a, b)   SkFixedMulCommon(a, b, SK_Fixed1 - 1)
2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulFloor(a, b)  SkFixedMulCommon(a, b, 0)
2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarDiv(a, b)       SkFixedDiv(a, b)
2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMod(a, b)       SkFixedMod(a, b)
2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMulDiv(a, b, c) SkMulDiv(a, b, c)
2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarInvert(x)       SkFixedInvert(x)
2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarFastInvert(x)   SkFixedFastInvert(x)
2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSqrt(x)         SkFixedSqrt(x)
2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarAve(a, b)       SkFixedAve(a, b)
2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarMean(a, b)      SkFixedMean(a, b)
2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarHalf(a)         ((a) >> 1)
2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarSqrt2          SK_FixedSqrt2
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarPI             SK_FixedPI
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarTanPIOver8     SK_FixedTanPIOver8
2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SK_ScalarRoot2Over2     SK_FixedRoot2Over2
2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkDegreesToRadians(degrees)     SkFractMul(degrees, SK_FractPIOver180)
2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSinCos(radians, cosPtr) SkFixedSinCos(radians, cosPtr)
2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarSin(radians)    SkFixedSin(radians)
2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarCos(radians)    SkFixedCos(radians)
2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarTan(val)        SkFixedTan(val)
2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarASin(val)       SkFixedASin(val)
2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarACos(val)       SkFixedACos(val)
2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarATan2(y, x)     SkFixedATan2(y,x)
2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarExp(x)          SkFixedExp(x)
2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkScalarLog(x)          SkFixedLog(x)
2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkMaxScalar(a, b)       SkMax32(a, b)
2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkMinScalar(a, b)       SkMin32(a, b)
280185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com
281185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com    static inline bool SkScalarIsInt(SkFixed x) {
282185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com        return 0 == (x & 0xffff);
283185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com    }
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2861b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com// DEPRECATED : use ToInt or ToScalar variant
2871b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com#define SkScalarFloor(x)    SkScalarFloorToInt(x)
2881b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com#define SkScalarCeil(x)     SkScalarCeilToInt(x)
2891b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com#define SkScalarRound(x)    SkScalarRoundToInt(x)
2901b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com
291647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com/**
292647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com *  Returns -1 || 0 || 1 depending on the sign of value:
293647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com *  -1 if x < 0
294647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com *   0 if x == 0
295647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com *   1 if x > 0
296647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com */
297647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.comstatic inline int SkScalarSignAsInt(SkScalar x) {
298647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com    return x < 0 ? -1 : (x > 0);
299647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com}
300647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com
301647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com// Scalar result version of above
302647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.comstatic inline SkScalar SkScalarSignAsScalar(SkScalar x) {
303647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com    return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
304647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com}
3051b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com
3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
308187d5595901d1120d9425851e5afdd773f574502reed@android.comstatic inline bool SkScalarNearlyZero(SkScalar x,
3091fd56dc6e189ea0e94b5df9af959c243573f8883epoger@google.com                                    SkScalar tolerance = SK_ScalarNearlyZero) {
3109b5ca29af81b93faea040daa25a620bee99d6c38reed@google.com    SkASSERT(tolerance >= 0);
3119b5ca29af81b93faea040daa25a620bee99d6c38reed@google.com    return SkScalarAbs(x) <= tolerance;
3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3141fd56dc6e189ea0e94b5df9af959c243573f8883epoger@google.comstatic inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
3151fd56dc6e189ea0e94b5df9af959c243573f8883epoger@google.com                                     SkScalar tolerance = SK_ScalarNearlyZero) {
3169b5ca29af81b93faea040daa25a620bee99d6c38reed@google.com    SkASSERT(tolerance >= 0);
3179b5ca29af81b93faea040daa25a620bee99d6c38reed@google.com    return SkScalarAbs(x-y) <= tolerance;
3181fd56dc6e189ea0e94b5df9af959c243573f8883epoger@google.com}
3191fd56dc6e189ea0e94b5df9af959c243573f8883epoger@google.com
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Linearly interpolate between A and B, based on t.
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    If t is 0, return A
3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    If t is 1, return B
3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    else interpolate.
3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    t must be [0..SK_Scalar1]
3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/
326187d5595901d1120d9425851e5afdd773f574502reed@android.comstatic inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(t >= 0 && t <= SK_Scalar1);
3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return A + SkScalarMul(B - A, t);
3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
33128be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org/** Interpolate along the function described by (keys[length], values[length])
33228be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org    for the passed searchKey.  SearchKeys outside the range keys[0]-keys[Length]
33328be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org    clamp to the min or max value.  This function was inspired by a desire
33428be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org    to change the multiplier for thickness in fakeBold; therefore it assumes
33528be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org    the number of pairs (length) will be small, and a linear search is used.
33628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org    Repeated keys are allowed for discontinuous functions (so long as keys is
33728be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org    monotonically increasing), and if key is the value of a repeated scalar in
33828be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org    keys, the first one will be used.  However, that may change if a binary
33928be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org    search is used.
34028be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org*/
34128be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgSkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
34228be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org                            const SkScalar values[], int length);
34328be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org
3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
345