1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Math utilities and approximations for common math functions.
31 * Reduced precision is usually acceptable in shaders...
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.
48 #include "u_endian.h" /* for UTIL_ARCH_BIG_ENDIAN */
56 #define M_SQRT2 1.41421356237309504880
59 #define POW2_TABLE_SIZE_LOG2 9
60 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
61 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
62 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
63 extern float pow2_table
[POW2_TABLE_SIZE
];
67 * Initialize math module. This should be called before using any
68 * other functions in this module.
89 * Extract the IEEE float32 exponent.
92 util_get_float32_exponent(float x
)
98 return ((f
.ui
>> 23) & 0xff) - 127;
103 * Fast version of 2^x
104 * Identity: exp2(a + b) = exp2(a) * exp2(b)
106 * Let fpart = x - ipart;
107 * So, exp2(x) = exp2(ipart) * exp2(fpart)
108 * Compute exp2(ipart) with i << ipart
109 * Compute exp2(fpart) with lookup table.
112 util_fast_exp2(float x
)
119 return 3.402823466e+38f
;
125 fpart
= x
- (float) ipart
;
128 * epart.f = (float) (1 << ipart)
129 * but faster and without integer overflow for ipart > 31
131 epart
.i
= (ipart
+ 127 ) << 23;
133 mpart
= pow2_table
[POW2_TABLE_OFFSET
+ (int)(fpart
* POW2_TABLE_SCALE
)];
135 return epart
.f
* mpart
;
140 * Fast approximation to exp(x).
143 util_fast_exp(float x
)
145 const float k
= 1.44269f
; /* = log2(e) */
146 return util_fast_exp2(k
* x
);
150 #define LOG2_TABLE_SIZE_LOG2 16
151 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
152 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
153 extern float log2_table
[LOG2_TABLE_SIZE
];
157 * Fast approximation to log2(x).
160 util_fast_log2(float x
)
165 epart
= (float)(((num
.i
& 0x7f800000) >> 23) - 127);
166 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
167 mpart
= log2_table
[((num
.i
& 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2
))) >> (23 - LOG2_TABLE_SIZE_LOG2
)];
168 return epart
+ mpart
;
173 * Fast approximation to x^y.
176 util_fast_pow(float x
, float y
)
178 return util_fast_exp2(util_fast_log2(x
) * y
);
183 * Floor(x), returned as int.
188 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
190 * IEEE floor for computers that round to nearest or even.
191 * 'f' must be between -4194304 and 4194303.
192 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
193 * but uses some IEEE specific tricks for better speed.
194 * Contributed by Josh Vanderhoof
198 af
= (3 << 22) + 0.5 + (double)f
;
199 bf
= (3 << 22) + 0.5 - (double)f
;
200 /* GCC generates an extra fstp/fld without this. */
201 __asm__ ("fstps %0" : "=m" (ai
) : "t" (af
) : "st");
202 __asm__ ("fstps %0" : "=m" (bi
) : "t" (bf
) : "st");
203 return (ai
- bi
) >> 1;
208 af
= (3 << 22) + 0.5 + (double) f
;
209 bf
= (3 << 22) + 0.5 - (double) f
;
210 u
.f
= (float) af
; ai
= u
.i
;
211 u
.f
= (float) bf
; bi
= u
.i
;
212 return (ai
- bi
) >> 1;
218 * Round float to nearest int.
223 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
225 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
227 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
236 return (int) (f
+ 0.5f
);
238 return (int) (f
- 0.5f
);
244 * Approximate floating point comparison
247 util_is_approx(float a
, float b
, float tol
)
249 return fabsf(b
- a
) <= tol
;
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
258 * NaN can be checked with x != x, however this fails with the fast math flag
266 util_is_inf_or_nan(float x
)
270 return (tmp
.ui
& 0x7f800000) == 0x7f800000;
279 return (tmp
.ui
& 0x7fffffff) > 0x7f800000;
284 util_inf_sign(float x
)
288 if ((tmp
.ui
& 0x7fffffff) != 0x7f800000) {
292 return (x
< 0) ? -1 : 1;
300 util_is_double_inf_or_nan(double x
)
304 return (tmp
.ui
& 0x7ff0000000000000ULL
) == 0x7ff0000000000000ULL
;
309 util_is_double_nan(double x
)
313 return (tmp
.ui
& 0x7fffffffffffffffULL
) > 0x7ff0000000000000ULL
;
318 util_double_inf_sign(double x
)
322 if ((tmp
.ui
& 0x7fffffffffffffffULL
) != 0x7ff0000000000000ULL
) {
326 return (x
< 0) ? -1 : 1;
334 util_is_half_inf_or_nan(int16_t x
)
336 return (x
& 0x7c00) == 0x7c00;
341 util_is_half_nan(int16_t x
)
343 return (x
& 0x7fff) > 0x7c00;
348 util_half_inf_sign(int16_t x
)
350 if ((x
& 0x7fff) != 0x7c00) {
354 return (x
< 0) ? -1 : 1;
361 static inline unsigned
379 * Convert uint8_t to float in [0, 1].
382 ubyte_to_float(uint8_t ub
)
384 return (float) ub
* (1.0f
/ 255.0f
);
389 * Convert float in [0,1] to uint8_t in [0,255] with clamping.
391 static inline uint8_t
392 float_to_ubyte(float f
)
394 /* return 0 for NaN too */
398 else if (f
>= 1.0f
) {
399 return (uint8_t) 255;
404 tmp
.f
= tmp
.f
* (255.0f
/256.0f
) + 32768.0f
;
405 return (uint8_t) tmp
.i
;
410 * Convert uint16_t to float in [0, 1].
413 ushort_to_float(uint16_t us
)
415 return (float) us
* (1.0f
/ 65535.0f
);
420 * Convert float in [0,1] to uint16_t in [0,65535] with clamping.
422 static inline uint16_t
423 float_to_ushort(float f
)
425 /* return 0 for NaN too */
429 else if (f
>= 1.0f
) {
430 return (uint16_t) 65535;
435 tmp
.f
= tmp
.f
* (65535.0f
/65536.0f
) + 128.0f
;
436 return (uint16_t) tmp
.i
;
441 byte_to_float_tex(int8_t b
)
443 return (b
== -128) ? -1.0F
: b
* 1.0F
/ 127.0F
;
447 float_to_byte_tex(float f
)
449 return (int8_t) (127.0F
* f
);
455 static inline unsigned
456 util_logbase2(unsigned n
)
458 #if defined(HAVE___BUILTIN_CLZ)
459 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n
| 1));
462 if (n
>= 1<<16) { n
>>= 16; pos
+= 16; }
463 if (n
>= 1<< 8) { n
>>= 8; pos
+= 8; }
464 if (n
>= 1<< 4) { n
>>= 4; pos
+= 4; }
465 if (n
>= 1<< 2) { n
>>= 2; pos
+= 2; }
466 if (n
>= 1<< 1) { pos
+= 1; }
471 static inline uint64_t
472 util_logbase2_64(uint64_t n
)
474 #if defined(HAVE___BUILTIN_CLZLL)
475 return ((sizeof(uint64_t) * 8 - 1) - __builtin_clzll(n
| 1));
478 if (n
>= 1ull<<32) { n
>>= 32; pos
+= 32; }
479 if (n
>= 1ull<<16) { n
>>= 16; pos
+= 16; }
480 if (n
>= 1ull<< 8) { n
>>= 8; pos
+= 8; }
481 if (n
>= 1ull<< 4) { n
>>= 4; pos
+= 4; }
482 if (n
>= 1ull<< 2) { n
>>= 2; pos
+= 2; }
483 if (n
>= 1ull<< 1) { pos
+= 1; }
489 * Returns the ceiling of log n base 2, and 0 when n == 0. Equivalently,
490 * returns the smallest x such that n <= 2**x.
492 static inline unsigned
493 util_logbase2_ceil(unsigned n
)
498 return 1 + util_logbase2(n
- 1);
501 static inline uint64_t
502 util_logbase2_ceil64(uint64_t n
)
507 return 1ull + util_logbase2_64(n
- 1);
511 * Returns the smallest power of two >= x
513 static inline unsigned
514 util_next_power_of_two(unsigned x
)
516 #if defined(HAVE___BUILTIN_CLZ)
520 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x
- 1)));
527 if (util_is_power_of_two_or_zero(x
))
531 val
= (val
>> 1) | val
;
532 val
= (val
>> 2) | val
;
533 val
= (val
>> 4) | val
;
534 val
= (val
>> 8) | val
;
535 val
= (val
>> 16) | val
;
541 static inline uint64_t
542 util_next_power_of_two64(uint64_t x
)
544 #if defined(HAVE___BUILTIN_CLZLL)
548 return (1ull << ((sizeof(uint64_t) * 8) - __builtin_clzll(x
- 1)));
555 if (util_is_power_of_two_or_zero64(x
))
559 val
= (val
>> 1) | val
;
560 val
= (val
>> 2) | val
;
561 val
= (val
>> 4) | val
;
562 val
= (val
>> 8) | val
;
563 val
= (val
>> 16) | val
;
564 val
= (val
>> 32) | val
;
572 * Algorithm taken from:
573 * http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
575 static inline unsigned
576 util_bitreverse(unsigned n
)
578 n
= ((n
>> 1) & 0x55555555u
) | ((n
& 0x55555555u
) << 1);
579 n
= ((n
>> 2) & 0x33333333u
) | ((n
& 0x33333333u
) << 2);
580 n
= ((n
>> 4) & 0x0f0f0f0fu
) | ((n
& 0x0f0f0f0fu
) << 4);
581 n
= ((n
>> 8) & 0x00ff00ffu
) | ((n
& 0x00ff00ffu
) << 8);
582 n
= ((n
>> 16) & 0xffffu
) | ((n
& 0xffffu
) << 16);
587 * Convert from little endian to CPU byte order.
590 #if UTIL_ARCH_BIG_ENDIAN
591 #define util_le64_to_cpu(x) util_bswap64(x)
592 #define util_le32_to_cpu(x) util_bswap32(x)
593 #define util_le16_to_cpu(x) util_bswap16(x)
595 #define util_le64_to_cpu(x) (x)
596 #define util_le32_to_cpu(x) (x)
597 #define util_le16_to_cpu(x) (x)
600 #define util_cpu_to_le64(x) util_le64_to_cpu(x)
601 #define util_cpu_to_le32(x) util_le32_to_cpu(x)
602 #define util_cpu_to_le16(x) util_le16_to_cpu(x)
605 * Reverse byte order of a 32 bit word.
607 static inline uint32_t
608 util_bswap32(uint32_t n
)
610 #if defined(HAVE___BUILTIN_BSWAP32)
611 return __builtin_bswap32(n
);
614 ((n
>> 8) & 0x0000ff00) |
615 ((n
<< 8) & 0x00ff0000) |
621 * Reverse byte order of a 64bit word.
623 static inline uint64_t
624 util_bswap64(uint64_t n
)
626 #if defined(HAVE___BUILTIN_BSWAP64)
627 return __builtin_bswap64(n
);
629 return ((uint64_t)util_bswap32((uint32_t)n
) << 32) |
630 util_bswap32((n
>> 32));
636 * Reverse byte order of a 16 bit word.
638 static inline uint16_t
639 util_bswap16(uint16_t n
)
646 util_memcpy_cpu_to_le32(void * restrict dest
, const void * restrict src
, size_t n
)
648 #if UTIL_ARCH_BIG_ENDIAN
652 for (i
= 0, e
= n
/ 4; i
< e
; i
++) {
653 uint32_t * restrict d
= (uint32_t* restrict
)dest
;
654 const uint32_t * restrict s
= (const uint32_t* restrict
)src
;
655 d
[i
] = util_bswap32(s
[i
]);
659 return memcpy(dest
, src
, n
);
664 * Clamp X to [MIN, MAX].
665 * This is a macro to allow float, int, uint, etc. types.
666 * We arbitrarily turn NaN into MIN.
668 #define CLAMP( X, MIN, MAX ) ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) )
670 /* Syntax sugar occuring frequently in graphics code */
671 #define SATURATE( X ) CLAMP(X, 0.0f, 1.0f)
673 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
674 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
676 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
677 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
679 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
680 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
684 * Align a value up to an alignment value
686 * If \c value is not already aligned to the requested alignment value, it
687 * will be rounded up.
689 * \param value Value to be rounded
690 * \param alignment Alignment value to be used. This must be a power of two.
692 * \sa ROUND_DOWN_TO()
694 static inline uintptr_t
695 ALIGN(uintptr_t value
, int32_t alignment
)
697 assert(util_is_power_of_two_nonzero(alignment
));
698 return (((value
) + (alignment
) - 1) & ~((alignment
) - 1));
702 * Like ALIGN(), but works with a non-power-of-two alignment.
704 static inline uintptr_t
705 ALIGN_NPOT(uintptr_t value
, int32_t alignment
)
707 assert(alignment
> 0);
708 return (value
+ alignment
- 1) / alignment
* alignment
;
712 * Align a value down to an alignment value
714 * If \c value is not already aligned to the requested alignment value, it
715 * will be rounded down.
717 * \param value Value to be rounded
718 * \param alignment Alignment value to be used. This must be a power of two.
722 static inline uintptr_t
723 ROUND_DOWN_TO(uintptr_t value
, int32_t alignment
)
725 assert(util_is_power_of_two_nonzero(alignment
));
726 return ((value
) & ~(alignment
- 1));
730 * Align a value, only works pot alignemnts.
733 align(int value
, int alignment
)
735 return (value
+ alignment
- 1) & ~(alignment
- 1);
738 static inline uint64_t
739 align64(uint64_t value
, unsigned alignment
)
741 return (value
+ alignment
- 1) & ~((uint64_t)alignment
- 1);
745 * Works like align but on npot alignments.
748 util_align_npot(size_t value
, size_t alignment
)
750 if (value
% alignment
)
751 return value
+ (alignment
- (value
% alignment
));
755 static inline unsigned
756 u_minify(unsigned value
, unsigned levels
)
758 return MAX2(1, value
>> levels
);
762 #define COPY_4V( DST, SRC ) \
764 (DST)[0] = (SRC)[0]; \
765 (DST)[1] = (SRC)[1]; \
766 (DST)[2] = (SRC)[2]; \
767 (DST)[3] = (SRC)[3]; \
773 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
778 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
788 static inline uint32_t
789 util_unsigned_fixed(float value
, unsigned frac_bits
)
791 return value
< 0 ? 0 : (uint32_t)(value
* (1<<frac_bits
));
794 static inline int32_t
795 util_signed_fixed(float value
, unsigned frac_bits
)
797 return (int32_t)(value
* (1<<frac_bits
));
801 util_fpstate_get(void);
803 util_fpstate_set_denorms_to_zero(unsigned current_fpstate
);
805 util_fpstate_set(unsigned fpstate
);
808 * For indexed draw calls, return true if the vertex count to be drawn is
809 * much lower than the vertex count that has to be uploaded, meaning
810 * that the driver should flatten indices instead of trying to upload
813 * This is used by vertex upload code in u_vbuf and glthread.
816 util_is_vbo_upload_ratio_too_large(unsigned draw_vertex_count
,
817 unsigned upload_vertex_count
)
819 if (draw_vertex_count
> 1024)
820 return upload_vertex_count
> draw_vertex_count
* 4;
821 else if (draw_vertex_count
> 32)
822 return upload_vertex_count
> draw_vertex_count
* 8;
824 return upload_vertex_count
> draw_vertex_count
* 16;
831 #endif /* U_MATH_H */