| OLD | NEW |
| 1 /************************************************************************** | 1 /************************************************************************** |
| 2 * | 2 * |
| 3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. | 3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. |
| 4 * All Rights Reserved. | 4 * All Rights Reserved. |
| 5 * | 5 * |
| 6 * Permission is hereby granted, free of charge, to any person obtaining a | 6 * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 * copy of this software and associated documentation files (the | 7 * copy of this software and associated documentation files (the |
| 8 * "Software"), to deal in the Software without restriction, including | 8 * "Software"), to deal in the Software without restriction, including |
| 9 * without limitation the rights to use, copy, modify, merge, publish, | 9 * without limitation the rights to use, copy, modify, merge, publish, |
| 10 * distribute, sub license, and/or sell copies of the Software, and to | 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 | 11 * permit persons to whom the Software is furnished to do so, subject to |
| 12 * the following conditions: | 12 * the following conditions: |
| 13 * | 13 * |
| 14 * The above copyright notice and this permission notice (including the | 14 * The above copyright notice and this permission notice (including the |
| 15 * next paragraph) shall be included in all copies or substantial portions | 15 * next paragraph) shall be included in all copies or substantial portions |
| 16 * of the Software. | 16 * of the Software. |
| 17 * | 17 * |
| 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. | 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. |
| 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR | 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, | 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 | 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 * | 25 * |
| 26 **************************************************************************/ | 26 **************************************************************************/ |
| 27 | 27 |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * Math utilities and approximations for common math functions. | 30 * Math utilities and approximations for common math functions. |
| 31 * Reduced precision is usually acceptable in shaders... | 31 * Reduced precision is usually acceptable in shaders... |
| 32 * | 32 * |
| 33 * "fast" is used in the names of functions which are low-precision, | 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. | 34 * or at least lower-precision than the normal C lib functions. |
| 35 */ | 35 */ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 54 #ifdef PIPE_OS_UNIX | 54 #ifdef PIPE_OS_UNIX |
| 55 #include <strings.h> /* for ffs */ | 55 #include <strings.h> /* for ffs */ |
| 56 #endif | 56 #endif |
| 57 | 57 |
| 58 | 58 |
| 59 #ifndef M_SQRT2 | 59 #ifndef M_SQRT2 |
| 60 #define M_SQRT2 1.41421356237309504880 | 60 #define M_SQRT2 1.41421356237309504880 |
| 61 #endif | 61 #endif |
| 62 | 62 |
| 63 | 63 |
| 64 #if defined(_MSC_VER) | 64 #if defined(_MSC_VER) |
| 65 | 65 |
| 66 #if _MSC_VER < 1400 && !defined(__cplusplus) | 66 #if _MSC_VER < 1400 && !defined(__cplusplus) |
| 67 | 67 |
| 68 static INLINE float cosf( float f ) | 68 static INLINE float cosf( float f ) |
| 69 { | 69 { |
| 70 return (float) cos( (double) f ); | 70 return (float) cos( (double) f ); |
| 71 } | 71 } |
| 72 | 72 |
| 73 static INLINE float sinf( float f ) | 73 static INLINE float sinf( float f ) |
| 74 { | 74 { |
| 75 return (float) sin( (double) f ); | 75 return (float) sin( (double) f ); |
| 76 } | 76 } |
| 77 | 77 |
| 78 static INLINE float ceilf( float f ) | 78 static INLINE float ceilf( float f ) |
| 79 { | 79 { |
| 80 return (float) ceil( (double) f ); | 80 return (float) ceil( (double) f ); |
| 81 } | 81 } |
| 82 | 82 |
| 83 static INLINE float floorf( float f ) | 83 static INLINE float floorf( float f ) |
| 84 { | 84 { |
| 85 return (float) floor( (double) f ); | 85 return (float) floor( (double) f ); |
| 86 } | 86 } |
| 87 | 87 |
| 88 static INLINE float powf( float f, float g ) | 88 static INLINE float powf( float f, float g ) |
| 89 { | 89 { |
| 90 return (float) pow( (double) f, (double) g ); | 90 return (float) pow( (double) f, (double) g ); |
| 91 } | 91 } |
| 92 | 92 |
| 93 static INLINE float sqrtf( float f ) | 93 static INLINE float sqrtf( float f ) |
| 94 { | 94 { |
| 95 return (float) sqrt( (double) f ); | 95 return (float) sqrt( (double) f ); |
| 96 } | 96 } |
| 97 | 97 |
| 98 static INLINE float fabsf( float f ) | 98 static INLINE float fabsf( float f ) |
| 99 { | 99 { |
| 100 return (float) fabs( (double) f ); | 100 return (float) fabs( (double) f ); |
| 101 } | 101 } |
| 102 | 102 |
| 103 static INLINE float logf( float f ) | 103 static INLINE float logf( float f ) |
| 104 { | 104 { |
| 105 return (float) log( (double) f ); | 105 return (float) log( (double) f ); |
| 106 } | 106 } |
| 107 | 107 |
| 108 #else | 108 #else |
| 109 /* Work-around an extra semi-colon in VS 2005 logf definition */ | 109 /* Work-around an extra semi-colon in VS 2005 logf definition */ |
| 110 #ifdef logf | 110 #ifdef logf |
| 111 #undef logf | 111 #undef logf |
| 112 #define logf(x) ((float)log((double)(x))) | 112 #define logf(x) ((float)log((double)(x))) |
| 113 #endif /* logf */ | 113 #endif /* logf */ |
| (...skipping 19 matching lines...) Expand all Loading... |
| 133 | 133 |
| 134 static INLINE float | 134 static INLINE float |
| 135 roundf(float x) | 135 roundf(float x) |
| 136 { | 136 { |
| 137 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f); | 137 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f); |
| 138 } | 138 } |
| 139 #endif | 139 #endif |
| 140 | 140 |
| 141 #endif /* _MSC_VER */ | 141 #endif /* _MSC_VER */ |
| 142 | 142 |
| 143 | |
| 144 #ifdef PIPE_OS_ANDROID | |
| 145 | |
| 146 static INLINE | |
| 147 double log2(double d) | |
| 148 { | |
| 149 return log(d) * (1.0 / M_LN2); | |
| 150 } | |
| 151 | |
| 152 /* workaround a conflict with main/imports.h */ | |
| 153 #ifdef log2f | |
| 154 #undef log2f | |
| 155 #endif | |
| 156 | |
| 157 static INLINE | |
| 158 float log2f(float f) | |
| 159 { | |
| 160 return logf(f) * (float) (1.0 / M_LN2); | |
| 161 } | |
| 162 | |
| 163 #endif | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 #define POW2_TABLE_SIZE_LOG2 9 | 143 #define POW2_TABLE_SIZE_LOG2 9 |
| 169 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2) | 144 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2) |
| 170 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2) | 145 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2) |
| 171 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2)) | 146 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2)) |
| 172 extern float pow2_table[POW2_TABLE_SIZE]; | 147 extern float pow2_table[POW2_TABLE_SIZE]; |
| 173 | 148 |
| 174 | 149 |
| 175 /** | 150 /** |
| 176 * Initialize math module. This should be called before using any | 151 * Initialize math module. This should be called before using any |
| 177 * other functions in this module. | 152 * other functions in this module. |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 return (ai - bi) >> 1; | 273 return (ai - bi) >> 1; |
| 299 } | 274 } |
| 300 | 275 |
| 301 | 276 |
| 302 /** | 277 /** |
| 303 * Round float to nearest int. | 278 * Round float to nearest int. |
| 304 */ | 279 */ |
| 305 static INLINE int | 280 static INLINE int |
| 306 util_iround(float f) | 281 util_iround(float f) |
| 307 { | 282 { |
| 308 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86) | 283 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86) |
| 309 int r; | 284 int r; |
| 310 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st"); | 285 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st"); |
| 311 return r; | 286 return r; |
| 312 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86) | 287 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86) |
| 313 int r; | 288 int r; |
| 314 _asm { | 289 _asm { |
| 315 fld f | 290 fld f |
| 316 fistp r | 291 fistp r |
| 317 } | 292 } |
| 318 return r; | 293 return r; |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 return r; | 470 return r; |
| 496 } | 471 } |
| 497 | 472 |
| 498 | 473 |
| 499 /* Destructively loop over all of the bits in a mask as in: | 474 /* Destructively loop over all of the bits in a mask as in: |
| 500 * | 475 * |
| 501 * while (mymask) { | 476 * while (mymask) { |
| 502 * int i = u_bit_scan(&mymask); | 477 * int i = u_bit_scan(&mymask); |
| 503 * ... process element i | 478 * ... process element i |
| 504 * } | 479 * } |
| 505 * | 480 * |
| 506 */ | 481 */ |
| 507 static INLINE int u_bit_scan(unsigned *mask) | 482 static INLINE int u_bit_scan(unsigned *mask) |
| 508 { | 483 { |
| 509 int i = ffs(*mask) - 1; | 484 int i = ffs(*mask) - 1; |
| 510 *mask &= ~(1 << i); | 485 *mask &= ~(1 << i); |
| 511 return i; | 486 return i; |
| 512 } | 487 } |
| 513 | 488 |
| 514 | 489 |
| 515 /** | 490 /** |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 return (int32_t)(value * (1<<frac_bits)); | 740 return (int32_t)(value * (1<<frac_bits)); |
| 766 } | 741 } |
| 767 | 742 |
| 768 | 743 |
| 769 | 744 |
| 770 #ifdef __cplusplus | 745 #ifdef __cplusplus |
| 771 } | 746 } |
| 772 #endif | 747 #endif |
| 773 | 748 |
| 774 #endif /* U_MATH_H */ | 749 #endif /* U_MATH_H */ |
| OLD | NEW |