OSDN Git Service

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