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 uint64_t
dui(double f)300 dui( double f )
301 {
302 union di di;
303 di.d = f;
304 return di.ui;
305 }
306
307 static inline float
uif(uint32_t ui)308 uif(uint32_t ui)
309 {
310 union fi fi;
311 fi.ui = ui;
312 return fi.f;
313 }
314
315 static inline double
uid(uint64_t ui)316 uid(uint64_t ui)
317 {
318 union di di;
319 di.ui = ui;
320 return di.d;
321 }
322
323 /**
324 * Convert uint8_t to float in [0, 1].
325 */
326 static inline float
ubyte_to_float(uint8_t ub)327 ubyte_to_float(uint8_t ub)
328 {
329 return (float) ub * (1.0f / 255.0f);
330 }
331
332
333 /**
334 * Convert float in [0,1] to uint8_t in [0,255] with clamping.
335 */
336 static inline uint8_t
float_to_ubyte(float f)337 float_to_ubyte(float f)
338 {
339 /* return 0 for NaN too */
340 if (!(f > 0.0f)) {
341 return (uint8_t) 0;
342 }
343 else if (f >= 1.0f) {
344 return (uint8_t) 255;
345 }
346 else {
347 union fi tmp;
348 tmp.f = f;
349 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
350 return (uint8_t) tmp.i;
351 }
352 }
353
354 /**
355 * Convert uint16_t to float in [0, 1].
356 */
357 static inline float
ushort_to_float(uint16_t us)358 ushort_to_float(uint16_t us)
359 {
360 return (float) us * (1.0f / 65535.0f);
361 }
362
363
364 /**
365 * Convert float in [0,1] to uint16_t in [0,65535] with clamping.
366 */
367 static inline uint16_t
float_to_ushort(float f)368 float_to_ushort(float f)
369 {
370 /* return 0 for NaN too */
371 if (!(f > 0.0f)) {
372 return (uint16_t) 0;
373 }
374 else if (f >= 1.0f) {
375 return (uint16_t) 65535;
376 }
377 else {
378 union fi tmp;
379 tmp.f = f;
380 tmp.f = tmp.f * (65535.0f/65536.0f) + 128.0f;
381 return (uint16_t) tmp.i;
382 }
383 }
384
385 static inline float
byte_to_float_tex(int8_t b)386 byte_to_float_tex(int8_t b)
387 {
388 return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
389 }
390
391 static inline int8_t
float_to_byte_tex(float f)392 float_to_byte_tex(float f)
393 {
394 return (int8_t) (127.0F * f);
395 }
396
397 /**
398 * Calc log base 2
399 */
400 static inline unsigned
util_logbase2(unsigned n)401 util_logbase2(unsigned n)
402 {
403 #if defined(HAVE___BUILTIN_CLZ)
404 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
405 #else
406 unsigned pos = 0;
407 if (n >= 1<<16) { n >>= 16; pos += 16; }
408 if (n >= 1<< 8) { n >>= 8; pos += 8; }
409 if (n >= 1<< 4) { n >>= 4; pos += 4; }
410 if (n >= 1<< 2) { n >>= 2; pos += 2; }
411 if (n >= 1<< 1) { pos += 1; }
412 return pos;
413 #endif
414 }
415
416 static inline uint64_t
util_logbase2_64(uint64_t n)417 util_logbase2_64(uint64_t n)
418 {
419 #if defined(HAVE___BUILTIN_CLZLL)
420 return ((sizeof(uint64_t) * 8 - 1) - __builtin_clzll(n | 1));
421 #else
422 uint64_t pos = 0ull;
423 if (n >= 1ull<<32) { n >>= 32; pos += 32; }
424 if (n >= 1ull<<16) { n >>= 16; pos += 16; }
425 if (n >= 1ull<< 8) { n >>= 8; pos += 8; }
426 if (n >= 1ull<< 4) { n >>= 4; pos += 4; }
427 if (n >= 1ull<< 2) { n >>= 2; pos += 2; }
428 if (n >= 1ull<< 1) { pos += 1; }
429 return pos;
430 #endif
431 }
432
433 /**
434 * Returns the ceiling of log n base 2, and 0 when n == 0. Equivalently,
435 * returns the smallest x such that n <= 2**x.
436 */
437 static inline unsigned
util_logbase2_ceil(unsigned n)438 util_logbase2_ceil(unsigned n)
439 {
440 if (n <= 1)
441 return 0;
442
443 return 1 + util_logbase2(n - 1);
444 }
445
446 static inline uint64_t
util_logbase2_ceil64(uint64_t n)447 util_logbase2_ceil64(uint64_t n)
448 {
449 if (n <= 1)
450 return 0;
451
452 return 1ull + util_logbase2_64(n - 1);
453 }
454
455 /**
456 * Returns the smallest power of two >= x
457 */
458 static inline unsigned
util_next_power_of_two(unsigned x)459 util_next_power_of_two(unsigned x)
460 {
461 #if defined(HAVE___BUILTIN_CLZ)
462 if (x <= 1)
463 return 1;
464
465 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
466 #else
467 unsigned val = x;
468
469 if (x <= 1)
470 return 1;
471
472 if (util_is_power_of_two_or_zero(x))
473 return x;
474
475 val--;
476 val = (val >> 1) | val;
477 val = (val >> 2) | val;
478 val = (val >> 4) | val;
479 val = (val >> 8) | val;
480 val = (val >> 16) | val;
481 val++;
482 return val;
483 #endif
484 }
485
486 static inline uint64_t
util_next_power_of_two64(uint64_t x)487 util_next_power_of_two64(uint64_t x)
488 {
489 #if defined(HAVE___BUILTIN_CLZLL)
490 if (x <= 1)
491 return 1;
492
493 return (1ull << ((sizeof(uint64_t) * 8) - __builtin_clzll(x - 1)));
494 #else
495 uint64_t val = x;
496
497 if (x <= 1)
498 return 1;
499
500 if (util_is_power_of_two_or_zero64(x))
501 return x;
502
503 val--;
504 val = (val >> 1) | val;
505 val = (val >> 2) | val;
506 val = (val >> 4) | val;
507 val = (val >> 8) | val;
508 val = (val >> 16) | val;
509 val = (val >> 32) | val;
510 val++;
511 return val;
512 #endif
513 }
514
515 /**
516 * Reverse bits in n
517 * Algorithm taken from:
518 * http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
519 */
520 static inline unsigned
util_bitreverse(unsigned n)521 util_bitreverse(unsigned n)
522 {
523 n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
524 n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
525 n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4);
526 n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8);
527 n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16);
528 return n;
529 }
530
531 /**
532 * Convert from little endian to CPU byte order.
533 */
534
535 #if UTIL_ARCH_BIG_ENDIAN
536 #define util_le64_to_cpu(x) util_bswap64(x)
537 #define util_le32_to_cpu(x) util_bswap32(x)
538 #define util_le16_to_cpu(x) util_bswap16(x)
539 #else
540 #define util_le64_to_cpu(x) (x)
541 #define util_le32_to_cpu(x) (x)
542 #define util_le16_to_cpu(x) (x)
543 #endif
544
545 #define util_cpu_to_le64(x) util_le64_to_cpu(x)
546 #define util_cpu_to_le32(x) util_le32_to_cpu(x)
547 #define util_cpu_to_le16(x) util_le16_to_cpu(x)
548
549 /**
550 * Reverse byte order of a 32 bit word.
551 */
552 static inline uint32_t
util_bswap32(uint32_t n)553 util_bswap32(uint32_t n)
554 {
555 #if defined(HAVE___BUILTIN_BSWAP32)
556 return __builtin_bswap32(n);
557 #else
558 return (n >> 24) |
559 ((n >> 8) & 0x0000ff00) |
560 ((n << 8) & 0x00ff0000) |
561 (n << 24);
562 #endif
563 }
564
565 /**
566 * Reverse byte order of a 64bit word.
567 */
568 static inline uint64_t
util_bswap64(uint64_t n)569 util_bswap64(uint64_t n)
570 {
571 #if defined(HAVE___BUILTIN_BSWAP64)
572 return __builtin_bswap64(n);
573 #else
574 return ((uint64_t)util_bswap32((uint32_t)n) << 32) |
575 util_bswap32((n >> 32));
576 #endif
577 }
578
579
580 /**
581 * Reverse byte order of a 16 bit word.
582 */
583 static inline uint16_t
util_bswap16(uint16_t n)584 util_bswap16(uint16_t n)
585 {
586 return (n >> 8) |
587 (n << 8);
588 }
589
590 /**
591 * Mask and sign-extend a number
592 *
593 * The bit at position `width - 1` is replicated to all the higher bits.
594 * This makes no assumptions about the high bits of the value and will
595 * overwrite them with the sign bit.
596 */
597 static inline int64_t
util_mask_sign_extend(uint64_t val,unsigned width)598 util_mask_sign_extend(uint64_t val, unsigned width)
599 {
600 assert(width > 0 && width <= 64);
601 unsigned shift = 64 - width;
602 return (int64_t)(val << shift) >> shift;
603 }
604
605 /**
606 * Sign-extend a number
607 *
608 * The bit at position `width - 1` is replicated to all the higher bits.
609 * This assumes and asserts that the value fits into `width` bits.
610 */
611 static inline int64_t
util_sign_extend(uint64_t val,unsigned width)612 util_sign_extend(uint64_t val, unsigned width)
613 {
614 assert(width == 64 || val < (UINT64_C(1) << width));
615 return util_mask_sign_extend(val, width);
616 }
617
618 static inline void*
util_memcpy_cpu_to_le32(void * restrict dest,const void * restrict src,size_t n)619 util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
620 {
621 #if UTIL_ARCH_BIG_ENDIAN
622 size_t i, e;
623 assert(n % 4 == 0);
624
625 for (i = 0, e = n / 4; i < e; i++) {
626 uint32_t * restrict d = (uint32_t* restrict)dest;
627 const uint32_t * restrict s = (const uint32_t* restrict)src;
628 d[i] = util_bswap32(s[i]);
629 }
630 return dest;
631 #else
632 return memcpy(dest, src, n);
633 #endif
634 }
635
636 /**
637 * Align a value up to an alignment value
638 *
639 * If \c value is not already aligned to the requested alignment value, it
640 * will be rounded up.
641 *
642 * \param value Value to be rounded
643 * \param alignment Alignment value to be used. This must be a power of two.
644 *
645 * \sa ROUND_DOWN_TO()
646 */
647
648 #if defined(ALIGN)
649 #undef ALIGN
650 #endif
651 static inline uint32_t
ALIGN(uint32_t value,uint32_t alignment)652 ALIGN(uint32_t value, uint32_t alignment)
653 {
654 assert(util_is_power_of_two_nonzero(alignment));
655 return ALIGN_POT(value, alignment);
656 }
657
658 /**
659 * Like ALIGN(), but works with a non-power-of-two alignment.
660 */
661 static inline uintptr_t
ALIGN_NPOT(uintptr_t value,int32_t alignment)662 ALIGN_NPOT(uintptr_t value, int32_t alignment)
663 {
664 assert(alignment > 0);
665 return (value + alignment - 1) / alignment * alignment;
666 }
667
668 /**
669 * Align a value down to an alignment value
670 *
671 * If \c value is not already aligned to the requested alignment value, it
672 * will be rounded down.
673 *
674 * \param value Value to be rounded
675 * \param alignment Alignment value to be used. This must be a power of two.
676 *
677 * \sa ALIGN()
678 */
679 static inline uint64_t
ROUND_DOWN_TO(uint64_t value,uint32_t alignment)680 ROUND_DOWN_TO(uint64_t value, uint32_t alignment)
681 {
682 assert(util_is_power_of_two_nonzero(alignment));
683 return ((value) & ~(uint64_t)(alignment - 1));
684 }
685
686 /**
687 * Align a value, only works pot alignemnts.
688 */
689 static inline uint32_t
align(uint32_t value,uint32_t alignment)690 align(uint32_t value, uint32_t alignment)
691 {
692 assert(util_is_power_of_two_nonzero(alignment));
693 return ALIGN_POT(value, alignment);
694 }
695
696 static inline uint64_t
align64(uint64_t value,uint64_t alignment)697 align64(uint64_t value, uint64_t alignment)
698 {
699 assert(util_is_power_of_two_nonzero64(alignment));
700 return ALIGN_POT(value, alignment);
701 }
702
703 /**
704 * Align a value(uintptr_t, intptr_t, ptrdiff_t), only works pot alignemnts.
705 */
706 static inline uintptr_t
align_uintptr(uintptr_t value,uintptr_t alignment)707 align_uintptr(uintptr_t value, uintptr_t alignment)
708 {
709 assert(util_is_power_of_two_nonzero_uintptr(alignment));
710 return ALIGN_POT(value, alignment);
711 }
712
713 /**
714 * Works like align but on npot alignments.
715 */
716 static inline size_t
util_align_npot(size_t value,size_t alignment)717 util_align_npot(size_t value, size_t alignment)
718 {
719 if (value % alignment)
720 return value + (alignment - (value % alignment));
721 return value;
722 }
723
724 static inline unsigned
u_minify(unsigned value,unsigned levels)725 u_minify(unsigned value, unsigned levels)
726 {
727 return MAX2(1, value >> levels);
728 }
729
730 #ifndef COPY_4V
731 #define COPY_4V( DST, SRC ) \
732 do { \
733 (DST)[0] = (SRC)[0]; \
734 (DST)[1] = (SRC)[1]; \
735 (DST)[2] = (SRC)[2]; \
736 (DST)[3] = (SRC)[3]; \
737 } while (0)
738 #endif
739
740
741 #ifndef COPY_4FV
742 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
743 #endif
744
745
746 #ifndef ASSIGN_4V
747 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
748 do { \
749 (DST)[0] = (V0); \
750 (DST)[1] = (V1); \
751 (DST)[2] = (V2); \
752 (DST)[3] = (V3); \
753 } while (0)
754 #endif
755
756
757 static inline uint32_t
util_unsigned_fixed(float value,unsigned frac_bits)758 util_unsigned_fixed(float value, unsigned frac_bits)
759 {
760 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
761 }
762
763 static inline int32_t
util_signed_fixed(float value,unsigned frac_bits)764 util_signed_fixed(float value, unsigned frac_bits)
765 {
766 return (int32_t)(value * (1<<frac_bits));
767 }
768
769 unsigned
770 util_fpstate_get(void);
771 unsigned
772 util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
773 void
774 util_fpstate_set(unsigned fpstate);
775
776 /**
777 * For indexed draw calls, return true if the vertex count to be drawn is
778 * much lower than the vertex count that has to be uploaded, meaning
779 * that the driver should flatten indices instead of trying to upload
780 * a too big range.
781 *
782 * This is used by vertex upload code in u_vbuf and glthread.
783 */
784 static inline bool
util_is_vbo_upload_ratio_too_large(unsigned draw_vertex_count,unsigned upload_vertex_count)785 util_is_vbo_upload_ratio_too_large(unsigned draw_vertex_count,
786 unsigned upload_vertex_count)
787 {
788 if (upload_vertex_count > 256)
789 return upload_vertex_count > draw_vertex_count * 4;
790 else if (upload_vertex_count > 64)
791 return upload_vertex_count > draw_vertex_count * 8;
792 else
793 return upload_vertex_count > draw_vertex_count * 16;
794 }
795
796 bool util_invert_mat4x4(float *out, const float *m);
797
798 /* Quantize the lod bias value to reduce the number of sampler state
799 * variants in gallium because apps use it for smooth mipmap transitions,
800 * thrashing cso_cache and degrading performance.
801 *
802 * This quantization matches the AMD hw specification, so having more
803 * precision would have no effect anyway.
804 */
805 static inline float
util_quantize_lod_bias(float lod)806 util_quantize_lod_bias(float lod)
807 {
808 lod = CLAMP(lod, -32, 31);
809 return roundf(lod * 256) / 256;
810 }
811
812 /**
813 * Adds two unsigned integers and if the addition
814 * overflows then clamp it to ~0U.
815 */
816 static inline unsigned
util_clamped_uadd(unsigned a,unsigned b)817 util_clamped_uadd(unsigned a, unsigned b)
818 {
819 unsigned res = a + b;
820 if (res < a) {
821 res = ~0U;
822 }
823 return res;
824 }
825
826 /**
827 * Checks the value 'n' is aligned to 'a'.
828 * The alignment must be a power of two.
829 */
830 static inline bool
util_is_aligned(uintmax_t n,uintmax_t a)831 util_is_aligned(uintmax_t n, uintmax_t a)
832 {
833 assert(a == (a & -a));
834 return (n & (a - 1)) == 0;
835 }
836
837 static inline bool
util_is_sint16(int x)838 util_is_sint16(int x)
839 {
840 return x >= INT16_MIN && x <= INT16_MAX;
841 }
842
843 #ifdef __cplusplus
844 }
845 #endif
846
847 #endif /* U_MATH_H */
848