diff options
author | mtklein <mtklein@chromium.org> | 2015-06-15 10:58:42 -0700 |
---|---|---|
committer | Steve Kondik <steve@cyngn.com> | 2016-06-27 17:33:43 -0700 |
commit | 010ac4a51644540bea0fc167971eb40a2f33b154 (patch) | |
tree | 8653d35214a38f7c525eafabba2996f07bcfec8f | |
parent | c873676a722e29629abb38ea140c3e468eedfced (diff) | |
download | android_external_skia-010ac4a51644540bea0fc167971eb40a2f33b154.tar.gz android_external_skia-010ac4a51644540bea0fc167971eb40a2f33b154.tar.bz2 android_external_skia-010ac4a51644540bea0fc167971eb40a2f33b154.zip |
Thorough tests for saturatedAdd and mulDiv255Round.
BUG=skia:3951
Committed: https://skia.googlesource.com/skia/+/ce9d11189a5924b47c3629063b72bae9d466c2c7
CQ_EXTRA_TRYBOTS=client.skia.android:Test-Android-GCC-Nexus5-CPU-NEON-Arm7-Release-Trybot
Review URL: https://codereview.chromium.org/1184113003
-rw-r--r-- | src/core/SkNx.h | 2 | ||||
-rw-r--r-- | tests/SkNxTest.cpp | 36 |
2 files changed, 37 insertions, 1 deletions
diff --git a/src/core/SkNx.h b/src/core/SkNx.h index d968cad19f..1342266b93 100644 --- a/src/core/SkNx.h +++ b/src/core/SkNx.h @@ -184,7 +184,7 @@ public: SkNi saturatedAdd(const SkNi& o) const { SkASSERT((T)(~0) > 0); // TODO: support signed T T sum = fVal + o.fVal; - return SkNi(sum > fVal ? sum : (T)(~0)); + return SkNi(sum < fVal ? (T)(~0) : sum); } SkNi operator + (const SkNi& o) const { return SkNi(fVal + o.fVal); } diff --git a/tests/SkNxTest.cpp b/tests/SkNxTest.cpp index 2463b46f08..eab625d41e 100644 --- a/tests/SkNxTest.cpp +++ b/tests/SkNxTest.cpp @@ -5,6 +5,7 @@ * found in the LICENSE file. */ +#include "Sk4px.h" #include "SkNx.h" #include "SkRandom.h" #include "Test.h" @@ -154,3 +155,38 @@ DEF_TEST(SkNi_min, r) { }} #endif } + +DEF_TEST(SkNi_saturatedAdd, r) { + for (int a = 0; a < (1<<8); a++) { + for (int b = 0; b < (1<<8); b++) { + int exact = a+b; + if (exact > 255) { exact = 255; } + if (exact < 0) { exact = 0; } + + REPORTER_ASSERT(r, Sk16b(a).saturatedAdd(Sk16b(b)).kth<0>() == exact); + } + } +} + +DEF_TEST(Sk4px_muldiv255round, r) { + for (int a = 0; a < (1<<8); a++) { + for (int b = 0; b < (1<<8); b++) { + int exact = (a*b+127)/255; + + // Duplicate a and b 16x each. + Sk4px av((SkAlpha)a), + bv((SkAlpha)b); + + // This way should always be exactly correct. + int correct = av.mulWiden(bv).div255RoundNarrow().kth<0>(); + REPORTER_ASSERT(r, correct == exact); + + // We're a bit more flexible on this method: correct for 0 or 255, otherwise off by <=1. + int fast = av.fastMulDiv255Round(bv).kth<0>(); + REPORTER_ASSERT(r, fast-exact >= -1 && fast-exact <= 1); + if (a == 0 || a == 255 || b == 0 || b == 255) { + REPORTER_ASSERT(r, fast == exact); + } + } + } +} |