diff options
author | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-05-05 16:04:42 +0000 |
---|---|---|
committer | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2014-05-05 16:04:42 +0000 |
commit | 4e332f82fce0126045e9cb2ef0a2097a6c4c40a3 (patch) | |
tree | f2bef88528e573fc428eebfb8a653f4ed0d4a186 /include/core/SkScalar.h | |
parent | 3df4e954029919ae894797635c9f310681dc83fd (diff) |
add rounding-using-doubles methods on SkScalar and SkRect
Inspired by the excellent repro case for https://crbug.com/364224
patch from issue 265933010
BUG=skia:
R=bungeman@google.com
Author: reed@google.com
Review URL: https://codereview.chromium.org/267003002
git-svn-id: http://skia.googlecode.com/svn/trunk@14566 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/core/SkScalar.h')
-rw-r--r-- | include/core/SkScalar.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/include/core/SkScalar.h b/include/core/SkScalar.h index b9256badb4..b37cf5c998 100644 --- a/include/core/SkScalar.h +++ b/include/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) |