Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  * \file imports.c
  3.  * Standard C library function wrappers.
  4.  *
  5.  * Imports are services which the device driver or window system or
  6.  * operating system provides to the core renderer.  The core renderer (Mesa)
  7.  * will call these functions in order to do memory allocation, simple I/O,
  8.  * etc.
  9.  *
  10.  * Some drivers will want to override/replace this file with something
  11.  * specialized, but that'll be rare.
  12.  *
  13.  * Eventually, I want to move roll the glheader.h file into this.
  14.  *
  15.  * \todo Functions still needed:
  16.  * - scanf
  17.  * - qsort
  18.  * - rand and RAND_MAX
  19.  */
  20.  
  21. /*
  22.  * Mesa 3-D graphics library
  23.  * Version:  7.1
  24.  *
  25.  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  26.  *
  27.  * Permission is hereby granted, free of charge, to any person obtaining a
  28.  * copy of this software and associated documentation files (the "Software"),
  29.  * to deal in the Software without restriction, including without limitation
  30.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  31.  * and/or sell copies of the Software, and to permit persons to whom the
  32.  * Software is furnished to do so, subject to the following conditions:
  33.  *
  34.  * The above copyright notice and this permission notice shall be included
  35.  * in all copies or substantial portions of the Software.
  36.  *
  37.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  38.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  39.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  40.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  41.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  42.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  43.  */
  44.  
  45.  
  46.  
  47. #include "imports.h"
  48. #include "context.h"
  49. #include "version.h"
  50.  
  51. #ifdef _GNU_SOURCE
  52. #include <locale.h>
  53. #ifdef __APPLE__
  54. #include <xlocale.h>
  55. #endif
  56. #endif
  57.  
  58. #undef _WIN32
  59.  
  60.  
  61.  
  62. #define MAXSTRING 4000  /* for vsnprintf() */
  63.  
  64.  
  65. /**********************************************************************/
  66. /** \name Memory */
  67. /*@{*/
  68.  
  69. /**
  70.  * Allocate aligned memory.
  71.  *
  72.  * \param bytes number of bytes to allocate.
  73.  * \param alignment alignment (must be greater than zero).
  74.  *
  75.  * Allocates extra memory to accommodate rounding up the address for
  76.  * alignment and to record the real malloc address.
  77.  *
  78.  * \sa _mesa_align_free().
  79.  */
  80. void *
  81. _mesa_align_malloc(size_t bytes, unsigned long alignment)
  82. {
  83. #if defined(HAVE_POSIX_MEMALIGN)
  84.    void *mem;
  85.    int err = posix_memalign(& mem, alignment, bytes);
  86.    if (err)
  87.       return NULL;
  88.    return mem;
  89. #elif defined(_WIN32) && defined(_MSC_VER)
  90.    return _aligned_malloc(bytes, alignment);
  91. #else
  92.    uintptr_t ptr, buf;
  93.  
  94.    ASSERT( alignment > 0 );
  95.  
  96.    ptr = (uintptr_t) malloc(bytes + alignment + sizeof(void *));
  97.    if (!ptr)
  98.       return NULL;
  99.  
  100.    buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
  101.    *(uintptr_t *)(buf - sizeof(void *)) = ptr;
  102.  
  103. #ifdef DEBUG
  104.    /* mark the non-aligned area */
  105.    while ( ptr < buf - sizeof(void *) ) {
  106.       *(unsigned long *)ptr = 0xcdcdcdcd;
  107.       ptr += sizeof(unsigned long);
  108.    }
  109. #endif
  110.  
  111.    return (void *) buf;
  112. #endif /* defined(HAVE_POSIX_MEMALIGN) */
  113. }
  114.  
  115. /**
  116.  * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
  117.  * malloc()
  118.  */
  119. void *
  120. _mesa_align_calloc(size_t bytes, unsigned long alignment)
  121. {
  122. #if defined(HAVE_POSIX_MEMALIGN)
  123.    void *mem;
  124.  
  125.    mem = _mesa_align_malloc(bytes, alignment);
  126.    if (mem != NULL) {
  127.       (void) memset(mem, 0, bytes);
  128.    }
  129.  
  130.    return mem;
  131. #elif defined(_WIN32) && defined(_MSC_VER)
  132.    void *mem;
  133.  
  134.    mem = _aligned_malloc(bytes, alignment);
  135.    if (mem != NULL) {
  136.       (void) memset(mem, 0, bytes);
  137.    }
  138.  
  139.    return mem;
  140. #else
  141.    uintptr_t ptr, buf;
  142.  
  143.    ASSERT( alignment > 0 );
  144.  
  145.    ptr = (uintptr_t) calloc(1, bytes + alignment + sizeof(void *));
  146.    if (!ptr)
  147.       return NULL;
  148.  
  149.    buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
  150.    *(uintptr_t *)(buf - sizeof(void *)) = ptr;
  151.  
  152. #ifdef DEBUG
  153.    /* mark the non-aligned area */
  154.    while ( ptr < buf - sizeof(void *) ) {
  155.       *(unsigned long *)ptr = 0xcdcdcdcd;
  156.       ptr += sizeof(unsigned long);
  157.    }
  158. #endif
  159.  
  160.    return (void *)buf;
  161. #endif /* defined(HAVE_POSIX_MEMALIGN) */
  162. }
  163.  
  164. /**
  165.  * Free memory which was allocated with either _mesa_align_malloc()
  166.  * or _mesa_align_calloc().
  167.  * \param ptr pointer to the memory to be freed.
  168.  * The actual address to free is stored in the word immediately before the
  169.  * address the client sees.
  170.  */
  171. void
  172. _mesa_align_free(void *ptr)
  173. {
  174. #if defined(HAVE_POSIX_MEMALIGN)
  175.    free(ptr);
  176. #elif defined(_WIN32) && defined(_MSC_VER)
  177.    _aligned_free(ptr);
  178. #else
  179.    void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
  180.    void *realAddr = *cubbyHole;
  181.    free(realAddr);
  182. #endif /* defined(HAVE_POSIX_MEMALIGN) */
  183. }
  184.  
  185. /**
  186.  * Reallocate memory, with alignment.
  187.  */
  188. void *
  189. _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
  190.                     unsigned long alignment)
  191. {
  192. #if defined(_WIN32) && defined(_MSC_VER)
  193.    (void) oldSize;
  194.    return _aligned_realloc(oldBuffer, newSize, alignment);
  195. #else
  196.    const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
  197.    void *newBuf = _mesa_align_malloc(newSize, alignment);
  198.    if (newBuf && oldBuffer && copySize > 0) {
  199.       memcpy(newBuf, oldBuffer, copySize);
  200.    }
  201.    if (oldBuffer)
  202.       _mesa_align_free(oldBuffer);
  203.    return newBuf;
  204. #endif
  205. }
  206.  
  207.  
  208.  
  209. /** Reallocate memory */
  210. void *
  211. _mesa_realloc(void *oldBuffer, size_t oldSize, size_t newSize)
  212. {
  213.    const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
  214.    void *newBuffer = malloc(newSize);
  215.    if (newBuffer && oldBuffer && copySize > 0)
  216.       memcpy(newBuffer, oldBuffer, copySize);
  217.    if (oldBuffer)
  218.       free(oldBuffer);
  219.    return newBuffer;
  220. }
  221.  
  222. /**
  223.  * Fill memory with a constant 16bit word.
  224.  * \param dst destination pointer.
  225.  * \param val value.
  226.  * \param n number of words.
  227.  */
  228. void
  229. _mesa_memset16( unsigned short *dst, unsigned short val, size_t n )
  230. {
  231.    while (n-- > 0)
  232.       *dst++ = val;
  233. }
  234.  
  235. /*@}*/
  236.  
  237.  
  238. /**********************************************************************/
  239. /** \name Math */
  240. /*@{*/
  241.  
  242. /** Wrapper around sqrt() */
  243. double
  244. _mesa_sqrtd(double x)
  245. {
  246.    return sqrt(x);
  247. }
  248.  
  249.  
  250. /*
  251.  * A High Speed, Low Precision Square Root
  252.  * by Paul Lalonde and Robert Dawson
  253.  * from "Graphics Gems", Academic Press, 1990
  254.  *
  255.  * SPARC implementation of a fast square root by table
  256.  * lookup.
  257.  * SPARC floating point format is as follows:
  258.  *
  259.  * BIT 31       30      23      22      0
  260.  *     sign     exponent        mantissa
  261.  */
  262. static short sqrttab[0x100];    /* declare table of square roots */
  263.  
  264. void
  265. _mesa_init_sqrt_table(void)
  266. {
  267. #if defined(USE_IEEE) && !defined(DEBUG)
  268.    unsigned short i;
  269.    fi_type fi;     /* to access the bits of a float in  C quickly  */
  270.                    /* we use a union defined in glheader.h         */
  271.  
  272.    for(i=0; i<= 0x7f; i++) {
  273.       fi.i = 0;
  274.  
  275.       /*
  276.        * Build a float with the bit pattern i as mantissa
  277.        * and an exponent of 0, stored as 127
  278.        */
  279.  
  280.       fi.i = (i << 16) | (127 << 23);
  281.       fi.f = _mesa_sqrtd(fi.f);
  282.  
  283.       /*
  284.        * Take the square root then strip the first 7 bits of
  285.        * the mantissa into the table
  286.        */
  287.  
  288.       sqrttab[i] = (fi.i & 0x7fffff) >> 16;
  289.  
  290.       /*
  291.        * Repeat the process, this time with an exponent of
  292.        * 1, stored as 128
  293.        */
  294.  
  295.       fi.i = 0;
  296.       fi.i = (i << 16) | (128 << 23);
  297.       fi.f = sqrt(fi.f);
  298.       sqrttab[i+0x80] = (fi.i & 0x7fffff) >> 16;
  299.    }
  300. #else
  301.    (void) sqrttab;  /* silence compiler warnings */
  302. #endif /*HAVE_FAST_MATH*/
  303. }
  304.  
  305.  
  306. /**
  307.  * Single precision square root.
  308.  */
  309. float
  310. _mesa_sqrtf( float x )
  311. {
  312. #if defined(USE_IEEE) && !defined(DEBUG)
  313.    fi_type num;
  314.                                 /* to access the bits of a float in C
  315.                                  * we use a union from glheader.h     */
  316.  
  317.    short e;                     /* the exponent */
  318.    if (x == 0.0F) return 0.0F;  /* check for square root of 0 */
  319.    num.f = x;
  320.    e = (num.i >> 23) - 127;     /* get the exponent - on a SPARC the */
  321.                                 /* exponent is stored with 127 added */
  322.    num.i &= 0x7fffff;           /* leave only the mantissa */
  323.    if (e & 0x01) num.i |= 0x800000;
  324.                                 /* the exponent is odd so we have to */
  325.                                 /* look it up in the second half of  */
  326.                                 /* the lookup table, so we set the   */
  327.                                 /* high bit                                */
  328.    e >>= 1;                     /* divide the exponent by two */
  329.                                 /* note that in C the shift */
  330.                                 /* operators are sign preserving */
  331.                                 /* for signed operands */
  332.    /* Do the table lookup, based on the quaternary mantissa,
  333.     * then reconstruct the result back into a float
  334.     */
  335.    num.i = ((sqrttab[num.i >> 16]) << 16) | ((e + 127) << 23);
  336.  
  337.    return num.f;
  338. #else
  339.    return (float) _mesa_sqrtd((double) x);
  340. #endif
  341. }
  342.  
  343.  
  344. /**
  345.  inv_sqrt - A single precision 1/sqrt routine for IEEE format floats.
  346.  written by Josh Vanderhoof, based on newsgroup posts by James Van Buskirk
  347.  and Vesa Karvonen.
  348. */
  349. float
  350. _mesa_inv_sqrtf(float n)
  351. {
  352. #if defined(USE_IEEE) && !defined(DEBUG)
  353.         float r0, x0, y0;
  354.         float r1, x1, y1;
  355.         float r2, x2, y2;
  356. #if 0 /* not used, see below -BP */
  357.         float r3, x3, y3;
  358. #endif
  359.         fi_type u;
  360.         unsigned int magic;
  361.  
  362.         /*
  363.          Exponent part of the magic number -
  364.  
  365.          We want to:
  366.          1. subtract the bias from the exponent,
  367.          2. negate it
  368.          3. divide by two (rounding towards -inf)
  369.          4. add the bias back
  370.  
  371.          Which is the same as subtracting the exponent from 381 and dividing
  372.          by 2.
  373.  
  374.          floor(-(x - 127) / 2) + 127 = floor((381 - x) / 2)
  375.         */
  376.  
  377.         magic = 381 << 23;
  378.  
  379.         /*
  380.          Significand part of magic number -
  381.  
  382.          With the current magic number, "(magic - u.i) >> 1" will give you:
  383.  
  384.          for 1 <= u.f <= 2: 1.25 - u.f / 4
  385.          for 2 <= u.f <= 4: 1.00 - u.f / 8
  386.  
  387.          This isn't a bad approximation of 1/sqrt.  The maximum difference from
  388.          1/sqrt will be around .06.  After three Newton-Raphson iterations, the
  389.          maximum difference is less than 4.5e-8.  (Which is actually close
  390.          enough to make the following bias academic...)
  391.  
  392.          To get a better approximation you can add a bias to the magic
  393.          number.  For example, if you subtract 1/2 of the maximum difference in
  394.          the first approximation (.03), you will get the following function:
  395.  
  396.          for 1 <= u.f <= 2:    1.22 - u.f / 4
  397.          for 2 <= u.f <= 3.76: 0.97 - u.f / 8
  398.          for 3.76 <= u.f <= 4: 0.72 - u.f / 16
  399.          (The 3.76 to 4 range is where the result is < .5.)
  400.  
  401.          This is the closest possible initial approximation, but with a maximum
  402.          error of 8e-11 after three NR iterations, it is still not perfect.  If
  403.          you subtract 0.0332281 instead of .03, the maximum error will be
  404.          2.5e-11 after three NR iterations, which should be about as close as
  405.          is possible.
  406.  
  407.          for 1 <= u.f <= 2:    1.2167719 - u.f / 4
  408.          for 2 <= u.f <= 3.73: 0.9667719 - u.f / 8
  409.          for 3.73 <= u.f <= 4: 0.7167719 - u.f / 16
  410.  
  411.         */
  412.  
  413.         magic -= (int)(0.0332281 * (1 << 25));
  414.  
  415.         u.f = n;
  416.         u.i = (magic - u.i) >> 1;
  417.  
  418.         /*
  419.          Instead of Newton-Raphson, we use Goldschmidt's algorithm, which
  420.          allows more parallelism.  From what I understand, the parallelism
  421.          comes at the cost of less precision, because it lets error
  422.          accumulate across iterations.
  423.         */
  424.         x0 = 1.0f;
  425.         y0 = 0.5f * n;
  426.         r0 = u.f;
  427.  
  428.         x1 = x0 * r0;
  429.         y1 = y0 * r0 * r0;
  430.         r1 = 1.5f - y1;
  431.  
  432.         x2 = x1 * r1;
  433.         y2 = y1 * r1 * r1;
  434.         r2 = 1.5f - y2;
  435.  
  436. #if 1
  437.         return x2 * r2;  /* we can stop here, and be conformant -BP */
  438. #else
  439.         x3 = x2 * r2;
  440.         y3 = y2 * r2 * r2;
  441.         r3 = 1.5f - y3;
  442.  
  443.         return x3 * r3;
  444. #endif
  445. #else
  446.         return (float) (1.0 / sqrt(n));
  447. #endif
  448. }
  449.  
  450. /**
  451.  * Find the first bit set in a word.
  452.  */
  453. int
  454. _mesa_ffs(int32_t i)
  455. {
  456. #if (defined(_WIN32) ) || defined(__IBMC__) || defined(__IBMCPP__)
  457.    register int bit = 0;
  458.    if (i != 0) {
  459.       if ((i & 0xffff) == 0) {
  460.          bit += 16;
  461.          i >>= 16;
  462.       }
  463.       if ((i & 0xff) == 0) {
  464.          bit += 8;
  465.          i >>= 8;
  466.       }
  467.       if ((i & 0xf) == 0) {
  468.          bit += 4;
  469.          i >>= 4;
  470.       }
  471.       while ((i & 1) == 0) {
  472.          bit++;
  473.          i >>= 1;
  474.       }
  475.       bit++;
  476.    }
  477.    return bit;
  478. #else
  479.    return __builtin_ffs(i);
  480. #endif
  481. }
  482.  
  483.  
  484. /**
  485.  * Find position of first bit set in given value.
  486.  * XXX Warning: this function can only be used on 64-bit systems!
  487.  * \return  position of least-significant bit set, starting at 1, return zero
  488.  *          if no bits set.
  489.  */
  490. int
  491. _mesa_ffsll(int64_t val)
  492. {
  493. #ifdef ffsll
  494.    return ffsll(val);
  495. #else
  496.    int bit;
  497.  
  498.    assert(sizeof(val) == 8);
  499.  
  500.    bit = _mesa_ffs((int32_t)val);
  501.    if (bit != 0)
  502.       return bit;
  503.  
  504.    bit = _mesa_ffs((int32_t)(val >> 32));
  505.    if (bit != 0)
  506.       return 32 + bit;
  507.  
  508.    return 0;
  509. #endif
  510. }
  511.  
  512.  
  513. /**
  514.  * Return number of bits set in given GLuint.
  515.  */
  516. unsigned int
  517. _mesa_bitcount(unsigned int n)
  518. {
  519. #if defined(__GNUC__) && \
  520.         ((_GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
  521.    return __builtin_popcount(n);
  522. #else
  523.    unsigned int bits;
  524.    for (bits = 0; n > 0; n = n >> 1) {
  525.       bits += (n & 1);
  526.    }
  527.    return bits;
  528. #endif
  529. }
  530.  
  531.  
  532. /**
  533.  * Convert a 4-byte float to a 2-byte half float.
  534.  * Based on code from:
  535.  * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
  536.  */
  537. GLhalfARB
  538. _mesa_float_to_half(float val)
  539. {
  540.    const fi_type fi = {val};
  541.    const int flt_m = fi.i & 0x7fffff;
  542.    const int flt_e = (fi.i >> 23) & 0xff;
  543.    const int flt_s = (fi.i >> 31) & 0x1;
  544.    int s, e, m = 0;
  545.    GLhalfARB result;
  546.  
  547.    /* sign bit */
  548.    s = flt_s;
  549.  
  550.    /* handle special cases */
  551.    if ((flt_e == 0) && (flt_m == 0)) {
  552.       /* zero */
  553.       /* m = 0; - already set */
  554.       e = 0;
  555.    }
  556.    else if ((flt_e == 0) && (flt_m != 0)) {
  557.       /* denorm -- denorm float maps to 0 half */
  558.       /* m = 0; - already set */
  559.       e = 0;
  560.    }
  561.    else if ((flt_e == 0xff) && (flt_m == 0)) {
  562.       /* infinity */
  563.       /* m = 0; - already set */
  564.       e = 31;
  565.    }
  566.    else if ((flt_e == 0xff) && (flt_m != 0)) {
  567.       /* NaN */
  568.       m = 1;
  569.       e = 31;
  570.    }
  571.    else {
  572.       /* regular number */
  573.       const int new_exp = flt_e - 127;
  574.       if (new_exp < -24) {
  575.          /* this maps to 0 */
  576.          /* m = 0; - already set */
  577.          e = 0;
  578.       }
  579.       else if (new_exp < -14) {
  580.          /* this maps to a denorm */
  581.          unsigned int exp_val = (unsigned int) (-14 - new_exp); /* 2^-exp_val*/
  582.          e = 0;
  583.          switch (exp_val) {
  584.             case 0:
  585.                _mesa_warning(NULL,
  586.                    "float_to_half: logical error in denorm creation!\n");
  587.                /* m = 0; - already set */
  588.                break;
  589.             case 1: m = 512 + (flt_m >> 14); break;
  590.             case 2: m = 256 + (flt_m >> 15); break;
  591.             case 3: m = 128 + (flt_m >> 16); break;
  592.             case 4: m = 64 + (flt_m >> 17); break;
  593.             case 5: m = 32 + (flt_m >> 18); break;
  594.             case 6: m = 16 + (flt_m >> 19); break;
  595.             case 7: m = 8 + (flt_m >> 20); break;
  596.             case 8: m = 4 + (flt_m >> 21); break;
  597.             case 9: m = 2 + (flt_m >> 22); break;
  598.             case 10: m = 1; break;
  599.          }
  600.       }
  601.       else if (new_exp > 15) {
  602.          /* map this value to infinity */
  603.          /* m = 0; - already set */
  604.          e = 31;
  605.       }
  606.       else {
  607.          /* regular */
  608.          e = new_exp + 15;
  609.          m = flt_m >> 13;
  610.       }
  611.    }
  612.  
  613.    result = (s << 15) | (e << 10) | m;
  614.    return result;
  615. }
  616.  
  617.  
  618. /**
  619.  * Convert a 2-byte half float to a 4-byte float.
  620.  * Based on code from:
  621.  * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
  622.  */
  623. float
  624. _mesa_half_to_float(GLhalfARB val)
  625. {
  626.    /* XXX could also use a 64K-entry lookup table */
  627.    const int m = val & 0x3ff;
  628.    const int e = (val >> 10) & 0x1f;
  629.    const int s = (val >> 15) & 0x1;
  630.    int flt_m, flt_e, flt_s;
  631.    fi_type fi;
  632.    float result;
  633.  
  634.    /* sign bit */
  635.    flt_s = s;
  636.  
  637.    /* handle special cases */
  638.    if ((e == 0) && (m == 0)) {
  639.       /* zero */
  640.       flt_m = 0;
  641.       flt_e = 0;
  642.    }
  643.    else if ((e == 0) && (m != 0)) {
  644.       /* denorm -- denorm half will fit in non-denorm single */
  645.       const float half_denorm = 1.0f / 16384.0f; /* 2^-14 */
  646.       float mantissa = ((float) (m)) / 1024.0f;
  647.       float sign = s ? -1.0f : 1.0f;
  648.       return sign * mantissa * half_denorm;
  649.    }
  650.    else if ((e == 31) && (m == 0)) {
  651.       /* infinity */
  652.       flt_e = 0xff;
  653.       flt_m = 0;
  654.    }
  655.    else if ((e == 31) && (m != 0)) {
  656.       /* NaN */
  657.       flt_e = 0xff;
  658.       flt_m = 1;
  659.    }
  660.    else {
  661.       /* regular */
  662.       flt_e = e + 112;
  663.       flt_m = m << 13;
  664.    }
  665.  
  666.    fi.i = (flt_s << 31) | (flt_e << 23) | flt_m;
  667.    result = fi.f;
  668.    return result;
  669. }
  670.  
  671. /*@}*/
  672.  
  673.  
  674. /**********************************************************************/
  675. /** \name Sort & Search */
  676. /*@{*/
  677.  
  678. /**
  679.  * Wrapper for bsearch().
  680.  */
  681. void *
  682. _mesa_bsearch( const void *key, const void *base, size_t nmemb, size_t size,
  683.                int (*compar)(const void *, const void *) )
  684. {
  685. #if defined(_WIN32_WCE)
  686.    void *mid;
  687.    int cmp;
  688.    while (nmemb) {
  689.       nmemb >>= 1;
  690.       mid = (char *)base + nmemb * size;
  691.       cmp = (*compar)(key, mid);
  692.       if (cmp == 0)
  693.          return mid;
  694.       if (cmp > 0) {
  695.          base = (char *)mid + size;
  696.          --nmemb;
  697.       }
  698.    }
  699.    return NULL;
  700. #else
  701.    return bsearch(key, base, nmemb, size, compar);
  702. #endif
  703. }
  704.  
  705. /*@}*/
  706.  
  707.  
  708. /**********************************************************************/
  709. /** \name Environment vars */
  710. /*@{*/
  711.  
  712. /**
  713.  * Wrapper for getenv().
  714.  */
  715. char *
  716. _mesa_getenv( const char *var )
  717. {
  718. #if defined(_XBOX) || defined(_WIN32_WCE)
  719.    return NULL;
  720. #else
  721.    return getenv(var);
  722. #endif
  723. }
  724.  
  725. /*@}*/
  726.  
  727.  
  728. /**********************************************************************/
  729. /** \name String */
  730. /*@{*/
  731.  
  732. /**
  733.  * Implemented using malloc() and strcpy.
  734.  * Note that NULL is handled accordingly.
  735.  */
  736. char *
  737. _mesa_strdup( const char *s )
  738. {
  739.    if (s) {
  740.       size_t l = strlen(s);
  741.       char *s2 = (char *) malloc(l + 1);
  742.       if (s2)
  743.          strcpy(s2, s);
  744.       return s2;
  745.    }
  746.    else {
  747.       return NULL;
  748.    }
  749. }
  750.  
  751. /** Wrapper around strtof() */
  752. float
  753. _mesa_strtof( const char *s, char **end )
  754. {
  755. #if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__)
  756.    static locale_t loc = NULL;
  757.    if (!loc) {
  758.       loc = newlocale(LC_CTYPE_MASK, "C", NULL);
  759.    }
  760.    return strtof_l(s, end, loc);
  761. #elif defined(_ISOC99_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
  762.    return strtof(s, end);
  763. #else
  764.    return (float)strtod(s, end);
  765. #endif
  766. }
  767.  
  768. /** Compute simple checksum/hash for a string */
  769. unsigned int
  770. _mesa_str_checksum(const char *str)
  771. {
  772.    /* This could probably be much better */
  773.    unsigned int sum, i;
  774.    const char *c;
  775.    sum = i = 1;
  776.    for (c = str; *c; c++, i++)
  777.       sum += *c * (i % 100);
  778.    return sum + i;
  779. }
  780.  
  781.  
  782. /*@}*/
  783.  
  784.  
  785. /** Wrapper around vsnprintf() */
  786. int
  787. _mesa_snprintf( char *str, size_t size, const char *fmt, ... )
  788. {
  789.    int r;
  790.    va_list args;
  791.    va_start( args, fmt );
  792.    r = vsnprintf( str, size, fmt, args );
  793.    va_end( args );
  794.    return r;
  795. }
  796.  
  797.  
  798. /**********************************************************************/
  799. /** \name Diagnostics */
  800. /*@{*/
  801.  
  802. static void
  803. output_if_debug(const char *prefixString, const char *outputString,
  804.                 GLboolean newline)
  805. {
  806.    static int debug = -1;
  807.  
  808.    /* Check the MESA_DEBUG environment variable if it hasn't
  809.     * been checked yet.  We only have to check it once...
  810.     */
  811.    if (debug == -1) {
  812.       char *env = _mesa_getenv("MESA_DEBUG");
  813.  
  814.       /* In a debug build, we print warning messages *unless*
  815.        * MESA_DEBUG is 0.  In a non-debug build, we don't
  816.        * print warning messages *unless* MESA_DEBUG is
  817.        * set *to any value*.
  818.        */
  819. #ifdef DEBUG
  820.       debug = (env != NULL && atoi(env) == 0) ? 0 : 1;
  821. #else
  822.       debug = (env != NULL) ? 1 : 0;
  823. #endif
  824.    }
  825.  
  826.    /* Now only print the string if we're required to do so. */
  827.    if (debug) {
  828.       fprintf(stderr, "%s: %s", prefixString, outputString);
  829.       if (newline)
  830.          fprintf(stderr, "\n");
  831.  
  832. #if defined(_WIN32) && !defined(_WIN32_WCE)
  833.       /* stderr from windows applications without console is not usually
  834.        * visible, so communicate with the debugger instead */
  835.       {
  836.          char buf[4096];
  837.          _mesa_snprintf(buf, sizeof(buf), "%s: %s%s", prefixString, outputString, newline ? "\n" : "");
  838.          OutputDebugStringA(buf);
  839.       }
  840. #endif
  841.    }
  842. }
  843.  
  844.  
  845. /**
  846.  * Return string version of GL error code.
  847.  */
  848. static const char *
  849. error_string( GLenum error )
  850. {
  851.    switch (error) {
  852.    case GL_NO_ERROR:
  853.       return "GL_NO_ERROR";
  854.    case GL_INVALID_VALUE:
  855.       return "GL_INVALID_VALUE";
  856.    case GL_INVALID_ENUM:
  857.       return "GL_INVALID_ENUM";
  858.    case GL_INVALID_OPERATION:
  859.       return "GL_INVALID_OPERATION";
  860.    case GL_STACK_OVERFLOW:
  861.       return "GL_STACK_OVERFLOW";
  862.    case GL_STACK_UNDERFLOW:
  863.       return "GL_STACK_UNDERFLOW";
  864.    case GL_OUT_OF_MEMORY:
  865.       return "GL_OUT_OF_MEMORY";
  866.    case GL_TABLE_TOO_LARGE:
  867.       return "GL_TABLE_TOO_LARGE";
  868.    case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
  869.       return "GL_INVALID_FRAMEBUFFER_OPERATION";
  870.    default:
  871.       return "unknown";
  872.    }
  873. }
  874.  
  875.  
  876. /**
  877.  * When a new type of error is recorded, print a message describing
  878.  * previous errors which were accumulated.
  879.  */
  880. static void
  881. flush_delayed_errors( struct gl_context *ctx )
  882. {
  883.    char s[MAXSTRING];
  884.  
  885.    if (ctx->ErrorDebugCount) {
  886.       _mesa_snprintf(s, MAXSTRING, "%d similar %s errors",
  887.                      ctx->ErrorDebugCount,
  888.                      error_string(ctx->ErrorValue));
  889.  
  890.       output_if_debug("Mesa", s, GL_TRUE);
  891.  
  892.       ctx->ErrorDebugCount = 0;
  893.    }
  894. }
  895.  
  896.  
  897. /**
  898.  * Report a warning (a recoverable error condition) to stderr if
  899.  * either DEBUG is defined or the MESA_DEBUG env var is set.
  900.  *
  901.  * \param ctx GL context.
  902.  * \param fmtString printf()-like format string.
  903.  */
  904. void
  905. _mesa_warning( struct gl_context *ctx, const char *fmtString, ... )
  906. {
  907.    char str[MAXSTRING];
  908.    va_list args;
  909.    va_start( args, fmtString );
  910.    (void) vsnprintf( str, MAXSTRING, fmtString, args );
  911.    va_end( args );
  912.  
  913.    if (ctx)
  914.       flush_delayed_errors( ctx );
  915.  
  916.    output_if_debug("Mesa warning", str, GL_TRUE);
  917. }
  918.  
  919.  
  920. /**
  921.  * Report an internal implementation problem.
  922.  * Prints the message to stderr via fprintf().
  923.  *
  924.  * \param ctx GL context.
  925.  * \param fmtString problem description string.
  926.  */
  927. void
  928. _mesa_problem( const struct gl_context *ctx, const char *fmtString, ... )
  929. {
  930.    va_list args;
  931.    char str[MAXSTRING];
  932.    (void) ctx;
  933.  
  934.    va_start( args, fmtString );
  935.    vsnprintf( str, MAXSTRING, fmtString, args );
  936.    va_end( args );
  937.  
  938.    fprintf(stderr, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING, str);
  939.    fprintf(stderr, "Please report at bugs.freedesktop.org\n");
  940. }
  941.  
  942.  
  943. /**
  944.  * Record an OpenGL state error.  These usually occur when the user
  945.  * passes invalid parameters to a GL function.
  946.  *
  947.  * If debugging is enabled (either at compile-time via the DEBUG macro, or
  948.  * run-time via the MESA_DEBUG environment variable), report the error with
  949.  * _mesa_debug().
  950.  *
  951.  * \param ctx the GL context.
  952.  * \param error the error value.
  953.  * \param fmtString printf() style format string, followed by optional args
  954.  */
  955. void
  956. _mesa_error( struct gl_context *ctx, GLenum error, const char *fmtString, ... )
  957. {
  958.    static GLint debug = -1;
  959.  
  960.    /* Check debug environment variable only once:
  961.     */
  962.    if (debug == -1) {
  963.       const char *debugEnv = _mesa_getenv("MESA_DEBUG");
  964.  
  965. #ifdef DEBUG
  966.       if (debugEnv && strstr(debugEnv, "silent"))
  967.          debug = GL_FALSE;
  968.       else
  969.          debug = GL_TRUE;
  970. #else
  971.       if (debugEnv)
  972.          debug = GL_TRUE;
  973.       else
  974.          debug = GL_FALSE;
  975. #endif
  976.    }
  977.  
  978.    if (debug) {
  979.       if (ctx->ErrorValue == error &&
  980.           ctx->ErrorDebugFmtString == fmtString) {
  981.          ctx->ErrorDebugCount++;
  982.       }
  983.       else {
  984.          char s[MAXSTRING], s2[MAXSTRING];
  985.          va_list args;
  986.  
  987.          flush_delayed_errors( ctx );
  988.  
  989.          va_start(args, fmtString);
  990.          vsnprintf(s, MAXSTRING, fmtString, args);
  991.          va_end(args);
  992.  
  993.          _mesa_snprintf(s2, MAXSTRING, "%s in %s", error_string(error), s);
  994.          output_if_debug("Mesa: User error", s2, GL_TRUE);
  995.  
  996.          ctx->ErrorDebugFmtString = fmtString;
  997.          ctx->ErrorDebugCount = 0;
  998.       }
  999.    }
  1000.  
  1001.    _mesa_record_error(ctx, error);
  1002. }
  1003.  
  1004.  
  1005. /**
  1006.  * Report debug information.  Print error message to stderr via fprintf().
  1007.  * No-op if DEBUG mode not enabled.
  1008.  *
  1009.  * \param ctx GL context.
  1010.  * \param fmtString printf()-style format string, followed by optional args.
  1011.  */
  1012. void
  1013. _mesa_debug( const struct gl_context *ctx, const char *fmtString, ... )
  1014. {
  1015. #ifdef DEBUG
  1016.    char s[MAXSTRING];
  1017.    va_list args;
  1018.    va_start(args, fmtString);
  1019.    vsnprintf(s, MAXSTRING, fmtString, args);
  1020.    va_end(args);
  1021.    output_if_debug("Mesa", s, GL_FALSE);
  1022. #endif /* DEBUG */
  1023.    (void) ctx;
  1024.    (void) fmtString;
  1025. }
  1026.  
  1027. /*@}*/
  1028.