• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "c99_compat.h"
43 #include <assert.h>
44 #include <float.h>
45 #include <stdarg.h>
46 #include <math.h>
47 
48 #include "bitscan.h"
49 #include "u_endian.h" /* for UTIL_ARCH_BIG_ENDIAN */
50 #include "util/detect_cc.h"
51 #include "util/detect_arch.h"
52 #include "util/macros.h"
53 
54 #ifdef __HAIKU__
55 #include <sys/param.h>
56 #undef ALIGN
57 #endif
58 
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
63 
64 #ifndef M_SQRT2
65 #define M_SQRT2 1.41421356237309504880
66 #endif
67 
68 
69 /**
70  * Initialize math module.  This should be called before using any
71  * other functions in this module.
72  */
73 extern void
74 util_init_math(void);
75 
76 
77 union fi {
78    float f;
79    int32_t i;
80    uint32_t ui;
81 };
82 
83 
84 union di {
85    double d;
86    int64_t i;
87    uint64_t ui;
88 };
89 
90 
91 /**
92  * Extract the IEEE float32 exponent.
93  */
94 static inline signed
util_get_float32_exponent(float x)95 util_get_float32_exponent(float x)
96 {
97    union fi f;
98 
99    f.f = x;
100 
101    return ((f.ui >> 23) & 0xff) - 127;
102 }
103 
104 
105 #define LOG2_TABLE_SIZE_LOG2 8
106 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
107 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
108 extern float log2_table[LOG2_TABLE_SIZE];
109 
110 
111 /**
112  * Fast approximation to log2(x).
113  */
114 static inline float
util_fast_log2(float x)115 util_fast_log2(float x)
116 {
117    union fi num;
118    float epart, mpart;
119    num.f = x;
120    epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
121    /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
122    mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
123    return epart + mpart;
124 }
125 
126 
127 /**
128  * Floor(x), returned as int.
129  */
130 static inline int
util_ifloor(float f)131 util_ifloor(float f)
132 {
133 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
134    /*
135     * IEEE floor for computers that round to nearest or even.
136     * 'f' must be between -4194304 and 4194303.
137     * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
138     * but uses some IEEE specific tricks for better speed.
139     * Contributed by Josh Vanderhoof
140     */
141    int ai, bi;
142    double af, bf;
143    af = (3 << 22) + 0.5 + (double)f;
144    bf = (3 << 22) + 0.5 - (double)f;
145    /* GCC generates an extra fstp/fld without this. */
146    __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
147    __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
148    return (ai - bi) >> 1;
149 #else
150    int ai, bi;
151    double af, bf;
152    union fi u;
153    af = (3 << 22) + 0.5 + (double) f;
154    bf = (3 << 22) + 0.5 - (double) f;
155    u.f = (float) af;  ai = u.i;
156    u.f = (float) bf;  bi = u.i;
157    return (ai - bi) >> 1;
158 #endif
159 }
160 
161 
162 /**
163  * Round float to nearest int.
164  * the range of f should be [INT_MIN, INT_MAX]
165  */
166 static inline int
util_iround(float f)167 util_iround(float f)
168 {
169    return (int)lrintf(f);
170 }
171 
172 
173 /**
174  * Approximate floating point comparison
175  */
176 static inline bool
util_is_approx(float a,float b,float tol)177 util_is_approx(float a, float b, float tol)
178 {
179    return fabsf(b - a) <= tol;
180 }
181 
182 
183 /**
184  * util_is_X_inf_or_nan = test if x is NaN or +/- Inf
185  * util_is_X_nan        = test if x is NaN
186  * util_X_inf_sign      = return +1 for +Inf, -1 for -Inf, or 0 for not Inf
187  *
188  * NaN can be checked with x != x, however this fails with the fast math flag
189  **/
190 
191 
192 /**
193  * Single-float
194  */
195 static inline bool
util_is_inf_or_nan(float x)196 util_is_inf_or_nan(float x)
197 {
198    union fi tmp;
199    tmp.f = x;
200    return (tmp.ui & 0x7f800000) == 0x7f800000;
201 }
202 
203 
204 static inline bool
util_is_nan(float x)205 util_is_nan(float x)
206 {
207    union fi tmp;
208    tmp.f = x;
209    return (tmp.ui & 0x7fffffff) > 0x7f800000;
210 }
211 
212 
213 static inline int
util_inf_sign(float x)214 util_inf_sign(float x)
215 {
216    union fi tmp;
217    tmp.f = x;
218    if ((tmp.ui & 0x7fffffff) != 0x7f800000) {
219       return 0;
220    }
221 
222    return (x < 0) ? -1 : 1;
223 }
224 
225 
226 /**
227  * Double-float
228  */
229 static inline bool
util_is_double_inf_or_nan(double x)230 util_is_double_inf_or_nan(double x)
231 {
232    union di tmp;
233    tmp.d = x;
234    return (tmp.ui & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL;
235 }
236 
237 
238 static inline bool
util_is_double_nan(double x)239 util_is_double_nan(double x)
240 {
241    union di tmp;
242    tmp.d = x;
243    return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL;
244 }
245 
246 
247 static inline int
util_double_inf_sign(double x)248 util_double_inf_sign(double x)
249 {
250    union di tmp;
251    tmp.d = x;
252    if ((tmp.ui & 0x7fffffffffffffffULL) != 0x7ff0000000000000ULL) {
253       return 0;
254    }
255 
256    return (x < 0) ? -1 : 1;
257 }
258 
259 
260 /**
261  * Half-float
262  */
263 static inline bool
util_is_half_inf_or_nan(int16_t x)264 util_is_half_inf_or_nan(int16_t x)
265 {
266    return (x & 0x7c00) == 0x7c00;
267 }
268 
269 
270 static inline bool
util_is_half_nan(int16_t x)271 util_is_half_nan(int16_t x)
272 {
273    return (x & 0x7fff) > 0x7c00;
274 }
275 
276 
277 static inline int
util_half_inf_sign(int16_t x)278 util_half_inf_sign(int16_t x)
279 {
280    if ((x & 0x7fff) != 0x7c00) {
281       return 0;
282    }
283 
284    return (x < 0) ? -1 : 1;
285 }
286 
287 
288 /**
289  * Return float bits.
290  */
291 static inline unsigned
fui(float f)292 fui( float f )
293 {
294    union fi fi;
295    fi.f = f;
296    return fi.ui;
297 }
298 
299 static inline float
uif(uint32_t ui)300 uif(uint32_t ui)
301 {
302    union fi fi;
303    fi.ui = ui;
304    return fi.f;
305 }
306 
307 
308 /**
309  * Convert uint8_t to float in [0, 1].
310  */
311 static inline float
ubyte_to_float(uint8_t ub)312 ubyte_to_float(uint8_t ub)
313 {
314    return (float) ub * (1.0f / 255.0f);
315 }
316 
317 
318 /**
319  * Convert float in [0,1] to uint8_t in [0,255] with clamping.
320  */
321 static inline uint8_t
float_to_ubyte(float f)322 float_to_ubyte(float f)
323 {
324    /* return 0 for NaN too */
325    if (!(f > 0.0f)) {
326       return (uint8_t) 0;
327    }
328    else if (f >= 1.0f) {
329       return (uint8_t) 255;
330    }
331    else {
332       union fi tmp;
333       tmp.f = f;
334       tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
335       return (uint8_t) tmp.i;
336    }
337 }
338 
339 /**
340  * Convert uint16_t to float in [0, 1].
341  */
342 static inline float
ushort_to_float(uint16_t us)343 ushort_to_float(uint16_t us)
344 {
345    return (float) us * (1.0f / 65535.0f);
346 }
347 
348 
349 /**
350  * Convert float in [0,1] to uint16_t in [0,65535] with clamping.
351  */
352 static inline uint16_t
float_to_ushort(float f)353 float_to_ushort(float f)
354 {
355    /* return 0 for NaN too */
356    if (!(f > 0.0f)) {
357       return (uint16_t) 0;
358    }
359    else if (f >= 1.0f) {
360       return (uint16_t) 65535;
361    }
362    else {
363       union fi tmp;
364       tmp.f = f;
365       tmp.f = tmp.f * (65535.0f/65536.0f) + 128.0f;
366       return (uint16_t) tmp.i;
367    }
368 }
369 
370 static inline float
byte_to_float_tex(int8_t b)371 byte_to_float_tex(int8_t b)
372 {
373    return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
374 }
375 
376 static inline int8_t
float_to_byte_tex(float f)377 float_to_byte_tex(float f)
378 {
379    return (int8_t) (127.0F * f);
380 }
381 
382 /**
383  * Calc log base 2
384  */
385 static inline unsigned
util_logbase2(unsigned n)386 util_logbase2(unsigned n)
387 {
388 #if defined(HAVE___BUILTIN_CLZ)
389    return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
390 #else
391    unsigned pos = 0;
392    if (n >= 1<<16) { n >>= 16; pos += 16; }
393    if (n >= 1<< 8) { n >>=  8; pos +=  8; }
394    if (n >= 1<< 4) { n >>=  4; pos +=  4; }
395    if (n >= 1<< 2) { n >>=  2; pos +=  2; }
396    if (n >= 1<< 1) {           pos +=  1; }
397    return pos;
398 #endif
399 }
400 
401 static inline uint64_t
util_logbase2_64(uint64_t n)402 util_logbase2_64(uint64_t n)
403 {
404 #if defined(HAVE___BUILTIN_CLZLL)
405    return ((sizeof(uint64_t) * 8 - 1) - __builtin_clzll(n | 1));
406 #else
407    uint64_t pos = 0ull;
408    if (n >= 1ull<<32) { n >>= 32; pos += 32; }
409    if (n >= 1ull<<16) { n >>= 16; pos += 16; }
410    if (n >= 1ull<< 8) { n >>=  8; pos +=  8; }
411    if (n >= 1ull<< 4) { n >>=  4; pos +=  4; }
412    if (n >= 1ull<< 2) { n >>=  2; pos +=  2; }
413    if (n >= 1ull<< 1) {           pos +=  1; }
414    return pos;
415 #endif
416 }
417 
418 /**
419  * Returns the ceiling of log n base 2, and 0 when n == 0. Equivalently,
420  * returns the smallest x such that n <= 2**x.
421  */
422 static inline unsigned
util_logbase2_ceil(unsigned n)423 util_logbase2_ceil(unsigned n)
424 {
425    if (n <= 1)
426       return 0;
427 
428    return 1 + util_logbase2(n - 1);
429 }
430 
431 static inline uint64_t
util_logbase2_ceil64(uint64_t n)432 util_logbase2_ceil64(uint64_t n)
433 {
434    if (n <= 1)
435       return 0;
436 
437    return 1ull + util_logbase2_64(n - 1);
438 }
439 
440 /**
441  * Returns the smallest power of two >= x
442  */
443 static inline unsigned
util_next_power_of_two(unsigned x)444 util_next_power_of_two(unsigned x)
445 {
446 #if defined(HAVE___BUILTIN_CLZ)
447    if (x <= 1)
448        return 1;
449 
450    return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
451 #else
452    unsigned val = x;
453 
454    if (x <= 1)
455       return 1;
456 
457    if (util_is_power_of_two_or_zero(x))
458       return x;
459 
460    val--;
461    val = (val >> 1) | val;
462    val = (val >> 2) | val;
463    val = (val >> 4) | val;
464    val = (val >> 8) | val;
465    val = (val >> 16) | val;
466    val++;
467    return val;
468 #endif
469 }
470 
471 static inline uint64_t
util_next_power_of_two64(uint64_t x)472 util_next_power_of_two64(uint64_t x)
473 {
474 #if defined(HAVE___BUILTIN_CLZLL)
475    if (x <= 1)
476        return 1;
477 
478    return (1ull << ((sizeof(uint64_t) * 8) - __builtin_clzll(x - 1)));
479 #else
480    uint64_t val = x;
481 
482    if (x <= 1)
483       return 1;
484 
485    if (util_is_power_of_two_or_zero64(x))
486       return x;
487 
488    val--;
489    val = (val >> 1)  | val;
490    val = (val >> 2)  | val;
491    val = (val >> 4)  | val;
492    val = (val >> 8)  | val;
493    val = (val >> 16) | val;
494    val = (val >> 32) | val;
495    val++;
496    return val;
497 #endif
498 }
499 
500 /**
501  * Reverse bits in n
502  * Algorithm taken from:
503  * http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
504  */
505 static inline unsigned
util_bitreverse(unsigned n)506 util_bitreverse(unsigned n)
507 {
508     n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
509     n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
510     n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4);
511     n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8);
512     n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16);
513     return n;
514 }
515 
516 /**
517  * Convert from little endian to CPU byte order.
518  */
519 
520 #if UTIL_ARCH_BIG_ENDIAN
521 #define util_le64_to_cpu(x) util_bswap64(x)
522 #define util_le32_to_cpu(x) util_bswap32(x)
523 #define util_le16_to_cpu(x) util_bswap16(x)
524 #else
525 #define util_le64_to_cpu(x) (x)
526 #define util_le32_to_cpu(x) (x)
527 #define util_le16_to_cpu(x) (x)
528 #endif
529 
530 #define util_cpu_to_le64(x) util_le64_to_cpu(x)
531 #define util_cpu_to_le32(x) util_le32_to_cpu(x)
532 #define util_cpu_to_le16(x) util_le16_to_cpu(x)
533 
534 /**
535  * Reverse byte order of a 32 bit word.
536  */
537 static inline uint32_t
util_bswap32(uint32_t n)538 util_bswap32(uint32_t n)
539 {
540 #if defined(HAVE___BUILTIN_BSWAP32)
541    return __builtin_bswap32(n);
542 #else
543    return (n >> 24) |
544           ((n >> 8) & 0x0000ff00) |
545           ((n << 8) & 0x00ff0000) |
546           (n << 24);
547 #endif
548 }
549 
550 /**
551  * Reverse byte order of a 64bit word.
552  */
553 static inline uint64_t
util_bswap64(uint64_t n)554 util_bswap64(uint64_t n)
555 {
556 #if defined(HAVE___BUILTIN_BSWAP64)
557    return __builtin_bswap64(n);
558 #else
559    return ((uint64_t)util_bswap32((uint32_t)n) << 32) |
560           util_bswap32((n >> 32));
561 #endif
562 }
563 
564 
565 /**
566  * Reverse byte order of a 16 bit word.
567  */
568 static inline uint16_t
util_bswap16(uint16_t n)569 util_bswap16(uint16_t n)
570 {
571    return (n >> 8) |
572           (n << 8);
573 }
574 
575 /**
576  * Mask and sign-extend a number
577  *
578  * The bit at position `width - 1` is replicated to all the higher bits.
579  * This makes no assumptions about the high bits of the value and will
580  * overwrite them with the sign bit.
581  */
582 static inline int64_t
util_mask_sign_extend(uint64_t val,unsigned width)583 util_mask_sign_extend(uint64_t val, unsigned width)
584 {
585    assert(width > 0 && width <= 64);
586    unsigned shift = 64 - width;
587    return (int64_t)(val << shift) >> shift;
588 }
589 
590 /**
591  * Sign-extend a number
592  *
593  * The bit at position `width - 1` is replicated to all the higher bits.
594  * This assumes and asserts that the value fits into `width` bits.
595  */
596 static inline int64_t
util_sign_extend(uint64_t val,unsigned width)597 util_sign_extend(uint64_t val, unsigned width)
598 {
599    assert(width == 64 || val < (UINT64_C(1) << width));
600    return util_mask_sign_extend(val, width);
601 }
602 
603 static inline void*
util_memcpy_cpu_to_le32(void * restrict dest,const void * restrict src,size_t n)604 util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
605 {
606 #if UTIL_ARCH_BIG_ENDIAN
607    size_t i, e;
608    assert(n % 4 == 0);
609 
610    for (i = 0, e = n / 4; i < e; i++) {
611       uint32_t * restrict d = (uint32_t* restrict)dest;
612       const uint32_t * restrict s = (const uint32_t* restrict)src;
613       d[i] = util_bswap32(s[i]);
614    }
615    return dest;
616 #else
617    return memcpy(dest, src, n);
618 #endif
619 }
620 
621 /**
622  * Clamp X to [MIN, MAX].
623  * This is a macro to allow float, int, uint, etc. types.
624  * We arbitrarily turn NaN into MIN.
625  */
626 #define CLAMP( X, MIN, MAX )  ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) )
627 
628 /* Syntax sugar occuring frequently in graphics code */
629 #define SATURATE( X ) CLAMP(X, 0.0f, 1.0f)
630 
631 #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
632 #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
633 
634 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
635 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
636 
637 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
638 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
639 
640 
641 /**
642  * Align a value up to an alignment value
643  *
644  * If \c value is not already aligned to the requested alignment value, it
645  * will be rounded up.
646  *
647  * \param value  Value to be rounded
648  * \param alignment  Alignment value to be used.  This must be a power of two.
649  *
650  * \sa ROUND_DOWN_TO()
651  */
652 
653 #if defined(ALIGN)
654 #undef ALIGN
655 #endif
656 static inline uintptr_t
ALIGN(uintptr_t value,int32_t alignment)657 ALIGN(uintptr_t value, int32_t alignment)
658 {
659    assert(util_is_power_of_two_nonzero(alignment));
660    return ALIGN_POT(value, alignment);
661 }
662 
663 /**
664  * Like ALIGN(), but works with a non-power-of-two alignment.
665  */
666 static inline uintptr_t
ALIGN_NPOT(uintptr_t value,int32_t alignment)667 ALIGN_NPOT(uintptr_t value, int32_t alignment)
668 {
669    assert(alignment > 0);
670    return (value + alignment - 1) / alignment * alignment;
671 }
672 
673 /**
674  * Align a value down to an alignment value
675  *
676  * If \c value is not already aligned to the requested alignment value, it
677  * will be rounded down.
678  *
679  * \param value  Value to be rounded
680  * \param alignment  Alignment value to be used.  This must be a power of two.
681  *
682  * \sa ALIGN()
683  */
684 static inline uint64_t
ROUND_DOWN_TO(uint64_t value,uint32_t alignment)685 ROUND_DOWN_TO(uint64_t value, uint32_t alignment)
686 {
687    assert(util_is_power_of_two_nonzero(alignment));
688    return ((value) & ~(uint64_t)(alignment - 1));
689 }
690 
691 /**
692  * Align a value, only works pot alignemnts.
693  */
694 static inline uint32_t
align(uint32_t value,uint32_t alignment)695 align(uint32_t value, uint32_t alignment)
696 {
697    assert(util_is_power_of_two_nonzero(alignment));
698    return ALIGN_POT(value, alignment);
699 }
700 
701 static inline uint64_t
align64(uint64_t value,uint64_t alignment)702 align64(uint64_t value, uint64_t alignment)
703 {
704    assert(util_is_power_of_two_nonzero64(alignment));
705    return ALIGN_POT(value, alignment);
706 }
707 
708 /**
709  * Works like align but on npot alignments.
710  */
711 static inline size_t
util_align_npot(size_t value,size_t alignment)712 util_align_npot(size_t value, size_t alignment)
713 {
714    if (value % alignment)
715       return value + (alignment - (value % alignment));
716    return value;
717 }
718 
719 static inline unsigned
u_minify(unsigned value,unsigned levels)720 u_minify(unsigned value, unsigned levels)
721 {
722     return MAX2(1, value >> levels);
723 }
724 
725 #ifndef COPY_4V
726 #define COPY_4V( DST, SRC )         \
727 do {                                \
728    (DST)[0] = (SRC)[0];             \
729    (DST)[1] = (SRC)[1];             \
730    (DST)[2] = (SRC)[2];             \
731    (DST)[3] = (SRC)[3];             \
732 } while (0)
733 #endif
734 
735 
736 #ifndef COPY_4FV
737 #define COPY_4FV( DST, SRC )  COPY_4V(DST, SRC)
738 #endif
739 
740 
741 #ifndef ASSIGN_4V
742 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
743 do {                                     \
744    (DST)[0] = (V0);                      \
745    (DST)[1] = (V1);                      \
746    (DST)[2] = (V2);                      \
747    (DST)[3] = (V3);                      \
748 } while (0)
749 #endif
750 
751 
752 static inline uint32_t
util_unsigned_fixed(float value,unsigned frac_bits)753 util_unsigned_fixed(float value, unsigned frac_bits)
754 {
755    return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
756 }
757 
758 static inline int32_t
util_signed_fixed(float value,unsigned frac_bits)759 util_signed_fixed(float value, unsigned frac_bits)
760 {
761    return (int32_t)(value * (1<<frac_bits));
762 }
763 
764 unsigned
765 util_fpstate_get(void);
766 unsigned
767 util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
768 void
769 util_fpstate_set(unsigned fpstate);
770 
771 /**
772  * For indexed draw calls, return true if the vertex count to be drawn is
773  * much lower than the vertex count that has to be uploaded, meaning
774  * that the driver should flatten indices instead of trying to upload
775  * a too big range.
776  *
777  * This is used by vertex upload code in u_vbuf and glthread.
778  */
779 static inline bool
util_is_vbo_upload_ratio_too_large(unsigned draw_vertex_count,unsigned upload_vertex_count)780 util_is_vbo_upload_ratio_too_large(unsigned draw_vertex_count,
781                                    unsigned upload_vertex_count)
782 {
783    if (upload_vertex_count > 256)
784       return upload_vertex_count > draw_vertex_count * 4;
785    else if (upload_vertex_count > 64)
786       return upload_vertex_count > draw_vertex_count * 8;
787    else
788       return upload_vertex_count > draw_vertex_count * 16;
789 }
790 
791 bool util_invert_mat4x4(float *out, const float *m);
792 
793 /* Quantize the lod bias value to reduce the number of sampler state
794  * variants in gallium because apps use it for smooth mipmap transitions,
795  * thrashing cso_cache and degrading performance.
796  *
797  * This quantization matches the AMD hw specification, so having more
798  * precision would have no effect anyway.
799  */
800 static inline float
util_quantize_lod_bias(float lod)801 util_quantize_lod_bias(float lod)
802 {
803    lod = CLAMP(lod, -32, 31);
804    return roundf(lod * 256) / 256;
805 }
806 
807 /**
808  * Adds two unsigned integers and if the addition
809  * overflows then clamp it to ~0U.
810  */
811 static inline unsigned
util_clamped_uadd(unsigned a,unsigned b)812 util_clamped_uadd(unsigned a, unsigned b)
813 {
814    unsigned res = a + b;
815    if (res < a) {
816       res = ~0U;
817    }
818    return res;
819 }
820 
821 #ifdef __cplusplus
822 }
823 #endif
824 
825 #endif /* U_MATH_H */
826