aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-04-22 20:21:56 +0000
committerGravatar reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-04-22 20:21:56 +0000
commitea774d2a5a049bf89474c0f047ed6a4e521de126 (patch)
tree0063617a51faaaec63250ebd5fc05a12bd18f921
parent4eedae6f72dc6f410010976d7157f045b150b904 (diff)
fix asserts in SkMulDiv255Round, and add test
Review URL: https://codereview.chromium.org/13934010 git-svn-id: http://skia.googlecode.com/svn/trunk@8813 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r--include/core/SkMath.h12
-rw-r--r--tests/MathTest.cpp28
2 files changed, 34 insertions, 6 deletions
diff --git a/include/core/SkMath.h b/include/core/SkMath.h
index 314311f655..cee3257197 100644
--- a/include/core/SkMath.h
+++ b/include/core/SkMath.h
@@ -139,7 +139,7 @@ static inline bool SkIsPow2(int value) {
* Return a*b/((1 << shift) - 1), rounding any fractional bits.
* Only valid if a and b are unsigned and <= 32767 and shift is > 0 and <= 8
*/
-static inline unsigned SkMul16ShiftRound(unsigned a, unsigned b, int shift) {
+static inline unsigned SkMul16ShiftRound(U16CPU a, U16CPU b, int shift) {
SkASSERT(a <= 32767);
SkASSERT(b <= 32767);
SkASSERT(shift > 0 && shift <= 8);
@@ -148,12 +148,12 @@ static inline unsigned SkMul16ShiftRound(unsigned a, unsigned b, int shift) {
}
/**
- * Return a*b/255, rounding any fractional bits. Only valid if both
- * a and b are 0..255
+ * Return a*b/255, rounding any fractional bits.
+ * Only valid if a and b are unsigned and <= 32767.
*/
-static inline U8CPU SkMulDiv255Round(U8CPU a, U8CPU b) {
- SkASSERT((uint8_t)a == a);
- SkASSERT((uint8_t)b == b);
+static inline U8CPU SkMulDiv255Round(U16CPU a, U16CPU b) {
+ SkASSERT(a <= 32767);
+ SkASSERT(b <= 32767);
unsigned prod = SkMulS16(a, b) + 128;
return (prod + (prod >> 8)) >> 8;
}
diff --git a/tests/MathTest.cpp b/tests/MathTest.cpp
index 105a3e4bdf..372b41c793 100644
--- a/tests/MathTest.cpp
+++ b/tests/MathTest.cpp
@@ -48,6 +48,32 @@ static void test_floor(skiatest::Reporter* reporter) {
///////////////////////////////////////////////////////////////////////////////
+// test that SkMul16ShiftRound and SkMulDiv255Round return the same result
+static void test_muldivround(skiatest::Reporter* reporter) {
+#if 0
+ // this "complete" test is too slow, so we test a random sampling of it
+
+ for (int a = 0; a <= 32767; ++a) {
+ for (int b = 0; b <= 32767; ++b) {
+ unsigned prod0 = SkMul16ShiftRound(a, b, 8);
+ unsigned prod1 = SkMulDiv255Round(a, b);
+ SkASSERT(prod0 == prod1);
+ }
+ }
+#endif
+
+ SkRandom rand;
+ for (int i = 0; i < 10000; ++i) {
+ unsigned a = rand.nextU() & 0x7FFF;
+ unsigned b = rand.nextU() & 0x7FFF;
+
+ unsigned prod0 = SkMul16ShiftRound(a, b, 8);
+ unsigned prod1 = SkMulDiv255Round(a, b);
+
+ REPORTER_ASSERT(reporter, prod0 == prod1);
+ }
+}
+
static float float_blend(int src, int dst, float unit) {
return dst + (src - dst) * unit;
}
@@ -595,6 +621,8 @@ static void TestMath(skiatest::Reporter* reporter) {
// disable for now
if (false) test_blend31(); // avoid bit rot, suppress warning
+
+ test_muldivround(reporter);
}
#include "TestClassDef.h"