• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SkMath_DEFINED
18 #define SkMath_DEFINED
19 
20 #include "SkTypes.h"
21 
22 //! Returns the number of leading zero bits (0...32)
23 int SkCLZ_portable(uint32_t);
24 
25 /** Computes the 64bit product of a * b, and then shifts the answer down by
26     shift bits, returning the low 32bits. shift must be [0..63]
27     e.g. to perform a fixedmul, call SkMulShift(a, b, 16)
28 */
29 int32_t SkMulShift(int32_t a, int32_t b, unsigned shift);
30 
31 /** Computes numer1 * numer2 / denom in full 64 intermediate precision.
32     It is an error for denom to be 0. There is no special handling if
33     the result overflows 32bits.
34 */
35 int32_t SkMulDiv(int32_t numer1, int32_t numer2, int32_t denom);
36 
37 /** Computes (numer1 << shift) / denom in full 64 intermediate precision.
38     It is an error for denom to be 0. There is no special handling if
39     the result overflows 32bits.
40 */
41 int32_t SkDivBits(int32_t numer, int32_t denom, int shift);
42 
43 /** Return the integer square root of value, with a bias of bitBias
44 */
45 int32_t SkSqrtBits(int32_t value, int bitBias);
46 
47 /** Return the integer square root of n, treated as a SkFixed (16.16)
48 */
49 #define SkSqrt32(n)         SkSqrtBits(n, 15)
50 
51 /** Return the integer cube root of value, with a bias of bitBias
52  */
53 int32_t SkCubeRootBits(int32_t value, int bitBias);
54 
55 /** Returns -1 if n < 0, else returns 0
56 */
57 #define SkExtractSign(n)    ((int32_t)(n) >> 31)
58 
59 /** If sign == -1, returns -n, else sign must be 0, and returns n.
60     Typically used in conjunction with SkExtractSign().
61 */
SkApplySign(int32_t n,int32_t sign)62 static inline int32_t SkApplySign(int32_t n, int32_t sign) {
63     SkASSERT(sign == 0 || sign == -1);
64     return (n ^ sign) - sign;
65 }
66 
67 /** Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches)
68 */
SkClampPos(int value)69 static inline int SkClampPos(int value) {
70     return value & ~(value >> 31);
71 }
72 
73 /** Given an integer and a positive (max) integer, return the value
74     pinned against 0 and max, inclusive.
75     Note: only works as long as max - value doesn't wrap around
76     @param value    The value we want returned pinned between [0...max]
77     @param max      The positive max value
78     @return 0 if value < 0, max if value > max, else value
79 */
SkClampMax(int value,int max)80 static inline int SkClampMax(int value, int max) {
81     // ensure that max is positive
82     SkASSERT(max >= 0);
83     // ensure that if value is negative, max - value doesn't wrap around
84     SkASSERT(value >= 0 || max - value > 0);
85 
86 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
87     if (value < 0) {
88         value = 0;
89     }
90     if (value > max) {
91         value = max;
92     }
93     return value;
94 #else
95 
96     int diff = max - value;
97     // clear diff if diff is positive
98     diff &= diff >> 31;
99 
100     // clear the result if value < 0
101     return (value + diff) & ~(value >> 31);
102 #endif
103 }
104 
105 /** Given a positive value and a positive max, return the value
106     pinned against max.
107     Note: only works as long as max - value doesn't wrap around
108     @return max if value >= max, else value
109 */
SkClampUMax(unsigned value,unsigned max)110 static inline unsigned SkClampUMax(unsigned value, unsigned max) {
111 #ifdef SK_CPU_HAS_CONDITIONAL_INSTR
112     if (value > max) {
113         value = max;
114     }
115     return value;
116 #else
117     int diff = max - value;
118     // clear diff if diff is positive
119     diff &= diff >> 31;
120 
121     return value + diff;
122 #endif
123 }
124 
125 ///////////////////////////////////////////////////////////////////////////////
126 
127 #if defined(__arm__) && !defined(__thumb__)
128     #define SkCLZ(x)    __builtin_clz(x)
129 #endif
130 
131 #ifndef SkCLZ
132     #define SkCLZ(x)    SkCLZ_portable(x)
133 #endif
134 
135 ///////////////////////////////////////////////////////////////////////////////
136 
137 /** Returns the smallest power-of-2 that is >= the specified value. If value
138     is already a power of 2, then it is returned unchanged. It is undefined
139     if value is <= 0.
140 */
SkNextPow2(int value)141 static inline int SkNextPow2(int value) {
142     SkASSERT(value > 0);
143     return 1 << (32 - SkCLZ(value - 1));
144 }
145 
146 /** Returns the log2 of the specified value, were that value to be rounded up
147     to the next power of 2. It is undefined to pass 0. Examples:
148          SkNextLog2(1) -> 0
149          SkNextLog2(2) -> 1
150          SkNextLog2(3) -> 2
151          SkNextLog2(4) -> 2
152          SkNextLog2(5) -> 3
153 */
SkNextLog2(uint32_t value)154 static inline int SkNextLog2(uint32_t value) {
155     SkASSERT(value != 0);
156     return 32 - SkCLZ(value - 1);
157 }
158 
159 ///////////////////////////////////////////////////////////////////////////////
160 
161 /** SkMulS16(a, b) multiplies a * b, but requires that a and b are both int16_t.
162     With this requirement, we can generate faster instructions on some
163     architectures.
164 */
165 #if defined(__arm__) \
166   && !defined(__thumb__) \
167   && !defined(__ARM_ARCH_4__) \
168   && !defined(__ARM_ARCH_5T__)
SkMulS16(S16CPU x,S16CPU y)169     static inline int32_t SkMulS16(S16CPU x, S16CPU y) {
170         SkASSERT((int16_t)x == x);
171         SkASSERT((int16_t)y == y);
172         int32_t product;
173         asm("smulbb %0, %1, %2 \n"
174             : "=r"(product)
175             : "r"(x), "r"(y)
176             );
177         return product;
178     }
179 #else
180     #ifdef SK_DEBUG
SkMulS16(S16CPU x,S16CPU y)181         static inline int32_t SkMulS16(S16CPU x, S16CPU y) {
182             SkASSERT((int16_t)x == x);
183             SkASSERT((int16_t)y == y);
184             return x * y;
185         }
186     #else
187         #define SkMulS16(x, y)  ((x) * (y))
188     #endif
189 #endif
190 
191 /** Return a*b/255, truncating away any fractional bits. Only valid if both
192     a and b are 0..255
193 */
SkMulDiv255Trunc(U8CPU a,U8CPU b)194 static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
195     SkASSERT((uint8_t)a == a);
196     SkASSERT((uint8_t)b == b);
197     unsigned prod = SkMulS16(a, b) + 1;
198     return (prod + (prod >> 8)) >> 8;
199 }
200 
201 /** Return a*b/255, rounding any fractional bits. Only valid if both
202     a and b are 0..255
203  */
SkMulDiv255Round(U8CPU a,U8CPU b)204 static inline U8CPU SkMulDiv255Round(U8CPU a, U8CPU b) {
205     SkASSERT((uint8_t)a == a);
206     SkASSERT((uint8_t)b == b);
207     unsigned prod = SkMulS16(a, b) + 128;
208     return (prod + (prod >> 8)) >> 8;
209 }
210 
211 /** Return a*b/((1 << shift) - 1), rounding any fractional bits.
212     Only valid if a and b are unsigned and <= 32767 and shift is > 0 and <= 8
213 */
SkMul16ShiftRound(unsigned a,unsigned b,int shift)214 static inline unsigned SkMul16ShiftRound(unsigned a, unsigned b, int shift) {
215     SkASSERT(a <= 32767);
216     SkASSERT(b <= 32767);
217     SkASSERT(shift > 0 && shift <= 8);
218     unsigned prod = SkMulS16(a, b) + (1 << (shift - 1));
219     return (prod + (prod >> shift)) >> shift;
220 }
221 
222 /** Just the rounding step in SkDiv255Round: round(value / 255)
223  */
SkDiv255Round(unsigned prod)224 static inline unsigned SkDiv255Round(unsigned prod) {
225     prod += 128;
226     return (prod + (prod >> 8)) >> 8;
227 }
228 
229 #endif
230 
231