aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Mike Reed <reed@google.com>2017-10-10 14:49:35 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-10-10 19:58:13 +0000
commite07622386b10c158cb0cc653ace264e24c5702af (patch)
tree07210d6ddd3ad14e7a66197261ad911d7f3cea80
parent57a0edf7ba03082e649f0c2ddc43930dfa849e7f (diff)
clone saturating cast code for doubles
Bug: skia: Change-Id: I4f35413995cf73c6f130476d6b36e530120aa7ed Reviewed-on: https://skia-review.googlesource.com/57901 Reviewed-by: Mike Klein <mtklein@google.com> Commit-Queue: Mike Reed <reed@google.com>
-rw-r--r--include/private/SkFloatingPoint.h9
-rw-r--r--src/core/SkScan_Path.cpp4
-rw-r--r--tests/MathTest.cpp24
3 files changed, 35 insertions, 2 deletions
diff --git a/include/private/SkFloatingPoint.h b/include/private/SkFloatingPoint.h
index 01afcd8942..05191e651d 100644
--- a/include/private/SkFloatingPoint.h
+++ b/include/private/SkFloatingPoint.h
@@ -90,6 +90,15 @@ static inline int sk_float_saturate2int(float x) {
return (int)x;
}
+/**
+ * Return the closest int for the given double. Returns SK_MaxS32 for NaN.
+ */
+static inline int sk_double_saturate2int(double x) {
+ x = SkTMin<double>(x, SK_MaxS32);
+ x = SkTMax<double>(x, SK_MinS32);
+ return (int)x;
+}
+
#define sk_float_floor2int(x) sk_float_saturate2int(sk_float_floor(x))
#define sk_float_round2int(x) sk_float_saturate2int(sk_float_floor((x) + 0.5f))
#define sk_float_ceil2int(x) sk_float_saturate2int(sk_float_ceil(x))
diff --git a/src/core/SkScan_Path.cpp b/src/core/SkScan_Path.cpp
index a8a43212c7..845b737368 100644
--- a/src/core/SkScan_Path.cpp
+++ b/src/core/SkScan_Path.cpp
@@ -573,7 +573,7 @@ static const double kRoundBias = 0.0;
static inline int round_down_to_int(SkScalar x) {
double xx = x;
xx -= 0.5 + kRoundBias;
- return (int)ceil(xx);
+ return sk_double_saturate2int(ceil(xx));
}
/**
@@ -583,7 +583,7 @@ static inline int round_down_to_int(SkScalar x) {
static inline int round_up_to_int(SkScalar x) {
double xx = x;
xx += 0.5 + kRoundBias;
- return (int)floor(xx);
+ return sk_double_saturate2int(floor(xx));
}
/**
diff --git a/tests/MathTest.cpp b/tests/MathTest.cpp
index fcbd63ddca..0d0bdce67d 100644
--- a/tests/MathTest.cpp
+++ b/tests/MathTest.cpp
@@ -693,3 +693,27 @@ DEF_TEST(FloatSaturate, reporter) {
REPORTER_ASSERT(reporter, r.fExpectedInt == i);
}
}
+
+DEF_TEST(DoubleSaturate, reporter) {
+ const struct {
+ double fDouble;
+ int fExpectedInt;
+ } recs[] = {
+ { 0, 0 },
+ { 100.5, 100 },
+ { SK_MaxS32, SK_MaxS32 },
+ { SK_MinS32, SK_MinS32 },
+ { SK_MaxS32 - 1, SK_MaxS32 - 1 },
+ { SK_MinS32 + 1, SK_MinS32 + 1 },
+ { SK_MaxS32 * 100.0, SK_MaxS32 },
+ { SK_MinS32 * 100.0, SK_MinS32 },
+ { SK_ScalarInfinity, SK_MaxS32 },
+ { SK_ScalarNegativeInfinity, SK_MinS32 },
+ { SK_ScalarNaN, SK_MaxS32 },
+ };
+
+ for (auto r : recs) {
+ int i = sk_double_saturate2int(r.fDouble);
+ REPORTER_ASSERT(reporter, r.fExpectedInt == i);
+ }
+}