1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkMathPriv_DEFINED
9#define SkMathPriv_DEFINED
10
11#include "include/core/SkMath.h"
12
13/**
14 * Return the integer square root of value, with a bias of bitBias
15 */
16int32_t SkSqrtBits(int32_t value, int bitBias);
17
18/** Return the integer square root of n, treated as a SkFixed (16.16)
19 */
20static inline int32_t SkSqrt32(int32_t n) { return SkSqrtBits(n, 15); }
21
22/**
23 * Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches)
24 */
25static inline int SkClampPos(int value) {
26 return value & ~(value >> 31);
27}
28
29/**
30 * Stores numer/denom and numer%denom into div and mod respectively.
31 */
32template <typename In, typename Out>
33inline void SkTDivMod(In numer, In denom, Out* div, Out* mod) {
34#ifdef SK_CPU_ARM32
35 // If we wrote this as in the else branch, GCC won't fuse the two into one
36 // divmod call, but rather a div call followed by a divmod. Silly! This
37 // version is just as fast as calling __aeabi_[u]idivmod manually, but with
38 // prettier code.
39 //
40 // This benches as around 2x faster than the code in the else branch.
41 const In d = numer/denom;
42 *div = static_cast<Out>(d);
43 *mod = static_cast<Out>(numer-d*denom);
44#else
45 // On x86 this will just be a single idiv.
46 *div = static_cast<Out>(numer/denom);
47 *mod = static_cast<Out>(numer%denom);
48#endif
49}
50
51/** Returns -1 if n < 0, else returns 0
52 */
53#define SkExtractSign(n) ((int32_t)(n) >> 31)
54
55/** If sign == -1, returns -n, else sign must be 0, and returns n.
56 Typically used in conjunction with SkExtractSign().
57 */
58static inline int32_t SkApplySign(int32_t n, int32_t sign) {
59 SkASSERT(sign == 0 || sign == -1);
60 return (n ^ sign) - sign;
61}
62
63/** Return x with the sign of y */
64static inline int32_t SkCopySign32(int32_t x, int32_t y) {
65 return SkApplySign(x, SkExtractSign(x ^ y));
66}
67
68/** Given a positive value and a positive max, return the value
69 pinned against max.
70 Note: only works as long as max - value doesn't wrap around
71 @return max if value >= max, else value
72 */
73static inline unsigned SkClampUMax(unsigned value, unsigned max) {
74 if (value > max) {
75 value = max;
76 }
77 return value;
78}
79
80// If a signed int holds min_int (e.g. 0x80000000) it is undefined what happens when
81// we negate it (even though we *know* we're 2's complement and we'll get the same
82// value back). So we create this helper function that casts to size_t (unsigned) first,
83// to avoid the complaint.
84static inline size_t sk_negate_to_size_t(int32_t value) {
85#if defined(_MSC_VER)
86#pragma warning(push)
87#pragma warning(disable : 4146) // Thanks MSVC, we know what we're negating an unsigned
88#endif
89 return -static_cast<size_t>(value);
90#if defined(_MSC_VER)
91#pragma warning(pop)
92#endif
93}
94
95///////////////////////////////////////////////////////////////////////////////
96
97/** Return a*b/255, truncating away any fractional bits. Only valid if both
98 a and b are 0..255
99 */
100static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
101 SkASSERT((uint8_t)a == a);
102 SkASSERT((uint8_t)b == b);
103 unsigned prod = a*b + 1;
104 return (prod + (prod >> 8)) >> 8;
105}
106
107/** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if
108 both a and b are 0..255. The expected result equals (a * b + 254) / 255.
109 */
110static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) {
111 SkASSERT((uint8_t)a == a);
112 SkASSERT((uint8_t)b == b);
113 unsigned prod = a*b + 255;
114 return (prod + (prod >> 8)) >> 8;
115}
116
117/** Just the rounding step in SkDiv255Round: round(value / 255)
118 */
119static inline unsigned SkDiv255Round(unsigned prod) {
120 prod += 128;
121 return (prod + (prod >> 8)) >> 8;
122}
123
124/**
125 * Swap byte order of a 4-byte value, e.g. 0xaarrggbb -> 0xbbggrraa.
126 */
127#if defined(_MSC_VER)
128 #include <stdlib.h>
129 static inline uint32_t SkBSwap32(uint32_t v) { return _byteswap_ulong(v); }
130#else
131 static inline uint32_t SkBSwap32(uint32_t v) { return __builtin_bswap32(v); }
132#endif
133
134//! Returns the number of leading zero bits (0...32)
135int SkCLZ_portable(uint32_t);
136
137#ifndef SkCLZ
138 #if defined(SK_BUILD_FOR_WIN)
139 #include <intrin.h>
140
141 static inline int SkCLZ(uint32_t mask) {
142 if (mask) {
143 unsigned long index;
144 _BitScanReverse(&index, mask);
145 // Suppress this bogus /analyze warning. The check for non-zero
146 // guarantees that _BitScanReverse will succeed.
147 #pragma warning(suppress : 6102) // Using 'index' from failed function call
148 return index ^ 0x1F;
149 } else {
150 return 32;
151 }
152 }
153 #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
154 static inline int SkCLZ(uint32_t mask) {
155 // __builtin_clz(0) is undefined, so we have to detect that case.
156 return mask ? __builtin_clz(mask) : 32;
157 }
158 #else
159 static inline int SkCLZ(uint32_t mask) {
160 return SkCLZ_portable(mask);
161 }
162 #endif
163#endif
164
165//! Returns the number of trailing zero bits (0...32)
166int SkCTZ_portable(uint32_t);
167
168#ifndef SkCTZ
169 #if defined(SK_BUILD_FOR_WIN)
170 #include <intrin.h>
171
172 static inline int SkCTZ(uint32_t mask) {
173 if (mask) {
174 unsigned long index;
175 _BitScanForward(&index, mask);
176 // Suppress this bogus /analyze warning. The check for non-zero
177 // guarantees that _BitScanReverse will succeed.
178 #pragma warning(suppress : 6102) // Using 'index' from failed function call
179 return index;
180 } else {
181 return 32;
182 }
183 }
184#elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
185 static inline int SkCTZ(uint32_t mask) {
186 // __builtin_ctz(0) is undefined, so we have to detect that case.
187 return mask ? __builtin_ctz(mask) : 32;
188 }
189#else
190 static inline int SkCTZ(uint32_t mask) {
191 return SkCTZ_portable(mask);
192 }
193#endif
194#endif
195
196/**
197 * Returns the smallest power-of-2 that is >= the specified value. If value
198 * is already a power of 2, then it is returned unchanged. It is undefined
199 * if value is <= 0.
200 */
201static inline int SkNextPow2(int value) {
202 SkASSERT(value > 0);
203 return 1 << (32 - SkCLZ(value - 1));
204}
205
206/**
207* Returns the largest power-of-2 that is <= the specified value. If value
208* is already a power of 2, then it is returned unchanged. It is undefined
209* if value is <= 0.
210*/
211static inline int SkPrevPow2(int value) {
212 SkASSERT(value > 0);
213 return 1 << (32 - SkCLZ(value >> 1));
214}
215
216/**
217 * Returns the log2 of the specified value, were that value to be rounded up
218 * to the next power of 2. It is undefined to pass 0. Examples:
219 * SkNextLog2(1) -> 0
220 * SkNextLog2(2) -> 1
221 * SkNextLog2(3) -> 2
222 * SkNextLog2(4) -> 2
223 * SkNextLog2(5) -> 3
224 */
225static inline int SkNextLog2(uint32_t value) {
226 SkASSERT(value != 0);
227 return 32 - SkCLZ(value - 1);
228}
229
230/**
231* Returns the log2 of the specified value, were that value to be rounded down
232* to the previous power of 2. It is undefined to pass 0. Examples:
233* SkPrevLog2(1) -> 0
234* SkPrevLog2(2) -> 1
235* SkPrevLog2(3) -> 1
236* SkPrevLog2(4) -> 2
237* SkPrevLog2(5) -> 2
238*/
239static inline int SkPrevLog2(uint32_t value) {
240 SkASSERT(value != 0);
241 return 32 - SkCLZ(value >> 1);
242}
243
244///////////////////////////////////////////////////////////////////////////////
245
246/**
247 * Return the smallest power-of-2 >= n.
248 */
249static inline uint32_t GrNextPow2(uint32_t n) {
250 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
251}
252
253/**
254 * Returns the next power of 2 >= n or n if the next power of 2 can't be represented by size_t.
255 */
256static inline size_t GrNextSizePow2(size_t n) {
257 constexpr int kNumSizeTBits = 8 * sizeof(size_t);
258 constexpr size_t kHighBitSet = size_t(1) << (kNumSizeTBits - 1);
259
260 if (!n) {
261 return 1;
262 } else if (n >= kHighBitSet) {
263 return n;
264 }
265
266 n--;
267 uint32_t shift = 1;
268 while (shift < kNumSizeTBits) {
269 n |= n >> shift;
270 shift <<= 1;
271 }
272 return n + 1;
273}
274
275// conservative check. will return false for very large values that "could" fit
276template <typename T> static inline bool SkFitsInFixed(T x) {
277 return SkTAbs(x) <= 32767.0f;
278}
279
280#endif
281