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