mesa/gallium: Move u_bit_scan{,64} from gallium to util.
[mesa.git] / src / gallium / auxiliary / util / u_math.h
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Math utilities and approximations for common math functions.
31 * Reduced precision is usually acceptable in shaders...
32 *
33 * "fast" is used in the names of functions which are low-precision,
34 * or at least lower-precision than the normal C lib functions.
35 */
36
37
38 #ifndef U_MATH_H
39 #define U_MATH_H
40
41
42 #include "pipe/p_compiler.h"
43
44 #include "c99_math.h"
45 #include <assert.h>
46 #include <float.h>
47 #include <stdarg.h>
48
49 #include "util/bitscan.h"
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55
56 #ifndef M_SQRT2
57 #define M_SQRT2 1.41421356237309504880
58 #endif
59
60 #define POW2_TABLE_SIZE_LOG2 9
61 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
62 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
63 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
64 extern float pow2_table[POW2_TABLE_SIZE];
65
66
67 /**
68 * Initialize math module. This should be called before using any
69 * other functions in this module.
70 */
71 extern void
72 util_init_math(void);
73
74
75 union fi {
76 float f;
77 int32_t i;
78 uint32_t ui;
79 };
80
81
82 union di {
83 double d;
84 int64_t i;
85 uint64_t ui;
86 };
87
88
89 /**
90 * Extract the IEEE float32 exponent.
91 */
92 static inline signed
93 util_get_float32_exponent(float x)
94 {
95 union fi f;
96
97 f.f = x;
98
99 return ((f.ui >> 23) & 0xff) - 127;
100 }
101
102
103 /**
104 * Fast version of 2^x
105 * Identity: exp2(a + b) = exp2(a) * exp2(b)
106 * Let ipart = int(x)
107 * Let fpart = x - ipart;
108 * So, exp2(x) = exp2(ipart) * exp2(fpart)
109 * Compute exp2(ipart) with i << ipart
110 * Compute exp2(fpart) with lookup table.
111 */
112 static inline float
113 util_fast_exp2(float x)
114 {
115 int32_t ipart;
116 float fpart, mpart;
117 union fi epart;
118
119 if(x > 129.00000f)
120 return 3.402823466e+38f;
121
122 if (x < -126.99999f)
123 return 0.0f;
124
125 ipart = (int32_t) x;
126 fpart = x - (float) ipart;
127
128 /* same as
129 * epart.f = (float) (1 << ipart)
130 * but faster and without integer overflow for ipart > 31
131 */
132 epart.i = (ipart + 127 ) << 23;
133
134 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
135
136 return epart.f * mpart;
137 }
138
139
140 /**
141 * Fast approximation to exp(x).
142 */
143 static inline float
144 util_fast_exp(float x)
145 {
146 const float k = 1.44269f; /* = log2(e) */
147 return util_fast_exp2(k * x);
148 }
149
150
151 #define LOG2_TABLE_SIZE_LOG2 16
152 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
153 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
154 extern float log2_table[LOG2_TABLE_SIZE];
155
156
157 /**
158 * Fast approximation to log2(x).
159 */
160 static inline float
161 util_fast_log2(float x)
162 {
163 union fi num;
164 float epart, mpart;
165 num.f = x;
166 epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
167 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
168 mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
169 return epart + mpart;
170 }
171
172
173 /**
174 * Fast approximation to x^y.
175 */
176 static inline float
177 util_fast_pow(float x, float y)
178 {
179 return util_fast_exp2(util_fast_log2(x) * y);
180 }
181
182 /* Note that this counts zero as a power of two.
183 */
184 static inline boolean
185 util_is_power_of_two( unsigned v )
186 {
187 return (v & (v-1)) == 0;
188 }
189
190
191 /**
192 * Floor(x), returned as int.
193 */
194 static inline int
195 util_ifloor(float f)
196 {
197 int ai, bi;
198 double af, bf;
199 union fi u;
200 af = (3 << 22) + 0.5 + (double) f;
201 bf = (3 << 22) + 0.5 - (double) f;
202 u.f = (float) af; ai = u.i;
203 u.f = (float) bf; bi = u.i;
204 return (ai - bi) >> 1;
205 }
206
207
208 /**
209 * Round float to nearest int.
210 */
211 static inline int
212 util_iround(float f)
213 {
214 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
215 int r;
216 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
217 return r;
218 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
219 int r;
220 _asm {
221 fld f
222 fistp r
223 }
224 return r;
225 #else
226 if (f >= 0.0f)
227 return (int) (f + 0.5f);
228 else
229 return (int) (f - 0.5f);
230 #endif
231 }
232
233
234 /**
235 * Approximate floating point comparison
236 */
237 static inline boolean
238 util_is_approx(float a, float b, float tol)
239 {
240 return fabsf(b - a) <= tol;
241 }
242
243
244 /**
245 * util_is_X_inf_or_nan = test if x is NaN or +/- Inf
246 * util_is_X_nan = test if x is NaN
247 * util_X_inf_sign = return +1 for +Inf, -1 for -Inf, or 0 for not Inf
248 *
249 * NaN can be checked with x != x, however this fails with the fast math flag
250 **/
251
252
253 /**
254 * Single-float
255 */
256 static inline boolean
257 util_is_inf_or_nan(float x)
258 {
259 union fi tmp;
260 tmp.f = x;
261 return (tmp.ui & 0x7f800000) == 0x7f800000;
262 }
263
264
265 static inline boolean
266 util_is_nan(float x)
267 {
268 union fi tmp;
269 tmp.f = x;
270 return (tmp.ui & 0x7fffffff) > 0x7f800000;
271 }
272
273
274 static inline int
275 util_inf_sign(float x)
276 {
277 union fi tmp;
278 tmp.f = x;
279 if ((tmp.ui & 0x7fffffff) != 0x7f800000) {
280 return 0;
281 }
282
283 return (x < 0) ? -1 : 1;
284 }
285
286
287 /**
288 * Double-float
289 */
290 static inline boolean
291 util_is_double_inf_or_nan(double x)
292 {
293 union di tmp;
294 tmp.d = x;
295 return (tmp.ui & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL;
296 }
297
298
299 static inline boolean
300 util_is_double_nan(double x)
301 {
302 union di tmp;
303 tmp.d = x;
304 return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL;
305 }
306
307
308 static inline int
309 util_double_inf_sign(double x)
310 {
311 union di tmp;
312 tmp.d = x;
313 if ((tmp.ui & 0x7fffffffffffffffULL) != 0x7ff0000000000000ULL) {
314 return 0;
315 }
316
317 return (x < 0) ? -1 : 1;
318 }
319
320
321 /**
322 * Half-float
323 */
324 static inline boolean
325 util_is_half_inf_or_nan(int16_t x)
326 {
327 return (x & 0x7c00) == 0x7c00;
328 }
329
330
331 static inline boolean
332 util_is_half_nan(int16_t x)
333 {
334 return (x & 0x7fff) > 0x7c00;
335 }
336
337
338 static inline int
339 util_half_inf_sign(int16_t x)
340 {
341 if ((x & 0x7fff) != 0x7c00) {
342 return 0;
343 }
344
345 return (x < 0) ? -1 : 1;
346 }
347
348
349 /**
350 * Find last bit set in a word. The least significant bit is 1.
351 * Return 0 if no bits are set.
352 */
353 static inline unsigned
354 util_last_bit(unsigned u)
355 {
356 #if defined(HAVE___BUILTIN_CLZ)
357 return u == 0 ? 0 : 32 - __builtin_clz(u);
358 #else
359 unsigned r = 0;
360 while (u) {
361 r++;
362 u >>= 1;
363 }
364 return r;
365 #endif
366 }
367
368 /**
369 * Find last bit set in a word. The least significant bit is 1.
370 * Return 0 if no bits are set.
371 */
372 static inline unsigned
373 util_last_bit64(uint64_t u)
374 {
375 #if defined(HAVE___BUILTIN_CLZLL)
376 return u == 0 ? 0 : 64 - __builtin_clzll(u);
377 #else
378 unsigned r = 0;
379 while (u) {
380 r++;
381 u >>= 1;
382 }
383 return r;
384 #endif
385 }
386
387 /**
388 * Find last bit in a word that does not match the sign bit. The least
389 * significant bit is 1.
390 * Return 0 if no bits are set.
391 */
392 static inline unsigned
393 util_last_bit_signed(int i)
394 {
395 if (i >= 0)
396 return util_last_bit(i);
397 else
398 return util_last_bit(~(unsigned)i);
399 }
400
401 /* Returns a bitfield in which the first count bits starting at start are
402 * set.
403 */
404 static inline unsigned
405 u_bit_consecutive(unsigned start, unsigned count)
406 {
407 assert(start + count <= 32);
408 if (count == 32)
409 return ~0;
410 return ((1u << count) - 1) << start;
411 }
412
413 /**
414 * Return float bits.
415 */
416 static inline unsigned
417 fui( float f )
418 {
419 union fi fi;
420 fi.f = f;
421 return fi.ui;
422 }
423
424 static inline float
425 uif(uint32_t ui)
426 {
427 union fi fi;
428 fi.ui = ui;
429 return fi.f;
430 }
431
432
433 /**
434 * Convert ubyte to float in [0, 1].
435 * XXX a 256-entry lookup table would be slightly faster.
436 */
437 static inline float
438 ubyte_to_float(ubyte ub)
439 {
440 return (float) ub * (1.0f / 255.0f);
441 }
442
443
444 /**
445 * Convert float in [0,1] to ubyte in [0,255] with clamping.
446 */
447 static inline ubyte
448 float_to_ubyte(float f)
449 {
450 union fi tmp;
451
452 tmp.f = f;
453 if (tmp.i < 0) {
454 return (ubyte) 0;
455 }
456 else if (tmp.i >= 0x3f800000 /* 1.0f */) {
457 return (ubyte) 255;
458 }
459 else {
460 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
461 return (ubyte) tmp.i;
462 }
463 }
464
465 static inline float
466 byte_to_float_tex(int8_t b)
467 {
468 return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
469 }
470
471 static inline int8_t
472 float_to_byte_tex(float f)
473 {
474 return (int8_t) (127.0F * f);
475 }
476
477 /**
478 * Calc log base 2
479 */
480 static inline unsigned
481 util_logbase2(unsigned n)
482 {
483 #if defined(HAVE___BUILTIN_CLZ)
484 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
485 #else
486 unsigned pos = 0;
487 if (n >= 1<<16) { n >>= 16; pos += 16; }
488 if (n >= 1<< 8) { n >>= 8; pos += 8; }
489 if (n >= 1<< 4) { n >>= 4; pos += 4; }
490 if (n >= 1<< 2) { n >>= 2; pos += 2; }
491 if (n >= 1<< 1) { pos += 1; }
492 return pos;
493 #endif
494 }
495
496
497 /**
498 * Returns the smallest power of two >= x
499 */
500 static inline unsigned
501 util_next_power_of_two(unsigned x)
502 {
503 #if defined(HAVE___BUILTIN_CLZ)
504 if (x <= 1)
505 return 1;
506
507 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
508 #else
509 unsigned val = x;
510
511 if (x <= 1)
512 return 1;
513
514 if (util_is_power_of_two(x))
515 return x;
516
517 val--;
518 val = (val >> 1) | val;
519 val = (val >> 2) | val;
520 val = (val >> 4) | val;
521 val = (val >> 8) | val;
522 val = (val >> 16) | val;
523 val++;
524 return val;
525 #endif
526 }
527
528
529 /**
530 * Return number of bits set in n.
531 */
532 static inline unsigned
533 util_bitcount(unsigned n)
534 {
535 #if defined(HAVE___BUILTIN_POPCOUNT)
536 return __builtin_popcount(n);
537 #else
538 /* K&R classic bitcount.
539 *
540 * For each iteration, clear the LSB from the bitfield.
541 * Requires only one iteration per set bit, instead of
542 * one iteration per bit less than highest set bit.
543 */
544 unsigned bits;
545 for (bits = 0; n; bits++) {
546 n &= n - 1;
547 }
548 return bits;
549 #endif
550 }
551
552
553 static inline unsigned
554 util_bitcount64(uint64_t n)
555 {
556 #ifdef HAVE___BUILTIN_POPCOUNTLL
557 return __builtin_popcountll(n);
558 #else
559 return util_bitcount(n) + util_bitcount(n >> 32);
560 #endif
561 }
562
563
564 /**
565 * Reverse bits in n
566 * Algorithm taken from:
567 * http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
568 */
569 static inline unsigned
570 util_bitreverse(unsigned n)
571 {
572 n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
573 n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
574 n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4);
575 n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8);
576 n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16);
577 return n;
578 }
579
580 /**
581 * Convert from little endian to CPU byte order.
582 */
583
584 #ifdef PIPE_ARCH_BIG_ENDIAN
585 #define util_le64_to_cpu(x) util_bswap64(x)
586 #define util_le32_to_cpu(x) util_bswap32(x)
587 #define util_le16_to_cpu(x) util_bswap16(x)
588 #else
589 #define util_le64_to_cpu(x) (x)
590 #define util_le32_to_cpu(x) (x)
591 #define util_le16_to_cpu(x) (x)
592 #endif
593
594 #define util_cpu_to_le64(x) util_le64_to_cpu(x)
595 #define util_cpu_to_le32(x) util_le32_to_cpu(x)
596 #define util_cpu_to_le16(x) util_le16_to_cpu(x)
597
598 /**
599 * Reverse byte order of a 32 bit word.
600 */
601 static inline uint32_t
602 util_bswap32(uint32_t n)
603 {
604 #if defined(HAVE___BUILTIN_BSWAP32)
605 return __builtin_bswap32(n);
606 #else
607 return (n >> 24) |
608 ((n >> 8) & 0x0000ff00) |
609 ((n << 8) & 0x00ff0000) |
610 (n << 24);
611 #endif
612 }
613
614 /**
615 * Reverse byte order of a 64bit word.
616 */
617 static inline uint64_t
618 util_bswap64(uint64_t n)
619 {
620 #if defined(HAVE___BUILTIN_BSWAP64)
621 return __builtin_bswap64(n);
622 #else
623 return ((uint64_t)util_bswap32((uint32_t)n) << 32) |
624 util_bswap32((n >> 32));
625 #endif
626 }
627
628
629 /**
630 * Reverse byte order of a 16 bit word.
631 */
632 static inline uint16_t
633 util_bswap16(uint16_t n)
634 {
635 return (n >> 8) |
636 (n << 8);
637 }
638
639 static inline void*
640 util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
641 {
642 #ifdef PIPE_ARCH_BIG_ENDIAN
643 size_t i, e;
644 assert(n % 4 == 0);
645
646 for (i = 0, e = n / 4; i < e; i++) {
647 uint32_t * restrict d = (uint32_t* restrict)dest;
648 const uint32_t * restrict s = (const uint32_t* restrict)src;
649 d[i] = util_bswap32(s[i]);
650 }
651 return dest;
652 #else
653 return memcpy(dest, src, n);
654 #endif
655 }
656
657 /**
658 * Clamp X to [MIN, MAX].
659 * This is a macro to allow float, int, uint, etc. types.
660 */
661 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
662
663 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
664 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
665
666 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
667 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
668
669 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
670 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
671
672
673 /**
674 * Align a value, only works pot alignemnts.
675 */
676 static inline int
677 align(int value, int alignment)
678 {
679 return (value + alignment - 1) & ~(alignment - 1);
680 }
681
682 static inline uint64_t
683 align64(uint64_t value, unsigned alignment)
684 {
685 return (value + alignment - 1) & ~(alignment - 1);
686 }
687
688 /**
689 * Works like align but on npot alignments.
690 */
691 static inline size_t
692 util_align_npot(size_t value, size_t alignment)
693 {
694 if (value % alignment)
695 return value + (alignment - (value % alignment));
696 return value;
697 }
698
699 static inline unsigned
700 u_minify(unsigned value, unsigned levels)
701 {
702 return MAX2(1, value >> levels);
703 }
704
705 #ifndef COPY_4V
706 #define COPY_4V( DST, SRC ) \
707 do { \
708 (DST)[0] = (SRC)[0]; \
709 (DST)[1] = (SRC)[1]; \
710 (DST)[2] = (SRC)[2]; \
711 (DST)[3] = (SRC)[3]; \
712 } while (0)
713 #endif
714
715
716 #ifndef COPY_4FV
717 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
718 #endif
719
720
721 #ifndef ASSIGN_4V
722 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
723 do { \
724 (DST)[0] = (V0); \
725 (DST)[1] = (V1); \
726 (DST)[2] = (V2); \
727 (DST)[3] = (V3); \
728 } while (0)
729 #endif
730
731
732 static inline uint32_t
733 util_unsigned_fixed(float value, unsigned frac_bits)
734 {
735 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
736 }
737
738 static inline int32_t
739 util_signed_fixed(float value, unsigned frac_bits)
740 {
741 return (int32_t)(value * (1<<frac_bits));
742 }
743
744 unsigned
745 util_fpstate_get(void);
746 unsigned
747 util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
748 void
749 util_fpstate_set(unsigned fpstate);
750
751
752
753 #ifdef __cplusplus
754 }
755 #endif
756
757 #endif /* U_MATH_H */