SkScalar.h revision e1e7d7a6b55504d97ce9f30202a179b63800093b
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 */ 43373ebc634573364c27b1ebd35bb537ef1285cba4bsalomon@google.com #define SK_ScalarInfinity (*SkTCast<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 */ 52373ebc634573364c27b1ebd35bb537ef1285cba4bsalomon@google.com #define SK_ScalarNaN (*SkTCast<const float*>(&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; } 569a323c98f6cd5d1dc002936588d182121231c69dreed@google.com 57077910e20cda41d7981084fbd047a108894bc8dfreed@google.com /** Returns true if x is not NaN and not infinite */ 58077910e20cda41d7981084fbd047a108894bc8dfreed@google.com static inline bool SkScalarIsFinite(float x) { 599a323c98f6cd5d1dc002936588d182121231c69dreed@google.com // We rely on the following behavior of infinities and nans 609a323c98f6cd5d1dc002936588d182121231c69dreed@google.com // 0 * finite --> 0 619a323c98f6cd5d1dc002936588d182121231c69dreed@google.com // 0 * infinity --> NaN 629a323c98f6cd5d1dc002936588d182121231c69dreed@google.com // 0 * NaN --> NaN 639a323c98f6cd5d1dc002936588d182121231c69dreed@google.com float prod = x * 0; 649a323c98f6cd5d1dc002936588d182121231c69dreed@google.com // At this point, prod will either be NaN or 0 659a323c98f6cd5d1dc002936588d182121231c69dreed@google.com // Therefore we can return (prod == prod) or (0 == prod). 669a323c98f6cd5d1dc002936588d182121231c69dreed@google.com return prod == prod; 67077910e20cda41d7981084fbd047a108894bc8dfreed@google.com } 689a323c98f6cd5d1dc002936588d182121231c69dreed@google.com 69b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com#ifdef SK_DEBUG 70b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com /** SkIntToScalar(n) returns its integer argument as an SkScalar 71b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * 72b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * If we're compiling in DEBUG mode, and can thus afford some extra runtime 73b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * cycles, check to make sure that the parameter passed in has not already 74b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * been converted to SkScalar. (A double conversion like this is harmless 75b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * for SK_SCALAR_IS_FLOAT, but for SK_SCALAR_IS_FIXED this causes trouble.) 76b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * 77b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * Note that we need all of these method signatures to properly handle the 78b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * various types that we pass into SkIntToScalar() to date: 79b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * int, size_t, U8CPU, etc., even though what we really mean is "anything 80b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * but a float". 81b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com */ 82dd8dd8f77b5802bdcb7b980761b54a055586b351reed@google.com static inline float SkIntToScalar(signed int param) { 83b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com return (float)param; 84b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com } 85b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com static inline float SkIntToScalar(unsigned int param) { 86b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com return (float)param; 87b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com } 88a030818936aa8f56db3cbf2d7fa14c1e5a543055reed@google.com static inline float SkIntToScalar(signed long param) { 89b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com return (float)param; 90b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com } 91a030818936aa8f56db3cbf2d7fa14c1e5a543055reed@google.com static inline float SkIntToScalar(unsigned long param) { 92b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com return (float)param; 93b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com } 94d4144060731a94a5b057dfad407e6e3c294435cfrobertphillips@google.com static inline float SkIntToScalar(float /* param */) { 95b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com /* If the parameter passed into SkIntToScalar is a float, 96b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * one of two things has happened: 97b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * 1. the parameter was an SkScalar (which is typedef'd to float) 98b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * 2. the parameter was a float instead of an int 99b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * 100b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com * Either way, it's not good. 101b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com */ 1020c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com SkDEBUGFAIL("looks like you passed an SkScalar into SkIntToScalar"); 103b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com return (float)0; 104b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com } 105b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com#else // not SK_DEBUG 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** SkIntToScalar(n) returns its integer argument as an SkScalar 1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkIntToScalar(n) ((float)(n)) 109b28b5e4e26183b6e14846fb00501cc445a4fdd7eepoger@google.com#endif // not SK_DEBUG 1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar 1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkFixedToScalar(x) SkFixedToFloat(x) 1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed 1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarToFixed(x) SkFloatToFixed(x) 1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarToFloat(n) (n) 1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkFloatToScalar(n) (n) 1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarToDouble(n) (double)(n) 1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDoubleToScalar(n) (float)(n) 1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** SkScalarFraction(x) returns the signed fractional part of the argument 1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarFraction(x) sk_float_mod(x, 1.0f) 1261b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com 1271b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com #define SkScalarFloorToScalar(x) sk_float_floor(x) 1281b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com #define SkScalarCeilToScalar(x) sk_float_ceil(x) 12924569b5bad4156a1eb95da92c96fc96b41e11622reed@google.com #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f) 1301b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com 1311b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com #define SkScalarFloorToInt(x) sk_float_floor2int(x) 1321b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com #define SkScalarCeilToInt(x) sk_float_ceil2int(x) 1331b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com #define SkScalarRoundToInt(x) sk_float_round2int(x) 1341b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com 1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the absolute value of the specified SkScalar 1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarAbs(x) sk_float_abs(x) 138eebf5cb6c0f5ed2630de2e7712d61b4ec1d49015reed@android.com /** Return x with the sign of y 139eebf5cb6c0f5ed2630de2e7712d61b4ec1d49015reed@android.com */ 140eebf5cb6c0f5ed2630de2e7712d61b4ec1d49015reed@android.com #define SkScalarCopySign(x, y) sk_float_copysign(x, y) 1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the value pinned between 0 and max inclusive 1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { 1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return x < 0 ? 0 : x > max ? max : x; 1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the value pinned between min and max inclusive 1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { 1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return x < min ? min : x > max ? max : x; 1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the specified SkScalar squared (x*x) 1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } 1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the product of two SkScalars 1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMul(a, b) ((float)(a) * (b)) 1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the product of two SkScalars plus a third SkScalar 1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c)) 1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the product of a SkScalar and an int rounded to the nearest integer value 1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b)) 1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the product of a SkScalar and an int promoted to the next larger int 1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b)) 1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the product of a SkScalar and an int truncated to the next smaller int 1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b)) 1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the quotient of two SkScalars (a/b) 1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarDiv(a, b) ((float)(a) / (b)) 1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the mod of two SkScalars (a mod b) 1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMod(x,y) sk_float_mod(x,y) 1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the product of the first two arguments, divided by the third argument 1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c)) 1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the multiplicative inverse of the SkScalar (1/x) 1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarInvert(x) (SK_Scalar1 / (x)) 1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarFastInvert(x) (SK_Scalar1 / (x)) 1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the square root of the SkScalar 1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarSqrt(x) sk_float_sqrt(x) 1850567f222b92d17923c687bed5f54b31652150891bungeman@google.com /** Returns b to the e 1860567f222b92d17923c687bed5f54b31652150891bungeman@google.com */ 1870567f222b92d17923c687bed5f54b31652150891bungeman@google.com #define SkScalarPow(b, e) sk_float_pow(b, e) 1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the average of two SkScalars (a+b)/2 1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarAve(a, b) (((a) + (b)) * 0.5f) 1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns the geometric mean of two SkScalars 1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMean(a, b) sk_float_sqrt((float)(a) * (b)) 1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Returns one half of the specified SkScalar 1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarHalf(a) ((a) * 0.5f) 1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarSqrt2 1.41421356f 1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarPI 3.14159265f 2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarTanPIOver8 0.414213562f 2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarRoot2Over2 0.707106781f 2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) 2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com float SkScalarSinCos(SkScalar radians, SkScalar* cosValue); 2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarSin(radians) (float)sk_float_sin(radians) 2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarCos(radians) (float)sk_float_cos(radians) 2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarTan(radians) (float)sk_float_tan(radians) 2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarASin(val) (float)sk_float_asin(val) 2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarACos(val) (float)sk_float_acos(val) 2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) 2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarExp(x) (float)sk_float_exp(x) 2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarLog(x) (float)sk_float_log(x) 2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } 2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } 2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 217185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com static inline bool SkScalarIsInt(SkScalar x) { 218185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com return x == (float)(int)x; 219185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com } 2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com typedef SkFixed SkScalar; 2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_Scalar1 SK_Fixed1 2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarHalf SK_FixedHalf 2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarInfinity SK_FixedMax 2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarMax SK_FixedMax 2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarMin SK_FixedMin 2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarNaN SK_FixedNaN 2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarIsNaN(x) ((x) == SK_FixedNaN) 230077910e20cda41d7981084fbd047a108894bc8dfreed@google.com #define SkScalarIsFinite(x) ((x) != SK_FixedNaN) 231077910e20cda41d7981084fbd047a108894bc8dfreed@google.com 2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkIntToScalar(n) SkIntToFixed(n) 2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkFixedToScalar(x) (x) 2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarToFixed(x) (x) 2357886ad3de1aa523d5c71f1fa9f355dfcb2412d1dreed@google.com #define SkScalarToFloat(n) SkFixedToFloat(n) 2367886ad3de1aa523d5c71f1fa9f355dfcb2412d1dreed@google.com #define SkFloatToScalar(n) SkFloatToFixed(n) 2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2387886ad3de1aa523d5c71f1fa9f355dfcb2412d1dreed@google.com #define SkScalarToDouble(n) SkFixedToDouble(n) 2397886ad3de1aa523d5c71f1fa9f355dfcb2412d1dreed@google.com #define SkDoubleToScalar(n) SkDoubleToFixed(n) 2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarFraction(x) SkFixedFraction(x) 2411b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com 2421b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com #define SkScalarFloorToScalar(x) SkFixedFloorToFixed(x) 2431b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com #define SkScalarCeilToScalar(x) SkFixedCeilToFixed(x) 2441b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com #define SkScalarRoundToScalar(x) SkFixedRoundToFixed(x) 2451b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com 2461b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com #define SkScalarFloorToInt(x) SkFixedFloorToInt(x) 2471b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com #define SkScalarCeilToInt(x) SkFixedCeilToInt(x) 2481b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com #define SkScalarRoundToInt(x) SkFixedRoundToInt(x) 2491b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com 2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarAbs(x) SkFixedAbs(x) 251eebf5cb6c0f5ed2630de2e7712d61b4ec1d49015reed@android.com #define SkScalarCopySign(x, y) SkCopySign32(x, y) 2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarClampMax(x, max) SkClampMax(x, max) 2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarPin(x, min, max) SkPin32(x, min, max) 2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarSquare(x) SkFixedSquare(x) 2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMul(a, b) SkFixedMul(a, b) 2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMulAdd(a, b, c) SkFixedMulAdd(a, b, c) 2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMulRound(a, b) SkFixedMulCommon(a, b, SK_FixedHalf) 2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMulCeil(a, b) SkFixedMulCommon(a, b, SK_Fixed1 - 1) 2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMulFloor(a, b) SkFixedMulCommon(a, b, 0) 2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarDiv(a, b) SkFixedDiv(a, b) 2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMod(a, b) SkFixedMod(a, b) 2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMulDiv(a, b, c) SkMulDiv(a, b, c) 2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarInvert(x) SkFixedInvert(x) 2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarFastInvert(x) SkFixedFastInvert(x) 2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarSqrt(x) SkFixedSqrt(x) 2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarAve(a, b) SkFixedAve(a, b) 2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarMean(a, b) SkFixedMean(a, b) 2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarHalf(a) ((a) >> 1) 2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarSqrt2 SK_FixedSqrt2 2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarPI SK_FixedPI 2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarTanPIOver8 SK_FixedTanPIOver8 2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_ScalarRoot2Over2 SK_FixedRoot2Over2 2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDegreesToRadians(degrees) SkFractMul(degrees, SK_FractPIOver180) 2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarSinCos(radians, cosPtr) SkFixedSinCos(radians, cosPtr) 2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarSin(radians) SkFixedSin(radians) 2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarCos(radians) SkFixedCos(radians) 2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarTan(val) SkFixedTan(val) 2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarASin(val) SkFixedASin(val) 2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarACos(val) SkFixedACos(val) 2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarATan2(y, x) SkFixedATan2(y,x) 2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarExp(x) SkFixedExp(x) 2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkScalarLog(x) SkFixedLog(x) 2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkMaxScalar(a, b) SkMax32(a, b) 2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkMinScalar(a, b) SkMin32(a, b) 288185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com 289185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com static inline bool SkScalarIsInt(SkFixed x) { 290185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com return 0 == (x & 0xffff); 291185d3d016d1e493694e9aaf45b070f34ba456983reed@google.com } 2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2941b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com// DEPRECATED : use ToInt or ToScalar variant 2951b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com#define SkScalarFloor(x) SkScalarFloorToInt(x) 2961b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com#define SkScalarCeil(x) SkScalarCeilToInt(x) 2971b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com#define SkScalarRound(x) SkScalarRoundToInt(x) 2981b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com 299647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com/** 300647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com * Returns -1 || 0 || 1 depending on the sign of value: 301647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com * -1 if x < 0 302647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com * 0 if x == 0 303647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com * 1 if x > 0 304647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com */ 305647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.comstatic inline int SkScalarSignAsInt(SkScalar x) { 306647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com return x < 0 ? -1 : (x > 0); 307647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com} 308647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com 309647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com// Scalar result version of above 310647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.comstatic inline SkScalar SkScalarSignAsScalar(SkScalar x) { 311647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0); 312647a804c3dd53b6743091ec97dd12111f90efec3bsalomon@google.com} 3131b20280f9b2e95fdcf4a2ee6f7f9d5a92c5b94c2reed@google.com 3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12)) 3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 316187d5595901d1120d9425851e5afdd773f574502reed@android.comstatic inline bool SkScalarNearlyZero(SkScalar x, 3171fd56dc6e189ea0e94b5df9af959c243573f8883epoger@google.com SkScalar tolerance = SK_ScalarNearlyZero) { 3189b5ca29af81b93faea040daa25a620bee99d6c38reed@google.com SkASSERT(tolerance >= 0); 3199b5ca29af81b93faea040daa25a620bee99d6c38reed@google.com return SkScalarAbs(x) <= tolerance; 3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3221fd56dc6e189ea0e94b5df9af959c243573f8883epoger@google.comstatic inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y, 3231fd56dc6e189ea0e94b5df9af959c243573f8883epoger@google.com SkScalar tolerance = SK_ScalarNearlyZero) { 3249b5ca29af81b93faea040daa25a620bee99d6c38reed@google.com SkASSERT(tolerance >= 0); 3259b5ca29af81b93faea040daa25a620bee99d6c38reed@google.com return SkScalarAbs(x-y) <= tolerance; 3261fd56dc6e189ea0e94b5df9af959c243573f8883epoger@google.com} 3271fd56dc6e189ea0e94b5df9af959c243573f8883epoger@google.com 3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Linearly interpolate between A and B, based on t. 3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com If t is 0, return A 3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com If t is 1, return B 3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else interpolate. 3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com t must be [0..SK_Scalar1] 3338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 334187d5595901d1120d9425851e5afdd773f574502reed@android.comstatic inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) { 3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT(t >= 0 && t <= SK_Scalar1); 3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return A + SkScalarMul(B - A, t); 3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 339f4959ab11827bef99e8985031feb457cae1f987akeyar@chromium.orgstatic inline SkScalar SkScalarLog2(SkScalar x) { 340f4959ab11827bef99e8985031feb457cae1f987akeyar@chromium.org static const SkScalar log2_conversion_factor = SkScalarDiv(1, SkScalarLog(2)); 341f4959ab11827bef99e8985031feb457cae1f987akeyar@chromium.org 342f4959ab11827bef99e8985031feb457cae1f987akeyar@chromium.org return SkScalarMul(SkScalarLog(x), log2_conversion_factor); 343f4959ab11827bef99e8985031feb457cae1f987akeyar@chromium.org} 344f4959ab11827bef99e8985031feb457cae1f987akeyar@chromium.org 34528be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org/** Interpolate along the function described by (keys[length], values[length]) 34628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length] 34728be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org clamp to the min or max value. This function was inspired by a desire 34828be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org to change the multiplier for thickness in fakeBold; therefore it assumes 34928be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org the number of pairs (length) will be small, and a linear search is used. 35028be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org Repeated keys are allowed for discontinuous functions (so long as keys is 35128be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org monotonically increasing), and if key is the value of a repeated scalar in 35228be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org keys, the first one will be used. However, that may change if a binary 35328be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org search is used. 35428be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org*/ 35528be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgSkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[], 35628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org const SkScalar values[], int length); 35728be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org 358e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com/* 359e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com * Helper to compare an array of scalars. 360e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com */ 361e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.comstatic inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) { 362e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com#ifdef SK_SCALAR_IS_FLOAT 363e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com SkASSERT(n >= 0); 364e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com for (int i = 0; i < n; ++i) { 365e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com if (a[i] != b[i]) { 366e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com return false; 367e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com } 368e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com } 369e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com return true; 370e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com#else 371e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com return 0 == memcmp(a, b, n * sizeof(SkScalar)); 372e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com#endif 373e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com} 374e1e7d7a6b55504d97ce9f30202a179b63800093breed@google.com 3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 376