From bd1605905b1ce165e6b5891547c2b612358b47a8 Mon Sep 17 00:00:00 2001 From: reed Date: Fri, 31 Oct 2014 06:55:41 -0700 Subject: [PATCH] begin clean up for scalar is double option BUG=skia: Review URL: https://codereview.chromium.org/694693002 --- include/core/SkScalar.h | 241 ++++++++++++++++++++-------------------- 1 file changed, 123 insertions(+), 118 deletions(-) diff --git a/include/core/SkScalar.h b/include/core/SkScalar.h index b37cf5c998..feea687d0a 100644 --- a/include/core/SkScalar.h +++ b/include/core/SkScalar.h @@ -13,66 +13,29 @@ //#define SK_SUPPORT_DEPRECATED_SCALARROUND -typedef float SkScalar; +// TODO: move this sort of check into SkPostConfig.h +#define SK_SCALAR_IS_DOUBLE 0 +#undef SK_SCALAR_IS_FLOAT +#define SK_SCALAR_IS_FLOAT 1 -/** SK_Scalar1 is defined to be 1.0 represented as an SkScalar -*/ -#define SK_Scalar1 (1.0f) -/** SK_Scalar1 is defined to be 1/2 represented as an SkScalar -*/ -#define SK_ScalarHalf (0.5f) -/** SK_ScalarInfinity is defined to be infinity as an SkScalar -*/ -#define SK_ScalarInfinity SK_FloatInfinity -/** SK_ScalarNegativeInfinity is defined to be negative infinity as an SkScalar -*/ -#define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity -/** SK_ScalarMax is defined to be the largest value representable as an SkScalar -*/ -#define SK_ScalarMax (3.402823466e+38f) -/** SK_ScalarMin is defined to be the smallest value representable as an SkScalar -*/ -#define SK_ScalarMin (-SK_ScalarMax) -/** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar -*/ -#define SK_ScalarNaN SK_FloatNaN -/** SkScalarIsNaN(n) returns true if argument is not a number -*/ -static inline bool SkScalarIsNaN(float x) { return x != x; } - -/** Returns true if x is not NaN and not infinite */ -static inline bool SkScalarIsFinite(float x) { - // We rely on the following behavior of infinities and nans - // 0 * finite --> 0 - // 0 * infinity --> NaN - // 0 * NaN --> NaN - float prod = x * 0; - // At this point, prod will either be NaN or 0 - // Therefore we can return (prod == prod) or (0 == prod). - return prod == prod; -} -/** SkIntToScalar(n) returns its integer argument as an SkScalar -*/ -#define SkIntToScalar(n) ((float)(n)) -/** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar -*/ -#define SkFixedToScalar(x) SkFixedToFloat(x) -/** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed -*/ -#define SkScalarToFixed(x) SkFloatToFixed(x) +#if SK_SCALAR_IS_FLOAT -#define SkScalarToFloat(n) (n) -#ifndef SK_SCALAR_TO_FLOAT_EXCLUDED -#define SkFloatToScalar(n) (n) -#endif +typedef float SkScalar; -#define SkScalarToDouble(n) (double)(n) -#define SkDoubleToScalar(n) (float)(n) +#define SK_Scalar1 1.0f +#define SK_ScalarHalf 0.5f +#define SK_ScalarSqrt2 1.41421356f +#define SK_ScalarPI 3.14159265f +#define SK_ScalarTanPIOver8 0.414213562f +#define SK_ScalarRoot2Over2 0.707106781f +#define SK_ScalarMax 3.402823466e+38f +#define SK_ScalarInfinity SK_FloatInfinity +#define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity +#define SK_ScalarNaN SK_FloatNaN -/** SkScalarFraction(x) returns the signed fractional part of the argument -*/ -#define SkScalarFraction(x) sk_float_mod(x, 1.0f) +#define SkFixedToScalar(x) SkFixedToFloat(x) +#define SkScalarToFixed(x) SkFloatToFixed(x) #define SkScalarFloorToScalar(x) sk_float_floor(x) #define SkScalarCeilToScalar(x) sk_float_ceil(x) @@ -81,7 +44,93 @@ static inline bool SkScalarIsFinite(float x) { #define SkScalarFloorToInt(x) sk_float_floor2int(x) #define SkScalarCeilToInt(x) sk_float_ceil2int(x) #define SkScalarRoundToInt(x) sk_float_round2int(x) -#define SkScalarTruncToInt(x) static_cast(x) + +#define SkScalarAbs(x) sk_float_abs(x) +#define SkScalarCopySign(x, y) sk_float_copysign(x, y) +#define SkScalarMod(x, y) sk_float_mod(x,y) +#define SkScalarFraction(x) sk_float_mod(x, 1.0f) +#define SkScalarSqrt(x) sk_float_sqrt(x) +#define SkScalarPow(b, e) sk_float_pow(b, e) + +#define SkScalarSin(radians) (float)sk_float_sin(radians) +#define SkScalarCos(radians) (float)sk_float_cos(radians) +#define SkScalarTan(radians) (float)sk_float_tan(radians) +#define SkScalarASin(val) (float)sk_float_asin(val) +#define SkScalarACos(val) (float)sk_float_acos(val) +#define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) +#define SkScalarExp(x) (float)sk_float_exp(x) +#define SkScalarLog(x) (float)sk_float_log(x) + +#else // SK_SCALAR_IS_DOUBLE + +typedef double SkScalar; + +#define SK_Scalar1 1.0 +#define SK_ScalarHalf 0.5 +#define SK_ScalarSqrt2 1.414213562373095 +#define SK_ScalarPI 3.141592653589793 +#define SK_ScalarTanPIOver8 0.4142135623731 +#define SK_ScalarRoot2Over2 0.70710678118655 +#define SK_ScalarMax 1.7976931348623157+308 +#define SK_ScalarInfinity SK_DoubleInfinity +#define SK_ScalarNegativeInfinity SK_DoubleNegativeInfinity +#define SK_ScalarNaN SK_DoubleNaN + +#define SkFixedToScalar(x) SkFixedToDouble(x) +#define SkScalarToFixed(x) SkDoubleToFixed(x) + +#define SkScalarFloorToScalar(x) floor(x) +#define SkScalarCeilToScalar(x) ceil(x) +#define SkScalarRoundToScalar(x) floor((x) + 0.5) + +#define SkScalarFloorToInt(x) (int)floor(x) +#define SkScalarCeilToInt(x) (int)ceil(x) +#define SkScalarRoundToInt(x) (int)floor((x) + 0.5) + +#define SkScalarAbs(x) abs(x) +#define SkScalarCopySign(x, y) copysign(x, y) +#define SkScalarMod(x, y) fmod(x,y) +#define SkScalarFraction(x) fmod(x, 1.0) +#define SkScalarSqrt(x) sqrt(x) +#define SkScalarPow(b, e) pow(b, e) + +#define SkScalarSin(radians) sin(radians) +#define SkScalarCos(radians) cos(radians) +#define SkScalarTan(radians) tan(radians) +#define SkScalarASin(val) asin(val) +#define SkScalarACos(val) acos(val) +#define SkScalarATan2(y, x) atan2(y,x) +#define SkScalarExp(x) exp(x) +#define SkScalarLog(x) log(x) + +#endif + +////////////////////////////////////////////////////////////////////////////////////////////////// + +#define SkIntToScalar(x) static_cast(x) +#define SkScalarTruncToInt(x) static_cast(x) + +#define SkScalarToFloat(x) static_cast(x) +#define SkFloatToScalar(x) static_cast(x) +#define SkScalarToDouble(x) static_cast(x) +#define SkDoubleToScalar(x) static_cast(x) + +#define SK_ScalarMin (-SK_ScalarMax) + +static inline bool SkScalarIsNaN(SkScalar x) { return x != x; } + +/** Returns true if x is not NaN and not infinite + */ +static inline bool SkScalarIsFinite(SkScalar x) { + // We rely on the following behavior of infinities and nans + // 0 * finite --> 0 + // 0 * infinity --> NaN + // 0 * NaN --> NaN + SkScalar prod = x * 0; + // At this point, prod will either be NaN or 0 + // Therefore we can return (prod == prod) or (0 == prod). + return prod == prod; +} /** * Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5) explicitly using @@ -103,79 +152,35 @@ static inline int SkDScalarRoundToInt(SkScalar x) { return (int)floor(xx); } -/** Returns the absolute value of the specified SkScalar -*/ -#define SkScalarAbs(x) sk_float_abs(x) -/** Return x with the sign of y - */ -#define SkScalarCopySign(x, y) sk_float_copysign(x, y) -/** Returns the value pinned between 0 and max inclusive -*/ -inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { +static inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { return x < 0 ? 0 : x > max ? max : x; } -/** Returns the value pinned between min and max inclusive -*/ -inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { + +static inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { return x < min ? min : x > max ? max : x; } -/** Returns the specified SkScalar squared (x*x) -*/ -inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } -/** Returns the product of two SkScalars -*/ -#define SkScalarMul(a, b) ((float)(a) * (b)) -/** Returns the product of two SkScalars plus a third SkScalar -*/ -#define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c)) -/** Returns the quotient of two SkScalars (a/b) -*/ -#define SkScalarDiv(a, b) ((float)(a) / (b)) -/** Returns the mod of two SkScalars (a mod b) -*/ -#define SkScalarMod(x,y) sk_float_mod(x,y) -/** Returns the product of the first two arguments, divided by the third argument -*/ -#define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c)) -/** Returns the multiplicative inverse of the SkScalar (1/x) -*/ + +SkScalar SkScalarSinCos(SkScalar radians, SkScalar* cosValue); + +static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } + +#define SkScalarMul(a, b) ((SkScalar)(a) * (b)) +#define SkScalarMulAdd(a, b, c) ((SkScalar)(a) * (b) + (c)) +#define SkScalarDiv(a, b) ((SkScalar)(a) / (b)) +#define SkScalarMulDiv(a, b, c) ((SkScalar)(a) * (b) / (c)) #define SkScalarInvert(x) (SK_Scalar1 / (x)) #define SkScalarFastInvert(x) (SK_Scalar1 / (x)) -/** Returns the square root of the SkScalar -*/ -#define SkScalarSqrt(x) sk_float_sqrt(x) -/** Returns b to the e -*/ -#define SkScalarPow(b, e) sk_float_pow(b, e) -/** Returns the average of two SkScalars (a+b)/2 -*/ -#define SkScalarAve(a, b) (((a) + (b)) * 0.5f) -/** Returns one half of the specified SkScalar -*/ -#define SkScalarHalf(a) ((a) * 0.5f) - -#define SK_ScalarSqrt2 1.41421356f -#define SK_ScalarPI 3.14159265f -#define SK_ScalarTanPIOver8 0.414213562f -#define SK_ScalarRoot2Over2 0.707106781f +#define SkScalarAve(a, b) (((a) + (b)) * SK_ScalarHalf) +#define SkScalarHalf(a) ((a) * SK_ScalarHalf) #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI)) -float SkScalarSinCos(SkScalar radians, SkScalar* cosValue); -#define SkScalarSin(radians) (float)sk_float_sin(radians) -#define SkScalarCos(radians) (float)sk_float_cos(radians) -#define SkScalarTan(radians) (float)sk_float_tan(radians) -#define SkScalarASin(val) (float)sk_float_asin(val) -#define SkScalarACos(val) (float)sk_float_acos(val) -#define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) -#define SkScalarExp(x) (float)sk_float_exp(x) -#define SkScalarLog(x) (float)sk_float_log(x) - -inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } -inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } + +static inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } +static inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } static inline bool SkScalarIsInt(SkScalar x) { - return x == (float)(int)x; + return x == (SkScalar)(int)x; } // DEPRECATED : use ToInt or ToScalar variant -- 2.34.1