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