1/* 2 * Mesa 3-D graphics library 3 * Version: 7.5 4 * 5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25 26/** 27 * \file imports.h 28 * Standard C library function wrappers. 29 * 30 * This file provides wrappers for all the standard C library functions 31 * like malloc(), free(), printf(), getenv(), etc. 32 */ 33 34 35#ifndef IMPORTS_H 36#define IMPORTS_H 37 38 39#include "compiler.h" 40#include "glheader.h" 41#include "errors.h" 42 43#ifdef __cplusplus 44extern "C" { 45#endif 46 47 48/**********************************************************************/ 49/** Memory macros */ 50/*@{*/ 51 52/** Allocate \p BYTES bytes */ 53#define MALLOC(BYTES) malloc(BYTES) 54/** Allocate and zero \p BYTES bytes */ 55#define CALLOC(BYTES) calloc(1, BYTES) 56/** Allocate a structure of type \p T */ 57#define MALLOC_STRUCT(T) (struct T *) malloc(sizeof(struct T)) 58/** Allocate and zero a structure of type \p T */ 59#define CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T)) 60/** Free memory */ 61#define FREE(PTR) free(PTR) 62 63/*@}*/ 64 65 66/* 67 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers 68 * as offsets into buffer stores. Since the vertex array pointer and 69 * buffer store pointer are both pointers and we need to add them, we use 70 * this macro. 71 * Both pointers/offsets are expressed in bytes. 72 */ 73#define ADD_POINTERS(A, B) ( (GLubyte *) (A) + (uintptr_t) (B) ) 74 75 76/** 77 * Sometimes we treat GLfloats as GLints. On x86 systems, moving a float 78 * as a int (thereby using integer registers instead of FP registers) is 79 * a performance win. Typically, this can be done with ordinary casts. 80 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0) 81 * these casts generate warnings. 82 * The following union typedef is used to solve that. 83 */ 84typedef union { GLfloat f; GLint i; GLuint u; } fi_type; 85 86 87 88/********************************************************************** 89 * Math macros 90 */ 91 92#define MAX_GLUSHORT 0xffff 93#define MAX_GLUINT 0xffffffff 94 95/* Degrees to radians conversion: */ 96#define DEG2RAD (M_PI/180.0) 97 98 99/*** 100 *** SQRTF: single-precision square root 101 ***/ 102#define SQRTF(X) (float) sqrt((float) (X)) 103 104 105/*** 106 *** INV_SQRTF: single-precision inverse square root 107 ***/ 108#define INV_SQRTF(X) (1.0F / SQRTF(X)) 109 110 111/** 112 * \name Work-arounds for platforms that lack C99 math functions 113 */ 114/*@{*/ 115#if (!defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE < 600)) && !defined(_ISOC99_SOURCE) \ 116 && (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)) \ 117 && (!defined(_MSC_VER) || (_MSC_VER < 1400)) 118#define acosf(f) ((float) acos(f)) 119#define asinf(f) ((float) asin(f)) 120#define atan2f(x,y) ((float) atan2(x,y)) 121#define atanf(f) ((float) atan(f)) 122#define ceilf(f) ((float) ceil(f)) 123#define cosf(f) ((float) cos(f)) 124#define coshf(f) ((float) cosh(f)) 125#define expf(f) ((float) exp(f)) 126#define exp2f(f) ((float) exp2(f)) 127#define floorf(f) ((float) floor(f)) 128#define logf(f) ((float) log(f)) 129 130#ifdef ANDROID 131#define log2f(f) (logf(f) * (float) (1.0 / M_LN2)) 132#else 133#define log2f(f) ((float) log2(f)) 134#endif 135 136#define powf(x,y) ((float) pow(x,y)) 137#define sinf(f) ((float) sin(f)) 138#define sinhf(f) ((float) sinh(f)) 139#define sqrtf(f) ((float) sqrt(f)) 140#define tanf(f) ((float) tan(f)) 141#define tanhf(f) ((float) tanh(f)) 142#define acoshf(f) ((float) acosh(f)) 143#define asinhf(f) ((float) asinh(f)) 144#define atanhf(f) ((float) atanh(f)) 145#endif 146 147#if defined(_MSC_VER) 148#if _MSC_VER < 1800 /* Not required on VS2013 and above. */ 149static inline float truncf(float x) { return x < 0.0f ? ceilf(x) : floorf(x); } 150static inline float exp2f(float x) { return powf(2.0f, x); } 151static inline float log2f(float x) { return logf(x) * 1.442695041f; } 152static inline float asinhf(float x) { return logf(x + sqrtf(x * x + 1.0f)); } 153static inline float acoshf(float x) { return logf(x + sqrtf(x * x - 1.0f)); } 154static inline float atanhf(float x) { return (logf(1.0f + x) - logf(1.0f - x)) / 2.0f; } 155static inline int isblank(int ch) { return ch == ' ' || ch == '\t'; } 156#define strtoll(p, e, b) _strtoi64(p, e, b) 157#endif /* _MSC_VER < 1800 */ 158#endif 159/*@}*/ 160 161/*** 162 *** LOG2: Log base 2 of float 163 ***/ 164#ifdef USE_IEEE 165#if 0 166/* This is pretty fast, but not accurate enough (only 2 fractional bits). 167 * Based on code from http://www.stereopsis.com/log2.html 168 */ 169static inline GLfloat LOG2(GLfloat x) 170{ 171 const GLfloat y = x * x * x * x; 172 const GLuint ix = *((GLuint *) &y); 173 const GLuint exp = (ix >> 23) & 0xFF; 174 const GLint log2 = ((GLint) exp) - 127; 175 return (GLfloat) log2 * (1.0 / 4.0); /* 4, because of x^4 above */ 176} 177#endif 178/* Pretty fast, and accurate. 179 * Based on code from http://www.flipcode.com/totd/ 180 */ 181static inline GLfloat LOG2(GLfloat val) 182{ 183 fi_type num; 184 GLint log_2; 185 num.f = val; 186 log_2 = ((num.i >> 23) & 255) - 128; 187 num.i &= ~(255 << 23); 188 num.i += 127 << 23; 189 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3; 190 return num.f + log_2; 191} 192#else 193/* 194 * NOTE: log_base_2(x) = log(x) / log(2) 195 * NOTE: 1.442695 = 1/log(2). 196 */ 197#define LOG2(x) ((GLfloat) (log(x) * 1.442695F)) 198#endif 199 200 201/*** 202 *** IS_INF_OR_NAN: test if float is infinite or NaN 203 ***/ 204#ifdef USE_IEEE 205static inline int IS_INF_OR_NAN( float x ) 206{ 207 fi_type tmp; 208 tmp.f = x; 209 return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31); 210} 211#elif defined(isfinite) 212#define IS_INF_OR_NAN(x) (!isfinite(x)) 213#elif defined(finite) 214#define IS_INF_OR_NAN(x) (!finite(x)) 215#elif defined(__VMS) 216#define IS_INF_OR_NAN(x) (!finite(x)) 217#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 218#define IS_INF_OR_NAN(x) (!isfinite(x)) 219#else 220#define IS_INF_OR_NAN(x) (!finite(x)) 221#endif 222 223 224/*** 225 *** IS_NEGATIVE: test if float is negative 226 ***/ 227#if defined(USE_IEEE) 228static inline int GET_FLOAT_BITS( float x ) 229{ 230 fi_type fi; 231 fi.f = x; 232 return fi.i; 233} 234#define IS_NEGATIVE(x) (GET_FLOAT_BITS(x) < 0) 235#else 236#define IS_NEGATIVE(x) (x < 0.0F) 237#endif 238 239 240/*** 241 *** DIFFERENT_SIGNS: test if two floats have opposite signs 242 ***/ 243#if defined(USE_IEEE) 244#define DIFFERENT_SIGNS(x,y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1<<31)) 245#else 246/* Could just use (x*y<0) except for the flatshading requirements. 247 * Maybe there's a better way? 248 */ 249#define DIFFERENT_SIGNS(x,y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F) 250#endif 251 252 253/*** 254 *** CEILF: ceiling of float 255 *** FLOORF: floor of float 256 *** FABSF: absolute value of float 257 *** LOGF: the natural logarithm (base e) of the value 258 *** EXPF: raise e to the value 259 *** LDEXPF: multiply value by an integral power of two 260 *** FREXPF: extract mantissa and exponent from value 261 ***/ 262#if defined(__gnu_linux__) 263/* C99 functions */ 264#define CEILF(x) ceilf(x) 265#define FLOORF(x) floorf(x) 266#define FABSF(x) fabsf(x) 267#define LOGF(x) logf(x) 268#define EXPF(x) expf(x) 269#define LDEXPF(x,y) ldexpf(x,y) 270#define FREXPF(x,y) frexpf(x,y) 271#else 272#define CEILF(x) ((GLfloat) ceil(x)) 273#define FLOORF(x) ((GLfloat) floor(x)) 274#define FABSF(x) ((GLfloat) fabs(x)) 275#define LOGF(x) ((GLfloat) log(x)) 276#define EXPF(x) ((GLfloat) exp(x)) 277#define LDEXPF(x,y) ((GLfloat) ldexp(x,y)) 278#define FREXPF(x,y) ((GLfloat) frexp(x,y)) 279#endif 280 281 282/** 283 * Convert float to int by rounding to nearest integer, away from zero. 284 */ 285static inline int IROUND(float f) 286{ 287 return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F)); 288} 289 290 291/** 292 * Convert float to int64 by rounding to nearest integer. 293 */ 294static inline GLint64 IROUND64(float f) 295{ 296 return (GLint64) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F)); 297} 298 299 300/** 301 * Convert positive float to int by rounding to nearest integer. 302 */ 303static inline int IROUND_POS(float f) 304{ 305 assert(f >= 0.0F); 306 return (int) (f + 0.5F); 307} 308 309 310/** 311 * Convert float to int using a fast method. The rounding mode may vary. 312 * XXX We could use an x86-64/SSE2 version here. 313 */ 314#if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__) 315static inline int F_TO_I(float f) 316{ 317 int r; 318 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st"); 319 return r; 320} 321#elif defined(USE_X86_ASM) && defined(_MSC_VER) 322static inline int F_TO_I(float f) 323{ 324 int r; 325 _asm { 326 fld f 327 fistp r 328 } 329 return r; 330} 331#elif defined(__WATCOMC__) && defined(__386__) 332long F_TO_I(float f); 333#pragma aux iround = \ 334 "push eax" \ 335 "fistp dword ptr [esp]" \ 336 "pop eax" \ 337 parm [8087] \ 338 value [eax] \ 339 modify exact [eax]; 340#else 341#define F_TO_I(f) IROUND(f) 342#endif 343 344 345/*** 346 *** IFLOOR: return (as an integer) floor of float 347 ***/ 348#if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__) 349/* 350 * IEEE floor for computers that round to nearest or even. 351 * 'f' must be between -4194304 and 4194303. 352 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1", 353 * but uses some IEEE specific tricks for better speed. 354 * Contributed by Josh Vanderhoof 355 */ 356static inline int ifloor(float f) 357{ 358 int ai, bi; 359 double af, bf; 360 af = (3 << 22) + 0.5 + (double)f; 361 bf = (3 << 22) + 0.5 - (double)f; 362 /* GCC generates an extra fstp/fld without this. */ 363 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st"); 364 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st"); 365 return (ai - bi) >> 1; 366} 367#define IFLOOR(x) ifloor(x) 368#elif defined(USE_IEEE) 369static inline int ifloor(float f) 370{ 371 int ai, bi; 372 double af, bf; 373 fi_type u; 374 375 af = (3 << 22) + 0.5 + (double)f; 376 bf = (3 << 22) + 0.5 - (double)f; 377 u.f = (float) af; ai = u.i; 378 u.f = (float) bf; bi = u.i; 379 return (ai - bi) >> 1; 380} 381#define IFLOOR(x) ifloor(x) 382#else 383static inline int ifloor(float f) 384{ 385 int i = IROUND(f); 386 return (i > f) ? i - 1 : i; 387} 388#define IFLOOR(x) ifloor(x) 389#endif 390 391 392/*** 393 *** ICEIL: return (as an integer) ceiling of float 394 ***/ 395#if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__) 396/* 397 * IEEE ceil for computers that round to nearest or even. 398 * 'f' must be between -4194304 and 4194303. 399 * This ceil operation is done by "(iround(f + .5) + iround(f - .5) + 1) >> 1", 400 * but uses some IEEE specific tricks for better speed. 401 * Contributed by Josh Vanderhoof 402 */ 403static inline int iceil(float f) 404{ 405 int ai, bi; 406 double af, bf; 407 af = (3 << 22) + 0.5 + (double)f; 408 bf = (3 << 22) + 0.5 - (double)f; 409 /* GCC generates an extra fstp/fld without this. */ 410 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st"); 411 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st"); 412 return (ai - bi + 1) >> 1; 413} 414#define ICEIL(x) iceil(x) 415#elif defined(USE_IEEE) 416static inline int iceil(float f) 417{ 418 int ai, bi; 419 double af, bf; 420 fi_type u; 421 af = (3 << 22) + 0.5 + (double)f; 422 bf = (3 << 22) + 0.5 - (double)f; 423 u.f = (float) af; ai = u.i; 424 u.f = (float) bf; bi = u.i; 425 return (ai - bi + 1) >> 1; 426} 427#define ICEIL(x) iceil(x) 428#else 429static inline int iceil(float f) 430{ 431 int i = IROUND(f); 432 return (i < f) ? i + 1 : i; 433} 434#define ICEIL(x) iceil(x) 435#endif 436 437 438/** 439 * Is x a power of two? 440 */ 441static inline int 442_mesa_is_pow_two(int x) 443{ 444 return !(x & (x - 1)); 445} 446 447/** 448 * Round given integer to next higer power of two 449 * If X is zero result is undefined. 450 * 451 * Source for the fallback implementation is 452 * Sean Eron Anderson's webpage "Bit Twiddling Hacks" 453 * http://graphics.stanford.edu/~seander/bithacks.html 454 * 455 * When using builtin function have to do some work 456 * for case when passed values 1 to prevent hiting 457 * undefined result from __builtin_clz. Undefined 458 * results would be different depending on optimization 459 * level used for build. 460 */ 461static inline int32_t 462_mesa_next_pow_two_32(uint32_t x) 463{ 464#if defined(__GNUC__) && \ 465 ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */ 466 uint32_t y = (x != 1); 467 return (1 + y) << ((__builtin_clz(x - y) ^ 31) ); 468#else 469 x--; 470 x |= x >> 1; 471 x |= x >> 2; 472 x |= x >> 4; 473 x |= x >> 8; 474 x |= x >> 16; 475 x++; 476 return x; 477#endif 478} 479 480static inline int64_t 481_mesa_next_pow_two_64(uint64_t x) 482{ 483#if defined(__GNUC__) && \ 484 ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */ 485 uint64_t y = (x != 1); 486 if (sizeof(x) == sizeof(long)) 487 return (1 + y) << ((__builtin_clzl(x - y) ^ 63)); 488 else 489 return (1 + y) << ((__builtin_clzll(x - y) ^ 63)); 490#else 491 x--; 492 x |= x >> 1; 493 x |= x >> 2; 494 x |= x >> 4; 495 x |= x >> 8; 496 x |= x >> 16; 497 x |= x >> 32; 498 x++; 499 return x; 500#endif 501} 502 503 504/* 505 * Returns the floor form of binary logarithm for a 32-bit integer. 506 */ 507static inline GLuint 508_mesa_logbase2(GLuint n) 509{ 510#if defined(__GNUC__) && \ 511 ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */ 512 return (31 - __builtin_clz(n | 1)); 513#else 514 GLuint pos = 0; 515 if (n >= 1<<16) { n >>= 16; pos += 16; } 516 if (n >= 1<< 8) { n >>= 8; pos += 8; } 517 if (n >= 1<< 4) { n >>= 4; pos += 4; } 518 if (n >= 1<< 2) { n >>= 2; pos += 2; } 519 if (n >= 1<< 1) { pos += 1; } 520 return pos; 521#endif 522} 523 524 525/** 526 * Return 1 if this is a little endian machine, 0 if big endian. 527 */ 528static inline GLboolean 529_mesa_little_endian(void) 530{ 531 const GLuint ui = 1; /* intentionally not static */ 532 return *((const GLubyte *) &ui); 533} 534 535 536 537/********************************************************************** 538 * Functions 539 */ 540 541extern void * 542_mesa_align_malloc( size_t bytes, unsigned long alignment ); 543 544extern void * 545_mesa_align_calloc( size_t bytes, unsigned long alignment ); 546 547extern void 548_mesa_align_free( void *ptr ); 549 550extern void * 551_mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize, 552 unsigned long alignment); 553 554extern void * 555_mesa_exec_malloc( GLuint size ); 556 557extern void 558_mesa_exec_free( void *addr ); 559 560extern void * 561_mesa_realloc( void *oldBuffer, size_t oldSize, size_t newSize ); 562 563 564#ifndef FFS_DEFINED 565#define FFS_DEFINED 1 566#ifdef __GNUC__ 567 568#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(ANDROID) || defined(__APPLE__) 569#define ffs __builtin_ffs 570#define ffsll __builtin_ffsll 571#endif 572 573#else 574 575extern int ffs(int i); 576extern int ffsll(long long int i); 577 578#endif /*__ GNUC__ */ 579#endif /* FFS_DEFINED */ 580 581 582#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */ 583#define _mesa_bitcount(i) __builtin_popcount(i) 584#define _mesa_bitcount_64(i) __builtin_popcountll(i) 585#else 586extern unsigned int 587_mesa_bitcount(unsigned int n); 588extern unsigned int 589_mesa_bitcount_64(uint64_t n); 590#endif 591 592/** 593 * Find the last (most significant) bit set in a word. 594 * 595 * Essentially ffs() in the reverse direction. 596 */ 597static inline unsigned int 598_mesa_fls(unsigned int n) 599{ 600#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) 601 return n == 0 ? 0 : 32 - __builtin_clz(n); 602#else 603 unsigned int v = 1; 604 605 if (n == 0) 606 return 0; 607 608 while (n >>= 1) 609 v++; 610 611 return v; 612#endif 613} 614 615extern GLhalfARB 616_mesa_float_to_half(float f); 617 618extern float 619_mesa_half_to_float(GLhalfARB h); 620 621 622extern void * 623_mesa_bsearch( const void *key, const void *base, size_t nmemb, size_t size, 624 int (*compar)(const void *, const void *) ); 625 626extern char * 627_mesa_getenv( const char *var ); 628 629extern char * 630_mesa_strdup( const char *s ); 631 632extern float 633_mesa_strtof( const char *s, char **end ); 634 635extern unsigned int 636_mesa_str_checksum(const char *str); 637 638extern int 639_mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4); 640 641extern int 642_mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg); 643 644 645#if defined(_MSC_VER) && !defined(snprintf) 646#define snprintf _snprintf 647#endif 648 649 650#ifdef __cplusplus 651} 652#endif 653 654 655#endif /* IMPORTS_H */ 656