fixed pointer arithmetic error in glCopyPixels
[mesa.git] / src / mesa / main / macros.h
index a9443c5c603bbc675c78db3770fa057102af19f4..bb83b30d5d42482496766e35c6816276593d260f 100644 (file)
@@ -1,21 +1,21 @@
-/* $Id: macros.h,v 1.5 1999/10/13 18:42:50 brianp Exp $ */
+/* $Id: macros.h,v 1.25 2002/02/13 00:53:19 keithw Exp $ */
 
 /*
  * Mesa 3-D graphics library
- * Version:  3.1
- * 
- * Copyright (C) 1999  Brian Paul   All Rights Reserved.
- * 
+ * Version:  3.5
+ *
+ * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be included
  * in all copies or substantial portions of the Software.
- * 
+ *
  * 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
@@ -25,9 +25,6 @@
  */
 
 
-
-
-
 /*
  * A collection of useful macros.
  */
 #ifndef MACROS_H
 #define MACROS_H
 
-#if defined( XFree86LOADER ) && defined( XFree86Server )
-#include <GL/glx_ansic.h>
-#else
-#include <math.h>
-#include <string.h>
-#endif
 
+#include "glheader.h"
+/* Do not reference mtypes.h from this file.
+ */
 
-#ifdef DEBUG
-#  include <assert.h>
-#  define ASSERT(X)   assert(X)
-#else
-#  define ASSERT(X)
+
+/* Limits: */
+#define MAX_GLUSHORT   0xffff
+#define MAX_GLUINT     0xffffffff
+
+
+/* Pi */
+#ifndef M_PI
+#define M_PI (3.1415926)
 #endif
 
 
-#if defined(__GNUC__)
-#define INLINE __inline__
-#elif defined(__MSC__)
-#define INLINE __inline
-#else
-#define INLINE
+/* Degrees to radians conversion: */
+#define DEG2RAD (M_PI/180.0)
+
+
+#ifndef NULL
+#define NULL 0
 #endif
 
 
-/* Stepping a GLfloat pointer by a byte stride 
+
+/*
+ * Bitmask helpers
  */
-#define STRIDE_F(p, i)  (p = (GLfloat *)((GLubyte *)p + i))
-#define STRIDE_UI(p, i)  (p = (GLuint *)((GLubyte *)p + i))
-#define STRIDE_T(p, t, i)  (p = (t *)((GLubyte *)p + i))
+#define SET_BITS(WORD, BITS)    (WORD) |= (BITS)
+#define CLEAR_BITS(WORD, BITS)  (WORD) &= ~(BITS)
+#define TEST_BITS(WORD, BITS)   ((WORD) & (BITS))
 
 
-/* Limits: */
-#define MAX_GLUSHORT   0xffff
-#define MAX_GLUINT     0xffffffff
+/* Stepping a GLfloat pointer by a byte stride
+ */
+#define STRIDE_F(p, i)  (p = (GLfloat *)((GLubyte *)p + i))
+#define STRIDE_UI(p, i)  (p = (GLuint *)((GLubyte *)p + i))
+#define STRIDE_4UB(p, i)  (p = (GLubyte (*)[4])((GLubyte *)p + i))
+#define STRIDE_4CHAN(p, i)  (p = (GLchan (*)[4])((GLubyte *)p + i))
+#define STRIDE_CHAN(p, i)  (p = (GLchan *)((GLubyte *)p + i))
+#define STRIDE_T(p, t, i)  (p = (t)((GLubyte *)p + i))
 
 
 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
 
 
+#define TEST_EQ_4V(a,b)  ((a)[0] == (b)[0] &&  \
+                         (a)[1] == (b)[1] &&   \
+                         (a)[2] == (b)[2] &&   \
+                         (a)[3] == (b)[3])
+
+#define TEST_EQ_3V(a,b)  ((a)[0] == (b)[0] &&  \
+                         (a)[1] == (b)[1] &&   \
+                         (a)[2] == (b)[2])
+
+#if defined(__i386__)
+#define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
+#else
+#define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
+#endif
+
+
+
 /* Copy short vectors: */
 #define COPY_2V( DST, SRC )                    \
 do {                                           \
@@ -85,7 +107,6 @@ do {                                         \
    (DST)[1] = (SRC)[1];                                \
 } while (0)
 
-
 #define COPY_3V( DST, SRC )                    \
 do {                                           \
    (DST)[0] = (SRC)[0];                                \
@@ -101,6 +122,21 @@ do {                                               \
    (DST)[3] = (SRC)[3];                                \
 } while (0)
 
+#if defined(__i386__)
+#define COPY_4UBV(DST, SRC)                    \
+do {                                           \
+   *((GLuint*)(DST)) = *((GLuint*)(SRC));      \
+} while (0)
+#else
+/* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
+#define COPY_4UBV(DST, SRC)                    \
+do {                                           \
+   (DST)[0] = (SRC)[0];                                \
+   (DST)[1] = (SRC)[1];                                \
+   (DST)[2] = (SRC)[2];                                \
+   (DST)[3] = (SRC)[3];                                \
+} while (0)
+#endif
 
 #define COPY_2FV( DST, SRC )                   \
 do {                                           \
@@ -109,7 +145,6 @@ do {                                                \
    (DST)[1] = _tmp[1];                         \
 } while (0)
 
-
 #define COPY_3FV( DST, SRC )                   \
 do {                                           \
    const GLfloat *_tmp = (SRC);                        \
@@ -137,7 +172,13 @@ do {                                               \
    case 2: (DST)[1] = (SRC)[1];                        \
    case 1: (DST)[0] = (SRC)[0];                        \
    }                                           \
-} while(0)                        
+} while(0)
+
+#define COPY_CLEAN_4V(DST, SZ, SRC)            \
+do {                                           \
+      ASSIGN_4V( DST, 0, 0, 0, 1 );            \
+      COPY_SZ_4V( DST, SZ, SRC );              \
+} while (0)
 
 #define SUB_4V( DST, SRCA, SRCB )              \
 do {                                           \
@@ -165,26 +206,26 @@ do {                                              \
 
 #define ACC_4V( DST, SRC )                     \
 do {                                           \
-      (DST)[0] += (SRC)[0];                            \
-      (DST)[1] += (SRC)[1];                            \
-      (DST)[2] += (SRC)[2];                            \
-      (DST)[3] += (SRC)[3];                            \
+      (DST)[0] += (SRC)[0];                    \
+      (DST)[1] += (SRC)[1];                    \
+      (DST)[2] += (SRC)[2];                    \
+      (DST)[3] += (SRC)[3];                    \
 } while (0)
 
 #define ACC_SCALE_4V( DST, SRCA, SRCB )                \
 do {                                           \
-      (DST)[0] += (SRCA)[0] * (SRCB)[0];               \
-      (DST)[1] += (SRCA)[1] * (SRCB)[1];               \
-      (DST)[2] += (SRCA)[2] * (SRCB)[2];               \
-      (DST)[3] += (SRCA)[3] * (SRCB)[3];               \
+      (DST)[0] += (SRCA)[0] * (SRCB)[0];       \
+      (DST)[1] += (SRCA)[1] * (SRCB)[1];       \
+      (DST)[2] += (SRCA)[2] * (SRCB)[2];       \
+      (DST)[3] += (SRCA)[3] * (SRCB)[3];       \
 } while (0)
 
 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB )    \
 do {                                           \
-      (DST)[0] += S * (SRCB)[0];                       \
-      (DST)[1] += S * (SRCB)[1];                       \
-      (DST)[2] += S * (SRCB)[2];                       \
-      (DST)[3] += S * (SRCB)[3];                       \
+      (DST)[0] += S * (SRCB)[0];               \
+      (DST)[1] += S * (SRCB)[1];               \
+      (DST)[2] += S * (SRCB)[2];               \
+      (DST)[3] += S * (SRCB)[3];               \
 } while (0)
 
 #define SCALE_SCALAR_4V( DST, S, SRCB )                \
@@ -229,6 +270,13 @@ do {                                               \
       (DST)[2] = (SRCA)[2] * (SRCB)[2];                \
 } while (0)
 
+#define SELF_SCALE_3V( DST, SRC )              \
+do {                                           \
+      (DST)[0] *= (SRC)[0];                    \
+      (DST)[1] *= (SRC)[1];                    \
+      (DST)[2] *= (SRC)[2];                    \
+} while (0)
+
 #define ACC_3V( DST, SRC )                     \
 do {                                           \
       (DST)[0] += (SRC)[0];                    \
@@ -329,29 +377,19 @@ do {                                              \
 
 
 
-/*
- * Copy a vector of 4 GLubytes from SRC to DST.
- */
-#define COPY_4UBV(DST, SRC)                    \
-do {                                           \
-   if (sizeof(GLuint)==4*sizeof(GLubyte)) {    \
-      *((GLuint*)(DST)) = *((GLuint*)(SRC));   \
-   }                                           \
-   else {                                      \
-      (DST)[0] = (SRC)[0];                     \
-      (DST)[1] = (SRC)[1];                     \
-      (DST)[2] = (SRC)[2];                     \
-      (DST)[3] = (SRC)[3];                     \
-   }                                           \
-} while (0)
-
-
 /* Assign scalers to short vectors: */
-#define ASSIGN_2V( V, V0, V1 )  \
-do { V[0] = V0;  V[1] = V1; } while(0)
+#define ASSIGN_2V( V, V0, V1 ) \
+do {                           \
+    V[0] = V0;                         \
+    V[1] = V1;                         \
+} while(0)
 
-#define ASSIGN_3V( V, V0, V1, V2 )  \
-do { V[0] = V0;  V[1] = V1;  V[2] = V2; } while(0)
+#define ASSIGN_3V( V, V0, V1, V2 )     \
+do {                                   \
+    V[0] = V0;                                 \
+    V[1] = V1;                                 \
+    V[2] = V2;                                 \
+} while(0)
 
 #define ASSIGN_4V( V, V0, V1, V2, V3 )                 \
 do {                                           \
@@ -391,7 +429,6 @@ do {                                                \
 /* Min of two values: */
 #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
 
-
 /* MAX of two values: */
 #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
 
@@ -401,12 +438,11 @@ do {                                              \
 /* Dot product of two 3-element vectors */
 #define DOT3( a, b )  ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
 
-
 /* Dot product of two 4-element vectors */
 #define DOT4( a, b )  ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
                        (a)[2]*(b)[2] + (a)[3]*(b)[3] )
 
-#define DOT4V(v,a,b,c,d) (v[0]*a + v[1]*b + v[2]*c + v[3]*d)
+#define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
 
 
 #define CROSS3(n, u, v)                        \
@@ -417,155 +453,31 @@ do {                                             \
 } while (0)
 
 
-/*
- * Integer / float conversion for colors, normals, etc.
- */
-
-
-
-
-#define BYTE_TO_UBYTE(b)   (b < 0 ? 0 : (GLubyte) b)
-#define SHORT_TO_UBYTE(s)  (s < 0 ? 0 : (GLubyte) (s >> 7))
-#define USHORT_TO_UBYTE(s)              (GLubyte) (s >> 8)
-#define INT_TO_UBYTE(i)    (i < 0 ? 0 : (GLubyte) (i >> 23))
-#define UINT_TO_UBYTE(i)                (GLubyte) (i >> 24)
-
-
-
-
-/* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
-#define UBYTE_TO_FLOAT(B)      ((GLfloat) (B) * (1.0F / 255.0F))
-
-/* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
-#define FLOAT_TO_UBYTE(X)      ((GLubyte) (GLint) (((X)) * 255.0F))
-
-
-/* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
-#define BYTE_TO_FLOAT(B)       ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
-
-/* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
-#define FLOAT_TO_BYTE(X)       ( (((GLint) (255.0F * (X))) - 1) / 2 )
-
-
-/* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
-#define USHORT_TO_FLOAT(S)     ((GLfloat) (S) * (1.0F / 65535.0F))
-
-/* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
-#define FLOAT_TO_USHORT(X)     ((GLushort) (GLint) ((X) * 65535.0F))
-
-
-/* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
-#define SHORT_TO_FLOAT(S)      ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
-
-/* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
-#define FLOAT_TO_SHORT(X)      ( (((GLint) (65535.0F * (X))) - 1) / 2 )
-
-
-/* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
-#define UINT_TO_FLOAT(U)       ((GLfloat) (U) * (1.0F / 4294967295.0F))
 
-/* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
-#define FLOAT_TO_UINT(X)       ((GLuint) ((X) * 4294967295.0))
-
-
-/* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
-#define INT_TO_FLOAT(I)                ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
-
-/* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
-/* causes overflow:
-#define FLOAT_TO_INT(X)                ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
-*/
-/* a close approximation: */
-#define FLOAT_TO_INT(X)                ( (GLint) (2147483647.0 * (X)) )
-
-
-
-/*
- * Memory allocation
- * XXX these should probably go into a new glmemory.h file.
+/* Generic color packing macros
  */
-#ifdef DEBUG
-extern void *gl_malloc(size_t bytes);
-extern void *gl_calloc(size_t bytes);
-extern void gl_free(void *ptr);
-#define MALLOC(BYTES)      gl_malloc(BYTES)
-#define CALLOC(BYTES)      gl_calloc(BYTES)
-#define MALLOC_STRUCT(T)   (struct T *) gl_malloc(sizeof(struct T))
-#define CALLOC_STRUCT(T)   (struct T *) gl_calloc(sizeof(struct T))
-#define FREE(PTR)          gl_free(PTR)
-#else
-#define MALLOC(BYTES)      (void *) malloc(BYTES)
-#define CALLOC(BYTES)      (void *) calloc(1, BYTES)
-#define MALLOC_STRUCT(T)   (struct T *) malloc(sizeof(struct T))
-#define CALLOC_STRUCT(T)   (struct T *) calloc(1,sizeof(struct T))
-#define FREE(PTR)          free(PTR)
-#endif
-
-
-/* Memory copy: */
-#ifdef SUNOS4
-#define MEMCPY( DST, SRC, BYTES) \
-       memcpy( (char *) (DST), (char *) (SRC), (int) (BYTES) )
-#else
-#define MEMCPY( DST, SRC, BYTES) \
-       memcpy( (void *) (DST), (void *) (SRC), (size_t) (BYTES) )
-#endif
-
-
-/* Memory set: */
-#ifdef SUNOS4
-#define MEMSET( DST, VAL, N ) \
-       memset( (char *) (DST), (int) (VAL), (int) (N) )
-#else
-#define MEMSET( DST, VAL, N ) \
-       memset( (void *) (DST), (int) (VAL), (size_t) (N) )
-#endif
 
+#define PACK_COLOR_8888( a, b, c, d )                                  \
+   (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
 
-/* MACs and BeOS don't support static larger than 32kb, so... */
-#if defined(macintosh) && !defined(__MRC__)
-  extern char *AGLAlloc(int size);
-  extern void AGLFree(char* ptr);
-#  define DEFARRAY(TYPE,NAME,SIZE)                     TYPE *NAME = (TYPE*)AGLAlloc(sizeof(TYPE)*(SIZE))
-#  define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2)             TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])AGLAlloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
-#  define CHECKARRAY(NAME,CMD)                         do {if (!(NAME)) {CMD;}} while (0) 
-#  define UNDEFARRAY(NAME)                             do {if ((NAME)) {AGLFree((char*)NAME);}  }while (0)
-#elif defined(__BEOS__)
-#  define DEFARRAY(TYPE,NAME,SIZE)                     TYPE *NAME = (TYPE*)malloc(sizeof(TYPE)*(SIZE))
-#  define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2)             TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])malloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
-#  define CHECKARRAY(NAME,CMD)                         do {if (!(NAME)) {CMD;}} while (0)
-#  define UNDEFARRAY(NAME)                             do {if ((NAME)) {free((char*)NAME);}  }while (0)
-#else
-#  define DEFARRAY(TYPE,NAME,SIZE)                     TYPE NAME[SIZE]
-#  define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2)             TYPE NAME[SIZE1][SIZE2]
-#  define CHECKARRAY(NAME,CMD)                         do {} while(0)
-#  define UNDEFARRAY(NAME)
-#endif
+#define PACK_COLOR_888( a, b, c )                                      \
+   (((a) << 16) | ((b) << 8) | (c))
 
+#define PACK_COLOR_565( a, b, c )                                      \
+   ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
 
-/* Some compilers don't like some of Mesa's const usage */
-#ifdef NO_CONST
-#  define CONST
-#else
-#  define CONST const
-#endif
+#define PACK_COLOR_1555( a, b, c, d )                                  \
+   ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) |  \
+    ((a) ? 0x8000 : 0))
 
+#define PACK_COLOR_4444( a, b, c, d )                                  \
+   ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
 
+#define PACK_COLOR_88( a, b )                                          \
+   (((a) << 8) | (b))
 
-/* Pi */
-#ifndef M_PI
-#define M_PI (3.1415926)
-#endif
+#define PACK_COLOR_332( a, b, c )                                      \
+   (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
 
 
-/* Degrees to radians conversion: */
-#define DEG2RAD (M_PI/180.0)
-
-
-#ifndef NULL
-#define NULL 0
 #endif
-
-
-
-#endif /*MACROS_H*/