gallium/util: Define ffsll on MinGW.
[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 #include "util/u_debug.h"
44
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50
51 #include <math.h>
52 #include <float.h>
53 #include <stdarg.h>
54
55 #ifdef PIPE_OS_UNIX
56 #include <strings.h> /* for ffs */
57 #endif
58
59
60 #ifndef M_SQRT2
61 #define M_SQRT2 1.41421356237309504880
62 #endif
63
64
65 #if defined(_MSC_VER)
66
67 #if _MSC_VER < 1400 && !defined(__cplusplus)
68
69 static INLINE float cosf( float f )
70 {
71 return (float) cos( (double) f );
72 }
73
74 static INLINE float sinf( float f )
75 {
76 return (float) sin( (double) f );
77 }
78
79 static INLINE float ceilf( float f )
80 {
81 return (float) ceil( (double) f );
82 }
83
84 static INLINE float floorf( float f )
85 {
86 return (float) floor( (double) f );
87 }
88
89 static INLINE float powf( float f, float g )
90 {
91 return (float) pow( (double) f, (double) g );
92 }
93
94 static INLINE float sqrtf( float f )
95 {
96 return (float) sqrt( (double) f );
97 }
98
99 static INLINE float fabsf( float f )
100 {
101 return (float) fabs( (double) f );
102 }
103
104 static INLINE float logf( float f )
105 {
106 return (float) log( (double) f );
107 }
108
109 #else
110 /* Work-around an extra semi-colon in VS 2005 logf definition */
111 #ifdef logf
112 #undef logf
113 #define logf(x) ((float)log((double)(x)))
114 #endif /* logf */
115
116 #if _MSC_VER < 1800
117 #define isfinite(x) _finite((double)(x))
118 #define isnan(x) _isnan((double)(x))
119 #endif /* _MSC_VER < 1800 */
120 #endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
121
122 #if _MSC_VER < 1800
123 static INLINE double log2( double x )
124 {
125 const double invln2 = 1.442695041;
126 return log( x ) * invln2;
127 }
128
129 static INLINE double
130 round(double x)
131 {
132 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
133 }
134
135 static INLINE float
136 roundf(float x)
137 {
138 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
139 }
140 #endif
141
142 #ifndef INFINITY
143 #define INFINITY (DBL_MAX + DBL_MAX)
144 #endif
145
146 #ifndef NAN
147 #define NAN (INFINITY - INFINITY)
148 #endif
149
150 #endif /* _MSC_VER */
151
152
153 #if __STDC_VERSION__ < 199901L && (!defined(__cplusplus) || defined(_MSC_VER))
154 static INLINE long int
155 lrint(double d)
156 {
157 long int rounded = (long int)(d + 0.5);
158
159 if (d - floor(d) == 0.5) {
160 if (rounded % 2 != 0)
161 rounded += (d > 0) ? -1 : 1;
162 }
163
164 return rounded;
165 }
166
167 static INLINE long int
168 lrintf(float f)
169 {
170 long int rounded = (long int)(f + 0.5f);
171
172 if (f - floorf(f) == 0.5f) {
173 if (rounded % 2 != 0)
174 rounded += (f > 0) ? -1 : 1;
175 }
176
177 return rounded;
178 }
179
180 static INLINE long long int
181 llrint(double d)
182 {
183 long long int rounded = (long long int)(d + 0.5);
184
185 if (d - floor(d) == 0.5) {
186 if (rounded % 2 != 0)
187 rounded += (d > 0) ? -1 : 1;
188 }
189
190 return rounded;
191 }
192
193 static INLINE long long int
194 llrintf(float f)
195 {
196 long long int rounded = (long long int)(f + 0.5f);
197
198 if (f - floorf(f) == 0.5f) {
199 if (rounded % 2 != 0)
200 rounded += (f > 0) ? -1 : 1;
201 }
202
203 return rounded;
204 }
205 #endif /* C99 */
206
207 #define POW2_TABLE_SIZE_LOG2 9
208 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
209 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
210 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
211 extern float pow2_table[POW2_TABLE_SIZE];
212
213
214 /**
215 * Initialize math module. This should be called before using any
216 * other functions in this module.
217 */
218 extern void
219 util_init_math(void);
220
221
222 union fi {
223 float f;
224 int32_t i;
225 uint32_t ui;
226 };
227
228
229 union di {
230 double d;
231 int64_t i;
232 uint64_t ui;
233 };
234
235
236 /**
237 * Extract the IEEE float32 exponent.
238 */
239 static INLINE signed
240 util_get_float32_exponent(float x)
241 {
242 union fi f;
243
244 f.f = x;
245
246 return ((f.ui >> 23) & 0xff) - 127;
247 }
248
249
250 /**
251 * Fast version of 2^x
252 * Identity: exp2(a + b) = exp2(a) * exp2(b)
253 * Let ipart = int(x)
254 * Let fpart = x - ipart;
255 * So, exp2(x) = exp2(ipart) * exp2(fpart)
256 * Compute exp2(ipart) with i << ipart
257 * Compute exp2(fpart) with lookup table.
258 */
259 static INLINE float
260 util_fast_exp2(float x)
261 {
262 int32_t ipart;
263 float fpart, mpart;
264 union fi epart;
265
266 if(x > 129.00000f)
267 return 3.402823466e+38f;
268
269 if (x < -126.99999f)
270 return 0.0f;
271
272 ipart = (int32_t) x;
273 fpart = x - (float) ipart;
274
275 /* same as
276 * epart.f = (float) (1 << ipart)
277 * but faster and without integer overflow for ipart > 31
278 */
279 epart.i = (ipart + 127 ) << 23;
280
281 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
282
283 return epart.f * mpart;
284 }
285
286
287 /**
288 * Fast approximation to exp(x).
289 */
290 static INLINE float
291 util_fast_exp(float x)
292 {
293 const float k = 1.44269f; /* = log2(e) */
294 return util_fast_exp2(k * x);
295 }
296
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
329 /* Note that this counts zero as a power of two.
330 */
331 static INLINE boolean
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
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
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
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
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
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
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
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
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
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
472 util_is_half_inf_or_nan(int16_t x)
473 {
474 return (x & 0x7c00) == 0x7c00;
475 }
476
477
478 static INLINE boolean
479 util_is_half_nan(int16_t x)
480 {
481 return (x & 0x7fff) > 0x7c00;
482 }
483
484
485 static INLINE int
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
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
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 #define ffsll __builtin_ffsll
534 #endif
535
536 #endif /* FFS_DEFINED */
537
538 /**
539 * Find last bit set in a word. The least significant bit is 1.
540 * Return 0 if no bits are set.
541 */
542 static INLINE unsigned
543 util_last_bit(unsigned u)
544 {
545 #if defined(__GNUC__)
546 return u == 0 ? 0 : 32 - __builtin_clz(u);
547 #else
548 unsigned r = 0;
549 while (u) {
550 r++;
551 u >>= 1;
552 }
553 return r;
554 #endif
555 }
556
557 /**
558 * Find last bit in a word that does not match the sign bit. The least
559 * significant bit is 1.
560 * Return 0 if no bits are set.
561 */
562 static INLINE unsigned
563 util_last_bit_signed(int i)
564 {
565 if (i >= 0)
566 return util_last_bit(i);
567 else
568 return util_last_bit(~(unsigned)i);
569 }
570
571 /* Destructively loop over all of the bits in a mask as in:
572 *
573 * while (mymask) {
574 * int i = u_bit_scan(&mymask);
575 * ... process element i
576 * }
577 *
578 */
579 static INLINE int
580 u_bit_scan(unsigned *mask)
581 {
582 int i = ffs(*mask) - 1;
583 *mask &= ~(1 << i);
584 return i;
585 }
586
587 static INLINE int
588 u_bit_scan64(uint64_t *mask)
589 {
590 int i = ffsll(*mask) - 1;
591 *mask &= ~(1llu << i);
592 return i;
593 }
594
595 /**
596 * Return float bits.
597 */
598 static INLINE unsigned
599 fui( float f )
600 {
601 union fi fi;
602 fi.f = f;
603 return fi.ui;
604 }
605
606 static INLINE float
607 uif(uint32_t ui)
608 {
609 union fi fi;
610 fi.ui = ui;
611 return fi.f;
612 }
613
614
615 /**
616 * Convert ubyte to float in [0, 1].
617 * XXX a 256-entry lookup table would be slightly faster.
618 */
619 static INLINE float
620 ubyte_to_float(ubyte ub)
621 {
622 return (float) ub * (1.0f / 255.0f);
623 }
624
625
626 /**
627 * Convert float in [0,1] to ubyte in [0,255] with clamping.
628 */
629 static INLINE ubyte
630 float_to_ubyte(float f)
631 {
632 union fi tmp;
633
634 tmp.f = f;
635 if (tmp.i < 0) {
636 return (ubyte) 0;
637 }
638 else if (tmp.i >= 0x3f800000 /* 1.0f */) {
639 return (ubyte) 255;
640 }
641 else {
642 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
643 return (ubyte) tmp.i;
644 }
645 }
646
647 static INLINE float
648 byte_to_float_tex(int8_t b)
649 {
650 return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
651 }
652
653 static INLINE int8_t
654 float_to_byte_tex(float f)
655 {
656 return (int8_t) (127.0F * f);
657 }
658
659 /**
660 * Calc log base 2
661 */
662 static INLINE unsigned
663 util_logbase2(unsigned n)
664 {
665 #if defined(PIPE_CC_GCC)
666 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
667 #else
668 unsigned pos = 0;
669 if (n >= 1<<16) { n >>= 16; pos += 16; }
670 if (n >= 1<< 8) { n >>= 8; pos += 8; }
671 if (n >= 1<< 4) { n >>= 4; pos += 4; }
672 if (n >= 1<< 2) { n >>= 2; pos += 2; }
673 if (n >= 1<< 1) { pos += 1; }
674 return pos;
675 #endif
676 }
677
678
679 /**
680 * Returns the smallest power of two >= x
681 */
682 static INLINE unsigned
683 util_next_power_of_two(unsigned x)
684 {
685 #if defined(PIPE_CC_GCC)
686 if (x <= 1)
687 return 1;
688
689 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
690 #else
691 unsigned val = x;
692
693 if (x <= 1)
694 return 1;
695
696 if (util_is_power_of_two(x))
697 return x;
698
699 val--;
700 val = (val >> 1) | val;
701 val = (val >> 2) | val;
702 val = (val >> 4) | val;
703 val = (val >> 8) | val;
704 val = (val >> 16) | val;
705 val++;
706 return val;
707 #endif
708 }
709
710
711 /**
712 * Return number of bits set in n.
713 */
714 static INLINE unsigned
715 util_bitcount(unsigned n)
716 {
717 #if defined(PIPE_CC_GCC)
718 return __builtin_popcount(n);
719 #else
720 /* K&R classic bitcount.
721 *
722 * For each iteration, clear the LSB from the bitfield.
723 * Requires only one iteration per set bit, instead of
724 * one iteration per bit less than highest set bit.
725 */
726 unsigned bits = 0;
727 for (bits; n; bits++) {
728 n &= n - 1;
729 }
730 return bits;
731 #endif
732 }
733
734
735 static INLINE unsigned
736 util_bitcount64(uint64_t n)
737 {
738 #ifdef HAVE___BUILTIN_POPCOUNTLL
739 return __builtin_popcountll(n);
740 #else
741 return util_bitcount(n) + util_bitcount(n >> 32);
742 #endif
743 }
744
745
746 /**
747 * Reverse bits in n
748 * Algorithm taken from:
749 * http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
750 */
751 static INLINE unsigned
752 util_bitreverse(unsigned n)
753 {
754 n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
755 n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
756 n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4);
757 n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8);
758 n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16);
759 return n;
760 }
761
762 /**
763 * Convert from little endian to CPU byte order.
764 */
765
766 #ifdef PIPE_ARCH_BIG_ENDIAN
767 #define util_le64_to_cpu(x) util_bswap64(x)
768 #define util_le32_to_cpu(x) util_bswap32(x)
769 #define util_le16_to_cpu(x) util_bswap16(x)
770 #else
771 #define util_le64_to_cpu(x) (x)
772 #define util_le32_to_cpu(x) (x)
773 #define util_le16_to_cpu(x) (x)
774 #endif
775
776 #define util_cpu_to_le64(x) util_le64_to_cpu(x)
777 #define util_cpu_to_le32(x) util_le32_to_cpu(x)
778 #define util_cpu_to_le16(x) util_le16_to_cpu(x)
779
780 /**
781 * Reverse byte order of a 32 bit word.
782 */
783 static INLINE uint32_t
784 util_bswap32(uint32_t n)
785 {
786 /* We need the gcc version checks for non-autoconf build system */
787 #if defined(HAVE___BUILTIN_BSWAP32) || (defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403))
788 return __builtin_bswap32(n);
789 #else
790 return (n >> 24) |
791 ((n >> 8) & 0x0000ff00) |
792 ((n << 8) & 0x00ff0000) |
793 (n << 24);
794 #endif
795 }
796
797 /**
798 * Reverse byte order of a 64bit word.
799 */
800 static INLINE uint64_t
801 util_bswap64(uint64_t n)
802 {
803 #if defined(HAVE___BUILTIN_BSWAP64)
804 return __builtin_bswap64(n);
805 #else
806 return ((uint64_t)util_bswap32((uint32_t)n) << 32) |
807 util_bswap32((n >> 32));
808 #endif
809 }
810
811
812 /**
813 * Reverse byte order of a 16 bit word.
814 */
815 static INLINE uint16_t
816 util_bswap16(uint16_t n)
817 {
818 return (n >> 8) |
819 (n << 8);
820 }
821
822 static INLINE void*
823 util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
824 {
825 #ifdef PIPE_ARCH_BIG_ENDIAN
826 size_t i, e;
827 assert(n % 4 == 0);
828
829 for (i = 0, e = n / 4; i < e; i++) {
830 uint32_t * restrict d = (uint32_t* restrict)dest;
831 const uint32_t * restrict s = (const uint32_t* restrict)src;
832 d[i] = util_bswap32(s[i]);
833 }
834 return dest;
835 #else
836 return memcpy(dest, src, n);
837 #endif
838 }
839
840 /**
841 * Clamp X to [MIN, MAX].
842 * This is a macro to allow float, int, uint, etc. types.
843 */
844 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
845
846 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
847 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
848
849 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
850 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
851
852 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
853 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
854
855
856 /**
857 * Align a value, only works pot alignemnts.
858 */
859 static INLINE int
860 align(int value, int alignment)
861 {
862 return (value + alignment - 1) & ~(alignment - 1);
863 }
864
865 /**
866 * Works like align but on npot alignments.
867 */
868 static INLINE size_t
869 util_align_npot(size_t value, size_t alignment)
870 {
871 if (value % alignment)
872 return value + (alignment - (value % alignment));
873 return value;
874 }
875
876 static INLINE unsigned
877 u_minify(unsigned value, unsigned levels)
878 {
879 return MAX2(1, value >> levels);
880 }
881
882 #ifndef COPY_4V
883 #define COPY_4V( DST, SRC ) \
884 do { \
885 (DST)[0] = (SRC)[0]; \
886 (DST)[1] = (SRC)[1]; \
887 (DST)[2] = (SRC)[2]; \
888 (DST)[3] = (SRC)[3]; \
889 } while (0)
890 #endif
891
892
893 #ifndef COPY_4FV
894 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
895 #endif
896
897
898 #ifndef ASSIGN_4V
899 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
900 do { \
901 (DST)[0] = (V0); \
902 (DST)[1] = (V1); \
903 (DST)[2] = (V2); \
904 (DST)[3] = (V3); \
905 } while (0)
906 #endif
907
908
909 static INLINE uint32_t
910 util_unsigned_fixed(float value, unsigned frac_bits)
911 {
912 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
913 }
914
915 static INLINE int32_t
916 util_signed_fixed(float value, unsigned frac_bits)
917 {
918 return (int32_t)(value * (1<<frac_bits));
919 }
920
921 unsigned
922 util_fpstate_get(void);
923 unsigned
924 util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
925 void
926 util_fpstate_set(unsigned fpstate);
927
928
929
930 #ifdef __cplusplus
931 }
932 #endif
933
934 #endif /* U_MATH_H */