diff options
Diffstat (limited to 'core/SkScalar.h')
-rw-r--r-- | core/SkScalar.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/core/SkScalar.h b/core/SkScalar.h index b9256ba..b37cf5c 100644 --- a/core/SkScalar.h +++ b/core/SkScalar.h @@ -83,6 +83,26 @@ static inline bool SkScalarIsFinite(float x) { #define SkScalarRoundToInt(x) sk_float_round2int(x) #define SkScalarTruncToInt(x) static_cast<int>(x) +/** + * Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5) explicitly using + * double, to avoid possibly losing the low bit(s) of the answer before calling floor(). + * + * This routine will likely be slower than SkScalarRoundToInt(), and should only be used when the + * extra precision is known to be valuable. + * + * In particular, this catches the following case: + * SkScalar x = 0.49999997; + * int ix = SkScalarRoundToInt(x); + * SkASSERT(0 == ix); // <--- fails + * ix = SkDScalarRoundToInt(x); + * SkASSERT(0 == ix); // <--- succeeds + */ +static inline int SkDScalarRoundToInt(SkScalar x) { + double xx = x; + xx += 0.5; + return (int)floor(xx); +} + /** Returns the absolute value of the specified SkScalar */ #define SkScalarAbs(x) sk_float_abs(x) |