Merge branch 'mesa_7_7_branch'
[mesa.git] / src / mesa / main / imports.c
index 615f7c9a6daf70ea1c6a5c65e1506357ac206674..def045269c58647c0bd005fd9ab9875d55d24b36 100644 (file)
 #include "context.h"
 #include "version.h"
 
+#ifdef _GNU_SOURCE
+#include <locale.h>
+#ifdef __APPLE__
+#include <xlocale.h>
+#endif
+#endif
+
 
 #define MAXSTRING 4000  /* for vsnprintf() */
 
@@ -101,8 +108,8 @@ _mesa_align_malloc(size_t bytes, unsigned long alignment)
 {
 #if defined(HAVE_POSIX_MEMALIGN)
    void *mem;
-
-   (void) posix_memalign(& mem, alignment, bytes);
+   int err = posix_memalign(& mem, alignment, bytes);
+   (void) err;
    return mem;
 #elif defined(_WIN32) && defined(_MSC_VER)
    return _aligned_malloc(bytes, alignment);
@@ -453,7 +460,7 @@ _mesa_inv_sqrtf(float n)
 #if 0 /* not used, see below -BP */
         float r3, x3, y3;
 #endif
-        union { float f; unsigned int i; } u;
+        fi_type u;
         unsigned int magic;
 
         /*
@@ -622,11 +629,15 @@ _mesa_ffsll(int64_t val)
 unsigned int
 _mesa_bitcount(unsigned int n)
 {
+#if defined(__GNUC__)
+   return __builtin_popcount(n);
+#else
    unsigned int bits;
    for (bits = 0; n > 0; n = n >> 1) {
       bits += (n & 1);
    }
    return bits;
+#endif
 }
 
 
@@ -638,10 +649,10 @@ _mesa_bitcount(unsigned int n)
 GLhalfARB
 _mesa_float_to_half(float val)
 {
-   const int flt = *((int *) (void *) &val);
-   const int flt_m = flt & 0x7fffff;
-   const int flt_e = (flt >> 23) & 0xff;
-   const int flt_s = (flt >> 31) & 0x1;
+   const fi_type fi = {val};
+   const int flt_m = fi.i & 0x7fffff;
+   const int flt_e = (fi.i >> 23) & 0xff;
+   const int flt_s = (fi.i >> 31) & 0x1;
    int s, e, m = 0;
    GLhalfARB result;
    
@@ -728,7 +739,8 @@ _mesa_half_to_float(GLhalfARB val)
    const int m = val & 0x3ff;
    const int e = (val >> 10) & 0x1f;
    const int s = (val >> 15) & 0x1;
-   int flt_m, flt_e, flt_s, flt;
+   int flt_m, flt_e, flt_s;
+   fi_type fi;
    float result;
 
    /* sign bit */
@@ -763,8 +775,8 @@ _mesa_half_to_float(GLhalfARB val)
       flt_m = m << 13;
    }
 
-   flt = (flt_s << 31) | (flt_e << 23) | flt_m;
-   result = *((float *) (void *) &flt);
+   fi.i = (flt_s << 31) | (flt_e << 23) | flt_m;
+   result = fi.f;
    return result;
 }
 
@@ -908,9 +920,31 @@ _mesa_atoi(const char *s)
 double
 _mesa_strtod( const char *s, char **end )
 {
+#ifdef _GNU_SOURCE
+   static locale_t loc = NULL;
+   if (!loc) {
+      loc = newlocale(LC_CTYPE_MASK, "C", NULL);
+   }
+   return strtod_l(s, end, loc);
+#else
    return strtod(s, end);
+#endif
 }
 
+/** Compute simple checksum/hash for a string */
+unsigned int
+_mesa_str_checksum(const char *str)
+{
+   /* This could probably be much better */
+   unsigned int sum, i;
+   const char *c;
+   sum = i = 1;
+   for (c = str; *c; c++, i++)
+      sum += *c * (i % 100);
+   return sum + i;
+}
+
+
 /*@}*/
 
 
@@ -1008,6 +1042,68 @@ output_if_debug(const char *prefixString, const char *outputString,
       fprintf(stderr, "%s: %s", prefixString, outputString);
       if (newline)
          fprintf(stderr, "\n");
+
+#if defined(_WIN32) && !defined(_WIN32_WCE)
+      /* stderr from windows applications without console is not usually 
+       * visible, so communicate with the debugger instead */ 
+      {
+         char buf[4096];
+         _mesa_snprintf(buf, sizeof(buf), "%s: %s%s", prefixString, outputString, newline ? "\n" : "");
+         OutputDebugStringA(buf);
+      }
+#endif
+   }
+}
+
+
+/**
+ * Return string version of GL error code.
+ */
+static const char *
+error_string( GLenum error )
+{
+   switch (error) {
+   case GL_NO_ERROR:
+      return "GL_NO_ERROR";
+   case GL_INVALID_VALUE:
+      return "GL_INVALID_VALUE";
+   case GL_INVALID_ENUM:
+      return "GL_INVALID_ENUM";
+   case GL_INVALID_OPERATION:
+      return "GL_INVALID_OPERATION";
+   case GL_STACK_OVERFLOW:
+      return "GL_STACK_OVERFLOW";
+   case GL_STACK_UNDERFLOW:
+      return "GL_STACK_UNDERFLOW";
+   case GL_OUT_OF_MEMORY:
+      return "GL_OUT_OF_MEMORY";
+   case GL_TABLE_TOO_LARGE:
+      return "GL_TABLE_TOO_LARGE";
+   case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
+      return "GL_INVALID_FRAMEBUFFER_OPERATION";
+   default:
+      return "unknown";
+   }
+}
+
+
+/**
+ * When a new type of error is recorded, print a message describing
+ * previous errors which were accumulated.
+ */
+static void
+flush_delayed_errors( GLcontext *ctx )
+{
+   char s[MAXSTRING];
+
+   if (ctx->ErrorDebugCount) {
+      _mesa_snprintf(s, MAXSTRING, "%d similar %s errors", 
+                     ctx->ErrorDebugCount,
+                     error_string(ctx->ErrorValue));
+
+      output_if_debug("Mesa", s, GL_TRUE);
+
+      ctx->ErrorDebugCount = 0;
    }
 }
 
@@ -1024,10 +1120,12 @@ _mesa_warning( GLcontext *ctx, const char *fmtString, ... )
 {
    char str[MAXSTRING];
    va_list args;
-   (void) ctx;
    va_start( args, fmtString );  
    (void) vsnprintf( str, MAXSTRING, fmtString, args );
    va_end( args );
+   
+   if (ctx)
+      flush_delayed_errors( ctx );
 
    output_if_debug("Mesa warning", str, GL_TRUE);
 }
@@ -1071,74 +1169,46 @@ _mesa_problem( const GLcontext *ctx, const char *fmtString, ... )
 void
 _mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
 {
-   const char *debugEnv;
-   GLboolean debug;
+   static GLint debug = -1;
 
-   debugEnv = _mesa_getenv("MESA_DEBUG");
+   /* Check debug environment variable only once:
+    */
+   if (debug == -1) {
+      const char *debugEnv = _mesa_getenv("MESA_DEBUG");
 
 #ifdef DEBUG
-   if (debugEnv && _mesa_strstr(debugEnv, "silent"))
-      debug = GL_FALSE;
-   else
-      debug = GL_TRUE;
+      if (debugEnv && _mesa_strstr(debugEnv, "silent"))
+         debug = GL_FALSE;
+      else
+         debug = GL_TRUE;
 #else
-   if (debugEnv)
-      debug = GL_TRUE;
-   else
-      debug = GL_FALSE;
+      if (debugEnv)
+         debug = GL_TRUE;
+      else
+         debug = GL_FALSE;
 #endif
+   }
 
-   if (debug) {
-      va_list args;
-      char where[MAXSTRING];
-      const char *errstr;
-
-      va_start( args, fmtString );  
-      vsnprintf( where, MAXSTRING, fmtString, args );
-      va_end( args );
-
-      switch (error) {
-        case GL_NO_ERROR:
-           errstr = "GL_NO_ERROR";
-           break;
-        case GL_INVALID_VALUE:
-           errstr = "GL_INVALID_VALUE";
-           break;
-        case GL_INVALID_ENUM:
-           errstr = "GL_INVALID_ENUM";
-           break;
-        case GL_INVALID_OPERATION:
-           errstr = "GL_INVALID_OPERATION";
-           break;
-        case GL_STACK_OVERFLOW:
-           errstr = "GL_STACK_OVERFLOW";
-           break;
-        case GL_STACK_UNDERFLOW:
-           errstr = "GL_STACK_UNDERFLOW";
-           break;
-        case GL_OUT_OF_MEMORY:
-           errstr = "GL_OUT_OF_MEMORY";
-           break;
-         case GL_TABLE_TOO_LARGE:
-            errstr = "GL_TABLE_TOO_LARGE";
-            break;
-         case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
-            errstr = "GL_INVALID_FRAMEBUFFER_OPERATION";
-            break;
-        default:
-           errstr = "unknown";
-           break;
+   if (debug) {      
+      if (ctx->ErrorValue == error &&
+          ctx->ErrorDebugFmtString == fmtString) {
+         ctx->ErrorDebugCount++;
       }
-
-      {
+      else {
          char s[MAXSTRING], s2[MAXSTRING];
          va_list args;
+
+         flush_delayed_errors( ctx );
+         
          va_start(args, fmtString);
          vsnprintf(s, MAXSTRING, fmtString, args);
          va_end(args);
 
-         _mesa_snprintf(s2, MAXSTRING, "%s in %s", errstr, s);
+         _mesa_snprintf(s2, MAXSTRING, "%s in %s", error_string(error), s);
          output_if_debug("Mesa: User error", s2, GL_TRUE);
+         
+         ctx->ErrorDebugFmtString = fmtString;
+         ctx->ErrorDebugCount = 0;
       }
    }
 
@@ -1169,13 +1239,3 @@ _mesa_debug( const GLcontext *ctx, const char *fmtString, ... )
 }
 
 /*@}*/
-
-
-/**
- * Wrapper for exit().
- */
-void
-_mesa_exit( int status )
-{
-   exit(status);
-}