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__) && !defined(__thumb__) && !defined(__ARM_ARCH_4__)
SkMulS16(S16CPU x,S16CPU y)166 static inline int32_t SkMulS16(S16CPU x, S16CPU y) {
167 SkASSERT((int16_t)x == x);
168 SkASSERT((int16_t)y == y);
169 int32_t product;
170 asm("smulbb %0, %1, %2 \n"
171 : "=r"(product)
172 : "r"(x), "r"(y)
173 :
174 );
175 return product;
176 }
177 #else
178 #ifdef SK_DEBUG
SkMulS16(S16CPU x,S16CPU y)179 static inline int32_t SkMulS16(S16CPU x, S16CPU y) {
180 SkASSERT((int16_t)x == x);
181 SkASSERT((int16_t)y == y);
182 return x * y;
183 }
184 #else
185 #define SkMulS16(x, y) ((x) * (y))
186 #endif
187 #endif
188
189 /** Return a*b/255, truncating away any fractional bits. Only valid if both
190 a and b are 0..255
191 */
SkMulDiv255Trunc(U8CPU a,U8CPU b)192 static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
193 SkASSERT((uint8_t)a == a);
194 SkASSERT((uint8_t)b == b);
195 unsigned prod = SkMulS16(a, b) + 1;
196 return (prod + (prod >> 8)) >> 8;
197 }
198
199 /** Return a*b/255, rounding any fractional bits. Only valid if both
200 a and b are 0..255
201 */
SkMulDiv255Round(U8CPU a,U8CPU b)202 static inline U8CPU SkMulDiv255Round(U8CPU a, U8CPU b) {
203 SkASSERT((uint8_t)a == a);
204 SkASSERT((uint8_t)b == b);
205 unsigned prod = SkMulS16(a, b) + 128;
206 return (prod + (prod >> 8)) >> 8;
207 }
208
209 /** Return a*b/((1 << shift) - 1), rounding any fractional bits.
210 Only valid if a and b are unsigned and <= 32767 and shift is > 0 and <= 8
211 */
SkMul16ShiftRound(unsigned a,unsigned b,int shift)212 static inline unsigned SkMul16ShiftRound(unsigned a, unsigned b, int shift) {
213 SkASSERT(a <= 32767);
214 SkASSERT(b <= 32767);
215 SkASSERT(shift > 0 && shift <= 8);
216 unsigned prod = SkMulS16(a, b) + (1 << (shift - 1));
217 return (prod + (prod >> shift)) >> shift;
218 }
219
220 #endif
221
222