diff options
author | benjaminwagner <benjaminwagner@google.com> | 2016-04-07 09:23:11 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-04-07 09:23:12 -0700 |
commit | 70f1a6c64ecb891d77aee576bd045f8b4a03f2a3 (patch) | |
tree | da07295eb50e9155cce082d0136ff7ee9b6e0f9e /include | |
parent | 73add93a9cac83f2369dec94e3e392d26590c838 (diff) |
Add pinned versions of *ToFixed.
BUG=skia:4632
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1707023002
Review URL: https://codereview.chromium.org/1707023002
Diffstat (limited to 'include')
-rw-r--r-- | include/private/SkFixed.h | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/include/private/SkFixed.h b/include/private/SkFixed.h index 56821d4d82..c90d5e9ffd 100644 --- a/include/private/SkFixed.h +++ b/include/private/SkFixed.h @@ -9,6 +9,8 @@ #define SkFixed_DEFINED #include "SkScalar.h" +#include "math.h" + #include "SkTypes.h" /** \file SkFixed.h @@ -29,15 +31,18 @@ typedef int32_t SkFixed; #define SK_FixedRoot2Over2 (0xB505) #define SkFixedToFloat(x) ((x) * 1.52587890625e-5f) -#if 1 - #define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1)) -#else - // pins over/under flows to max/min int32 (slower than just a cast) - static inline SkFixed SkFloatToFixed(float x) { - int64_t n = x * SK_Fixed1; - return (SkFixed)n; - } -#endif +#define SkFloatToFixed(x) ((SkFixed)((x) * SK_Fixed1)) + +// Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). +static inline SkFixed SkFloatPinToFixed(float x) { + x *= SK_Fixed1; + // Casting float to int outside the range of the target type (int32_t) is undefined behavior. + if (x >= SK_FixedMax) return SK_FixedMax; + if (x <= SK_FixedMin) return SK_FixedMin; + const SkFixed result = static_cast<SkFixed>(x); + SkASSERT(truncf(x) == static_cast<float>(result)); + return result; +} #ifdef SK_DEBUG static inline SkFixed SkFloatToFixed_Check(float x) { @@ -53,6 +58,17 @@ typedef int32_t SkFixed; #define SkFixedToDouble(x) ((x) * 1.52587890625e-5) #define SkDoubleToFixed(x) ((SkFixed)((x) * SK_Fixed1)) +// Pins over/under flows to SK_FixedMax/SK_FixedMin (slower than just a cast). +static inline SkFixed SkDoublePinToFixed(double x) { + x *= SK_Fixed1; + // Casting double to int outside the range of the target type (int32_t) is undefined behavior. + if (x >= SK_FixedMax) return SK_FixedMax; + if (x <= SK_FixedMin) return SK_FixedMin; + const SkFixed result = static_cast<SkFixed>(x); + SkASSERT(trunc(x) == static_cast<double>(result)); + return result; +} + /** Converts an integer to a SkFixed, asserting that the result does not overflow a 32 bit signed integer */ @@ -147,11 +163,13 @@ inline SkFixed SkFixedMul_longlong(SkFixed a, SkFixed b) { #define SkFixedToScalar(x) SkFixedToFloat(x) #define SkScalarToFixed(x) SkFloatToFixed(x) +#define SkScalarPinToFixed(x) SkFloatPinToFixed(x) #else // SK_SCALAR_IS_DOUBLE #define SkFixedToScalar(x) SkFixedToDouble(x) #define SkScalarToFixed(x) SkDoubleToFixed(x) +#define SkScalarPinToFixed(x) SkDoublePinToFixed(x) #endif |