u_math.h revision 63e0e4b8f5a37f5f6a1b9a783f201d748eb312e8
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#define isfinite(x) _finite((double)(x)) 116#define isnan(x) _isnan((double)(x)) 117#endif /* _MSC_VER < 1400 && !defined(__cplusplus) */ 118 119static INLINE double log2( double x ) 120{ 121 const double invln2 = 1.442695041; 122 return log( x ) * invln2; 123} 124 125static INLINE double 126round(double x) 127{ 128 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5); 129} 130 131static INLINE float 132roundf(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 142static INLINE 143double 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 153static INLINE 154float 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)) 168extern 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 */ 175extern void 176util_init_math(void); 177 178 179union fi { 180 float f; 181 int32_t i; 182 uint32_t ui; 183}; 184 185 186union 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 */ 202static INLINE float 203util_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 */ 233static INLINE float 234util_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) 244extern float log2_table[LOG2_TABLE_SIZE]; 245 246 247/** 248 * Fast approximation to log2(x). 249 */ 250static INLINE float 251util_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 */ 266static INLINE float 267util_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 */ 274static INLINE boolean 275util_is_power_of_two( unsigned v ) 276{ 277 return (v & (v-1)) == 0; 278} 279 280 281/** 282 * Floor(x), returned as int. 283 */ 284static INLINE int 285util_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 */ 301static INLINE int 302util_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 */ 327static INLINE boolean 328util_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 */ 346static INLINE boolean 347util_is_inf_or_nan(float x) 348{ 349 union fi tmp; 350 tmp.f = x; 351 return (tmp.ui & 0x7f800000) == 0x7f800000; 352} 353 354 355static INLINE boolean 356util_is_nan(float x) 357{ 358 union fi tmp; 359 tmp.f = x; 360 return (tmp.ui & 0x7fffffff) > 0x7f800000; 361} 362 363 364static INLINE int 365util_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 */ 380static INLINE boolean 381util_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 389static INLINE boolean 390util_is_double_nan(double x) 391{ 392 union di tmp; 393 tmp.d = x; 394 return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL; 395} 396 397 398static INLINE int 399util_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 */ 414static INLINE boolean 415util_is_half_inf_or_nan(int16_t x) 416{ 417 return (x & 0x7c00) == 0x7c00; 418} 419 420 421static INLINE boolean 422util_is_half_nan(int16_t x) 423{ 424 return (x & 0x7fff) > 0x7c00; 425} 426 427 428static INLINE int 429util_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) 447unsigned char _BitScanForward(unsigned long* Index, unsigned long Mask); 448#pragma intrinsic(_BitScanForward) 449static INLINE 450unsigned 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) 459static INLINE 460unsigned 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/* Destructively loop over all of the bits in a mask as in: 482 * 483 * while (mymask) { 484 * int i = u_bit_scan(&mymask); 485 * ... process element i 486 * } 487 * 488 */ 489static INLINE int u_bit_scan(unsigned *mask) 490{ 491 int i = ffs(*mask) - 1; 492 *mask &= ~(1 << i); 493 return i; 494} 495 496 497/** 498 * Return float bits. 499 */ 500static INLINE unsigned 501fui( float f ) 502{ 503 union fi fi; 504 fi.f = f; 505 return fi.ui; 506} 507 508 509/** 510 * Convert ubyte to float in [0, 1]. 511 * XXX a 256-entry lookup table would be slightly faster. 512 */ 513static INLINE float 514ubyte_to_float(ubyte ub) 515{ 516 return (float) ub * (1.0f / 255.0f); 517} 518 519 520/** 521 * Convert float in [0,1] to ubyte in [0,255] with clamping. 522 */ 523static INLINE ubyte 524float_to_ubyte(float f) 525{ 526 const int ieee_0996 = 0x3f7f0000; /* 0.996 or so */ 527 union fi tmp; 528 529 tmp.f = f; 530 if (tmp.i < 0) { 531 return (ubyte) 0; 532 } 533 else if (tmp.i >= ieee_0996) { 534 return (ubyte) 255; 535 } 536 else { 537 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f; 538 return (ubyte) tmp.i; 539 } 540} 541 542static INLINE float 543byte_to_float_tex(int8_t b) 544{ 545 return (b == -128) ? -1.0F : b * 1.0F / 127.0F; 546} 547 548static INLINE int8_t 549float_to_byte_tex(float f) 550{ 551 return (int8_t) (127.0F * f); 552} 553 554/** 555 * Calc log base 2 556 */ 557static INLINE unsigned 558util_logbase2(unsigned n) 559{ 560#if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304) 561 return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1)); 562#else 563 unsigned pos = 0; 564 if (n >= 1<<16) { n >>= 16; pos += 16; } 565 if (n >= 1<< 8) { n >>= 8; pos += 8; } 566 if (n >= 1<< 4) { n >>= 4; pos += 4; } 567 if (n >= 1<< 2) { n >>= 2; pos += 2; } 568 if (n >= 1<< 1) { pos += 1; } 569 return pos; 570#endif 571} 572 573 574/** 575 * Returns the smallest power of two >= x 576 */ 577static INLINE unsigned 578util_next_power_of_two(unsigned x) 579{ 580#if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304) 581 if (x <= 1) 582 return 1; 583 584 return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1))); 585#else 586 unsigned val = x; 587 588 if (x <= 1) 589 return 1; 590 591 if (util_is_power_of_two(x)) 592 return x; 593 594 val--; 595 val = (val >> 1) | val; 596 val = (val >> 2) | val; 597 val = (val >> 4) | val; 598 val = (val >> 8) | val; 599 val = (val >> 16) | val; 600 val++; 601 return val; 602#endif 603} 604 605 606/** 607 * Return number of bits set in n. 608 */ 609static INLINE unsigned 610util_bitcount(unsigned n) 611{ 612#if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 304) 613 return __builtin_popcount(n); 614#else 615 /* K&R classic bitcount. 616 * 617 * For each iteration, clear the LSB from the bitfield. 618 * Requires only one iteration per set bit, instead of 619 * one iteration per bit less than highest set bit. 620 */ 621 unsigned bits = 0; 622 for (bits; n; bits++) { 623 n &= n - 1; 624 } 625 return bits; 626#endif 627} 628 629 630/** 631 * Convert from little endian to CPU byte order. 632 */ 633 634#ifdef PIPE_ARCH_BIG_ENDIAN 635#define util_le32_to_cpu(x) util_bswap32(x) 636#define util_le16_to_cpu(x) util_bswap16(x) 637#else 638#define util_le32_to_cpu(x) (x) 639#define util_le16_to_cpu(x) (x) 640#endif 641 642 643/** 644 * Reverse byte order of a 32 bit word. 645 */ 646static INLINE uint32_t 647util_bswap32(uint32_t n) 648{ 649#if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION >= 403) 650 return __builtin_bswap32(n); 651#else 652 return (n >> 24) | 653 ((n >> 8) & 0x0000ff00) | 654 ((n << 8) & 0x00ff0000) | 655 (n << 24); 656#endif 657} 658 659 660/** 661 * Reverse byte order of a 16 bit word. 662 */ 663static INLINE uint16_t 664util_bswap16(uint16_t n) 665{ 666 return (n >> 8) | 667 (n << 8); 668} 669 670 671/** 672 * Clamp X to [MIN, MAX]. 673 * This is a macro to allow float, int, uint, etc. types. 674 */ 675#define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) ) 676 677#define MIN2( A, B ) ( (A)<(B) ? (A) : (B) ) 678#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) ) 679 680#define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C)) 681#define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C)) 682 683#define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D)) 684#define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D)) 685 686 687/** 688 * Align a value, only works pot alignemnts. 689 */ 690static INLINE int 691align(int value, int alignment) 692{ 693 return (value + alignment - 1) & ~(alignment - 1); 694} 695 696/** 697 * Works like align but on npot alignments. 698 */ 699static INLINE size_t 700util_align_npot(size_t value, size_t alignment) 701{ 702 if (value % alignment) 703 return value + (alignment - (value % alignment)); 704 return value; 705} 706 707static INLINE unsigned 708u_minify(unsigned value, unsigned levels) 709{ 710 return MAX2(1, value >> levels); 711} 712 713#ifndef COPY_4V 714#define COPY_4V( DST, SRC ) \ 715do { \ 716 (DST)[0] = (SRC)[0]; \ 717 (DST)[1] = (SRC)[1]; \ 718 (DST)[2] = (SRC)[2]; \ 719 (DST)[3] = (SRC)[3]; \ 720} while (0) 721#endif 722 723 724#ifndef COPY_4FV 725#define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC) 726#endif 727 728 729#ifndef ASSIGN_4V 730#define ASSIGN_4V( DST, V0, V1, V2, V3 ) \ 731do { \ 732 (DST)[0] = (V0); \ 733 (DST)[1] = (V1); \ 734 (DST)[2] = (V2); \ 735 (DST)[3] = (V3); \ 736} while (0) 737#endif 738 739 740static INLINE uint32_t util_unsigned_fixed(float value, unsigned frac_bits) 741{ 742 return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits)); 743} 744 745static INLINE int32_t util_signed_fixed(float value, unsigned frac_bits) 746{ 747 return (int32_t)(value * (1<<frac_bits)); 748} 749 750 751 752#ifdef __cplusplus 753} 754#endif 755 756#endif /* U_MATH_H */ 757