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
diff --git a/tests/ScalarTest.cpp b/tests/ScalarTest.cpp
index 82f61ec..6b7fe6b 100644
--- a/tests/ScalarTest.cpp
+++ b/tests/ScalarTest.cpp
@@ -12,6 +12,19 @@
#include "SkRect.h"
#include "Test.h"
+static void test_roundtoint(skiatest::Reporter* reporter) {
+ SkScalar x = 0.49999997;
+ int ix = SkScalarRoundToInt(x);
+ // We "should" get 0, since x < 0.5, but we don't due to float addition rounding up the low
+ // bit after adding 0.5.
+ REPORTER_ASSERT(reporter, 1 == ix);
+
+ // This version explicitly performs the +0.5 step using double, which should avoid losing the
+ // low bits.
+ ix = SkDScalarRoundToInt(x);
+ REPORTER_ASSERT(reporter, 0 == ix);
+}
+
struct PointSet {
const SkPoint* fPts;
size_t fCount;
@@ -187,4 +200,5 @@
DEF_TEST(Scalar, reporter) {
test_isfinite(reporter);
+ test_roundtoint(reporter);
}