Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: src/core/SkMath.cpp

Issue 1457863002: Revert of Fix UB in SkDivBits (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « BUILD.public ('k') | tools/dm_flags.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2008 The Android Open Source Project 2 * Copyright 2008 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkMathPriv.h" 8 #include "SkMathPriv.h"
9 #include "SkFloatBits.h" 9 #include "SkFloatBits.h"
10 #include "SkFloatingPoint.h" 10 #include "SkFloatingPoint.h"
(...skipping 27 matching lines...) Expand all
38 } 38 }
39 if (x & 0x2) { 39 if (x & 0x2) {
40 sub_shift(zeros, x, 1); 40 sub_shift(zeros, x, 1);
41 } 41 }
42 42
43 return zeros; 43 return zeros;
44 } 44 }
45 45
46 /////////////////////////////////////////////////////////////////////////////// 46 ///////////////////////////////////////////////////////////////////////////////
47 47
48 #define DIVBITS_ITER(n) \
49 case n: \
50 if ((numer = (numer << 1) - denom) >= 0) \
51 result |= 1 << (n - 1); else numer += denom
48 52
49 #define DIVBITS_ITER(k) \ 53 int32_t SkDivBits(int32_t numer, int32_t denom, int shift_bias) {
50 case k: \ 54 SkASSERT(denom != 0);
51 if (numer*2 >= denom) { \ 55 if (numer == 0) {
52 numer = numer*2 - denom; \
53 result |= 1 << (k-1); \
54 } else { \
55 numer *= 2; \
56 }
57
58 // NOTE: if you're thinking of editing this method, consider replacing it with
59 // a simple shift and divide. This legacy method predated reliable hardware div ision.
60 int32_t SkDivBits(int32_t n, int32_t d, int shift_bias) {
61 SkASSERT(d != 0);
62 if (n == 0) {
63 return 0; 56 return 0;
64 } 57 }
65 58
66 // Make numer and denom positive, and sign hold the resulting sign 59 // make numer and denom positive, and sign hold the resulting sign
67 // We'll be left-shifting numer, so it's important it's a uint32_t. 60 int32_t sign = SkExtractSign(numer ^ denom);
68 // We put denom in a uint32_t just to keep things simple. 61 numer = SkAbs32(numer);
69 int32_t sign = SkExtractSign(n ^ d); 62 denom = SkAbs32(denom);
70 uint32_t numer = SkAbs32(n);
71 uint32_t denom = SkAbs32(d);
72
73 // It's probably a bug to use n or d below here.
74 63
75 int nbits = SkCLZ(numer) - 1; 64 int nbits = SkCLZ(numer) - 1;
76 int dbits = SkCLZ(denom) - 1; 65 int dbits = SkCLZ(denom) - 1;
77 int bits = shift_bias - nbits + dbits; 66 int bits = shift_bias - nbits + dbits;
78 67
79 if (bits < 0) { // answer will underflow 68 if (bits < 0) { // answer will underflow
80 return 0; 69 return 0;
81 } 70 }
82 if (bits > 31) { // answer will overflow 71 if (bits > 31) { // answer will overflow
83 return SkApplySign(SK_MaxS32, sign); 72 return SkApplySign(SK_MaxS32, sign);
84 } 73 }
85 74
86 denom <<= dbits; 75 denom <<= dbits;
87 numer <<= nbits; 76 numer <<= nbits;
88 77
89 SkFixed result = 0; 78 SkFixed result = 0;
90 79
91 // do the first one 80 // do the first one
92 if (numer >= denom) { 81 if ((numer -= denom) >= 0) {
93 numer -= denom;
94 result = 1; 82 result = 1;
83 } else {
84 numer += denom;
95 } 85 }
96 86
97 // Now fall into our switch statement if there are more bits to compute 87 // Now fall into our switch statement if there are more bits to compute
98 if (bits > 0) { 88 if (bits > 0) {
99 // make room for the rest of the answer bits 89 // make room for the rest of the answer bits
100 result <<= bits; 90 result <<= bits;
101 switch (bits) { 91 switch (bits) {
102 DIVBITS_ITER(31); DIVBITS_ITER(30); DIVBITS_ITER(29); 92 DIVBITS_ITER(31); DIVBITS_ITER(30); DIVBITS_ITER(29);
103 DIVBITS_ITER(28); DIVBITS_ITER(27); DIVBITS_ITER(26); 93 DIVBITS_ITER(28); DIVBITS_ITER(27); DIVBITS_ITER(26);
104 DIVBITS_ITER(25); DIVBITS_ITER(24); DIVBITS_ITER(23); 94 DIVBITS_ITER(25); DIVBITS_ITER(24); DIVBITS_ITER(23);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 if (SkScalarNearlyZero(*cosValue)) { 146 if (SkScalarNearlyZero(*cosValue)) {
157 *cosValue = 0; 147 *cosValue = 0;
158 } 148 }
159 } 149 }
160 150
161 if (SkScalarNearlyZero(sinValue)) { 151 if (SkScalarNearlyZero(sinValue)) {
162 sinValue = 0; 152 sinValue = 0;
163 } 153 }
164 return sinValue; 154 return sinValue;
165 } 155 }
OLDNEW
« no previous file with comments | « BUILD.public ('k') | tools/dm_flags.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698