Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2008  Brian Paul   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 "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22.  * 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
  44. extern "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.  */
  84. typedef 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.  * \name Work-arounds for platforms that lack C99 math functions
  101.  */
  102. /*@{*/
  103. #if (!defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE < 600)) && !defined(_ISOC99_SOURCE) \
  104.    && (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)) \
  105.    && (!defined(_MSC_VER) || (_MSC_VER < 1400))
  106. #define acosf(f) ((float) acos(f))
  107. #define asinf(f) ((float) asin(f))
  108. #define atan2f(x,y) ((float) atan2(x,y))
  109. #define atanf(f) ((float) atan(f))
  110. #define ceilf(f) ((float) ceil(f))
  111. #define cosf(f) ((float) cos(f))
  112. #define coshf(f) ((float) cosh(f))
  113. #define expf(f) ((float) exp(f))
  114. #define exp2f(f) ((float) exp2(f))
  115. #define floorf(f) ((float) floor(f))
  116. #define logf(f) ((float) log(f))
  117.  
  118.  
  119. #define powf(x,y) ((float) pow(x,y))
  120. #define sinf(f) ((float) sin(f))
  121. #define sinhf(f) ((float) sinh(f))
  122. #define sqrtf(f) ((float) sqrt(f))
  123. #define tanf(f) ((float) tan(f))
  124. #define tanhf(f) ((float) tanh(f))
  125. #define acoshf(f) ((float) acosh(f))
  126. #define asinhf(f) ((float) asinh(f))
  127. #define atanhf(f) ((float) atanh(f))
  128. #endif
  129.  
  130. #if defined(_MSC_VER)
  131. static inline float truncf(float x) { return x < 0.0f ? ceilf(x) : floorf(x); }
  132. static inline float exp2f(float x) { return powf(2.0f, x); }
  133. static inline float log2f(float x) { return logf(x) * 1.442695041f; }
  134. static inline float asinhf(float x) { return logf(x + sqrtf(x * x + 1.0f)); }
  135. static inline float acoshf(float x) { return logf(x + sqrtf(x * x - 1.0f)); }
  136. static inline float atanhf(float x) { return (logf(1.0f + x) - logf(1.0f - x)) / 2.0f; }
  137. static inline int isblank(int ch) { return ch == ' ' || ch == '\t'; }
  138. #define strtoll(p, e, b) _strtoi64(p, e, b)
  139. #endif
  140. /*@}*/
  141.  
  142.  
  143. /*
  144.  * signbit() is a macro on Linux.  Not available on Windows.
  145.  */
  146. #ifndef signbit
  147. #define signbit(x) ((x) < 0.0f)
  148. #endif
  149.  
  150.  
  151. /** single-precision inverse square root */
  152. static inline float
  153. INV_SQRTF(float x)
  154. {
  155.    /* XXX we could try Quake's fast inverse square root function here */
  156.    return 1.0F / sqrtf(x);
  157. }
  158.  
  159.  
  160. /***
  161.  *** LOG2: Log base 2 of float
  162.  ***/
  163. static inline GLfloat LOG2(GLfloat x)
  164. {
  165. #ifdef USE_IEEE
  166. #if 0
  167.    /* This is pretty fast, but not accurate enough (only 2 fractional bits).
  168.     * Based on code from http://www.stereopsis.com/log2.html
  169.     */
  170.    const GLfloat y = x * x * x * x;
  171.    const GLuint ix = *((GLuint *) &y);
  172.    const GLuint exp = (ix >> 23) & 0xFF;
  173.    const GLint log2 = ((GLint) exp) - 127;
  174.    return (GLfloat) log2 * (1.0 / 4.0);  /* 4, because of x^4 above */
  175. #endif
  176.    /* Pretty fast, and accurate.
  177.     * Based on code from http://www.flipcode.com/totd/
  178.     */
  179.    fi_type num;
  180.    GLint log_2;
  181.    num.f = x;
  182.    log_2 = ((num.i >> 23) & 255) - 128;
  183.    num.i &= ~(255 << 23);
  184.    num.i += 127 << 23;
  185.    num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
  186.    return num.f + log_2;
  187. #else
  188.    /*
  189.     * NOTE: log_base_2(x) = log(x) / log(2)
  190.     * NOTE: 1.442695 = 1/log(2).
  191.     */
  192.    return (GLfloat) (log(x) * 1.442695F);
  193. #endif
  194. }
  195.  
  196.  
  197.  
  198. /***
  199.  *** IS_INF_OR_NAN: test if float is infinite or NaN
  200.  ***/
  201. #ifdef USE_IEEE
  202. static inline int IS_INF_OR_NAN( float x )
  203. {
  204.    fi_type tmp;
  205.    tmp.f = x;
  206.    return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
  207. }
  208. #elif defined(isfinite)
  209. #define IS_INF_OR_NAN(x)        (!isfinite(x))
  210. #elif defined(finite)
  211. #define IS_INF_OR_NAN(x)        (!finite(x))
  212. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  213. #define IS_INF_OR_NAN(x)        (!isfinite(x))
  214. #else
  215. #define IS_INF_OR_NAN(x)        (!finite(x))
  216. #endif
  217.  
  218.  
  219. /***
  220.  *** CEILF: ceiling of float
  221.  *** FLOORF: floor of float
  222.  *** FABSF: absolute value of float
  223.  *** LOGF: the natural logarithm (base e) of the value
  224.  *** EXPF: raise e to the value
  225.  *** LDEXPF: multiply value by an integral power of two
  226.  *** FREXPF: extract mantissa and exponent from value
  227.  ***/
  228. #if defined(__gnu_linux__)
  229. /* C99 functions */
  230. #define CEILF(x)   ceilf(x)
  231. #define FLOORF(x)  floorf(x)
  232. #define FABSF(x)   fabsf(x)
  233. #define LOGF(x)    logf(x)
  234. #define EXPF(x)    expf(x)
  235. #define LDEXPF(x,y)  ldexpf(x,y)
  236. #define FREXPF(x,y)  frexpf(x,y)
  237. #else
  238. #define CEILF(x)   ((GLfloat) ceil(x))
  239. #define FLOORF(x)  ((GLfloat) floor(x))
  240. #define FABSF(x)   ((GLfloat) fabs(x))
  241. #define LOGF(x)    ((GLfloat) log(x))
  242. #define EXPF(x)    ((GLfloat) exp(x))
  243. #define LDEXPF(x,y)  ((GLfloat) ldexp(x,y))
  244. #define FREXPF(x,y)  ((GLfloat) frexp(x,y))
  245. #endif
  246.  
  247.  
  248. /**
  249.  * Convert float to int by rounding to nearest integer, away from zero.
  250.  */
  251. static inline int IROUND(float f)
  252. {
  253.    return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
  254. }
  255.  
  256.  
  257. /**
  258.  * Convert float to int64 by rounding to nearest integer.
  259.  */
  260. static inline GLint64 IROUND64(float f)
  261. {
  262.    return (GLint64) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
  263. }
  264.  
  265.  
  266. /**
  267.  * Convert positive float to int by rounding to nearest integer.
  268.  */
  269. static inline int IROUND_POS(float f)
  270. {
  271.    assert(f >= 0.0F);
  272.    return (int) (f + 0.5F);
  273. }
  274.  
  275.  
  276. /**
  277.  * Convert float to int using a fast method.  The rounding mode may vary.
  278.  * XXX We could use an x86-64/SSE2 version here.
  279.  */
  280. static inline int F_TO_I(float f)
  281. {
  282. #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
  283.    int r;
  284.    __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
  285.    return r;
  286. #elif defined(USE_X86_ASM) && defined(_MSC_VER)
  287.    int r;
  288.    _asm {
  289.          fld f
  290.          fistp r
  291.         }
  292.    return r;
  293. #else
  294.    return IROUND(f);
  295. #endif
  296. }
  297.  
  298.  
  299. /** Return (as an integer) floor of float */
  300. static inline int IFLOOR(float f)
  301. {
  302. #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
  303.    /*
  304.     * IEEE floor for computers that round to nearest or even.
  305.     * 'f' must be between -4194304 and 4194303.
  306.     * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
  307.     * but uses some IEEE specific tricks for better speed.
  308.     * Contributed by Josh Vanderhoof
  309.     */
  310.    int ai, bi;
  311.    double af, bf;
  312.    af = (3 << 22) + 0.5 + (double)f;
  313.    bf = (3 << 22) + 0.5 - (double)f;
  314.    /* GCC generates an extra fstp/fld without this. */
  315.    __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
  316.    __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
  317.    return (ai - bi) >> 1;
  318. #elif defined(USE_IEEE)
  319.    int ai, bi;
  320.    double af, bf;
  321.    fi_type u;
  322.    af = (3 << 22) + 0.5 + (double)f;
  323.    bf = (3 << 22) + 0.5 - (double)f;
  324.    u.f = (float) af;  ai = u.i;
  325.    u.f = (float) bf;  bi = u.i;
  326.    return (ai - bi) >> 1;
  327. #else
  328.    int i = IROUND(f);
  329.    return (i > f) ? i - 1 : i;
  330. #endif
  331. }
  332.  
  333.  
  334. /** Return (as an integer) ceiling of float */
  335. static inline int ICEIL(float f)
  336. {
  337. #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
  338.    /*
  339.     * IEEE ceil for computers that round to nearest or even.
  340.     * 'f' must be between -4194304 and 4194303.
  341.     * This ceil operation is done by "(iround(f + .5) + iround(f - .5) + 1) >> 1",
  342.     * but uses some IEEE specific tricks for better speed.
  343.     * Contributed by Josh Vanderhoof
  344.     */
  345.    int ai, bi;
  346.    double af, bf;
  347.    af = (3 << 22) + 0.5 + (double)f;
  348.    bf = (3 << 22) + 0.5 - (double)f;
  349.    /* GCC generates an extra fstp/fld without this. */
  350.    __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
  351.    __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
  352.    return (ai - bi + 1) >> 1;
  353. #elif defined(USE_IEEE)
  354.    int ai, bi;
  355.    double af, bf;
  356.    fi_type u;
  357.    af = (3 << 22) + 0.5 + (double)f;
  358.    bf = (3 << 22) + 0.5 - (double)f;
  359.    u.f = (float) af; ai = u.i;
  360.    u.f = (float) bf; bi = u.i;
  361.    return (ai - bi + 1) >> 1;
  362. #else
  363.    int i = IROUND(f);
  364.    return (i < f) ? i + 1 : i;
  365. #endif
  366. }
  367.  
  368.  
  369. /**
  370.  * Is x a power of two?
  371.  */
  372. static inline int
  373. _mesa_is_pow_two(int x)
  374. {
  375.    return !(x & (x - 1));
  376. }
  377.  
  378. /**
  379.  * Round given integer to next higer power of two
  380.  * If X is zero result is undefined.
  381.  *
  382.  * Source for the fallback implementation is
  383.  * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
  384.  * http://graphics.stanford.edu/~seander/bithacks.html
  385.  *
  386.  * When using builtin function have to do some work
  387.  * for case when passed values 1 to prevent hiting
  388.  * undefined result from __builtin_clz. Undefined
  389.  * results would be different depending on optimization
  390.  * level used for build.
  391.  */
  392. static inline int32_t
  393. _mesa_next_pow_two_32(uint32_t x)
  394. {
  395. #if defined(__GNUC__) && \
  396.         ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
  397.         uint32_t y = (x != 1);
  398.         return (1 + y) << ((__builtin_clz(x - y) ^ 31) );
  399. #else
  400.         x--;
  401.         x |= x >> 1;
  402.         x |= x >> 2;
  403.         x |= x >> 4;
  404.         x |= x >> 8;
  405.         x |= x >> 16;
  406.         x++;
  407.         return x;
  408. #endif
  409. }
  410.  
  411. static inline int64_t
  412. _mesa_next_pow_two_64(uint64_t x)
  413. {
  414. #if defined(__GNUC__) && \
  415.         ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
  416.         uint64_t y = (x != 1);
  417.         if (sizeof(x) == sizeof(long))
  418.                 return (1 + y) << ((__builtin_clzl(x - y) ^ 63));
  419.         else
  420.                 return (1 + y) << ((__builtin_clzll(x - y) ^ 63));
  421. #else
  422.         x--;
  423.         x |= x >> 1;
  424.         x |= x >> 2;
  425.         x |= x >> 4;
  426.         x |= x >> 8;
  427.         x |= x >> 16;
  428.         x |= x >> 32;
  429.         x++;
  430.         return x;
  431. #endif
  432. }
  433.  
  434.  
  435. /*
  436.  * Returns the floor form of binary logarithm for a 32-bit integer.
  437.  */
  438. static inline GLuint
  439. _mesa_logbase2(GLuint n)
  440. {
  441. #if defined(__GNUC__) && \
  442.    ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
  443.    return (31 - __builtin_clz(n | 1));
  444. #else
  445.    GLuint pos = 0;
  446.    if (n >= 1<<16) { n >>= 16; pos += 16; }
  447.    if (n >= 1<< 8) { n >>=  8; pos +=  8; }
  448.    if (n >= 1<< 4) { n >>=  4; pos +=  4; }
  449.    if (n >= 1<< 2) { n >>=  2; pos +=  2; }
  450.    if (n >= 1<< 1) {           pos +=  1; }
  451.    return pos;
  452. #endif
  453. }
  454.  
  455.  
  456. /**
  457.  * Return 1 if this is a little endian machine, 0 if big endian.
  458.  */
  459. static inline GLboolean
  460. _mesa_little_endian(void)
  461. {
  462.    const GLuint ui = 1; /* intentionally not static */
  463.    return *((const GLubyte *) &ui);
  464. }
  465.  
  466.  
  467.  
  468. /**********************************************************************
  469.  * Functions
  470.  */
  471.  
  472. extern void *
  473. _mesa_align_malloc( size_t bytes, unsigned long alignment );
  474.  
  475. extern void *
  476. _mesa_align_calloc( size_t bytes, unsigned long alignment );
  477.  
  478. extern void
  479. _mesa_align_free( void *ptr );
  480.  
  481. extern void *
  482. _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
  483.                     unsigned long alignment);
  484.  
  485. extern void *
  486. _mesa_exec_malloc( GLuint size );
  487.  
  488. extern void
  489. _mesa_exec_free( void *addr );
  490.  
  491. extern void *
  492. _mesa_realloc( void *oldBuffer, size_t oldSize, size_t newSize );
  493.  
  494.  
  495. #ifndef FFS_DEFINED
  496. #define FFS_DEFINED 1
  497. #ifdef __GNUC__
  498. #define ffs __builtin_ffs
  499. #define ffsll __builtin_ffsll
  500. #else
  501. extern int ffs(int i);
  502. extern int ffsll(long long int i);
  503. #endif /*__ GNUC__ */
  504. #endif /* FFS_DEFINED */
  505.  
  506.  
  507. #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
  508. #define _mesa_bitcount(i) __builtin_popcount(i)
  509. #define _mesa_bitcount_64(i) __builtin_popcountll(i)
  510. #else
  511. extern unsigned int
  512. _mesa_bitcount(unsigned int n);
  513. extern unsigned int
  514. _mesa_bitcount_64(uint64_t n);
  515. #endif
  516.  
  517. /**
  518.  * Find the last (most significant) bit set in a word.
  519.  *
  520.  * Essentially ffs() in the reverse direction.
  521.  */
  522. static inline unsigned int
  523. _mesa_fls(unsigned int n)
  524. {
  525. #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304)
  526.    return n == 0 ? 0 : 32 - __builtin_clz(n);
  527. #else
  528.    unsigned int v = 1;
  529.  
  530.    if (n == 0)
  531.       return 0;
  532.  
  533.    while (n >>= 1)
  534.        v++;
  535.  
  536.    return v;
  537. #endif
  538. }
  539.  
  540. extern int
  541. _mesa_round_to_even(float val);
  542.  
  543. extern GLhalfARB
  544. _mesa_float_to_half(float f);
  545.  
  546. extern float
  547. _mesa_half_to_float(GLhalfARB h);
  548.  
  549.  
  550. extern void *
  551. _mesa_bsearch( const void *key, const void *base, size_t nmemb, size_t size,
  552.                int (*compar)(const void *, const void *) );
  553.  
  554. extern char *
  555. _mesa_getenv( const char *var );
  556.  
  557. extern char *
  558. _mesa_strdup( const char *s );
  559.  
  560. extern float
  561. _mesa_strtof( const char *s, char **end );
  562.  
  563. extern unsigned int
  564. _mesa_str_checksum(const char *str);
  565.  
  566. extern int
  567. _mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4);
  568.  
  569. extern int
  570. _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg);
  571.  
  572.  
  573. #if defined(_MSC_VER) && !defined(snprintf)
  574. #define snprintf _snprintf
  575. #endif
  576.  
  577.  
  578. #ifdef __cplusplus
  579. }
  580. #endif
  581.  
  582.  
  583. #endif /* IMPORTS_H */
  584.