blob: af1908374c73061f066b966bfb7f569205bbfda3 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
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)
23int 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*/
29int32_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*/
35int32_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*/
41int32_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*/
45int32_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 */
53int32_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*/
62static inline int32_t SkApplySign(int32_t n, int32_t sign) {
63 SkASSERT(sign == 0 || sign == -1);
64 return (n ^ sign) - sign;
65}
66
reed@android.comeebf5cb2010-02-09 18:30:59 +000067/** Return x with the sign of y */
68static inline int32_t SkCopySign32(int32_t x, int32_t y) {
69 return SkApplySign(x, SkExtractSign(x ^ y));
70}
71
reed@android.com8a1c16f2008-12-17 15:59:43 +000072/** Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches)
73*/
74static inline int SkClampPos(int value) {
75 return value & ~(value >> 31);
76}
77
78/** Given an integer and a positive (max) integer, return the value
79 pinned against 0 and max, inclusive.
80 Note: only works as long as max - value doesn't wrap around
81 @param value The value we want returned pinned between [0...max]
82 @param max The positive max value
83 @return 0 if value < 0, max if value > max, else value
84*/
85static inline int SkClampMax(int value, int max) {
86 // ensure that max is positive
87 SkASSERT(max >= 0);
88 // ensure that if value is negative, max - value doesn't wrap around
89 SkASSERT(value >= 0 || max - value > 0);
90
91#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
92 if (value < 0) {
93 value = 0;
94 }
95 if (value > max) {
96 value = max;
97 }
98 return value;
99#else
100
101 int diff = max - value;
102 // clear diff if diff is positive
103 diff &= diff >> 31;
104
105 // clear the result if value < 0
106 return (value + diff) & ~(value >> 31);
107#endif
108}
109
110/** Given a positive value and a positive max, return the value
111 pinned against max.
112 Note: only works as long as max - value doesn't wrap around
113 @return max if value >= max, else value
114*/
115static inline unsigned SkClampUMax(unsigned value, unsigned max) {
116#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
117 if (value > max) {
118 value = max;
119 }
120 return value;
121#else
122 int diff = max - value;
123 // clear diff if diff is positive
124 diff &= diff >> 31;
125
126 return value + diff;
127#endif
128}
129
130///////////////////////////////////////////////////////////////////////////////
131
132#if defined(__arm__) && !defined(__thumb__)
133 #define SkCLZ(x) __builtin_clz(x)
134#endif
135
136#ifndef SkCLZ
137 #define SkCLZ(x) SkCLZ_portable(x)
138#endif
139
140///////////////////////////////////////////////////////////////////////////////
141
142/** Returns the smallest power-of-2 that is >= the specified value. If value
143 is already a power of 2, then it is returned unchanged. It is undefined
144 if value is <= 0.
145*/
146static inline int SkNextPow2(int value) {
147 SkASSERT(value > 0);
148 return 1 << (32 - SkCLZ(value - 1));
149}
150
151/** Returns the log2 of the specified value, were that value to be rounded up
152 to the next power of 2. It is undefined to pass 0. Examples:
153 SkNextLog2(1) -> 0
154 SkNextLog2(2) -> 1
155 SkNextLog2(3) -> 2
156 SkNextLog2(4) -> 2
157 SkNextLog2(5) -> 3
158*/
159static inline int SkNextLog2(uint32_t value) {
160 SkASSERT(value != 0);
161 return 32 - SkCLZ(value - 1);
162}
163
reed@android.comf2b98d62010-12-20 18:26:13 +0000164/** Returns true if value is a power of 2. Does not explicitly check for
165 value <= 0.
166 */
167static inline bool SkIsPow2(int value) {
168 return (value & (value - 1)) == 0;
169}
170
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171///////////////////////////////////////////////////////////////////////////////
172
173/** SkMulS16(a, b) multiplies a * b, but requires that a and b are both int16_t.
174 With this requirement, we can generate faster instructions on some
175 architectures.
176*/
deanm@chromium.orgd5ed3952009-08-21 17:17:35 +0000177#if defined(__arm__) \
178 && !defined(__thumb__) \
reed@android.com69975b02010-01-04 19:23:32 +0000179 && !defined(__ARM_ARCH_4T__) \
deanm@chromium.orgd5ed3952009-08-21 17:17:35 +0000180 && !defined(__ARM_ARCH_5T__)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 static inline int32_t SkMulS16(S16CPU x, S16CPU y) {
182 SkASSERT((int16_t)x == x);
183 SkASSERT((int16_t)y == y);
184 int32_t product;
185 asm("smulbb %0, %1, %2 \n"
186 : "=r"(product)
187 : "r"(x), "r"(y)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 );
189 return product;
190 }
191#else
192 #ifdef SK_DEBUG
193 static inline int32_t SkMulS16(S16CPU x, S16CPU y) {
194 SkASSERT((int16_t)x == x);
195 SkASSERT((int16_t)y == y);
196 return x * y;
197 }
198 #else
199 #define SkMulS16(x, y) ((x) * (y))
200 #endif
201#endif
202
203/** Return a*b/255, truncating away any fractional bits. Only valid if both
204 a and b are 0..255
205*/
206static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
207 SkASSERT((uint8_t)a == a);
208 SkASSERT((uint8_t)b == b);
209 unsigned prod = SkMulS16(a, b) + 1;
210 return (prod + (prod >> 8)) >> 8;
211}
212
213/** Return a*b/255, rounding any fractional bits. Only valid if both
214 a and b are 0..255
215 */
216static inline U8CPU SkMulDiv255Round(U8CPU a, U8CPU b) {
217 SkASSERT((uint8_t)a == a);
218 SkASSERT((uint8_t)b == b);
219 unsigned prod = SkMulS16(a, b) + 128;
220 return (prod + (prod >> 8)) >> 8;
221}
222
senorblanco@chromium.orgec7a30c2010-12-07 21:07:56 +0000223/** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if
224 both a and b are 0..255. The expected result equals (a * b + 254) / 255.
225 */
226static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) {
227 SkASSERT((uint8_t)a == a);
228 SkASSERT((uint8_t)b == b);
229 unsigned prod = SkMulS16(a, b) + 255;
230 return (prod + (prod >> 8)) >> 8;
231}
232
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233/** Return a*b/((1 << shift) - 1), rounding any fractional bits.
234 Only valid if a and b are unsigned and <= 32767 and shift is > 0 and <= 8
235*/
236static inline unsigned SkMul16ShiftRound(unsigned a, unsigned b, int shift) {
237 SkASSERT(a <= 32767);
238 SkASSERT(b <= 32767);
239 SkASSERT(shift > 0 && shift <= 8);
240 unsigned prod = SkMulS16(a, b) + (1 << (shift - 1));
241 return (prod + (prod >> shift)) >> shift;
242}
243
reed@android.coma0f5d152009-06-22 17:38:10 +0000244/** Just the rounding step in SkDiv255Round: round(value / 255)
245 */
246static inline unsigned SkDiv255Round(unsigned prod) {
247 prod += 128;
248 return (prod + (prod >> 8)) >> 8;
249}
250
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251#endif
252