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