1 /* $Id: macros.h,v 1.5 1999/10/13 18:42:50 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 * A collection of useful macros.
39 #if defined( XFree86LOADER ) && defined( XFree86Server )
40 #include <GL/glx_ansic.h>
49 # define ASSERT(X) assert(X)
56 #define INLINE __inline__
57 #elif defined(__MSC__)
58 #define INLINE __inline
64 /* Stepping a GLfloat pointer by a byte stride
66 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
67 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
68 #define STRIDE_T(p, t, i) (p = (t *)((GLubyte *)p + i))
72 #define MAX_GLUSHORT 0xffff
73 #define MAX_GLUINT 0xffffffff
76 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
77 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
78 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
81 /* Copy short vectors: */
82 #define COPY_2V( DST, SRC ) \
84 (DST)[0] = (SRC)[0]; \
85 (DST)[1] = (SRC)[1]; \
89 #define COPY_3V( DST, SRC ) \
91 (DST)[0] = (SRC)[0]; \
92 (DST)[1] = (SRC)[1]; \
93 (DST)[2] = (SRC)[2]; \
96 #define COPY_4V( DST, SRC ) \
98 (DST)[0] = (SRC)[0]; \
99 (DST)[1] = (SRC)[1]; \
100 (DST)[2] = (SRC)[2]; \
101 (DST)[3] = (SRC)[3]; \
105 #define COPY_2FV( DST, SRC ) \
107 const GLfloat *_tmp = (SRC); \
108 (DST)[0] = _tmp[0]; \
109 (DST)[1] = _tmp[1]; \
113 #define COPY_3FV( DST, SRC ) \
115 const GLfloat *_tmp = (SRC); \
116 (DST)[0] = _tmp[0]; \
117 (DST)[1] = _tmp[1]; \
118 (DST)[2] = _tmp[2]; \
121 #define COPY_4FV( DST, SRC ) \
123 const GLfloat *_tmp = (SRC); \
124 (DST)[0] = _tmp[0]; \
125 (DST)[1] = _tmp[1]; \
126 (DST)[2] = _tmp[2]; \
127 (DST)[3] = _tmp[3]; \
132 #define COPY_SZ_4V(DST, SZ, SRC) \
135 case 4: (DST)[3] = (SRC)[3]; \
136 case 3: (DST)[2] = (SRC)[2]; \
137 case 2: (DST)[1] = (SRC)[1]; \
138 case 1: (DST)[0] = (SRC)[0]; \
142 #define SUB_4V( DST, SRCA, SRCB ) \
144 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
145 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
146 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
147 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
150 #define ADD_4V( DST, SRCA, SRCB ) \
152 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
153 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
154 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
155 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
158 #define SCALE_4V( DST, SRCA, SRCB ) \
160 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
161 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
162 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
163 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
166 #define ACC_4V( DST, SRC ) \
168 (DST)[0] += (SRC)[0]; \
169 (DST)[1] += (SRC)[1]; \
170 (DST)[2] += (SRC)[2]; \
171 (DST)[3] += (SRC)[3]; \
174 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
176 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
177 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
178 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
179 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
182 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
184 (DST)[0] += S * (SRCB)[0]; \
185 (DST)[1] += S * (SRCB)[1]; \
186 (DST)[2] += S * (SRCB)[2]; \
187 (DST)[3] += S * (SRCB)[3]; \
190 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
192 (DST)[0] = S * (SRCB)[0]; \
193 (DST)[1] = S * (SRCB)[1]; \
194 (DST)[2] = S * (SRCB)[2]; \
195 (DST)[3] = S * (SRCB)[3]; \
199 #define SELF_SCALE_SCALAR_4V( DST, S ) \
209 * Similarly for 3-vectors.
211 #define SUB_3V( DST, SRCA, SRCB ) \
213 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
214 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
215 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
218 #define ADD_3V( DST, SRCA, SRCB ) \
220 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
221 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
222 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
225 #define SCALE_3V( DST, SRCA, SRCB ) \
227 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
228 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
229 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
232 #define ACC_3V( DST, SRC ) \
234 (DST)[0] += (SRC)[0]; \
235 (DST)[1] += (SRC)[1]; \
236 (DST)[2] += (SRC)[2]; \
239 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
241 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
242 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
243 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
246 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
248 (DST)[0] = S * (SRCB)[0]; \
249 (DST)[1] = S * (SRCB)[1]; \
250 (DST)[2] = S * (SRCB)[2]; \
253 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
255 (DST)[0] += S * (SRCB)[0]; \
256 (DST)[1] += S * (SRCB)[1]; \
257 (DST)[2] += S * (SRCB)[2]; \
260 #define SELF_SCALE_SCALAR_3V( DST, S ) \
267 #define ACC_SCALAR_3V( DST, S ) \
274 /* And also for 2-vectors
276 #define SUB_2V( DST, SRCA, SRCB ) \
278 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
279 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
282 #define ADD_2V( DST, SRCA, SRCB ) \
284 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
285 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
288 #define SCALE_2V( DST, SRCA, SRCB ) \
290 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
291 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
294 #define ACC_2V( DST, SRC ) \
296 (DST)[0] += (SRC)[0]; \
297 (DST)[1] += (SRC)[1]; \
300 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
302 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
303 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
306 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
308 (DST)[0] = S * (SRCB)[0]; \
309 (DST)[1] = S * (SRCB)[1]; \
312 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
314 (DST)[0] += S * (SRCB)[0]; \
315 (DST)[1] += S * (SRCB)[1]; \
318 #define SELF_SCALE_SCALAR_2V( DST, S ) \
324 #define ACC_SCALAR_2V( DST, S ) \
333 * Copy a vector of 4 GLubytes from SRC to DST.
335 #define COPY_4UBV(DST, SRC) \
337 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
338 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
341 (DST)[0] = (SRC)[0]; \
342 (DST)[1] = (SRC)[1]; \
343 (DST)[2] = (SRC)[2]; \
344 (DST)[3] = (SRC)[3]; \
349 /* Assign scalers to short vectors: */
350 #define ASSIGN_2V( V, V0, V1 ) \
351 do { V[0] = V0; V[1] = V1; } while(0)
353 #define ASSIGN_3V( V, V0, V1, V2 ) \
354 do { V[0] = V0; V[1] = V1; V[2] = V2; } while(0)
356 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
367 /* Absolute value (for Int, Float, Double): */
368 #define ABSI(X) ((X) < 0 ? -(X) : (X))
369 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
370 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
374 /* Round a floating-point value to the nearest integer: */
375 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
378 /* Compute ceiling of integer quotient of A divided by B: */
379 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
382 /* Clamp X to [MIN,MAX]: */
383 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
385 /* Assign X to CLAMP(X, MIN, MAX) */
386 #define CLAMP_SELF(x, mn, mx) \
387 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
391 /* Min of two values: */
392 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
395 /* MAX of two values: */
396 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
398 /* Dot product of two 2-element vectors */
399 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
401 /* Dot product of two 3-element vectors */
402 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
405 /* Dot product of two 4-element vectors */
406 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
407 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
409 #define DOT4V(v,a,b,c,d) (v[0]*a + v[1]*b + v[2]*c + v[3]*d)
412 #define CROSS3(n, u, v) \
414 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
415 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
416 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
421 * Integer / float conversion for colors, normals, etc.
427 #define BYTE_TO_UBYTE(b) (b < 0 ? 0 : (GLubyte) b)
428 #define SHORT_TO_UBYTE(s) (s < 0 ? 0 : (GLubyte) (s >> 7))
429 #define USHORT_TO_UBYTE(s) (GLubyte) (s >> 8)
430 #define INT_TO_UBYTE(i) (i < 0 ? 0 : (GLubyte) (i >> 23))
431 #define UINT_TO_UBYTE(i) (GLubyte) (i >> 24)
436 /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
437 #define UBYTE_TO_FLOAT(B) ((GLfloat) (B) * (1.0F / 255.0F))
439 /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
440 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F))
443 /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
444 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
446 /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
447 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
450 /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
451 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
453 /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
454 #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
457 /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
458 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
460 /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
461 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
464 /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
465 #define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F))
467 /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
468 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
471 /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
472 #define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
474 /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
476 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
478 /* a close approximation: */
479 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
485 * XXX these should probably go into a new glmemory.h file.
488 extern void *gl_malloc(size_t bytes
);
489 extern void *gl_calloc(size_t bytes
);
490 extern void gl_free(void *ptr
);
491 #define MALLOC(BYTES) gl_malloc(BYTES)
492 #define CALLOC(BYTES) gl_calloc(BYTES)
493 #define MALLOC_STRUCT(T) (struct T *) gl_malloc(sizeof(struct T))
494 #define CALLOC_STRUCT(T) (struct T *) gl_calloc(sizeof(struct T))
495 #define FREE(PTR) gl_free(PTR)
497 #define MALLOC(BYTES) (void *) malloc(BYTES)
498 #define CALLOC(BYTES) (void *) calloc(1, BYTES)
499 #define MALLOC_STRUCT(T) (struct T *) malloc(sizeof(struct T))
500 #define CALLOC_STRUCT(T) (struct T *) calloc(1,sizeof(struct T))
501 #define FREE(PTR) free(PTR)
507 #define MEMCPY( DST, SRC, BYTES) \
508 memcpy( (char *) (DST), (char *) (SRC), (int) (BYTES) )
510 #define MEMCPY( DST, SRC, BYTES) \
511 memcpy( (void *) (DST), (void *) (SRC), (size_t) (BYTES) )
517 #define MEMSET( DST, VAL, N ) \
518 memset( (char *) (DST), (int) (VAL), (int) (N) )
520 #define MEMSET( DST, VAL, N ) \
521 memset( (void *) (DST), (int) (VAL), (size_t) (N) )
525 /* MACs and BeOS don't support static larger than 32kb, so... */
526 #if defined(macintosh) && !defined(__MRC__)
527 extern char *AGLAlloc(int size
);
528 extern void AGLFree(char* ptr
);
529 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)AGLAlloc(sizeof(TYPE)*(SIZE))
530 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])AGLAlloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
531 # define CHECKARRAY(NAME,CMD) do {if (!(NAME)) {CMD;}} while (0)
532 # define UNDEFARRAY(NAME) do {if ((NAME)) {AGLFree((char*)NAME);} }while (0)
533 #elif defined(__BEOS__)
534 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)malloc(sizeof(TYPE)*(SIZE))
535 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])malloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
536 # define CHECKARRAY(NAME,CMD) do {if (!(NAME)) {CMD;}} while (0)
537 # define UNDEFARRAY(NAME) do {if ((NAME)) {free((char*)NAME);} }while (0)
539 # define DEFARRAY(TYPE,NAME,SIZE) TYPE NAME[SIZE]
540 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE NAME[SIZE1][SIZE2]
541 # define CHECKARRAY(NAME,CMD) do {} while(0)
542 # define UNDEFARRAY(NAME)
546 /* Some compilers don't like some of Mesa's const usage */
557 #define M_PI (3.1415926)
561 /* Degrees to radians conversion: */
562 #define DEG2RAD (M_PI/180.0)