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

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

Issue 113873008: remove unused SkFixed and SkFract functions (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years 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 | Annotate | Revision Log
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 "SkCordic.h"
10 #include "SkFloatBits.h" 9 #include "SkFloatBits.h"
11 #include "SkFloatingPoint.h" 10 #include "SkFloatingPoint.h"
12 #include "Sk64.h" 11 #include "Sk64.h"
13 #include "SkScalar.h" 12 #include "SkScalar.h"
14 13
15 const uint32_t gIEEENotANumber = 0x7FFFFFFF; 14 const uint32_t gIEEENotANumber = 0x7FFFFFFF;
16 const uint32_t gIEEEInfinity = 0x7F800000; 15 const uint32_t gIEEEInfinity = 0x7F800000;
17 const uint32_t gIEEENegativeInfinity = 0xFF800000; 16 const uint32_t gIEEENegativeInfinity = 0xFF800000;
18 17
19 #define sub_shift(zeros, x, n) \ 18 #define sub_shift(zeros, x, n) \
(...skipping 27 matching lines...) Expand all
47 46
48 int32_t SkMulDiv(int32_t numer1, int32_t numer2, int32_t denom) { 47 int32_t SkMulDiv(int32_t numer1, int32_t numer2, int32_t denom) {
49 SkASSERT(denom); 48 SkASSERT(denom);
50 49
51 Sk64 tmp; 50 Sk64 tmp;
52 tmp.setMul(numer1, numer2); 51 tmp.setMul(numer1, numer2);
53 tmp.div(denom, Sk64::kTrunc_DivOption); 52 tmp.div(denom, Sk64::kTrunc_DivOption);
54 return tmp.get32(); 53 return tmp.get32();
55 } 54 }
56 55
57 int32_t SkMulShift(int32_t a, int32_t b, unsigned shift) {
58 int sign = SkExtractSign(a ^ b);
59
60 if (shift > 63) {
61 return sign;
62 }
63
64 a = SkAbs32(a);
65 b = SkAbs32(b);
66
67 uint32_t ah = a >> 16;
68 uint32_t al = a & 0xFFFF;
69 uint32_t bh = b >> 16;
70 uint32_t bl = b & 0xFFFF;
71
72 uint32_t A = ah * bh;
73 uint32_t B = ah * bl + al * bh;
74 uint32_t C = al * bl;
75
76 /* [ A ]
77 [ B ]
78 [ C ]
79 */
80 uint32_t lo = C + (B << 16);
81 int32_t hi = A + (B >> 16) + (lo < C);
82
83 if (sign < 0) {
84 hi = -hi - Sk32ToBool(lo);
85 lo = 0 - lo;
86 }
87
88 if (shift == 0) {
89 #ifdef SK_DEBUGx
90 SkASSERT(((int32_t)lo >> 31) == hi);
91 #endif
92 return lo;
93 } else if (shift >= 32) {
94 return hi >> (shift - 32);
95 } else {
96 #ifdef SK_DEBUGx
97 int32_t tmp = hi >> shift;
98 SkASSERT(tmp == 0 || tmp == -1);
99 #endif
100 // we want (hi << (32 - shift)) | (lo >> shift) but rounded
101 int roundBit = (lo >> (shift - 1)) & 1;
102 return ((hi << (32 - shift)) | (lo >> shift)) + roundBit;
103 }
104 }
105
106 SkFixed SkFixedMul_portable(SkFixed a, SkFixed b) { 56 SkFixed SkFixedMul_portable(SkFixed a, SkFixed b) {
107 #if 0 57 #if defined(SkLONGLONG)
108 Sk64 tmp;
109
110 tmp.setMul(a, b);
111 tmp.shiftRight(16);
112 return tmp.fLo;
113 #elif defined(SkLONGLONG)
114 return static_cast<SkFixed>((SkLONGLONG)a * b >> 16); 58 return static_cast<SkFixed>((SkLONGLONG)a * b >> 16);
115 #else 59 #else
116 int sa = SkExtractSign(a); 60 int sa = SkExtractSign(a);
117 int sb = SkExtractSign(b); 61 int sb = SkExtractSign(b);
118 // now make them positive 62 // now make them positive
119 a = SkApplySign(a, sa); 63 a = SkApplySign(a, sa);
120 b = SkApplySign(b, sb); 64 b = SkApplySign(b, sb);
121 65
122 uint32_t ah = a >> 16; 66 uint32_t ah = a >> 16;
123 uint32_t al = a & 0xFFFF; 67 uint32_t al = a & 0xFFFF;
124 uint32_t bh = b >> 16; 68 uint32_t bh = b >> 16;
125 uint32_t bl = b & 0xFFFF; 69 uint32_t bl = b & 0xFFFF;
126 70
127 uint32_t R = ah * b + al * bh + (al * bl >> 16); 71 uint32_t R = ah * b + al * bh + (al * bl >> 16);
128 72
129 return SkApplySign(R, sa ^ sb); 73 return SkApplySign(R, sa ^ sb);
130 #endif 74 #endif
131 } 75 }
132 76
133 SkFract SkFractMul_portable(SkFract a, SkFract b) {
134 #if 0
135 Sk64 tmp;
136 tmp.setMul(a, b);
137 return tmp.getFract();
138 #elif defined(SkLONGLONG)
139 return static_cast<SkFract>((SkLONGLONG)a * b >> 30);
140 #else
141 int sa = SkExtractSign(a);
142 int sb = SkExtractSign(b);
143 // now make them positive
144 a = SkApplySign(a, sa);
145 b = SkApplySign(b, sb);
146
147 uint32_t ah = a >> 16;
148 uint32_t al = a & 0xFFFF;
149 uint32_t bh = b >> 16;
150 uint32_t bl = b & 0xFFFF;
151
152 uint32_t A = ah * bh;
153 uint32_t B = ah * bl + al * bh;
154 uint32_t C = al * bl;
155
156 /* [ A ]
157 [ B ]
158 [ C ]
159 */
160 uint32_t Lo = C + (B << 16);
161 uint32_t Hi = A + (B >>16) + (Lo < C);
162
163 SkASSERT((Hi >> 29) == 0); // else overflow
164
165 int32_t R = (Hi << 2) + (Lo >> 30);
166
167 return SkApplySign(R, sa ^ sb);
168 #endif
169 }
170
171 int SkFixedMulCommon(SkFixed a, int b, int bias) {
172 // this function only works if b is 16bits
173 SkASSERT(b == (int16_t)b);
174 SkASSERT(b >= 0);
175
176 int sa = SkExtractSign(a);
177 a = SkApplySign(a, sa);
178 uint32_t ah = a >> 16;
179 uint32_t al = a & 0xFFFF;
180 uint32_t R = ah * b + ((al * b + bias) >> 16);
181 return SkApplySign(R, sa);
182 }
183
184 #ifdef SK_DEBUGx
185 #define TEST_FASTINVERT
186 #endif
187
188 SkFixed SkFixedFastInvert(SkFixed x) {
189 /* Adapted (stolen) from gglRecip()
190 */
191
192 if (x == SK_Fixed1) {
193 return SK_Fixed1;
194 }
195
196 int sign = SkExtractSign(x);
197 uint32_t a = SkApplySign(x, sign);
198
199 if (a <= 2) {
200 return SkApplySign(SK_MaxS32, sign);
201 }
202
203 #ifdef TEST_FASTINVERT
204 SkFixed orig = a;
205 uint32_t slow = SkFixedDiv(SK_Fixed1, a);
206 #endif
207
208 // normalize a
209 int lz = SkCLZ(a);
210 a = a << lz >> 16;
211
212 // compute 1/a approximation (0.5 <= a < 1.0)
213 uint32_t r = 0x17400 - a; // (2.90625 (~2.914) - 2*a) >> 1
214
215 // Newton-Raphson iteration:
216 // x = r*(2 - a*r) = ((r/2)*(1 - a*r/2))*4
217 r = ( (0x10000 - ((a*r)>>16)) * r ) >> 15;
218 r = ( (0x10000 - ((a*r)>>16)) * r ) >> (30 - lz);
219
220 #ifdef TEST_FASTINVERT
221 SkDebugf("SkFixedFastInvert(%x %g) = %x %g Slow[%x %g]\n",
222 orig, orig/65536.,
223 r, r/65536.,
224 slow, slow/65536.);
225 #endif
226
227 return SkApplySign(r, sign);
228 }
229
230 /////////////////////////////////////////////////////////////////////////////// 77 ///////////////////////////////////////////////////////////////////////////////
231 78
232 #define DIVBITS_ITER(n) \ 79 #define DIVBITS_ITER(n) \
233 case n: \ 80 case n: \
234 if ((numer = (numer << 1) - denom) >= 0) \ 81 if ((numer = (numer << 1) - denom) >= 0) \
235 result |= 1 << (n - 1); else numer += denom 82 result |= 1 << (n - 1); else numer += denom
236 83
237 int32_t SkDivBits(int32_t numer, int32_t denom, int shift_bias) { 84 int32_t SkDivBits(int32_t numer, int32_t denom, int shift_bias) {
238 SkASSERT(denom != 0); 85 SkASSERT(denom != 0);
239 if (numer == 0) { 86 if (numer == 0) {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 DIVBITS_ITER( 1); 135 DIVBITS_ITER( 1);
289 } 136 }
290 } 137 }
291 138
292 if (result < 0) { 139 if (result < 0) {
293 result = SK_MaxS32; 140 result = SK_MaxS32;
294 } 141 }
295 return SkApplySign(result, sign); 142 return SkApplySign(result, sign);
296 } 143 }
297 144
298 /* mod(float numer, float denom) seems to always return the sign
299 of the numer, so that's what we do too
300 */
301 SkFixed SkFixedMod(SkFixed numer, SkFixed denom) {
302 int sn = SkExtractSign(numer);
303 int sd = SkExtractSign(denom);
304
305 numer = SkApplySign(numer, sn);
306 denom = SkApplySign(denom, sd);
307
308 if (numer < denom) {
309 return SkApplySign(numer, sn);
310 } else if (numer == denom) {
311 return 0;
312 } else {
313 SkFixed div = SkFixedDiv(numer, denom);
314 return SkApplySign(SkFixedMul(denom, div & 0xFFFF), sn);
315 }
316 }
317
318 /* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf 145 /* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf
319 */ 146 */
320 int32_t SkSqrtBits(int32_t x, int count) { 147 int32_t SkSqrtBits(int32_t x, int count) {
321 SkASSERT(x >= 0 && count > 0 && (unsigned)count <= 30); 148 SkASSERT(x >= 0 && count > 0 && (unsigned)count <= 30);
322 149
323 uint32_t root = 0; 150 uint32_t root = 0;
324 uint32_t remHi = 0; 151 uint32_t remHi = 0;
325 uint32_t remLo = x; 152 uint32_t remLo = x;
326 153
327 do { 154 do {
328 root <<= 1; 155 root <<= 1;
329 156
330 remHi = (remHi<<2) | (remLo>>30); 157 remHi = (remHi<<2) | (remLo>>30);
331 remLo <<= 2; 158 remLo <<= 2;
332 159
333 uint32_t testDiv = (root << 1) + 1; 160 uint32_t testDiv = (root << 1) + 1;
334 if (remHi >= testDiv) { 161 if (remHi >= testDiv) {
335 remHi -= testDiv; 162 remHi -= testDiv;
336 root++; 163 root++;
337 } 164 }
338 } while (--count >= 0); 165 } while (--count >= 0);
339 166
340 return root; 167 return root;
341 } 168 }
342 169
343 int32_t SkCubeRootBits(int32_t value, int bits) {
344 SkASSERT(bits > 0);
345
346 int sign = SkExtractSign(value);
347 value = SkApplySign(value, sign);
348
349 uint32_t root = 0;
350 uint32_t curr = (uint32_t)value >> 30;
351 value <<= 2;
352
353 do {
354 root <<= 1;
355 uint32_t guess = root * root + root;
356 guess = (guess << 1) + guess; // guess *= 3
357 if (guess < curr) {
358 curr -= guess + 1;
359 root |= 1;
360 }
361 curr = (curr << 3) | ((uint32_t)value >> 29);
362 value <<= 3;
363 } while (--bits);
364
365 return SkApplySign(root, sign);
366 }
367
368 SkFixed SkFixedMean(SkFixed a, SkFixed b) {
369 Sk64 tmp;
370
371 tmp.setMul(a, b);
372 return tmp.getSqrt();
373 }
374
375 /////////////////////////////////////////////////////////////////////////////// 170 ///////////////////////////////////////////////////////////////////////////////
376 171
377 float SkScalarSinCos(float radians, float* cosValue) { 172 float SkScalarSinCos(float radians, float* cosValue) {
378 float sinValue = sk_float_sin(radians); 173 float sinValue = sk_float_sin(radians);
379 174
380 if (cosValue) { 175 if (cosValue) {
381 *cosValue = sk_float_cos(radians); 176 *cosValue = sk_float_cos(radians);
382 if (SkScalarNearlyZero(*cosValue)) { 177 if (SkScalarNearlyZero(*cosValue)) {
383 *cosValue = 0; 178 *cosValue = 0;
384 } 179 }
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 SkASSERT(SkAbs32(diff) <= 7); 291 SkASSERT(SkAbs32(diff) <= 7);
497 } 292 }
498 #endif 293 #endif
499 294
500 if (cosValuePtr) { 295 if (cosValuePtr) {
501 *cosValuePtr = cosValue; 296 *cosValuePtr = cosValue;
502 } 297 }
503 return sinValue; 298 return sinValue;
504 } 299 }
505 300
506 ///////////////////////////////////////////////////////////////////////////////
507
508 SkFixed SkFixedTan(SkFixed radians) { return SkCordicTan(radians); }
509 SkFixed SkFixedASin(SkFixed x) { return SkCordicASin(x); }
510 SkFixed SkFixedACos(SkFixed x) { return SkCordicACos(x); }
511 SkFixed SkFixedATan2(SkFixed y, SkFixed x) { return SkCordicATan2(y, x); }
512 SkFixed SkFixedExp(SkFixed x) { return SkCordicExp(x); }
513 SkFixed SkFixedLog(SkFixed x) { return SkCordicLog(x); }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698