OLD | NEW |
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 "SkFixed.h" | 9 #include "SkFixed.h" |
10 #include "SkFloatBits.h" | 10 #include "SkFloatBits.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 } | 39 } |
40 if (x & 0x2) { | 40 if (x & 0x2) { |
41 sub_shift(zeros, x, 1); | 41 sub_shift(zeros, x, 1); |
42 } | 42 } |
43 | 43 |
44 return zeros; | 44 return zeros; |
45 } | 45 } |
46 | 46 |
47 /////////////////////////////////////////////////////////////////////////////// | 47 /////////////////////////////////////////////////////////////////////////////// |
48 | 48 |
49 #define DIVBITS_ITER(n) \ | |
50 case n: \ | |
51 if ((numer = (numer << 1) - denom) >= 0) \ | |
52 result |= 1 << (n - 1); else numer += denom | |
53 | |
54 int32_t SkDivBits(int32_t numer, int32_t denom, int shift_bias) { | |
55 SkASSERT(denom != 0); | |
56 if (numer == 0) { | |
57 return 0; | |
58 } | |
59 | |
60 // make numer and denom positive, and sign hold the resulting sign | |
61 int32_t sign = SkExtractSign(numer ^ denom); | |
62 numer = SkAbs32(numer); | |
63 denom = SkAbs32(denom); | |
64 | |
65 int nbits = SkCLZ(numer) - 1; | |
66 int dbits = SkCLZ(denom) - 1; | |
67 int bits = shift_bias - nbits + dbits; | |
68 | |
69 if (bits < 0) { // answer will underflow | |
70 return 0; | |
71 } | |
72 if (bits > 31) { // answer will overflow | |
73 return SkApplySign(SK_MaxS32, sign); | |
74 } | |
75 | |
76 denom <<= dbits; | |
77 numer <<= nbits; | |
78 | |
79 SkFixed result = 0; | |
80 | |
81 // do the first one | |
82 if ((numer -= denom) >= 0) { | |
83 result = 1; | |
84 } else { | |
85 numer += denom; | |
86 } | |
87 | |
88 // Now fall into our switch statement if there are more bits to compute | |
89 if (bits > 0) { | |
90 // make room for the rest of the answer bits | |
91 result <<= bits; | |
92 switch (bits) { | |
93 DIVBITS_ITER(31); DIVBITS_ITER(30); DIVBITS_ITER(29); | |
94 DIVBITS_ITER(28); DIVBITS_ITER(27); DIVBITS_ITER(26); | |
95 DIVBITS_ITER(25); DIVBITS_ITER(24); DIVBITS_ITER(23); | |
96 DIVBITS_ITER(22); DIVBITS_ITER(21); DIVBITS_ITER(20); | |
97 DIVBITS_ITER(19); DIVBITS_ITER(18); DIVBITS_ITER(17); | |
98 DIVBITS_ITER(16); DIVBITS_ITER(15); DIVBITS_ITER(14); | |
99 DIVBITS_ITER(13); DIVBITS_ITER(12); DIVBITS_ITER(11); | |
100 DIVBITS_ITER(10); DIVBITS_ITER( 9); DIVBITS_ITER( 8); | |
101 DIVBITS_ITER( 7); DIVBITS_ITER( 6); DIVBITS_ITER( 5); | |
102 DIVBITS_ITER( 4); DIVBITS_ITER( 3); DIVBITS_ITER( 2); | |
103 // we merge these last two together, makes GCC make better ARM | |
104 default: | |
105 DIVBITS_ITER( 1); | |
106 } | |
107 } | |
108 | |
109 if (result < 0) { | |
110 result = SK_MaxS32; | |
111 } | |
112 return SkApplySign(result, sign); | |
113 } | |
114 | |
115 /* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf | 49 /* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf |
116 */ | 50 */ |
117 int32_t SkSqrtBits(int32_t x, int count) { | 51 int32_t SkSqrtBits(int32_t x, int count) { |
118 SkASSERT(x >= 0 && count > 0 && (unsigned)count <= 30); | 52 SkASSERT(x >= 0 && count > 0 && (unsigned)count <= 30); |
119 | 53 |
120 uint32_t root = 0; | 54 uint32_t root = 0; |
121 uint32_t remHi = 0; | 55 uint32_t remHi = 0; |
122 uint32_t remLo = x; | 56 uint32_t remLo = x; |
123 | 57 |
124 do { | 58 do { |
(...skipping 22 matching lines...) Expand all Loading... |
147 if (SkScalarNearlyZero(*cosValue)) { | 81 if (SkScalarNearlyZero(*cosValue)) { |
148 *cosValue = 0; | 82 *cosValue = 0; |
149 } | 83 } |
150 } | 84 } |
151 | 85 |
152 if (SkScalarNearlyZero(sinValue)) { | 86 if (SkScalarNearlyZero(sinValue)) { |
153 sinValue = 0; | 87 sinValue = 0; |
154 } | 88 } |
155 return sinValue; | 89 return sinValue; |
156 } | 90 } |
OLD | NEW |