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>
This commit is contained in:
Mike Reed 2017-10-10 14:49:35 -04:00 committed by Skia Commit-Bot
parent 57a0edf7ba
commit e07622386b
3 changed files with 35 additions and 2 deletions

View File

@ -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))

View File

@ -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));
}
/**

View File

@ -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);
}
}