diff options
author | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-12-12 19:47:09 +0000 |
---|---|---|
committer | commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-12-12 19:47:09 +0000 |
commit | f2d0cd78dd0eabb327709c5c03b0b782e7732700 (patch) | |
tree | 241814e5cfb5e1b9d2c5e63bc9a11f76db4db37d | |
parent | 67ce5659f731241077e13ebeaaa728bf2fabb7e5 (diff) | |
download | src-f2d0cd78dd0eabb327709c5c03b0b782e7732700.tar.gz |
Preventing division by 0
I did bench tests and I can't compute the denom before the if condition without taking a hit. After a few quick tests on my linux desktop, computing the 1st denom costs 6ms, the 2nd one 5ms and both together cost 9ms. The reason for this is that both if conditions here are exceptions and are expected to be false, so the denom computation should be skipped, if possible. The bench test I ran was :
out/Release/bench --match Xfermode_Hue --config 8888 --minMs 1000
BUG=
R=senorblanco@chromium.org, senorblanco@google.com, reed@google.com, sugoi@google.com
Author: sugoi@chromium.org
Review URL: https://codereview.chromium.org/114173002
git-svn-id: http://skia.googlecode.com/svn/trunk/src@12649 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | core/SkXfermode.cpp | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/core/SkXfermode.cpp b/core/SkXfermode.cpp index 8cb79c2d..176bcb50 100644 --- a/core/SkXfermode.cpp +++ b/core/SkXfermode.cpp @@ -468,16 +468,18 @@ static inline void clipColor(int* r, int* g, int* b, int a) { int L = Lum(*r, *g, *b); int n = minimum(*r, *g, *b); int x = maximum(*r, *g, *b); - if(n < 0) { - *r = L + SkMulDiv(*r - L, L, L - n); - *g = L + SkMulDiv(*g - L, L, L - n); - *b = L + SkMulDiv(*b - L, L, L - n); + int denom; + if ((n < 0) && (denom = L - n)) { // Compute denom and make sure it's non zero + *r = L + SkMulDiv(*r - L, L, denom); + *g = L + SkMulDiv(*g - L, L, denom); + *b = L + SkMulDiv(*b - L, L, denom); } - if (x > a) { - *r = L + SkMulDiv(*r - L, a - L, x - L); - *g = L + SkMulDiv(*g - L, a - L, x - L); - *b = L + SkMulDiv(*b - L, a - L, x - L); + if ((x > a) && (denom = x - L)) { // Compute denom and make sure it's non zero + int numer = a - L; + *r = L + SkMulDiv(*r - L, numer, denom); + *g = L + SkMulDiv(*g - L, numer, denom); + *b = L + SkMulDiv(*b - L, numer, denom); } } |