79869a119af2b776aa5c1e638462575034d524d2
[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
183 /**
184 * Floor(x), returned as int.
185 */
186 static inline int
187 util_ifloor(float f)
188 {
189 int ai, bi;
190 double af, bf;
191 union fi u;
192 af = (3 << 22) + 0.5 + (double) f;
193 bf = (3 << 22) + 0.5 - (double) f;
194 u.f = (float) af; ai = u.i;
195 u.f = (float) bf; bi = u.i;
196 return (ai - bi) >> 1;
197 }
198
199
200 /**
201 * Round float to nearest int.
202 */
203 static inline int
204 util_iround(float f)
205 {
206 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
207 int r;
208 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
209 return r;
210 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
211 int r;
212 _asm {
213 fld f
214 fistp r
215 }
216 return r;
217 #else
218 if (f >= 0.0f)
219 return (int) (f + 0.5f);
220 else
221 return (int) (f - 0.5f);
222 #endif
223 }
224
225
226 /**
227 * Approximate floating point comparison
228 */
229 static inline boolean
230 util_is_approx(float a, float b, float tol)
231 {
232 return fabsf(b - a) <= tol;
233 }
234
235
236 /**
237 * util_is_X_inf_or_nan = test if x is NaN or +/- Inf
238 * util_is_X_nan = test if x is NaN
239 * util_X_inf_sign = return +1 for +Inf, -1 for -Inf, or 0 for not Inf
240 *
241 * NaN can be checked with x != x, however this fails with the fast math flag
242 **/
243
244
245 /**
246 * Single-float
247 */
248 static inline boolean
249 util_is_inf_or_nan(float x)
250 {
251 union fi tmp;
252 tmp.f = x;
253 return (tmp.ui & 0x7f800000) == 0x7f800000;
254 }
255
256
257 static inline boolean
258 util_is_nan(float x)
259 {
260 union fi tmp;
261 tmp.f = x;
262 return (tmp.ui & 0x7fffffff) > 0x7f800000;
263 }
264
265
266 static inline int
267 util_inf_sign(float x)
268 {
269 union fi tmp;
270 tmp.f = x;
271 if ((tmp.ui & 0x7fffffff) != 0x7f800000) {
272 return 0;
273 }
274
275 return (x < 0) ? -1 : 1;
276 }
277
278
279 /**
280 * Double-float
281 */
282 static inline boolean
283 util_is_double_inf_or_nan(double x)
284 {
285 union di tmp;
286 tmp.d = x;
287 return (tmp.ui & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL;
288 }
289
290
291 static inline boolean
292 util_is_double_nan(double x)
293 {
294 union di tmp;
295 tmp.d = x;
296 return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL;
297 }
298
299
300 static inline int
301 util_double_inf_sign(double x)
302 {
303 union di tmp;
304 tmp.d = x;
305 if ((tmp.ui & 0x7fffffffffffffffULL) != 0x7ff0000000000000ULL) {
306 return 0;
307 }
308
309 return (x < 0) ? -1 : 1;
310 }
311
312
313 /**
314 * Half-float
315 */
316 static inline boolean
317 util_is_half_inf_or_nan(int16_t x)
318 {
319 return (x & 0x7c00) == 0x7c00;
320 }
321
322
323 static inline boolean
324 util_is_half_nan(int16_t x)
325 {
326 return (x & 0x7fff) > 0x7c00;
327 }
328
329
330 static inline int
331 util_half_inf_sign(int16_t x)
332 {
333 if ((x & 0x7fff) != 0x7c00) {
334 return 0;
335 }
336
337 return (x < 0) ? -1 : 1;
338 }
339
340
341 /**
342 * Return float bits.
343 */
344 static inline unsigned
345 fui( float f )
346 {
347 union fi fi;
348 fi.f = f;
349 return fi.ui;
350 }
351
352 static inline float
353 uif(uint32_t ui)
354 {
355 union fi fi;
356 fi.ui = ui;
357 return fi.f;
358 }
359
360
361 /**
362 * Convert ubyte to float in [0, 1].
363 * XXX a 256-entry lookup table would be slightly faster.
364 */
365 static inline float
366 ubyte_to_float(ubyte ub)
367 {
368 return (float) ub * (1.0f / 255.0f);
369 }
370
371
372 /**
373 * Convert float in [0,1] to ubyte in [0,255] with clamping.
374 */
375 static inline ubyte
376 float_to_ubyte(float f)
377 {
378 union fi tmp;
379
380 tmp.f = f;
381 if (tmp.i < 0) {
382 return (ubyte) 0;
383 }
384 else if (tmp.i >= 0x3f800000 /* 1.0f */) {
385 return (ubyte) 255;
386 }
387 else {
388 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
389 return (ubyte) tmp.i;
390 }
391 }
392
393 static inline float
394 byte_to_float_tex(int8_t b)
395 {
396 return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
397 }
398
399 static inline int8_t
400 float_to_byte_tex(float f)
401 {
402 return (int8_t) (127.0F * f);
403 }
404
405 /**
406 * Calc log base 2
407 */
408 static inline unsigned
409 util_logbase2(unsigned n)
410 {
411 #if defined(HAVE___BUILTIN_CLZ)
412 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
413 #else
414 unsigned pos = 0;
415 if (n >= 1<<16) { n >>= 16; pos += 16; }
416 if (n >= 1<< 8) { n >>= 8; pos += 8; }
417 if (n >= 1<< 4) { n >>= 4; pos += 4; }
418 if (n >= 1<< 2) { n >>= 2; pos += 2; }
419 if (n >= 1<< 1) { pos += 1; }
420 return pos;
421 #endif
422 }
423
424 static inline uint64_t
425 util_logbase2_64(uint64_t n)
426 {
427 #if defined(HAVE___BUILTIN_CLZLL)
428 return ((sizeof(uint64_t) * 8 - 1) - __builtin_clzll(n | 1));
429 #else
430 uint64_t pos = 0ull;
431 if (n >= 1ull<<32) { n >>= 32; pos += 32; }
432 if (n >= 1ull<<16) { n >>= 16; pos += 16; }
433 if (n >= 1ull<< 8) { n >>= 8; pos += 8; }
434 if (n >= 1ull<< 4) { n >>= 4; pos += 4; }
435 if (n >= 1ull<< 2) { n >>= 2; pos += 2; }
436 if (n >= 1ull<< 1) { pos += 1; }
437 return pos;
438 #endif
439 }
440
441 /**
442 * Returns the ceiling of log n base 2, and 0 when n == 0. Equivalently,
443 * returns the smallest x such that n <= 2**x.
444 */
445 static inline unsigned
446 util_logbase2_ceil(unsigned n)
447 {
448 if (n <= 1)
449 return 0;
450
451 return 1 + util_logbase2(n - 1);
452 }
453
454 static inline uint64_t
455 util_logbase2_ceil64(uint64_t n)
456 {
457 if (n <= 1)
458 return 0;
459
460 return 1ull + util_logbase2_64(n - 1);
461 }
462
463 /**
464 * Returns the smallest power of two >= x
465 */
466 static inline unsigned
467 util_next_power_of_two(unsigned x)
468 {
469 #if defined(HAVE___BUILTIN_CLZ)
470 if (x <= 1)
471 return 1;
472
473 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
474 #else
475 unsigned val = x;
476
477 if (x <= 1)
478 return 1;
479
480 if (util_is_power_of_two_or_zero(x))
481 return x;
482
483 val--;
484 val = (val >> 1) | val;
485 val = (val >> 2) | val;
486 val = (val >> 4) | val;
487 val = (val >> 8) | val;
488 val = (val >> 16) | val;
489 val++;
490 return val;
491 #endif
492 }
493
494 static inline uint64_t
495 util_next_power_of_two64(uint64_t x)
496 {
497 #if defined(HAVE___BUILTIN_CLZLL)
498 if (x <= 1)
499 return 1;
500
501 return (1ull << ((sizeof(uint64_t) * 8) - __builtin_clzll(x - 1)));
502 #else
503 uint64_t val = x;
504
505 if (x <= 1)
506 return 1;
507
508 if (util_is_power_of_two_or_zero64(x))
509 return x;
510
511 val--;
512 val = (val >> 1) | val;
513 val = (val >> 2) | val;
514 val = (val >> 4) | val;
515 val = (val >> 8) | val;
516 val = (val >> 16) | val;
517 val = (val >> 32) | val;
518 val++;
519 return val;
520 #endif
521 }
522
523
524 /**
525 * Return number of bits set in n.
526 */
527 static inline unsigned
528 util_bitcount(unsigned n)
529 {
530 #if defined(HAVE___BUILTIN_POPCOUNT)
531 return __builtin_popcount(n);
532 #else
533 /* K&R classic bitcount.
534 *
535 * For each iteration, clear the LSB from the bitfield.
536 * Requires only one iteration per set bit, instead of
537 * one iteration per bit less than highest set bit.
538 */
539 unsigned bits;
540 for (bits = 0; n; bits++) {
541 n &= n - 1;
542 }
543 return bits;
544 #endif
545 }
546
547
548 static inline unsigned
549 util_bitcount64(uint64_t n)
550 {
551 #ifdef HAVE___BUILTIN_POPCOUNTLL
552 return __builtin_popcountll(n);
553 #else
554 return util_bitcount(n) + util_bitcount(n >> 32);
555 #endif
556 }
557
558
559 /**
560 * Reverse bits in n
561 * Algorithm taken from:
562 * http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
563 */
564 static inline unsigned
565 util_bitreverse(unsigned n)
566 {
567 n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
568 n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
569 n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4);
570 n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8);
571 n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16);
572 return n;
573 }
574
575 /**
576 * Convert from little endian to CPU byte order.
577 */
578
579 #ifdef PIPE_ARCH_BIG_ENDIAN
580 #define util_le64_to_cpu(x) util_bswap64(x)
581 #define util_le32_to_cpu(x) util_bswap32(x)
582 #define util_le16_to_cpu(x) util_bswap16(x)
583 #else
584 #define util_le64_to_cpu(x) (x)
585 #define util_le32_to_cpu(x) (x)
586 #define util_le16_to_cpu(x) (x)
587 #endif
588
589 #define util_cpu_to_le64(x) util_le64_to_cpu(x)
590 #define util_cpu_to_le32(x) util_le32_to_cpu(x)
591 #define util_cpu_to_le16(x) util_le16_to_cpu(x)
592
593 /**
594 * Reverse byte order of a 32 bit word.
595 */
596 static inline uint32_t
597 util_bswap32(uint32_t n)
598 {
599 #if defined(HAVE___BUILTIN_BSWAP32)
600 return __builtin_bswap32(n);
601 #else
602 return (n >> 24) |
603 ((n >> 8) & 0x0000ff00) |
604 ((n << 8) & 0x00ff0000) |
605 (n << 24);
606 #endif
607 }
608
609 /**
610 * Reverse byte order of a 64bit word.
611 */
612 static inline uint64_t
613 util_bswap64(uint64_t n)
614 {
615 #if defined(HAVE___BUILTIN_BSWAP64)
616 return __builtin_bswap64(n);
617 #else
618 return ((uint64_t)util_bswap32((uint32_t)n) << 32) |
619 util_bswap32((n >> 32));
620 #endif
621 }
622
623
624 /**
625 * Reverse byte order of a 16 bit word.
626 */
627 static inline uint16_t
628 util_bswap16(uint16_t n)
629 {
630 return (n >> 8) |
631 (n << 8);
632 }
633
634 static inline void*
635 util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
636 {
637 #ifdef PIPE_ARCH_BIG_ENDIAN
638 size_t i, e;
639 assert(n % 4 == 0);
640
641 for (i = 0, e = n / 4; i < e; i++) {
642 uint32_t * restrict d = (uint32_t* restrict)dest;
643 const uint32_t * restrict s = (const uint32_t* restrict)src;
644 d[i] = util_bswap32(s[i]);
645 }
646 return dest;
647 #else
648 return memcpy(dest, src, n);
649 #endif
650 }
651
652 /**
653 * Clamp X to [MIN, MAX].
654 * This is a macro to allow float, int, uint, etc. types.
655 * We arbitrarily turn NaN into MIN.
656 */
657 #define CLAMP( X, MIN, MAX ) ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) )
658
659 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
660 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
661
662 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
663 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
664
665 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
666 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
667
668
669 /**
670 * Align a value, only works pot alignemnts.
671 */
672 static inline int
673 align(int value, int alignment)
674 {
675 return (value + alignment - 1) & ~(alignment - 1);
676 }
677
678 static inline uint64_t
679 align64(uint64_t value, unsigned alignment)
680 {
681 return (value + alignment - 1) & ~((uint64_t)alignment - 1);
682 }
683
684 /**
685 * Works like align but on npot alignments.
686 */
687 static inline size_t
688 util_align_npot(size_t value, size_t alignment)
689 {
690 if (value % alignment)
691 return value + (alignment - (value % alignment));
692 return value;
693 }
694
695 static inline unsigned
696 u_minify(unsigned value, unsigned levels)
697 {
698 return MAX2(1, value >> levels);
699 }
700
701 #ifndef COPY_4V
702 #define COPY_4V( DST, SRC ) \
703 do { \
704 (DST)[0] = (SRC)[0]; \
705 (DST)[1] = (SRC)[1]; \
706 (DST)[2] = (SRC)[2]; \
707 (DST)[3] = (SRC)[3]; \
708 } while (0)
709 #endif
710
711
712 #ifndef COPY_4FV
713 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
714 #endif
715
716
717 #ifndef ASSIGN_4V
718 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
719 do { \
720 (DST)[0] = (V0); \
721 (DST)[1] = (V1); \
722 (DST)[2] = (V2); \
723 (DST)[3] = (V3); \
724 } while (0)
725 #endif
726
727
728 static inline uint32_t
729 util_unsigned_fixed(float value, unsigned frac_bits)
730 {
731 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
732 }
733
734 static inline int32_t
735 util_signed_fixed(float value, unsigned frac_bits)
736 {
737 return (int32_t)(value * (1<<frac_bits));
738 }
739
740 unsigned
741 util_fpstate_get(void);
742 unsigned
743 util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
744 void
745 util_fpstate_set(unsigned fpstate);
746
747
748
749 #ifdef __cplusplus
750 }
751 #endif
752
753 #endif /* U_MATH_H */