1 #ifndef _DEINT32_H
2 #define _DEINT32_H
3 /*-------------------------------------------------------------------------
4 * drawElements Base Portability Library
5 * -------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief 32-bit integer math.
24 *//*--------------------------------------------------------------------*/
25
26 #include "deDefs.h"
27
28 #if (DE_COMPILER == DE_COMPILER_MSC)
29 # include <intrin.h>
30 #endif
31
32 DE_BEGIN_EXTERN_C
33
34 enum
35 {
36 DE_RCP_FRAC_BITS = 30 /*!< Number of fractional bits in deRcp32() result. */
37 };
38
39 void deRcp32 (deUint32 a, deUint32* rcp, int* exp);
40 void deInt32_computeLUTs (void);
41 void deInt32_selfTest (void);
42
43 /*--------------------------------------------------------------------*//*!
44 * \brief Compute the absolute of an int.
45 * \param a Input value.
46 * \return Absolute of the input value.
47 *
48 * \note The input 0x80000000u (for which the abs value cannot be
49 * represented), is asserted and returns the value itself.
50 *//*--------------------------------------------------------------------*/
deAbs32(int a)51 DE_INLINE int deAbs32 (int a)
52 {
53 DE_ASSERT((unsigned int) a != 0x80000000u);
54 return (a < 0) ? -a : a;
55 }
56
57 /*--------------------------------------------------------------------*//*!
58 * \brief Compute the signed minimum of two values.
59 * \param a First input value.
60 * \param b Second input value.
61 * \return The smallest of the two input values.
62 *//*--------------------------------------------------------------------*/
deMin32(int a,int b)63 DE_INLINE int deMin32 (int a, int b)
64 {
65 return (a <= b) ? a : b;
66 }
67
68 /*--------------------------------------------------------------------*//*!
69 * \brief Compute the signed maximum of two values.
70 * \param a First input value.
71 * \param b Second input value.
72 * \return The largest of the two input values.
73 *//*--------------------------------------------------------------------*/
deMax32(int a,int b)74 DE_INLINE int deMax32 (int a, int b)
75 {
76 return (a >= b) ? a : b;
77 }
78
79 /*--------------------------------------------------------------------*//*!
80 * \brief Compute the unsigned minimum of two values.
81 * \param a First input value.
82 * \param b Second input value.
83 * \return The smallest of the two input values.
84 *//*--------------------------------------------------------------------*/
deMinu32(deUint32 a,deUint32 b)85 DE_INLINE deUint32 deMinu32 (deUint32 a, deUint32 b)
86 {
87 return (a <= b) ? a : b;
88 }
89
90 /*--------------------------------------------------------------------*//*!
91 * \brief Compute the unsigned maximum of two values.
92 * \param a First input value.
93 * \param b Second input value.
94 * \return The largest of the two input values.
95 *//*--------------------------------------------------------------------*/
deMaxu32(deUint32 a,deUint32 b)96 DE_INLINE deUint32 deMaxu32 (deUint32 a, deUint32 b)
97 {
98 return (a >= b) ? a : b;
99 }
100
101 /*--------------------------------------------------------------------*//*!
102 * \brief Check if a value is in the <b>inclusive<b> range [mn, mx].
103 * \param a Value to check for range.
104 * \param mn Range minimum value.
105 * \param mx Range maximum value.
106 * \return True if (a >= mn) and (a <= mx), false otherwise.
107 *
108 * \see deInBounds32()
109 *//*--------------------------------------------------------------------*/
deInRange32(int a,int mn,int mx)110 DE_INLINE deBool deInRange32 (int a, int mn, int mx)
111 {
112 return (a >= mn) && (a <= mx);
113 }
114
115 /*--------------------------------------------------------------------*//*!
116 * \brief Check if a value is in the half-inclusive bounds [mn, mx[.
117 * \param a Value to check for range.
118 * \param mn Range minimum value.
119 * \param mx Range maximum value.
120 * \return True if (a >= mn) and (a < mx), false otherwise.
121 *
122 * \see deInRange32()
123 *//*--------------------------------------------------------------------*/
deInBounds32(int a,int mn,int mx)124 DE_INLINE deBool deInBounds32 (int a, int mn, int mx)
125 {
126 return (a >= mn) && (a < mx);
127 }
128
129 /*--------------------------------------------------------------------*//*!
130 * \brief Clamp a value into the range [mn, mx].
131 * \param a Value to clamp.
132 * \param mn Minimum value.
133 * \param mx Maximum value.
134 * \return The clamped value in [mn, mx] range.
135 *//*--------------------------------------------------------------------*/
deClamp32(int a,int mn,int mx)136 DE_INLINE int deClamp32 (int a, int mn, int mx)
137 {
138 DE_ASSERT(mn <= mx);
139 if (a < mn) return mn;
140 if (a > mx) return mx;
141 return a;
142 }
143
144 /*--------------------------------------------------------------------*//*!
145 * \brief Get the sign of an integer.
146 * \param a Input value.
147 * \return +1 if a>0, 0 if a==0, -1 if a<0.
148 *//*--------------------------------------------------------------------*/
deSign32(int a)149 DE_INLINE int deSign32 (int a)
150 {
151 if (a > 0) return +1;
152 if (a < 0) return -1;
153 return 0;
154 }
155
156 /*--------------------------------------------------------------------*//*!
157 * \brief Extract the sign bit of a.
158 * \param a Input value.
159 * \return 0x80000000 if a<0, 0 otherwise.
160 *//*--------------------------------------------------------------------*/
deSignBit32(int a)161 DE_INLINE int deSignBit32 (int a)
162 {
163 return (a & 0x80000000);
164 }
165
166 /*--------------------------------------------------------------------*//*!
167 * \brief Integer rotate right.
168 * \param val Value to rotate.
169 * \param r Number of bits to rotate (in range [0, 32]).
170 * \return The rotated value.
171 *//*--------------------------------------------------------------------*/
deRor32(int val,int r)172 DE_INLINE int deRor32 (int val, int r)
173 {
174 DE_ASSERT(r >= 0 && r <= 32);
175 if (r == 0 || r == 32)
176 return val;
177 else
178 return (int)(((deUint32)val >> r) | ((deUint32)val << (32-r)));
179 }
180
181 /*--------------------------------------------------------------------*//*!
182 * \brief Integer rotate left.
183 * \param val Value to rotate.
184 * \param r Number of bits to rotate (in range [0, 32]).
185 * \return The rotated value.
186 *//*--------------------------------------------------------------------*/
deRol32(int val,int r)187 DE_INLINE int deRol32 (int val, int r)
188 {
189 DE_ASSERT(r >= 0 && r <= 32);
190 if (r == 0 || r == 32)
191 return val;
192 else
193 return (int)(((deUint32)val << r) | ((deUint32)val >> (32-r)));
194 }
195
196 /*--------------------------------------------------------------------*//*!
197 * \brief Check if a value is a power-of-two.
198 * \param a Input value.
199 * \return True if input is a power-of-two value, false otherwise.
200 *
201 * \note Also returns true for zero.
202 *//*--------------------------------------------------------------------*/
deIsPowerOfTwo32(int a)203 DE_INLINE deBool deIsPowerOfTwo32 (int a)
204 {
205 return ((a & (a - 1)) == 0);
206 }
207
208 /*--------------------------------------------------------------------*//*!
209 * \brief Check if an integer is aligned to given power-of-two size.
210 * \param a Input value.
211 * \param align Alignment to check for.
212 * \return True if input is aligned, false otherwise.
213 *//*--------------------------------------------------------------------*/
deIsAligned32(int a,int align)214 DE_INLINE deBool deIsAligned32 (int a, int align)
215 {
216 DE_ASSERT(deIsPowerOfTwo32(align));
217 return ((a & (align-1)) == 0);
218 }
219
220 /*--------------------------------------------------------------------*//*!
221 * \brief Check if a pointer is aligned to given power-of-two size.
222 * \param ptr Input pointer.
223 * \param align Alignment to check for (power-of-two).
224 * \return True if input is aligned, false otherwise.
225 *//*--------------------------------------------------------------------*/
deIsAlignedPtr(const void * ptr,deUintptr align)226 DE_INLINE deBool deIsAlignedPtr (const void* ptr, deUintptr align)
227 {
228 DE_ASSERT((align & (align-1)) == 0); /* power of two */
229 return (((deUintptr)ptr & (align-1)) == 0);
230 }
231
232 /*--------------------------------------------------------------------*//*!
233 * \brief Align an integer to given power-of-two size.
234 * \param val Input to align.
235 * \param align Alignment to check for (power-of-two).
236 * \return The aligned value (larger or equal to input).
237 *//*--------------------------------------------------------------------*/
deAlign32(deInt32 val,deInt32 align)238 DE_INLINE deInt32 deAlign32 (deInt32 val, deInt32 align)
239 {
240 DE_ASSERT(deIsPowerOfTwo32(align));
241 return (val + align - 1) & ~(align - 1);
242 }
243
244 /*--------------------------------------------------------------------*//*!
245 * \brief Align a pointer to given power-of-two size.
246 * \param ptr Input pointer to align.
247 * \param align Alignment to check for (power-of-two).
248 * \return The aligned pointer (larger or equal to input).
249 *//*--------------------------------------------------------------------*/
deAlignPtr(void * ptr,deUintptr align)250 DE_INLINE void* deAlignPtr (void* ptr, deUintptr align)
251 {
252 deUintptr val = (deUintptr)ptr;
253 DE_ASSERT((align & (align-1)) == 0); /* power of two */
254 return (void*)((val + align - 1) & ~(align - 1));
255 }
256
257 /*--------------------------------------------------------------------*//*!
258 * \brief Compute number of leading zeros in an integer.
259 * \param a Input value.
260 * \return The number of leading zero bits in the input.
261 *//*--------------------------------------------------------------------*/
262 extern const deInt8 g_clzLUT[256];
263
deClz32(deUint32 a)264 DE_INLINE int deClz32 (deUint32 a)
265 {
266 #if (DE_COMPILER == DE_COMPILER_MSC)
267 unsigned long i;
268 if (_BitScanReverse(&i, (unsigned long)a) == 0)
269 return 32;
270 else
271 return 31-i;
272 #elif (DE_COMPILER == DE_COMPILER_GCC == DE_COMPILER == DE_COMPILER_CLANG)
273 if (a == 0)
274 return 32;
275 else
276 return __builtin_clz((unsigned int)a);
277 #else
278 if ((a & 0xFF000000u) != 0)
279 return (int)g_clzLUT[a >> 24];
280 if ((a & 0x00FF0000u) != 0)
281 return 8 + (int)g_clzLUT[a >> 16];
282 if ((a & 0x0000FF00u) != 0)
283 return 16 + (int)g_clzLUT[a >> 8];
284 return 24 + (int)g_clzLUT[a];
285 #endif
286 }
287
288 /*--------------------------------------------------------------------*//*!
289 * \brief Compute integer 'floor' of 'log2' for a positive integer.
290 * \param a Input value.
291 * \return floor(log2(a)).
292 *//*--------------------------------------------------------------------*/
deLog2Floor32(deInt32 a)293 DE_INLINE int deLog2Floor32 (deInt32 a)
294 {
295 DE_ASSERT(a > 0);
296 return 31 - deClz32(a);
297 }
298
299 /*--------------------------------------------------------------------*//*!
300 * \brief Compute integer 'ceil' of 'log2' for a positive integer.
301 * \param a Input value.
302 * \return ceil(log2(a)).
303 *//*--------------------------------------------------------------------*/
deLog2Ceil32(deInt32 a)304 DE_INLINE int deLog2Ceil32 (deInt32 a)
305 {
306 int log2floor = deLog2Floor32(a);
307 if (deIsPowerOfTwo32(a))
308 return log2floor;
309 else
310 return log2floor+1;
311 }
312
313 /* \todo [2012-04-28 pyry] Badly named, deprecated variant of deLog2Ceil32(). Remove once code has been fixed. */
deLog2Clz(deInt32 a)314 DE_INLINE deUint32 deLog2Clz(deInt32 a)
315 {
316 return (deUint32)deLog2Ceil32(a);
317 }
318
319 /*--------------------------------------------------------------------*//*!
320 * \brief Compute the bit population count of an integer.
321 * \param a Input value.
322 * \return The number of one bits in the input.
323 *//*--------------------------------------------------------------------*/
dePop32(int a)324 DE_INLINE int dePop32 (int a)
325 {
326 deUint32 mask0 = 0x55555555; /* 1-bit values. */
327 deUint32 mask1 = 0x33333333; /* 2-bit values. */
328 deUint32 mask2 = 0x0f0f0f0f; /* 4-bit values. */
329 deUint32 mask3 = 0x00ff00ff; /* 8-bit values. */
330 deUint32 mask4 = 0x0000ffff; /* 16-bit values. */
331 deUint32 t = (deUint32)a;
332 t = (t & mask0) + ((t>>1) & mask0);
333 t = (t & mask1) + ((t>>2) & mask1);
334 t = (t & mask2) + ((t>>4) & mask2);
335 t = (t & mask3) + ((t>>8) & mask3);
336 t = (t & mask4) + (t>>16);
337 return (int)t;
338 }
339
deSafeMul32(deInt32 a,deInt32 b)340 DE_INLINE deInt32 deSafeMul32 (deInt32 a, deInt32 b)
341 {
342 deInt32 res = a * b;
343 DE_ASSERT((deInt64)res == ((deInt64)a * (deInt64)b));
344 return res;
345 }
346
deSafeAdd32(deInt32 a,deInt32 b)347 DE_INLINE deInt32 deSafeAdd32 (deInt32 a, deInt32 b)
348 {
349 DE_ASSERT((deInt64)a + (deInt64)b == (deInt64)(a + b));
350 return (a + b);
351 }
352
353 /* \todo [petri] Move to deInt64.h? */
354
deMulAsr32(deInt32 a,deInt32 b,int shift)355 DE_INLINE deInt32 deMulAsr32 (deInt32 a, deInt32 b, int shift)
356 {
357 return (deInt32)(((deInt64)a * (deInt64)b) >> shift);
358 }
359
deSafeMulAsr32(deInt32 a,deInt32 b,int shift)360 DE_INLINE deInt32 deSafeMulAsr32 (deInt32 a, deInt32 b, int shift)
361 {
362 deInt64 res = ((deInt64)a * (deInt64)b) >> shift;
363 DE_ASSERT(res == (deInt64)(deInt32)res);
364 return (deInt32)res;
365 }
366
deSafeMuluAsr32(deUint32 a,deUint32 b,int shift)367 DE_INLINE deUint32 deSafeMuluAsr32 (deUint32 a, deUint32 b, int shift)
368 {
369 deUint64 res = ((deUint64)a * (deUint64)b) >> shift;
370 DE_ASSERT(res == (deUint64)(deUint32)res);
371 return (deUint32)res;
372 }
373
deMul32_32_64(deInt32 a,deInt32 b)374 DE_INLINE deInt64 deMul32_32_64 (deInt32 a, deInt32 b)
375 {
376 return ((deInt64)a * (deInt64)b);
377 }
378
deAbs64(deInt64 a)379 DE_INLINE deInt64 deAbs64 (deInt64 a)
380 {
381 DE_ASSERT((deUint64) a != 0x8000000000000000LL);
382 return (a >= 0) ? a : -a;
383 }
384
deClz64(deInt64 a)385 DE_INLINE int deClz64 (deInt64 a)
386 {
387 if (((deUint64)a >> 32) != 0)
388 return deClz32((deInt32)(a >> 32));
389 return deClz32((deInt32)a) + 32;
390 }
391
392 /* Common hash & compare functions. */
393
deInt32Hash(deInt32 a)394 DE_INLINE deUint32 deInt32Hash (deInt32 a)
395 {
396 /* From: http://www.concentric.net/~Ttwang/tech/inthash.htm */
397 deUint32 key = a;
398 key = (key ^ 61) ^ (key >> 16);
399 key = key + (key << 3);
400 key = key ^ (key >> 4);
401 key = key * 0x27d4eb2d; /* prime/odd constant */
402 key = key ^ (key >> 15);
403 return key;
404 }
405
deInt64Hash(deInt64 a)406 DE_INLINE deUint32 deInt64Hash (deInt64 a)
407 {
408 /* From: http://www.concentric.net/~Ttwang/tech/inthash.htm */
409 deUint64 key = a;
410 key = (~key) + (key << 21); /* key = (key << 21) - key - 1; */
411 key = key ^ (key >> 24);
412 key = (key + (key << 3)) + (key << 8); /* key * 265 */
413 key = key ^ (key >> 14);
414 key = (key + (key << 2)) + (key << 4); /* key * 21 */
415 key = key ^ (key >> 28);
416 key = key + (key << 31);
417 return (deUint32)key;
418 }
419
deInt16Hash(deInt16 v)420 DE_INLINE int deInt16Hash (deInt16 v) { return deInt32Hash(v); }
deUint16Hash(deUint16 v)421 DE_INLINE int deUint16Hash (deUint16 v) { return deInt32Hash((deInt32)v); }
deUint32Hash(deUint32 v)422 DE_INLINE int deUint32Hash (deUint32 v) { return deInt32Hash((deInt32)v); }
deUint64Hash(deUint64 v)423 DE_INLINE int deUint64Hash (deUint64 v) { return deInt64Hash((deInt64)v); }
424
deInt16Equal(deInt16 a,deInt16 b)425 DE_INLINE deBool deInt16Equal (deInt16 a, deInt16 b) { return (a == b); }
deUint16Equal(deUint16 a,deUint16 b)426 DE_INLINE deBool deUint16Equal (deUint16 a, deUint16 b) { return (a == b); }
deInt32Equal(deInt32 a,deInt32 b)427 DE_INLINE deBool deInt32Equal (deInt32 a, deInt32 b) { return (a == b); }
deUint32Equal(deUint32 a,deUint32 b)428 DE_INLINE deBool deUint32Equal (deUint32 a, deUint32 b) { return (a == b); }
deInt64Equal(deInt64 a,deInt64 b)429 DE_INLINE deBool deInt64Equal (deInt64 a, deInt64 b) { return (a == b); }
deUint64Equal(deUint64 a,deUint64 b)430 DE_INLINE deBool deUint64Equal (deUint64 a, deUint64 b) { return (a == b); }
431
dePointerHash(const void * ptr)432 DE_INLINE int dePointerHash (const void* ptr)
433 {
434 deUintptr val = (deUintptr)ptr;
435 #if (DE_PTR_SIZE == 4)
436 return deInt32Hash((int)val);
437 #elif (DE_PTR_SIZE == 8)
438 return deInt64Hash((deInt64)val);
439 #else
440 # error Unsupported pointer size.
441 #endif
442 }
443
dePointerEqual(const void * a,const void * b)444 DE_INLINE deBool dePointerEqual (const void* a, const void* b)
445 {
446 return (a == b);
447 }
448
449 /**
450 * \brief Modulo that generates the same sign as divisor and rounds toward
451 * negative infinity -- assuming c99 %-operator.
452 */
deInt32ModF(deInt32 n,deInt32 d)453 DE_INLINE deInt32 deInt32ModF(deInt32 n, deInt32 d)
454 {
455 deInt32 r = n%d;
456 if ((r > 0 && d < 0) || (r < 0 && d > 0)) r = r+d;
457 return r;
458 }
459
deInt64InInt32Range(deInt64 x)460 DE_INLINE deBool deInt64InInt32Range(deInt64 x)
461 {
462 return ((x >= (-1ll<<31)) && (x <= ((1ll<<31)-1)));
463 }
464
465 DE_END_EXTERN_C
466
467 #endif /* _DEINT32_H */
468