| 1 |
/************************************************************************** |
| 2 |
* |
| 3 |
* Copyright 2008 VMware, Inc. |
| 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 VMWARE 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 |
#include <assert.h> |
| 42 |
#include <float.h> |
| 43 |
#include <math.h> |
| 44 |
#include <stdarg.h> |
| 45 |
|
| 46 |
#include "bitscan.h" |
| 47 |
#include "u_endian.h" /* for UTIL_ARCH_BIG_ENDIAN */ |
| 48 |
|
| 49 |
#ifdef __cplusplus |
| 50 |
extern "C" { |
| 51 |
#endif |
| 52 |
|
| 53 |
|
| 54 |
#ifndef M_SQRT2 |
| 55 |
#define M_SQRT2 1.41421356237309504880 |
| 56 |
#endif |
| 57 |
|
| 58 |
#define POW2_TABLE_SIZE_LOG2 9 |
| 59 |
#define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2) |
| 60 |
#define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2) |
| 61 |
#define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2)) |
| 62 |
extern float pow2_table[POW2_TABLE_SIZE]; |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* Initialize math module. This should be called before using any |
| 67 |
* other functions in this module. |
| 68 |
*/ |
| 69 |
extern void |
| 70 |
util_init_math(void); |
| 71 |
|
| 72 |
|
| 73 |
union fi { |
| 74 |
float f; |
| 75 |
int32_t i; |
| 76 |
uint32_t ui; |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
union di { |
| 81 |
double d; |
| 82 |
int64_t i; |
| 83 |
uint64_t ui; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Extract the IEEE float32 exponent. |
| 89 |
*/ |
| 90 |
static inline signed |
| 91 |
util_get_float32_exponent(float x) |
| 92 |
{ |
| 93 |
union fi f; |
| 94 |
|
| 95 |
f.f = x; |
| 96 |
|
| 97 |
return ((f.ui >> 23) & 0xff) - 127; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Fast version of 2^x |
| 103 |
* Identity: exp2(a + b) = exp2(a) * exp2(b) |
| 104 |
* Let ipart = int(x) |
| 105 |
* Let fpart = x - ipart; |
| 106 |
* So, exp2(x) = exp2(ipart) * exp2(fpart) |
| 107 |
* Compute exp2(ipart) with i << ipart |
| 108 |
* Compute exp2(fpart) with lookup table. |
| 109 |
*/ |
| 110 |
static inline float |
| 111 |
util_fast_exp2(float x) |
| 112 |
{ |
| 113 |
int32_t ipart; |
| 114 |
float fpart, mpart; |
| 115 |
union fi epart; |
| 116 |
|
| 117 |
if(x > 129.00000f) |
| 118 |
return 3.402823466e+38f; |
| 119 |
|
| 120 |
if (x < -126.99999f) |
| 121 |
return 0.0f; |
| 122 |
|
| 123 |
ipart = (int32_t) x; |
| 124 |
fpart = x - (float) ipart; |
| 125 |
|
| 126 |
/* same as |
| 127 |
* epart.f = (float) (1 << ipart) |
| 128 |
* but faster and without integer overflow for ipart > 31 |
| 129 |
*/ |
| 130 |
epart.i = (ipart + 127 ) << 23; |
| 131 |
|
| 132 |
mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)]; |
| 133 |
|
| 134 |
return epart.f * mpart; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Fast approximation to exp(x). |
| 140 |
*/ |
| 141 |
static inline float |
| 142 |
util_fast_exp(float x) |
| 143 |
{ |
| 144 |
const float k = 1.44269f; /* = log2(e) */ |
| 145 |
return util_fast_exp2(k * x); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
#define LOG2_TABLE_SIZE_LOG2 16 |
| 150 |
#define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2) |
| 151 |
#define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1) |
| 152 |
extern float log2_table[LOG2_TABLE_SIZE]; |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Fast approximation to log2(x). |
| 157 |
*/ |
| 158 |
static inline float |
| 159 |
util_fast_log2(float x) |
| 160 |
{ |
| 161 |
union fi num; |
| 162 |
float epart, mpart; |
| 163 |
num.f = x; |
| 164 |
epart = (float)(((num.i & 0x7f800000) >> 23) - 127); |
| 165 |
/* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */ |
| 166 |
mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)]; |
| 167 |
return epart + mpart; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/** |
| 172 |
* Fast approximation to x^y. |
| 173 |
*/ |
| 174 |
static inline float |
| 175 |
util_fast_pow(float x, float y) |
| 176 |
{ |
| 177 |
return util_fast_exp2(util_fast_log2(x) * y); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
/** |
| 182 |
* Floor(x), returned as int. |
| 183 |
*/ |
| 184 |
static inline int |
| 185 |
util_ifloor(float f) |
| 186 |
{ |
| 187 |
#if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__) |
| 188 |
/* |
| 189 |
* IEEE floor for computers that round to nearest or even. |
| 190 |
* 'f' must be between -4194304 and 4194303. |
| 191 |
* This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1", |
| 192 |
* but uses some IEEE specific tricks for better speed. |
| 193 |
* Contributed by Josh Vanderhoof |
| 194 |
*/ |
| 195 |
int ai, bi; |
| 196 |
double af, bf; |
| 197 |
af = (3 << 22) + 0.5 + (double)f; |
| 198 |
bf = (3 << 22) + 0.5 - (double)f; |
| 199 |
/* GCC generates an extra fstp/fld without this. */ |
| 200 |
__asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st"); |
| 201 |
__asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st"); |
| 202 |
return (ai - bi) >> 1; |
| 203 |
#else |
| 204 |
int ai, bi; |
| 205 |
double af, bf; |
| 206 |
union fi u; |
| 207 |
af = (3 << 22) + 0.5 + (double) f; |
| 208 |
bf = (3 << 22) + 0.5 - (double) f; |
| 209 |
u.f = (float) af; ai = u.i; |
| 210 |
u.f = (float) bf; bi = u.i; |
| 211 |
return (ai - bi) >> 1; |
| 212 |
#endif |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
/** |
| 217 |
* Round float to nearest int. |
| 218 |
*/ |
| 219 |
static inline int |
| 220 |
util_iround(float f) |
| 221 |
{ |
| 222 |
#if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86) |
| 223 |
int r; |
| 224 |
__asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st"); |
| 225 |
return r; |
| 226 |
#elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86) |
| 227 |
int r; |
| 228 |
_asm { |
| 229 |
fld f |
| 230 |
fistp r |
| 231 |
} |
| 232 |
return r; |
| 233 |
#else |
| 234 |
if (f >= 0.0f) |
| 235 |
return (int) (f + 0.5f); |
| 236 |
else |
| 237 |
return (int) (f - 0.5f); |
| 238 |
#endif |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
/** |
| 243 |
* Approximate floating point comparison |
| 244 |
*/ |
| 245 |
static inline bool |
| 246 |
util_is_approx(float a, float b, float tol) |
| 247 |
{ |
| 248 |
return fabsf(b - a) <= tol; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
/** |
| 253 |
* util_is_X_inf_or_nan = test if x is NaN or +/- Inf |
| 254 |
* util_is_X_nan = test if x is NaN |
| 255 |
* util_X_inf_sign = return +1 for +Inf, -1 for -Inf, or 0 for not Inf |
| 256 |
* |
| 257 |
* NaN can be checked with x != x, however this fails with the fast math flag |
| 258 |
**/ |
| 259 |
|
| 260 |
|
| 261 |
/** |
| 262 |
* Single-float |
| 263 |
*/ |
| 264 |
static inline bool |
| 265 |
util_is_inf_or_nan(float x) |
| 266 |
{ |
| 267 |
union fi tmp; |
| 268 |
tmp.f = x; |
| 269 |
return (tmp.ui & 0x7f800000) == 0x7f800000; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
static inline bool |
| 274 |
util_is_nan(float x) |
| 275 |
{ |
| 276 |
union fi tmp; |
| 277 |
tmp.f = x; |
| 278 |
return (tmp.ui & 0x7fffffff) > 0x7f800000; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
static inline int |
| 283 |
util_inf_sign(float x) |
| 284 |
{ |
| 285 |
union fi tmp; |
| 286 |
tmp.f = x; |
| 287 |
if ((tmp.ui & 0x7fffffff) != 0x7f800000) { |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
return (x < 0) ? -1 : 1; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
/** |
| 296 |
* Double-float |
| 297 |
*/ |
| 298 |
static inline bool |
| 299 |
util_is_double_inf_or_nan(double x) |
| 300 |
{ |
| 301 |
union di tmp; |
| 302 |
tmp.d = x; |
| 303 |
return (tmp.ui & 0x7ff0000000000000ULL) == 0x7ff0000000000000ULL; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
static inline bool |
| 308 |
util_is_double_nan(double x) |
| 309 |
{ |
| 310 |
union di tmp; |
| 311 |
tmp.d = x; |
| 312 |
return (tmp.ui & 0x7fffffffffffffffULL) > 0x7ff0000000000000ULL; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
static inline int |
| 317 |
util_double_inf_sign(double x) |
| 318 |
{ |
| 319 |
union di tmp; |
| 320 |
tmp.d = x; |
| 321 |
if ((tmp.ui & 0x7fffffffffffffffULL) != 0x7ff0000000000000ULL) { |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
return (x < 0) ? -1 : 1; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
/** |
| 330 |
* Half-float |
| 331 |
*/ |
| 332 |
static inline bool |
| 333 |
util_is_half_inf_or_nan(int16_t x) |
| 334 |
{ |
| 335 |
return (x & 0x7c00) == 0x7c00; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
static inline bool |
| 340 |
util_is_half_nan(int16_t x) |
| 341 |
{ |
| 342 |
return (x & 0x7fff) > 0x7c00; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
static inline int |
| 347 |
util_half_inf_sign(int16_t x) |
| 348 |
{ |
| 349 |
if ((x & 0x7fff) != 0x7c00) { |
| 350 |
return 0; |
| 351 |
} |
| 352 |
|
| 353 |
return (x < 0) ? -1 : 1; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
/** |
| 358 |
* Return float bits. |
| 359 |
*/ |
| 360 |
static inline unsigned |
| 361 |
fui( float f ) |
| 362 |
{ |
| 363 |
union fi fi; |
| 364 |
fi.f = f; |
| 365 |
return fi.ui; |
| 366 |
} |
| 367 |
|
| 368 |
static inline float |
| 369 |
uif(uint32_t ui) |
| 370 |
{ |
| 371 |
union fi fi; |
| 372 |
fi.ui = ui; |
| 373 |
return fi.f; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
/** |
| 378 |
* Convert uint8_t to float in [0, 1]. |
| 379 |
*/ |
| 380 |
static inline float |
| 381 |
ubyte_to_float(uint8_t ub) |
| 382 |
{ |
| 383 |
return (float) ub * (1.0f / 255.0f); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
/** |
| 388 |
* Convert float in [0,1] to uint8_t in [0,255] with clamping. |
| 389 |
*/ |
| 390 |
static inline uint8_t |
| 391 |
float_to_ubyte(float f) |
| 392 |
{ |
| 393 |
/* return 0 for NaN too */ |
| 394 |
if (!(f > 0.0f)) { |
| 395 |
return (uint8_t) 0; |
| 396 |
} |
| 397 |
else if (f >= 1.0f) { |
| 398 |
return (uint8_t) 255; |
| 399 |
} |
| 400 |
else { |
| 401 |
union fi tmp; |
| 402 |
tmp.f = f; |
| 403 |
tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f; |
| 404 |
return (uint8_t) tmp.i; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Convert uint16_t to float in [0, 1]. |
| 410 |
*/ |
| 411 |
static inline float |
| 412 |
ushort_to_float(uint16_t us) |
| 413 |
{ |
| 414 |
return (float) us * (1.0f / 65535.0f); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
/** |
| 419 |
* Convert float in [0,1] to uint16_t in [0,65535] with clamping. |
| 420 |
*/ |
| 421 |
static inline uint16_t |
| 422 |
float_to_ushort(float f) |
| 423 |
{ |
| 424 |
/* return 0 for NaN too */ |
| 425 |
if (!(f > 0.0f)) { |
| 426 |
return (uint16_t) 0; |
| 427 |
} |
| 428 |
else if (f >= 1.0f) { |
| 429 |
return (uint16_t) 65535; |
| 430 |
} |
| 431 |
else { |
| 432 |
union fi tmp; |
| 433 |
tmp.f = f; |
| 434 |
tmp.f = tmp.f * (65535.0f/65536.0f) + 128.0f; |
| 435 |
return (uint16_t) tmp.i; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
static inline float |
| 440 |
byte_to_float_tex(int8_t b) |
| 441 |
{ |
| 442 |
return (b == -128) ? -1.0F : b * 1.0F / 127.0F; |
| 443 |
} |
| 444 |
|
| 445 |
static inline int8_t |
| 446 |
float_to_byte_tex(float f) |
| 447 |
{ |
| 448 |
return (int8_t) (127.0F * f); |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Calc log base 2 |
| 453 |
*/ |
| 454 |
static inline unsigned |
| 455 |
util_logbase2(unsigned n) |
| 456 |
{ |
| 457 |
#if defined(HAVE___BUILTIN_CLZ) |
| 458 |
return ((sizeof(unsigned) * 8 - 1) - __builtin_clz(n | 1)); |
| 459 |
#else |
| 460 |
unsigned pos = 0; |
| 461 |
if (n >= 1<<16) { n >>= 16; pos += 16; } |
| 462 |
if (n >= 1<< 8) { n >>= 8; pos += 8; } |
| 463 |
if (n >= 1<< 4) { n >>= 4; pos += 4; } |
| 464 |
if (n >= 1<< 2) { n >>= 2; pos += 2; } |
| 465 |
if (n >= 1<< 1) { pos += 1; } |
| 466 |
return pos; |
| 467 |
#endif |
| 468 |
} |
| 469 |
|
| 470 |
static inline uint64_t |
| 471 |
util_logbase2_64(uint64_t n) |
| 472 |
{ |
| 473 |
#if defined(HAVE___BUILTIN_CLZLL) |
| 474 |
return ((sizeof(uint64_t) * 8 - 1) - __builtin_clzll(n | 1)); |
| 475 |
#else |
| 476 |
uint64_t pos = 0ull; |
| 477 |
if (n >= 1ull<<32) { n >>= 32; pos += 32; } |
| 478 |
if (n >= 1ull<<16) { n >>= 16; pos += 16; } |
| 479 |
if (n >= 1ull<< 8) { n >>= 8; pos += 8; } |
| 480 |
if (n >= 1ull<< 4) { n >>= 4; pos += 4; } |
| 481 |
if (n >= 1ull<< 2) { n >>= 2; pos += 2; } |
| 482 |
if (n >= 1ull<< 1) { pos += 1; } |
| 483 |
return pos; |
| 484 |
#endif |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
* Returns the ceiling of log n base 2, and 0 when n == 0. Equivalently, |
| 489 |
* returns the smallest x such that n <= 2**x. |
| 490 |
*/ |
| 491 |
static inline unsigned |
| 492 |
util_logbase2_ceil(unsigned n) |
| 493 |
{ |
| 494 |
if (n <= 1) |
| 495 |
return 0; |
| 496 |
|
| 497 |
return 1 + util_logbase2(n - 1); |
| 498 |
} |
| 499 |
|
| 500 |
static inline uint64_t |
| 501 |
util_logbase2_ceil64(uint64_t n) |
| 502 |
{ |
| 503 |
if (n <= 1) |
| 504 |
return 0; |
| 505 |
|
| 506 |
return 1ull + util_logbase2_64(n - 1); |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
* Returns the smallest power of two >= x |
| 511 |
*/ |
| 512 |
static inline unsigned |
| 513 |
util_next_power_of_two(unsigned x) |
| 514 |
{ |
| 515 |
#if defined(HAVE___BUILTIN_CLZ) |
| 516 |
if (x <= 1) |
| 517 |
return 1; |
| 518 |
|
| 519 |
return (1 << ((sizeof(unsigned) * 8) - __builtin_clz(x - 1))); |
| 520 |
#else |
| 521 |
unsigned val = x; |
| 522 |
|
| 523 |
if (x <= 1) |
| 524 |
return 1; |
| 525 |
|
| 526 |
if (util_is_power_of_two_or_zero(x)) |
| 527 |
return x; |
| 528 |
|
| 529 |
val--; |
| 530 |
val = (val >> 1) | val; |
| 531 |
val = (val >> 2) | val; |
| 532 |
val = (val >> 4) | val; |
| 533 |
val = (val >> 8) | val; |
| 534 |
val = (val >> 16) | val; |
| 535 |
val++; |
| 536 |
return val; |
| 537 |
#endif |
| 538 |
} |
| 539 |
|
| 540 |
static inline uint64_t |
| 541 |
util_next_power_of_two64(uint64_t x) |
| 542 |
{ |
| 543 |
#if defined(HAVE___BUILTIN_CLZLL) |
| 544 |
if (x <= 1) |
| 545 |
return 1; |
| 546 |
|
| 547 |
return (1ull << ((sizeof(uint64_t) * 8) - __builtin_clzll(x - 1))); |
| 548 |
#else |
| 549 |
uint64_t val = x; |
| 550 |
|
| 551 |
if (x <= 1) |
| 552 |
return 1; |
| 553 |
|
| 554 |
if (util_is_power_of_two_or_zero64(x)) |
| 555 |
return x; |
| 556 |
|
| 557 |
val--; |
| 558 |
val = (val >> 1) | val; |
| 559 |
val = (val >> 2) | val; |
| 560 |
val = (val >> 4) | val; |
| 561 |
val = (val >> 8) | val; |
| 562 |
val = (val >> 16) | val; |
| 563 |
val = (val >> 32) | val; |
| 564 |
val++; |
| 565 |
return val; |
| 566 |
#endif |
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* Reverse bits in n |
| 571 |
* Algorithm taken from: |
| 572 |
* http://stackoverflow.com/questions/9144800/c-reverse-bits-in-unsigned-integer |
| 573 |
*/ |
| 574 |
static inline unsigned |
| 575 |
util_bitreverse(unsigned n) |
| 576 |
{ |
| 577 |
n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1); |
| 578 |
n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2); |
| 579 |
n = ((n >> 4) & 0x0f0f0f0fu) | ((n & 0x0f0f0f0fu) << 4); |
| 580 |
n = ((n >> 8) & 0x00ff00ffu) | ((n & 0x00ff00ffu) << 8); |
| 581 |
n = ((n >> 16) & 0xffffu) | ((n & 0xffffu) << 16); |
| 582 |
return n; |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* Convert from little endian to CPU byte order. |
| 587 |
*/ |
| 588 |
|
| 589 |
#if UTIL_ARCH_BIG_ENDIAN |
| 590 |
#define util_le64_to_cpu(x) util_bswap64(x) |
| 591 |
#define util_le32_to_cpu(x) util_bswap32(x) |
| 592 |
#define util_le16_to_cpu(x) util_bswap16(x) |
| 593 |
#else |
| 594 |
#define util_le64_to_cpu(x) (x) |
| 595 |
#define util_le32_to_cpu(x) (x) |
| 596 |
#define util_le16_to_cpu(x) (x) |
| 597 |
#endif |
| 598 |
|
| 599 |
#define util_cpu_to_le64(x) util_le64_to_cpu(x) |
| 600 |
#define util_cpu_to_le32(x) util_le32_to_cpu(x) |
| 601 |
#define util_cpu_to_le16(x) util_le16_to_cpu(x) |
| 602 |
|
| 603 |
/** |
| 604 |
* Reverse byte order of a 32 bit word. |
| 605 |
*/ |
| 606 |
static inline uint32_t |
| 607 |
util_bswap32(uint32_t n) |
| 608 |
{ |
| 609 |
#if defined(HAVE___BUILTIN_BSWAP32) |
| 610 |
return __builtin_bswap32(n); |
| 611 |
#else |
| 612 |
return (n >> 24) | |
| 613 |
((n >> 8) & 0x0000ff00) | |
| 614 |
((n << 8) & 0x00ff0000) | |
| 615 |
(n << 24); |
| 616 |
#endif |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* Reverse byte order of a 64bit word. |
| 621 |
*/ |
| 622 |
static inline uint64_t |
| 623 |
util_bswap64(uint64_t n) |
| 624 |
{ |
| 625 |
#if defined(HAVE___BUILTIN_BSWAP64) |
| 626 |
return __builtin_bswap64(n); |
| 627 |
#else |
| 628 |
return ((uint64_t)util_bswap32((uint32_t)n) << 32) | |
| 629 |
util_bswap32((n >> 32)); |
| 630 |
#endif |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
/** |
| 635 |
* Reverse byte order of a 16 bit word. |
| 636 |
*/ |
| 637 |
static inline uint16_t |
| 638 |
util_bswap16(uint16_t n) |
| 639 |
{ |
| 640 |
return (n >> 8) | |
| 641 |
(n << 8); |
| 642 |
} |
| 643 |
|
| 644 |
static inline void* |
| 645 |
util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n) |
| 646 |
{ |
| 647 |
#if UTIL_ARCH_BIG_ENDIAN |
| 648 |
size_t i, e; |
| 649 |
assert(n % 4 == 0); |
| 650 |
|
| 651 |
for (i = 0, e = n / 4; i < e; i++) { |
| 652 |
uint32_t * restrict d = (uint32_t* restrict)dest; |
| 653 |
const uint32_t * restrict s = (const uint32_t* restrict)src; |
| 654 |
d[i] = util_bswap32(s[i]); |
| 655 |
} |
| 656 |
return dest; |
| 657 |
#else |
| 658 |
return memcpy(dest, src, n); |
| 659 |
#endif |
| 660 |
} |
| 661 |
|
| 662 |
/** |
| 663 |
* Clamp X to [MIN, MAX]. |
| 664 |
* This is a macro to allow float, int, uint, etc. types. |
| 665 |
* We arbitrarily turn NaN into MIN. |
| 666 |
*/ |
| 667 |
#define CLAMP( X, MIN, MAX ) ( (X)>(MIN) ? ((X)>(MAX) ? (MAX) : (X)) : (MIN) ) |
| 668 |
|
| 669 |
#define MIN2( A, B ) ( (A)<(B) ? (A) : (B) ) |
| 670 |
#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) ) |
| 671 |
|
| 672 |
#define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C)) |
| 673 |
#define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C)) |
| 674 |
|
| 675 |
#define MIN4( A, B, C, D ) ((A) < (B) ? MIN3(A, C, D) : MIN3(B, C, D)) |
| 676 |
#define MAX4( A, B, C, D ) ((A) > (B) ? MAX3(A, C, D) : MAX3(B, C, D)) |
| 677 |
|
| 678 |
|
| 679 |
/** |
| 680 |
* Align a value up to an alignment value |
| 681 |
* |
| 682 |
* If \c value is not already aligned to the requested alignment value, it |
| 683 |
* will be rounded up. |
| 684 |
* |
| 685 |
* \param value Value to be rounded |
| 686 |
* \param alignment Alignment value to be used. This must be a power of two. |
| 687 |
* |
| 688 |
* \sa ROUND_DOWN_TO() |
| 689 |
*/ |
| 690 |
static inline uintptr_t |
| 691 |
ALIGN(uintptr_t value, int32_t alignment) |
| 692 |
{ |
| 693 |
assert(util_is_power_of_two_nonzero(alignment)); |
| 694 |
return (((value) + (alignment) - 1) & ~((alignment) - 1)); |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
* Like ALIGN(), but works with a non-power-of-two alignment. |
| 699 |
*/ |
| 700 |
static inline uintptr_t |
| 701 |
ALIGN_NPOT(uintptr_t value, int32_t alignment) |
| 702 |
{ |
| 703 |
assert(alignment > 0); |
| 704 |
return (value + alignment - 1) / alignment * alignment; |
| 705 |
} |
| 706 |
|
| 707 |
/** |
| 708 |
* Align a value down to an alignment value |
| 709 |
* |
| 710 |
* If \c value is not already aligned to the requested alignment value, it |
| 711 |
* will be rounded down. |
| 712 |
* |
| 713 |
* \param value Value to be rounded |
| 714 |
* \param alignment Alignment value to be used. This must be a power of two. |
| 715 |
* |
| 716 |
* \sa ALIGN() |
| 717 |
*/ |
| 718 |
static inline uintptr_t |
| 719 |
ROUND_DOWN_TO(uintptr_t value, int32_t alignment) |
| 720 |
{ |
| 721 |
assert(util_is_power_of_two_nonzero(alignment)); |
| 722 |
return ((value) & ~(alignment - 1)); |
| 723 |
} |
| 724 |
|
| 725 |
/** |
| 726 |
* Align a value, only works pot alignemnts. |
| 727 |
*/ |
| 728 |
static inline int |
| 729 |
align(int value, int alignment) |
| 730 |
{ |
| 731 |
return (value + alignment - 1) & ~(alignment - 1); |
| 732 |
} |
| 733 |
|
| 734 |
static inline uint64_t |
| 735 |
align64(uint64_t value, unsigned alignment) |
| 736 |
{ |
| 737 |
return (value + alignment - 1) & ~((uint64_t)alignment - 1); |
| 738 |
} |
| 739 |
|
| 740 |
/** |
| 741 |
* Works like align but on npot alignments. |
| 742 |
*/ |
| 743 |
static inline size_t |
| 744 |
util_align_npot(size_t value, size_t alignment) |
| 745 |
{ |
| 746 |
if (value % alignment) |
| 747 |
return value + (alignment - (value % alignment)); |
| 748 |
return value; |
| 749 |
} |
| 750 |
|
| 751 |
static inline unsigned |
| 752 |
u_minify(unsigned value, unsigned levels) |
| 753 |
{ |
| 754 |
return MAX2(1, value >> levels); |
| 755 |
} |
| 756 |
|
| 757 |
#ifndef COPY_4V |
| 758 |
#define COPY_4V( DST, SRC ) \ |
| 759 |
do { \ |
| 760 |
(DST)[0] = (SRC)[0]; \ |
| 761 |
(DST)[1] = (SRC)[1]; \ |
| 762 |
(DST)[2] = (SRC)[2]; \ |
| 763 |
(DST)[3] = (SRC)[3]; \ |
| 764 |
} while (0) |
| 765 |
#endif |
| 766 |
|
| 767 |
|
| 768 |
#ifndef COPY_4FV |
| 769 |
#define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC) |
| 770 |
#endif |
| 771 |
|
| 772 |
|
| 773 |
#ifndef ASSIGN_4V |
| 774 |
#define ASSIGN_4V( DST, V0, V1, V2, V3 ) \ |
| 775 |
do { \ |
| 776 |
(DST)[0] = (V0); \ |
| 777 |
(DST)[1] = (V1); \ |
| 778 |
(DST)[2] = (V2); \ |
| 779 |
(DST)[3] = (V3); \ |
| 780 |
} while (0) |
| 781 |
#endif |
| 782 |
|
| 783 |
|
| 784 |
static inline uint32_t |
| 785 |
util_unsigned_fixed(float value, unsigned frac_bits) |
| 786 |
{ |
| 787 |
return value < 0 ? 0 : (uint32_t)(value * (1<<frac_bits)); |
| 788 |
} |
| 789 |
|
| 790 |
static inline int32_t |
| 791 |
util_signed_fixed(float value, unsigned frac_bits) |
| 792 |
{ |
| 793 |
return (int32_t)(value * (1<<frac_bits)); |
| 794 |
} |
| 795 |
|
| 796 |
unsigned |
| 797 |
util_fpstate_get(void); |
| 798 |
unsigned |
| 799 |
util_fpstate_set_denorms_to_zero(unsigned current_fpstate); |
| 800 |
void |
| 801 |
util_fpstate_set(unsigned fpstate); |
| 802 |
|
| 803 |
/** |
| 804 |
* For indexed draw calls, return true if the vertex count to be drawn is |
| 805 |
* much lower than the vertex count that has to be uploaded, meaning |
| 806 |
* that the driver should flatten indices instead of trying to upload |
| 807 |
* a too big range. |
| 808 |
* |
| 809 |
* This is used by vertex upload code in u_vbuf and glthread. |
| 810 |
*/ |
| 811 |
static inline bool |
| 812 |
util_is_vbo_upload_ratio_too_large(unsigned draw_vertex_count, |
| 813 |
unsigned upload_vertex_count) |
| 814 |
{ |
| 815 |
if (draw_vertex_count > 1024) |
| 816 |
return upload_vertex_count > draw_vertex_count * 4; |
| 817 |
else if (draw_vertex_count > 32) |
| 818 |
return upload_vertex_count > draw_vertex_count * 8; |
| 819 |
else |
| 820 |
return upload_vertex_count > draw_vertex_count * 16; |
| 821 |
} |
| 822 |
|
| 823 |
#ifdef __cplusplus |
| 824 |
} |
| 825 |
#endif |
| 826 |
|
| 827 |
#endif /* U_MATH_H */ |