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