• 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 "pipe/p_compiler.h"
43 
44 #include "c99_math.h"
45 #include <assert.h>
46 #include <float.h>
47 #include <stdarg.h>
48 
49 #include "util/bitscan.h"
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 
56 #ifndef M_SQRT2
57 #define M_SQRT2 1.41421356237309504880
58 #endif
59 
60 #define POW2_TABLE_SIZE_LOG2 9
61 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
62 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
63 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
64 extern float pow2_table[POW2_TABLE_SIZE];
65 
66 
67 /**
68  * Initialize math module.  This should be called before using any
69  * other functions in this module.
70  */
71 extern void
72 util_init_math(void);
73 
74 
75 union fi {
76    float f;
77    int32_t i;
78    uint32_t ui;
79 };
80 
81 
82 union di {
83    double d;
84    int64_t i;
85    uint64_t ui;
86 };
87 
88 
89 /**
90  * Extract the IEEE float32 exponent.
91  */
92 static inline signed
util_get_float32_exponent(float x)93 util_get_float32_exponent(float x)
94 {
95    union fi f;
96 
97    f.f = x;
98 
99    return ((f.ui >> 23) & 0xff) - 127;
100 }
101 
102 
103 /**
104  * Fast version of 2^x
105  * Identity: exp2(a + b) = exp2(a) * exp2(b)
106  * Let ipart = int(x)
107  * Let fpart = x - ipart;
108  * So, exp2(x) = exp2(ipart) * exp2(fpart)
109  * Compute exp2(ipart) with i << ipart
110  * Compute exp2(fpart) with lookup table.
111  */
112 static inline float
util_fast_exp2(float x)113 util_fast_exp2(float x)
114 {
115    int32_t ipart;
116    float fpart, mpart;
117    union fi epart;
118 
119    if(x > 129.00000f)
120       return 3.402823466e+38f;
121 
122    if (x < -126.99999f)
123       return 0.0f;
124 
125    ipart = (int32_t) x;
126    fpart = x - (float) ipart;
127 
128    /* same as
129     *   epart.f = (float) (1 << ipart)
130     * but faster and without integer overflow for ipart > 31
131     */
132    epart.i = (ipart + 127 ) << 23;
133 
134    mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
135 
136    return epart.f * mpart;
137 }
138 
139 
140 /**
141  * Fast approximation to exp(x).
142  */
143 static inline float
util_fast_exp(float x)144 util_fast_exp(float x)
145 {
146    const float k = 1.44269f; /* = log2(e) */
147    return util_fast_exp2(k * x);
148 }
149 
150 
151 #define LOG2_TABLE_SIZE_LOG2 16
152 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
153 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
154 extern float log2_table[LOG2_TABLE_SIZE];
155 
156 
157 /**
158  * Fast approximation to log2(x).
159  */
160 static inline float
util_fast_log2(float x)161 util_fast_log2(float x)
162 {
163    union fi num;
164    float epart, mpart;
165    num.f = x;
166    epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
167    /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
168    mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
169    return epart + mpart;
170 }
171 
172 
173 /**
174  * Fast approximation to x^y.
175  */
176 static inline float
util_fast_pow(float x,float y)177 util_fast_pow(float x, float y)
178 {
179    return util_fast_exp2(util_fast_log2(x) * y);
180 }
181 
182 /* Note that this counts zero as a power of two.
183  */
184 static inline boolean
util_is_power_of_two(unsigned v)185 util_is_power_of_two( unsigned v )
186 {
187    return (v & (v-1)) == 0;
188 }
189 
190 
191 /**
192  * Floor(x), returned as int.
193  */
194 static inline int
util_ifloor(float f)195 util_ifloor(float f)
196 {
197    int ai, bi;
198    double af, bf;
199    union fi u;
200    af = (3 << 22) + 0.5 + (double) f;
201    bf = (3 << 22) + 0.5 - (double) f;
202    u.f = (float) af;  ai = u.i;
203    u.f = (float) bf;  bi = u.i;
204    return (ai - bi) >> 1;
205 }
206 
207 
208 /**
209  * Round float to nearest int.
210  */
211 static inline int
util_iround(float f)212 util_iround(float f)
213 {
214 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
215    int r;
216    __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
217    return r;
218 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
219    int r;
220    _asm {
221       fld f
222       fistp r
223    }
224    return r;
225 #else
226    if (f >= 0.0f)
227       return (int) (f + 0.5f);
228    else
229       return (int) (f - 0.5f);
230 #endif
231 }
232 
233 
234 /**
235  * Approximate floating point comparison
236  */
237 static inline boolean
util_is_approx(float a,float b,float tol)238 util_is_approx(float a, float b, float tol)
239 {
240    return fabsf(b - a) <= tol;
241 }
242 
243 
244 /**
245  * util_is_X_inf_or_nan = test if x is NaN or +/- Inf
246  * util_is_X_nan        = test if x is NaN
247  * util_X_inf_sign      = return +1 for +Inf, -1 for -Inf, or 0 for not Inf
248  *
249  * NaN can be checked with x != x, however this fails with the fast math flag
250  **/
251 
252 
253 /**
254  * Single-float
255  */
256 static inline boolean
util_is_inf_or_nan(float x)257 util_is_inf_or_nan(float x)
258 {
259    union fi tmp;
260    tmp.f = x;
261    return (tmp.ui & 0x7f800000) == 0x7f800000;
262 }
263 
264 
265 static inline boolean
util_is_nan(float x)266 util_is_nan(float x)
267 {
268    union fi tmp;
269    tmp.f = x;
270    return (tmp.ui & 0x7fffffff) > 0x7f800000;
271 }
272 
273 
274 static inline int
util_inf_sign(float x)275 util_inf_sign(float x)
276 {
277    union fi tmp;
278    tmp.f = x;
279    if ((tmp.ui & 0x7fffffff) != 0x7f800000) {
280       return 0;
281    }
282 
283    return (x < 0) ? -1 : 1;
284 }
285 
286 
287 /**
288  * Double-float
289  */
290 static inline boolean
util_is_double_inf_or_nan(double x)291 util_is_double_inf_or_nan(double x)
292 {
293    union di tmp;
294    tmp.d = x;
295    return (tmp.ui & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL;
296 }
297 
298 
299 static inline boolean
util_is_double_nan(double x)300 util_is_double_nan(double x)
301 {
302    union di tmp;
303    tmp.d = x;
304    return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL;
305 }
306 
307 
308 static inline int
util_double_inf_sign(double x)309 util_double_inf_sign(double x)
310 {
311    union di tmp;
312    tmp.d = x;
313    if ((tmp.ui & 0x7fffffffffffffffULL) != 0x7ff0000000000000ULL) {
314       return 0;
315    }
316 
317    return (x < 0) ? -1 : 1;
318 }
319 
320 
321 /**
322  * Half-float
323  */
324 static inline boolean
util_is_half_inf_or_nan(int16_t x)325 util_is_half_inf_or_nan(int16_t x)
326 {
327    return (x & 0x7c00) == 0x7c00;
328 }
329 
330 
331 static inline boolean
util_is_half_nan(int16_t x)332 util_is_half_nan(int16_t x)
333 {
334    return (x & 0x7fff) > 0x7c00;
335 }
336 
337 
338 static inline int
util_half_inf_sign(int16_t x)339 util_half_inf_sign(int16_t x)
340 {
341    if ((x & 0x7fff) != 0x7c00) {
342       return 0;
343    }
344 
345    return (x < 0) ? -1 : 1;
346 }
347 
348 
349 /**
350  * Return float bits.
351  */
352 static inline unsigned
fui(float f)353 fui( float f )
354 {
355    union fi fi;
356    fi.f = f;
357    return fi.ui;
358 }
359 
360 static inline float
uif(uint32_t ui)361 uif(uint32_t ui)
362 {
363    union fi fi;
364    fi.ui = ui;
365    return fi.f;
366 }
367 
368 
369 /**
370  * Convert ubyte to float in [0, 1].
371  * XXX a 256-entry lookup table would be slightly faster.
372  */
373 static inline float
ubyte_to_float(ubyte ub)374 ubyte_to_float(ubyte ub)
375 {
376    return (float) ub * (1.0f / 255.0f);
377 }
378 
379 
380 /**
381  * Convert float in [0,1] to ubyte in [0,255] with clamping.
382  */
383 static inline ubyte
float_to_ubyte(float f)384 float_to_ubyte(float f)
385 {
386    union fi tmp;
387 
388    tmp.f = f;
389    if (tmp.i < 0) {
390       return (ubyte) 0;
391    }
392    else if (tmp.i >= 0x3f800000 /* 1.0f */) {
393       return (ubyte) 255;
394    }
395    else {
396       tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
397       return (ubyte) tmp.i;
398    }
399 }
400 
401 static inline float
byte_to_float_tex(int8_t b)402 byte_to_float_tex(int8_t b)
403 {
404    return (b == -128) ? -1.0F : b * 1.0F / 127.0F;
405 }
406 
407 static inline int8_t
float_to_byte_tex(float f)408 float_to_byte_tex(float f)
409 {
410    return (int8_t) (127.0F * f);
411 }
412 
413 /**
414  * Calc log base 2
415  */
416 static inline unsigned
util_logbase2(unsigned n)417 util_logbase2(unsigned n)
418 {
419 #if defined(HAVE___BUILTIN_CLZ)
420    return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1));
421 #else
422    unsigned pos = 0;
423    if (n >= 1<<16) { n >>= 16; pos += 16; }
424    if (n >= 1<< 8) { n >>=  8; pos +=  8; }
425    if (n >= 1<< 4) { n >>=  4; pos +=  4; }
426    if (n >= 1<< 2) { n >>=  2; pos +=  2; }
427    if (n >= 1<< 1) {           pos +=  1; }
428    return pos;
429 #endif
430 }
431 
432 /**
433  * Returns the ceiling of log n base 2, and 0 when n == 0. Equivalently,
434  * returns the smallest x such that n <= 2**x.
435  */
436 static inline unsigned
util_logbase2_ceil(unsigned n)437 util_logbase2_ceil(unsigned n)
438 {
439    if (n <= 1)
440       return 0;
441 
442    return 1 + util_logbase2(n - 1);
443 }
444 
445 /**
446  * Returns the smallest power of two >= x
447  */
448 static inline unsigned
util_next_power_of_two(unsigned x)449 util_next_power_of_two(unsigned x)
450 {
451 #if defined(HAVE___BUILTIN_CLZ)
452    if (x <= 1)
453        return 1;
454 
455    return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1)));
456 #else
457    unsigned val = x;
458 
459    if (x <= 1)
460       return 1;
461 
462    if (util_is_power_of_two(x))
463       return x;
464 
465    val--;
466    val = (val >> 1) | val;
467    val = (val >> 2) | val;
468    val = (val >> 4) | val;
469    val = (val >> 8) | val;
470    val = (val >> 16) | val;
471    val++;
472    return val;
473 #endif
474 }
475 
476 
477 /**
478  * Return number of bits set in n.
479  */
480 static inline unsigned
util_bitcount(unsigned n)481 util_bitcount(unsigned n)
482 {
483 #if defined(HAVE___BUILTIN_POPCOUNT)
484    return __builtin_popcount(n);
485 #else
486    /* K&R classic bitcount.
487     *
488     * For each iteration, clear the LSB from the bitfield.
489     * Requires only one iteration per set bit, instead of
490     * one iteration per bit less than highest set bit.
491     */
492    unsigned bits;
493    for (bits = 0; n; bits++) {
494       n &= n - 1;
495    }
496    return bits;
497 #endif
498 }
499 
500 
501 static inline unsigned
util_bitcount64(uint64_t n)502 util_bitcount64(uint64_t n)
503 {
504 #ifdef HAVE___BUILTIN_POPCOUNTLL
505    return __builtin_popcountll(n);
506 #else
507    return util_bitcount(n) + util_bitcount(n >> 32);
508 #endif
509 }
510 
511 
512 /**
513  * Reverse bits in n
514  * Algorithm taken from:
515  * http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer
516  */
517 static inline unsigned
util_bitreverse(unsigned n)518 util_bitreverse(unsigned n)
519 {
520     n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
521     n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
522     n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4);
523     n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8);
524     n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16);
525     return n;
526 }
527 
528 /**
529  * Convert from little endian to CPU byte order.
530  */
531 
532 #ifdef PIPE_ARCH_BIG_ENDIAN
533 #define util_le64_to_cpu(x) util_bswap64(x)
534 #define util_le32_to_cpu(x) util_bswap32(x)
535 #define util_le16_to_cpu(x) util_bswap16(x)
536 #else
537 #define util_le64_to_cpu(x) (x)
538 #define util_le32_to_cpu(x) (x)
539 #define util_le16_to_cpu(x) (x)
540 #endif
541 
542 #define util_cpu_to_le64(x) util_le64_to_cpu(x)
543 #define util_cpu_to_le32(x) util_le32_to_cpu(x)
544 #define util_cpu_to_le16(x) util_le16_to_cpu(x)
545 
546 /**
547  * Reverse byte order of a 32 bit word.
548  */
549 static inline uint32_t
util_bswap32(uint32_t n)550 util_bswap32(uint32_t n)
551 {
552 #if defined(HAVE___BUILTIN_BSWAP32)
553    return __builtin_bswap32(n);
554 #else
555    return (n >> 24) |
556           ((n >> 8) & 0x0000ff00) |
557           ((n << 8) & 0x00ff0000) |
558           (n << 24);
559 #endif
560 }
561 
562 /**
563  * Reverse byte order of a 64bit word.
564  */
565 static inline uint64_t
util_bswap64(uint64_t n)566 util_bswap64(uint64_t n)
567 {
568 #if defined(HAVE___BUILTIN_BSWAP64)
569    return __builtin_bswap64(n);
570 #else
571    return ((uint64_t)util_bswap32((uint32_t)n) << 32) |
572           util_bswap32((n >> 32));
573 #endif
574 }
575 
576 
577 /**
578  * Reverse byte order of a 16 bit word.
579  */
580 static inline uint16_t
util_bswap16(uint16_t n)581 util_bswap16(uint16_t n)
582 {
583    return (n >> 8) |
584           (n << 8);
585 }
586 
587 static inline void*
util_memcpy_cpu_to_le32(void * restrict dest,const void * restrict src,size_t n)588 util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n)
589 {
590 #ifdef PIPE_ARCH_BIG_ENDIAN
591    size_t i, e;
592    assert(n % 4 == 0);
593 
594    for (i = 0, e = n / 4; i < e; i++) {
595       uint32_t * restrict d = (uint32_t* restrict)dest;
596       const uint32_t * restrict s = (const uint32_t* restrict)src;
597       d[i] = util_bswap32(s[i]);
598    }
599    return dest;
600 #else
601    return memcpy(dest, src, n);
602 #endif
603 }
604 
605 /**
606  * Clamp X to [MIN, MAX].
607  * This is a macro to allow float, int, uint, etc. types.
608  */
609 #define CLAMP( X, MIN, MAX )  ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
610 
611 #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
612 #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
613 
614 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
615 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
616 
617 #define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D))
618 #define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D))
619 
620 
621 /**
622  * Align a value, only works pot alignemnts.
623  */
624 static inline int
align(int value,int alignment)625 align(int value, int alignment)
626 {
627    return (value + alignment - 1) & ~(alignment - 1);
628 }
629 
630 static inline uint64_t
align64(uint64_t value,unsigned alignment)631 align64(uint64_t value, unsigned alignment)
632 {
633    return (value + alignment - 1) & ~((uint64_t)alignment - 1);
634 }
635 
636 /**
637  * Works like align but on npot alignments.
638  */
639 static inline size_t
util_align_npot(size_t value,size_t alignment)640 util_align_npot(size_t value, size_t alignment)
641 {
642    if (value % alignment)
643       return value + (alignment - (value % alignment));
644    return value;
645 }
646 
647 static inline unsigned
u_minify(unsigned value,unsigned levels)648 u_minify(unsigned value, unsigned levels)
649 {
650     return MAX2(1, value >> levels);
651 }
652 
653 #ifndef COPY_4V
654 #define COPY_4V( DST, SRC )         \
655 do {                                \
656    (DST)[0] = (SRC)[0];             \
657    (DST)[1] = (SRC)[1];             \
658    (DST)[2] = (SRC)[2];             \
659    (DST)[3] = (SRC)[3];             \
660 } while (0)
661 #endif
662 
663 
664 #ifndef COPY_4FV
665 #define COPY_4FV( DST, SRC )  COPY_4V(DST, SRC)
666 #endif
667 
668 
669 #ifndef ASSIGN_4V
670 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
671 do {                                     \
672    (DST)[0] = (V0);                      \
673    (DST)[1] = (V1);                      \
674    (DST)[2] = (V2);                      \
675    (DST)[3] = (V3);                      \
676 } while (0)
677 #endif
678 
679 
680 static inline uint32_t
util_unsigned_fixed(float value,unsigned frac_bits)681 util_unsigned_fixed(float value, unsigned frac_bits)
682 {
683    return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits));
684 }
685 
686 static inline int32_t
util_signed_fixed(float value,unsigned frac_bits)687 util_signed_fixed(float value, unsigned frac_bits)
688 {
689    return (int32_t)(value * (1<<frac_bits));
690 }
691 
692 unsigned
693 util_fpstate_get(void);
694 unsigned
695 util_fpstate_set_denorms_to_zero(unsigned current_fpstate);
696 void
697 util_fpstate_set(unsigned fpstate);
698 
699 
700 
701 #ifdef __cplusplus
702 }
703 #endif
704 
705 #endif /* U_MATH_H */
706