mesa: Allow setting GL_TEXTURE_MAX_LEVEL to 0 with GL_TEXTURE_RECTANGLE.
[mesa.git] / src / mesa / main / imports.c
index fc30a6eb671ae0bcf0edb1fa8af9e7ab78bae2e3..4afe156b0315ec1b0f1fd733d847af9d3836cce9 100644 (file)
@@ -20,7 +20,6 @@
 
 /*
  * Mesa 3-D graphics library
- * Version:  7.1
  *
  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 
 #endif
 
 
-#ifdef WIN32
+#ifdef _WIN32
 #define vsnprintf _vsnprintf
-#elif defined(__IBMC__) || defined(__IBMCPP__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
+#elif defined(__IBMC__) || defined(__IBMCPP__)
 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
-#ifdef __VMS
-#include "vsnprintf.c"
-#endif
 #endif
 
 /**********************************************************************/
@@ -97,7 +94,7 @@ _mesa_align_malloc(size_t bytes, unsigned long alignment)
 
    ASSERT( alignment > 0 );
 
-   ptr = (uintptr_t) malloc(bytes + alignment + sizeof(void *));
+   ptr = (uintptr_t)malloc(bytes + alignment + sizeof(void *));
    if (!ptr)
       return NULL;
 
@@ -146,7 +143,7 @@ _mesa_align_calloc(size_t bytes, unsigned long alignment)
 
    ASSERT( alignment > 0 );
 
-   ptr = (uintptr_t) calloc(1, bytes + alignment + sizeof(void *));
+   ptr = (uintptr_t)calloc(1, bytes + alignment + sizeof(void *));
    if (!ptr)
       return NULL;
 
@@ -171,6 +168,8 @@ _mesa_align_calloc(size_t bytes, unsigned long alignment)
  * \param ptr pointer to the memory to be freed.
  * The actual address to free is stored in the word immediately before the
  * address the client sees.
+ * Note that it is legal to pass NULL pointer to this function and will be
+ * handled accordingly.
  */
 void
 _mesa_align_free(void *ptr)
@@ -180,9 +179,11 @@ _mesa_align_free(void *ptr)
 #elif defined(_WIN32) && defined(_MSC_VER)
    _aligned_free(ptr);
 #else
-   void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
-   void *realAddr = *cubbyHole;
-   free(realAddr);
+   if (ptr) {
+      void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
+      void *realAddr = *cubbyHole;
+      free(realAddr);
+   }
 #endif /* defined(HAVE_POSIX_MEMALIGN) */
 }
 
@@ -202,8 +203,8 @@ _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
    if (newBuf && oldBuffer && copySize > 0) {
       memcpy(newBuf, oldBuffer, copySize);
    }
-   if (oldBuffer)
-      _mesa_align_free(oldBuffer);
+
+   _mesa_align_free(oldBuffer);
    return newBuf;
 #endif
 }
@@ -218,24 +219,10 @@ _mesa_realloc(void *oldBuffer, size_t oldSize, size_t newSize)
    void *newBuffer = malloc(newSize);
    if (newBuffer && oldBuffer && copySize > 0)
       memcpy(newBuffer, oldBuffer, copySize);
-   if (oldBuffer)
-      free(oldBuffer);
+   free(oldBuffer);
    return newBuffer;
 }
 
-/**
- * Fill memory with a constant 16bit word.
- * \param dst destination pointer.
- * \param val value.
- * \param n number of words.
- */
-void
-_mesa_memset16( unsigned short *dst, unsigned short val, size_t n )
-{
-   while (n-- > 0)
-      *dst++ = val;
-}
-
 /*@}*/
 
 
@@ -244,112 +231,6 @@ _mesa_memset16( unsigned short *dst, unsigned short val, size_t n )
 /*@{*/
 
 
-/**
- inv_sqrt - A single precision 1/sqrt routine for IEEE format floats.
- written by Josh Vanderhoof, based on newsgroup posts by James Van Buskirk
- and Vesa Karvonen.
-*/
-float
-_mesa_inv_sqrtf(float n)
-{
-#if defined(USE_IEEE) && !defined(DEBUG)
-        float r0, x0, y0;
-        float r1, x1, y1;
-        float r2, x2, y2;
-#if 0 /* not used, see below -BP */
-        float r3, x3, y3;
-#endif
-        fi_type u;
-        unsigned int magic;
-
-        /*
-         Exponent part of the magic number -
-
-         We want to:
-         1. subtract the bias from the exponent,
-         2. negate it
-         3. divide by two (rounding towards -inf)
-         4. add the bias back
-
-         Which is the same as subtracting the exponent from 381 and dividing
-         by 2.
-
-         floor(-(x - 127) / 2) + 127 = floor((381 - x) / 2)
-        */
-
-        magic = 381 << 23;
-
-        /*
-         Significand part of magic number -
-
-         With the current magic number, "(magic - u.i) >> 1" will give you:
-
-         for 1 <= u.f <= 2: 1.25 - u.f / 4
-         for 2 <= u.f <= 4: 1.00 - u.f / 8
-
-         This isn't a bad approximation of 1/sqrt.  The maximum difference from
-         1/sqrt will be around .06.  After three Newton-Raphson iterations, the
-         maximum difference is less than 4.5e-8.  (Which is actually close
-         enough to make the following bias academic...)
-
-         To get a better approximation you can add a bias to the magic
-         number.  For example, if you subtract 1/2 of the maximum difference in
-         the first approximation (.03), you will get the following function:
-
-         for 1 <= u.f <= 2:    1.22 - u.f / 4
-         for 2 <= u.f <= 3.76: 0.97 - u.f / 8
-         for 3.76 <= u.f <= 4: 0.72 - u.f / 16
-         (The 3.76 to 4 range is where the result is < .5.)
-
-         This is the closest possible initial approximation, but with a maximum
-         error of 8e-11 after three NR iterations, it is still not perfect.  If
-         you subtract 0.0332281 instead of .03, the maximum error will be
-         2.5e-11 after three NR iterations, which should be about as close as
-         is possible.
-
-         for 1 <= u.f <= 2:    1.2167719 - u.f / 4
-         for 2 <= u.f <= 3.73: 0.9667719 - u.f / 8
-         for 3.73 <= u.f <= 4: 0.7167719 - u.f / 16
-
-        */
-
-        magic -= (int)(0.0332281 * (1 << 25));
-
-        u.f = n;
-        u.i = (magic - u.i) >> 1;
-
-        /*
-         Instead of Newton-Raphson, we use Goldschmidt's algorithm, which
-         allows more parallelism.  From what I understand, the parallelism
-         comes at the cost of less precision, because it lets error
-         accumulate across iterations.
-        */
-        x0 = 1.0f;
-        y0 = 0.5f * n;
-        r0 = u.f;
-
-        x1 = x0 * r0;
-        y1 = y0 * r0 * r0;
-        r1 = 1.5f - y1;
-
-        x2 = x1 * r1;
-        y2 = y1 * r1 * r1;
-        r2 = 1.5f - y2;
-
-#if 1
-        return x2 * r2;  /* we can stop here, and be conformant -BP */
-#else
-        x3 = x2 * r2;
-        y3 = y2 * r2 * r2;
-        r3 = 1.5f - y3;
-
-        return x3 * r3;
-#endif
-#else
-        return (float) (1.0 / sqrt(n));
-#endif
-}
-
 #ifndef __GNUC__
 /**
  * Find the first bit set in a word.
@@ -437,10 +318,43 @@ _mesa_bitcount_64(uint64_t n)
 #endif
 
 
+/* Using C99 rounding functions for roundToEven() implementation is
+ * difficult, because round(), rint, and nearbyint() are affected by
+ * fesetenv(), which the application may have done for its own
+ * purposes.  Mesa's IROUND macro is close to what we want, but it
+ * rounds away from 0 on n + 0.5.
+ */
+int
+_mesa_round_to_even(float val)
+{
+   int rounded = IROUND(val);
+
+   if (val - floor(val) == 0.5) {
+      if (rounded % 2 != 0)
+         rounded += val > 0 ? -1 : 1;
+   }
+
+   return rounded;
+}
+
+
 /**
  * Convert a 4-byte float to a 2-byte half float.
- * Based on code from:
- * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
+ *
+ * Not all float32 values can be represented exactly as a float16 value. We
+ * round such intermediate float32 values to the nearest float16. When the
+ * float32 lies exactly between to float16 values, we round to the one with
+ * an even mantissa.
+ *
+ * This rounding behavior has several benefits:
+ *   - It has no sign bias.
+ *
+ *   - It reproduces the behavior of real hardware: opcode F32TO16 in Intel's
+ *     GPU ISA.
+ *
+ *   - By reproducing the behavior of the GPU (at least on Intel hardware),
+ *     compile-time evaluation of constant packHalf2x16 GLSL expressions will
+ *     result in the same value as if the expression were executed on the GPU.
  */
 GLhalfARB
 _mesa_float_to_half(float val)
@@ -479,32 +393,13 @@ _mesa_float_to_half(float val)
    else {
       /* regular number */
       const int new_exp = flt_e - 127;
-      if (new_exp < -24) {
-         /* this maps to 0 */
-         /* m = 0; - already set */
+      if (new_exp < -14) {
+         /* The float32 lies in the range (0.0, min_normal16) and is rounded
+          * to a nearby float16 value. The result will be either zero, subnormal,
+          * or normal.
+          */
          e = 0;
-      }
-      else if (new_exp < -14) {
-         /* this maps to a denorm */
-         unsigned int exp_val = (unsigned int) (-14 - new_exp); /* 2^-exp_val*/
-         e = 0;
-         switch (exp_val) {
-            case 0:
-               _mesa_warning(NULL,
-                   "float_to_half: logical error in denorm creation!\n");
-               /* m = 0; - already set */
-               break;
-            case 1: m = 512 + (flt_m >> 14); break;
-            case 2: m = 256 + (flt_m >> 15); break;
-            case 3: m = 128 + (flt_m >> 16); break;
-            case 4: m = 64 + (flt_m >> 17); break;
-            case 5: m = 32 + (flt_m >> 18); break;
-            case 6: m = 16 + (flt_m >> 19); break;
-            case 7: m = 8 + (flt_m >> 20); break;
-            case 8: m = 4 + (flt_m >> 21); break;
-            case 9: m = 2 + (flt_m >> 22); break;
-            case 10: m = 1; break;
-         }
+         m = _mesa_round_to_even((1 << 24) * fabsf(fi.f));
       }
       else if (new_exp > 15) {
          /* map this value to infinity */
@@ -512,12 +407,26 @@ _mesa_float_to_half(float val)
          e = 31;
       }
       else {
-         /* regular */
+         /* The float32 lies in the range
+          *   [min_normal16, max_normal16 + max_step16)
+          * and is rounded to a nearby float16 value. The result will be
+          * either normal or infinite.
+          */
          e = new_exp + 15;
-         m = flt_m >> 13;
+         m = _mesa_round_to_even(flt_m / (float) (1 << 13));
       }
    }
 
+   assert(0 <= m && m <= 1024);
+   if (m == 1024) {
+      /* The float32 was rounded upwards into the range of the next exponent,
+       * so bump the exponent. This correctly handles the case where f32
+       * should be rounded up to float16 infinity.
+       */
+      ++e;
+      m = 0;
+   }
+
    result = (s << 15) | (e << 10) | m;
    return result;
 }
@@ -646,7 +555,7 @@ _mesa_strdup( const char *s )
 {
    if (s) {
       size_t l = strlen(s);
-      char *s2 = (char *) malloc(l + 1);
+      char *s2 = malloc(l + 1);
       if (s2)
          strcpy(s2, s);
       return s2;
@@ -661,7 +570,8 @@ float
 _mesa_strtof( const char *s, char **end )
 {
 #if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \
-   !defined(ANDROID) && !defined(__HAIKU__)
+   !defined(ANDROID) && !defined(__HAIKU__) && !defined(__UCLIBC__) && \
+   !defined(__NetBSD__)
    static locale_t loc = NULL;
    if (!loc) {
       loc = newlocale(LC_CTYPE_MASK, "C", NULL);