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
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49
50 #include <math.h>
51 #include <float.h>
52 #include <stdarg.h>
53
54 #ifdef PIPE_OS_UNIX
55 #include <strings.h> /* for ffs */
56 #endif
57
58
59 #ifndef M_SQRT2
60 #define M_SQRT2 1.41421356237309504880
61 #endif
62
63
64 #if defined(_MSC_VER)
65
66 #if _MSC_VER < 1400 && !defined(__cplusplus)
67
cosf(float f)68 static inline float cosf( float f )
69 {
70 return (float) cos( (double) f );
71 }
72
sinf(float f)73 static inline float sinf( float f )
74 {
75 return (float) sin( (double) f );
76 }
77
ceilf(float f)78 static inline float ceilf( float f )
79 {
80 return (float) ceil( (double) f );
81 }
82
floorf(float f)83 static inline float floorf( float f )
84 {
85 return (float) floor( (double) f );
86 }
87
powf(float f,float g)88 static inline float powf( float f, float g )
89 {
90 return (float) pow( (double) f, (double) g );
91 }
92
sqrtf(float f)93 static inline float sqrtf( float f )
94 {
95 return (float) sqrt( (double) f );
96 }
97
fabsf(float f)98 static inline float fabsf( float f )
99 {
100 return (float) fabs( (double) f );
101 }
102
logf(float f)103 static inline float logf( float f )
104 {
105 return (float) log( (double) f );
106 }
107
108 #else
109 /* Work-around an extra semi-colon in VS 2005 logf definition */
110 #ifdef logf
111 #undef logf
112 #define logf(x) ((float)log((double)(x)))
113 #endif /* logf */
114
115 #if _MSC_VER < 1800
116 #define isfinite(x) _finite((double)(x))
117 #define isnan(x) _isnan((double)(x))
118 #endif /* _MSC_VER < 1800 */
119 #endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
120
121 #if _MSC_VER < 1800
log2(double x)122 static inline double log2( double x )
123 {
124 const double invln2 = 1.442695041;
125 return log( x ) * invln2;
126 }
127
128 static inline double
round(double x)129 round(double x)
130 {
131 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
132 }
133
134 static inline float
roundf(float x)135 roundf(float x)
136 {
137 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
138 }
139 #endif
140
141 #ifndef INFINITY
142 #define INFINITY (DBL_MAX + DBL_MAX)
143 #endif
144
145 #ifndef NAN
146 #define NAN (INFINITY - INFINITY)
147 #endif
148
149 #endif /* _MSC_VER */
150
151
152 #if __STDC_VERSION__ < 199901L && (!defined(__cplusplus) || defined(_MSC_VER))
153 static inline long int
lrint(double d)154 lrint(double d)
155 {
156 long int rounded = (long int)(d + 0.5);
157
158 if (d - floor(d) == 0.5) {
159 if (rounded % 2 != 0)
160 rounded += (d > 0) ? -1 : 1;
161 }
162
163 return rounded;
164 }
165
166 static inline long int
lrintf(float f)167 lrintf(float f)
168 {
169 long int rounded = (long int)(f + 0.5f);
170
171 if (f - floorf(f) == 0.5f) {
172 if (rounded % 2 != 0)
173 rounded += (f > 0) ? -1 : 1;
174 }
175
176 return rounded;
177 }
178
179 static inline long long int
llrint(double d)180 llrint(double d)
181 {
182 long long int rounded = (long long int)(d + 0.5);
183
184 if (d - floor(d) == 0.5) {
185 if (rounded % 2 != 0)
186 rounded += (d > 0) ? -1 : 1;
187 }
188
189 return rounded;
190 }
191
192 static inline long long int
llrintf(float f)193 llrintf(float f)
194 {
195 long long int rounded = (long long int)(f + 0.5f);
196
197 if (f - floorf(f) == 0.5f) {
198 if (rounded % 2 != 0)
199 rounded += (f > 0) ? -1 : 1;
200 }
201
202 return rounded;
203 }
204 #endif /* C99 */
205
206 #define POW2_TABLE_SIZE_LOG2 9
207 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
208 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
209 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
210 extern float pow2_table[POW2_TABLE_SIZE];
211
212
213 /**
214 * Initialize math module. This should be called before using any
215 * other functions in this module.
216 */
217 extern void
218 util_init_math(void);
219
220
221 union fi {
222 float f;
223 int32_t i;
224 uint32_t ui;
225 };
226
227
228 union di {
229 double d;
230 int64_t i;
231 uint64_t ui;
232 };
233
234
235 /**
236 * Extract the IEEE float32 exponent.
237 */
238 static inline signed
util_get_float32_exponent(float x)239 util_get_float32_exponent(float x) {
240 union fi f;
241
242 f.f = x;
243
244 return ((f.ui >> 23) & 0xff) - 127;
245 }
246
247
248 /**
249 * Fast version of 2^x
250 * Identity: exp2(a + b) = exp2(a) * exp2(b)
251 * Let ipart = int(x)
252 * Let fpart = x - ipart;
253 * So, exp2(x) = exp2(ipart) * exp2(fpart)
254 * Compute exp2(ipart) with i << ipart
255 * Compute exp2(fpart) with lookup table.
256 */
257 static inline float
util_fast_exp2(float x)258 util_fast_exp2(float x)
259 {
260 int32_t ipart;
261 float fpart, mpart;
262 union fi epart;
263
264 if(x > 129.00000f)
265 return 3.402823466e+38f;
266
267 if (x < -126.99999f)
268 return 0.0f;
269
270 ipart = (int32_t) x;
271 fpart = x - (float) ipart;
272
273 /* same as
274 * epart.f = (float) (1 << ipart)
275 * but faster and without integer overflow for ipart > 31
276 */
277 epart.i = (ipart + 127 ) << 23;
278
279 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
280
281 return epart.f * mpart;
282 }
283
284
285 /**
286 * Fast approximation to exp(x).
287 */
288 static inline float
util_fast_exp(float x)289 util_fast_exp(float x)
290 {
291 const float k = 1.44269f; /* = log2(e) */
292 return util_fast_exp2(k * x);
293 }
294
295
296 #if 0
297
298 #define LOG2_TABLE_SIZE_LOG2 16
299 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
300 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
301 extern float log2_table[LOG2_TABLE_SIZE];
302
303
304 /**
305 * Fast approximation to log2(x).
306 */
307 static inline float
308 util_fast_log2(float x)
309 {
310 union fi num;
311 float epart, mpart;
312 num.f = x;
313 epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
314 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
315 mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
316 return epart + mpart;
317 }
318
319
320 /**
321 * Fast approximation to x^y.
322 */
323 static inline float
324 util_fast_pow(float x, float y)
325 {
326 return util_fast_exp2(util_fast_log2(x) * y);
327 }
328 #endif
329 /* Note that this counts zero as a power of two.
330 */
331 static inline boolean
util_is_power_of_two(unsigned v)332 util_is_power_of_two( unsigned v )
333 {
334 return (v & (v-1)) == 0;
335 }
336
337
338 /**
339 * Floor(x), returned as int.
340 */
341 static inline int
util_ifloor(float f)342 util_ifloor(float f)
343 {
344 int ai, bi;
345 double af, bf;
346 union fi u;
347 af = (3 << 22) + 0.5 + (double) f;
348 bf = (3 << 22) + 0.5 - (double) f;
349 u.f = (float) af; ai = u.i;
350 u.f = (float) bf; bi = u.i;
351 return (ai - bi) >> 1;
352 }
353
354
355 /**
356 * Round float to nearest int.
357 */
358 static inline int
util_iround(float f)359 util_iround(float f)
360 {
361 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
362 int r;
363 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
364 return r;
365 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
366 int r;
367 _asm {
368 fld f
369 fistp r
370 }
371 return r;
372 #else
373 if (f >= 0.0f)
374 return (int) (f + 0.5f);
375 else
376 return (int) (f - 0.5f);
377 #endif
378 }
379
380
381 /**
382 * Approximate floating point comparison
383 */
384 static inline boolean
util_is_approx(float a,float b,float tol)385 util_is_approx(float a, float b, float tol)
386 {
387 return fabs(b - a) <= tol;
388 }
389
390
391 /**
392 * util_is_X_inf_or_nan = test if x is NaN or +/- Inf
393 * util_is_X_nan = test if x is NaN
394 * util_X_inf_sign = return +1 for +Inf, -1 for -Inf, or 0 for not Inf
395 *
396 * NaN can be checked with x != x, however this fails with the fast math flag
397 **/
398
399
400 /**
401 * Single-float
402 */
403 static inline boolean
util_is_inf_or_nan(float x)404 util_is_inf_or_nan(float x)
405 {
406 union fi tmp;
407 tmp.f = x;
408 return (tmp.ui & 0x7f800000) == 0x7f800000;
409 }
410
411
412 static inline boolean
util_is_nan(float x)413 util_is_nan(float x)
414 {
415 union fi tmp;
416 tmp.f = x;
417 return (tmp.ui & 0x7fffffff) > 0x7f800000;
418 }
419
420
421 static inline int
util_inf_sign(float x)422 util_inf_sign(float x)
423 {
424 union fi tmp;
425 tmp.f = x;
426 if ((tmp.ui & 0x7fffffff) != 0x7f800000) {
427 return 0;
428 }
429
430 return (x < 0) ? -1 : 1;
431 }
432
433
434 /**
435 * Double-float
436 */
437 static inline boolean
util_is_double_inf_or_nan(double x)438 util_is_double_inf_or_nan(double x)
439 {
440 union di tmp;
441 tmp.d = x;
442 return (tmp.ui & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL;
443 }
444
445
446 static inline boolean
util_is_double_nan(double x)447 util_is_double_nan(double x)
448 {
449 union di tmp;
450 tmp.d = x;
451 return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL;
452 }
453
454
455 static inline int
util_double_inf_sign(double x)456 util_double_inf_sign(double x)
457 {
458 union di tmp;
459 tmp.d = x;
460 if ((tmp.ui & 0x7fffffffffffffffULL) != 0x7ff0000000000000ULL) {
461 return 0;
462 }
463
464 return (x < 0) ? -1 : 1;
465 }
466
467
468 /**
469 * Half-float
470 */
471 static inline boolean
util_is_half_inf_or_nan(int16_t x)472 util_is_half_inf_or_nan(int16_t x)
473 {
474 return (x & 0x7c00) == 0x7c00;
475 }
476
477
478 static inline boolean
util_is_half_nan(int16_t x)479 util_is_half_nan(int16_t x)
480 {
481 return (x & 0x7fff) > 0x7c00;
482 }
483
484
485 static inline int
util_half_inf_sign(int16_t x)486 util_half_inf_sign(int16_t x)
487 {
488 if ((x & 0x7fff) != 0x7c00) {
489 return 0;
490 }
491
492 return (x < 0) ? -1 : 1;
493 }
494
495
496 /**
497 * Find first bit set in word. Least significant bit is 1.
498 * Return 0 if no bits set.
499 */
500 #ifndef FFS_DEFINED
501 #define FFS_DEFINED 1
502
503 #if defined(_MSC_VER) && _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64)
504 unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask);
505 #pragma intrinsic(_BitScanForward)
506 static inline
ffs(unsigned long u)507 unsigned long ffs( unsigned long u )
508 {
509 unsigned long i;
510 if (_BitScanForward(&i, u))
511 return i + 1;
512 else
513 return 0;
514 }
515 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
516 static inline
ffs(unsigned u)517 unsigned ffs( unsigned u )
518 {
519 unsigned i;
520
521 if (u == 0) {
522 return 0;
523 }
524
525 __asm bsf eax, [u]
526 __asm inc eax
527 __asm mov [i], eax
528
529 return i;
530 }
531 #elif defined(__MINGW32__) || defined(PIPE_OS_ANDROID)
532 #define ffs __builtin_ffs
533 #endif
534
535 #endif /* FFS_DEFINED */
536
537 /**
538 * Find last bit set in a word. The least significant bit is 1.
539 * Return 0 if no bits are set.
540 */
util_last_bit(unsigned u)541 static inline unsigned util_last_bit(unsigned u)
542 {
543 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304)
544 return u == 0 ? 0 : 32 - __builtin_clz(u);
545 #else
546 unsigned r = 0;
547 while (u) {
548 r++;
549 u >>= 1;
550 }
551 return r;
552 #endif
553 }
554
555 /**
556 * Find last bit in a word that does not match the sign bit. The least
557 * significant bit is 1.
558 * Return 0 if no bits are set.
559 */
util_last_bit_signed(int i)560 static inline unsigned util_last_bit_signed(int i)
561 {
562 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 407)
563 return 31 - __builtin_clrsb(i);
564 #else
565 if (i >= 0)
566 return util_last_bit(i);
567 else
568 return util_last_bit(~(unsigned)i);
569 #endif
570 }
571
572 /* Destructively loop over all of the bits in a mask as in:
573 *
574 * while (mymask) {
575 * int i = u_bit_scan(&mymask);
576 * ... process element i
577 * }
578 *
579 */
u_bit_scan(unsigned * mask)580 static inline int u_bit_scan(unsigned *mask)
581 {
582 int i = ffs(*mask) - 1;
583 *mask &= ~(1 << i);
584 return i;
585 }
586
587 /* For looping over a bitmask when you want to loop over consecutive bits
588 * manually, for example:
589 *
590 * while (mask) {
591 * int start, count, i;
592 *
593 * u_bit_scan_consecutive_range(&mask, &start, &count);
594 *
595 * for (i = 0; i < count; i++)
596 * ... process element (start+i)
597 * }
598 */
599 static inline void
u_bit_scan_consecutive_range(unsigned * mask,int * start,int * count)600 u_bit_scan_consecutive_range(unsigned *mask, int *start, int *count)
601 {
602 if (*mask == 0xffffffff) {
603 *start = 0;
604 *count = 32;
605 *mask = 0;
606 return;
607 }
608 *start = ffs(*mask) - 1;
609 *count = ffs(~(*mask >> *start)) - 1;
610 *mask &= ~(((1u << *count) - 1) << *start);
611 }
612
613 /**
614 * Return float bits.
615 */
616 static inline unsigned
fui(float f)617 fui( float f )
618 {
619 union fi fi;
620 fi.f = f;
621 return fi.ui;
622 }
623
624
625 /**
626 * Convert ubyte to float in [0, 1].
627 * XXX a 256-entry lookup table would be slightly faster.
628 */
629 static inline float
ubyte_to_float(ubyte ub)630 ubyte_to_float(ubyte ub)
631 {
632 return (float) ub * (1.0f / 255.0f);
633 }
634
635
636 /**
637 * Convert float in [0,1] to ubyte in [0,255] with clamping.
638 */
639 static inline ubyte
float_to_ubyte(float f)640 float_to_ubyte(float f)
641 {
642 union fi tmp;
643
644 tmp.f = f;
645 if (tmp.i < 0) {
646 return (ubyte) 0;
647 }
648 else if (tmp.i >= 0x3f800000 /* 1.0f */) {
649 return (ubyte) 255;
650 }
651 else {
652 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
653 return (ubyte) tmp.i;
654 }
655 }
656
657 static inline float
byte_to_float_tex(int8_t b)658 byte_to_float_tex(int8_t b)
659 {
660 return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
661 }
662
663 static inline int8_t
float_to_byte_tex(float f)664 float_to_byte_tex(float f)
665 {
666 return (int8_t) (127.0F * f);
667 }
668
669 /**
670 * Calc log base 2
671 */
672 static inline unsigned
util_logbase2(unsigned n)673 util_logbase2(unsigned n)
674 {
675 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
676 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
677 #else
678 unsigned pos = 0;
679 if (n >= 1<<16) { n >>= 16; pos += 16; }
680 if (n >= 1<< 8) { n >>= 8; pos += 8; }
681 if (n >= 1<< 4) { n >>= 4; pos += 4; }
682 if (n >= 1<< 2) { n >>= 2; pos += 2; }
683 if (n >= 1<< 1) { pos += 1; }
684 return pos;
685 #endif
686 }
687
688
689 /**
690 * Returns the smallest power of two >= x
691 */
692 static inline unsigned
util_next_power_of_two(unsigned x)693 util_next_power_of_two(unsigned x)
694 {
695 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
696 if (x <= 1)
697 return 1;
698
699 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
700 #else
701 unsigned val = x;
702
703 if (x <= 1)
704 return 1;
705
706 if (util_is_power_of_two(x))
707 return x;
708
709 val--;
710 val = (val >> 1) | val;
711 val = (val >> 2) | val;
712 val = (val >> 4) | val;
713 val = (val >> 8) | val;
714 val = (val >> 16) | val;
715 val++;
716 return val;
717 #endif
718 }
719
720
721 /**
722 * Return number of bits set in n.
723 */
724 static inline unsigned
util_bitcount(unsigned n)725 util_bitcount(unsigned n)
726 {
727 #if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304)
728 return __builtin_popcount(n);
729 #else
730 /* K&R classic bitcount.
731 *
732 * For each iteration, clear the LSB from the bitfield.
733 * Requires only one iteration per set bit, instead of
734 * one iteration per bit less than highest set bit.
735 */
736 unsigned bits = 0;
737 for (bits; n; bits++) {
738 n &= n - 1;
739 }
740 return bits;
741 #endif
742 }
743
744 /**
745 * Reverse bits in n
746 * Algorithm taken from:
747 * http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
748 */
749 static inline unsigned
util_bitreverse(unsigned n)750 util_bitreverse(unsigned n)
751 {
752 n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
753 n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
754 n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4);
755 n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8);
756 n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16);
757 return n;
758 }
759
760 /**
761 * Convert from little endian to CPU byte order.
762 */
763
764 #ifdef PIPE_ARCH_BIG_ENDIAN
765 #define util_le64_to_cpu(x) util_bswap64(x)
766 #define util_le32_to_cpu(x) util_bswap32(x)
767 #define util_le16_to_cpu(x) util_bswap16(x)
768 #else
769 #define util_le64_to_cpu(x) (x)
770 #define util_le32_to_cpu(x) (x)
771 #define util_le16_to_cpu(x) (x)
772 #endif
773
774 #define util_cpu_to_le64(x) util_le64_to_cpu(x)
775 #define util_cpu_to_le32(x) util_le32_to_cpu(x)
776 #define util_cpu_to_le16(x) util_le16_to_cpu(x)
777
778 /**
779 * Reverse byte order of a 32 bit word.
780 */
781 static inline uint32_t
util_bswap32(uint32_t n)782 util_bswap32(uint32_t n)
783 {
784 /* We need the gcc version checks for non-autoconf build system */
785 #if defined(HAVE___BUILTIN_BSWAP32) || (defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403))
786 return __builtin_bswap32(n);
787 #else
788 return (n >> 24) |
789 ((n >> 8) & 0x0000ff00) |
790 ((n << 8) & 0x00ff0000) |
791 (n << 24);
792 #endif
793 }
794
795 /**
796 * Reverse byte order of a 64bit word.
797 */
798 static inline uint64_t
util_bswap64(uint64_t n)799 util_bswap64(uint64_t n)
800 {
801 #if defined(HAVE___BUILTIN_BSWAP64)
802 return __builtin_bswap64(n);
803 #else
804 return ((uint64_t)util_bswap32(n) << 32) |
805 util_bswap32((n >> 32));
806 #endif
807 }
808
809
810 /**
811 * Reverse byte order of a 16 bit word.
812 */
813 static inline uint16_t
util_bswap16(uint16_t n)814 util_bswap16(uint16_t n)
815 {
816 return (n >> 8) |
817 (n << 8);
818 }
819
820
821 /**
822 * Clamp X to [MIN, MAX].
823 * This is a macro to allow float, int, uint, etc. types.
824 */
825 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
826
827 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
828 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
829
830 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
831 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
832
833 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
834 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
835
836
837 /**
838 * Align a value, only works pot alignemnts.
839 */
840 static inline int
align(int value,int alignment)841 align(int value, int alignment)
842 {
843 return (value + alignment - 1) & ~(alignment - 1);
844 }
845
846 /**
847 * Works like align but on npot alignments.
848 */
849 static inline size_t
util_align_npot(size_t value,size_t alignment)850 util_align_npot(size_t value, size_t alignment)
851 {
852 if (value % alignment)
853 return value + (alignment - (value % alignment));
854 return value;
855 }
856
857 static inline unsigned
u_minify(unsigned value,unsigned levels)858 u_minify(unsigned value, unsigned levels)
859 {
860 return MAX2(1, value >> levels);
861 }
862
863 #ifndef COPY_4V
864 #define COPY_4V( DST, SRC ) \
865 do { \
866 (DST)[0] = (SRC)[0]; \
867 (DST)[1] = (SRC)[1]; \
868 (DST)[2] = (SRC)[2]; \
869 (DST)[3] = (SRC)[3]; \
870 } while (0)
871 #endif
872
873
874 #ifndef COPY_4FV
875 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
876 #endif
877
878
879 #ifndef ASSIGN_4V
880 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
881 do { \
882 (DST)[0] = (V0); \
883 (DST)[1] = (V1); \
884 (DST)[2] = (V2); \
885 (DST)[3] = (V3); \
886 } while (0)
887 #endif
888
889
util_unsigned_fixed(float value,unsigned frac_bits)890 static inline uint32_t util_unsigned_fixed(float value, unsigned frac_bits)
891 {
892 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
893 }
894
util_signed_fixed(float value,unsigned frac_bits)895 static inline int32_t util_signed_fixed(float value, unsigned frac_bits)
896 {
897 return (int32_t)(value * (1<<frac_bits));
898 }
899
900 unsigned
901 util_fpstate_get(void);
902 unsigned
903 util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
904 void
905 util_fpstate_set(unsigned fpstate);
906
907
908
909 #ifdef __cplusplus
910 }
911 #endif
912
913 #endif /* U_MATH_H */
914