1 /* $Id: macros.h,v 1.1 1999/08/19 00:55:41 jtg 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.
46 # define ASSERT(X) assert(X)
52 #if defined(__GNUC__) || defined(__MWERKS__)
53 #define INLINE __inline__
54 #elif defined(__MSC__)
55 #define INLINE __inline
61 /* Stepping a GLfloat pointer by a byte stride
63 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
64 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
65 #define STRIDE_T(p, t, i) (p = (t *)((GLubyte *)p + i))
69 #define MAX_GLUSHORT 0xffff
70 #define MAX_GLUINT 0xffffffff
73 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
74 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
75 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
78 /* Copy short vectors: */
79 #define COPY_2V( DST, SRC ) \
81 (DST)[0] = (SRC)[0]; \
82 (DST)[1] = (SRC)[1]; \
86 #define COPY_3V( DST, SRC ) \
88 (DST)[0] = (SRC)[0]; \
89 (DST)[1] = (SRC)[1]; \
90 (DST)[2] = (SRC)[2]; \
93 #define COPY_4V( DST, SRC ) \
95 (DST)[0] = (SRC)[0]; \
96 (DST)[1] = (SRC)[1]; \
97 (DST)[2] = (SRC)[2]; \
98 (DST)[3] = (SRC)[3]; \
102 #define COPY_2FV( DST, SRC ) \
104 const GLfloat *_tmp = (SRC); \
105 (DST)[0] = _tmp[0]; \
106 (DST)[1] = _tmp[1]; \
110 #define COPY_3FV( DST, SRC ) \
112 const GLfloat *_tmp = (SRC); \
113 (DST)[0] = _tmp[0]; \
114 (DST)[1] = _tmp[1]; \
115 (DST)[2] = _tmp[2]; \
118 #define COPY_4FV( DST, SRC ) \
120 const GLfloat *_tmp = (SRC); \
121 (DST)[0] = _tmp[0]; \
122 (DST)[1] = _tmp[1]; \
123 (DST)[2] = _tmp[2]; \
124 (DST)[3] = _tmp[3]; \
129 #define COPY_SZ_4V(DST, SZ, SRC) \
132 case 4: (DST)[3] = (SRC)[3]; \
133 case 3: (DST)[2] = (SRC)[2]; \
134 case 2: (DST)[1] = (SRC)[1]; \
135 case 1: (DST)[0] = (SRC)[0]; \
139 #define SUB_4V( DST, SRCA, SRCB ) \
141 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
142 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
143 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
144 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
147 #define ADD_4V( DST, SRCA, SRCB ) \
149 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
150 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
151 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
152 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
155 #define SCALE_4V( DST, SRCA, SRCB ) \
157 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
158 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
159 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
160 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
163 #define ACC_4V( DST, SRC ) \
165 (DST)[0] += (SRC)[0]; \
166 (DST)[1] += (SRC)[1]; \
167 (DST)[2] += (SRC)[2]; \
168 (DST)[3] += (SRC)[3]; \
171 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
173 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
174 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
175 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
176 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
179 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
181 (DST)[0] += S * (SRCB)[0]; \
182 (DST)[1] += S * (SRCB)[1]; \
183 (DST)[2] += S * (SRCB)[2]; \
184 (DST)[3] += S * (SRCB)[3]; \
187 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
189 (DST)[0] = S * (SRCB)[0]; \
190 (DST)[1] = S * (SRCB)[1]; \
191 (DST)[2] = S * (SRCB)[2]; \
192 (DST)[3] = S * (SRCB)[3]; \
196 #define SELF_SCALE_SCALAR_4V( DST, S ) \
206 * Similarly for 3-vectors.
208 #define SUB_3V( DST, SRCA, SRCB ) \
210 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
211 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
212 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
215 #define ADD_3V( DST, SRCA, SRCB ) \
217 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
218 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
219 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
222 #define SCALE_3V( DST, SRCA, SRCB ) \
224 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
225 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
226 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
229 #define ACC_3V( DST, SRC ) \
231 (DST)[0] += (SRC)[0]; \
232 (DST)[1] += (SRC)[1]; \
233 (DST)[2] += (SRC)[2]; \
236 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
238 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
239 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
240 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
243 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
245 (DST)[0] = S * (SRCB)[0]; \
246 (DST)[1] = S * (SRCB)[1]; \
247 (DST)[2] = S * (SRCB)[2]; \
250 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
252 (DST)[0] += S * (SRCB)[0]; \
253 (DST)[1] += S * (SRCB)[1]; \
254 (DST)[2] += S * (SRCB)[2]; \
257 #define SELF_SCALE_SCALAR_3V( DST, S ) \
264 #define ACC_SCALAR_3V( DST, S ) \
271 /* And also for 2-vectors
273 #define SUB_2V( DST, SRCA, SRCB ) \
275 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
276 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
279 #define ADD_2V( DST, SRCA, SRCB ) \
281 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
282 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
285 #define SCALE_2V( DST, SRCA, SRCB ) \
287 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
288 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
291 #define ACC_2V( DST, SRC ) \
293 (DST)[0] += (SRC)[0]; \
294 (DST)[1] += (SRC)[1]; \
297 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
299 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
300 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
303 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
305 (DST)[0] = S * (SRCB)[0]; \
306 (DST)[1] = S * (SRCB)[1]; \
309 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
311 (DST)[0] += S * (SRCB)[0]; \
312 (DST)[1] += S * (SRCB)[1]; \
315 #define SELF_SCALE_SCALAR_2V( DST, S ) \
321 #define ACC_SCALAR_2V( DST, S ) \
330 * Copy a vector of 4 GLubytes from SRC to DST.
332 #define COPY_4UBV(DST, SRC) \
334 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
335 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
338 (DST)[0] = (SRC)[0]; \
339 (DST)[1] = (SRC)[1]; \
340 (DST)[2] = (SRC)[2]; \
341 (DST)[3] = (SRC)[3]; \
346 /* Assign scalers to short vectors: */
347 #define ASSIGN_2V( V, V0, V1 ) \
348 do { V[0] = V0; V[1] = V1; } while(0)
350 #define ASSIGN_3V( V, V0, V1, V2 ) \
351 do { V[0] = V0; V[1] = V1; V[2] = V2; } while(0)
353 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
364 /* Absolute value (for Int, Float, Double): */
365 #define ABSI(X) ((X) < 0 ? -(X) : (X))
366 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
367 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
371 /* Round a floating-point value to the nearest integer: */
372 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
375 /* Compute ceiling of integer quotient of A divided by B: */
376 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
379 /* Clamp X to [MIN,MAX]: */
380 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
382 /* Assign X to CLAMP(X, MIN, MAX) */
383 #define CLAMP_SELF(x, mn, mx) \
384 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
388 /* Min of two values: */
389 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
392 /* MAX of two values: */
393 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
395 /* Dot product of two 2-element vectors */
396 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
398 /* Dot product of two 3-element vectors */
399 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
402 /* Dot product of two 4-element vectors */
403 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
404 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
406 #define DOT4V(v,a,b,c,d) (v[0]*a + v[1]*b + v[2]*c + v[3]*d)
409 #define CROSS3(n, u, v) \
411 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
412 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
413 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
418 * Integer / float conversion for colors, normals, etc.
424 #define BYTE_TO_UBYTE(b) (b < 0 ? 0 : (GLubyte) b)
425 #define SHORT_TO_UBYTE(s) (s < 0 ? 0 : (GLubyte) (s >> 7))
426 #define USHORT_TO_UBYTE(s) (GLubyte) (s >> 8)
427 #define INT_TO_UBYTE(i) (i < 0 ? 0 : (GLubyte) (i >> 23))
428 #define UINT_TO_UBYTE(i) (GLubyte) (i >> 24)
433 /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
434 #define UBYTE_TO_FLOAT(B) ((GLfloat) (B) * (1.0F / 255.0F))
436 /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
437 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F))
440 /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
441 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
443 /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
444 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
447 /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
448 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
450 /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
451 #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
454 /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
455 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
457 /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
458 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
461 /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
462 #define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F))
464 /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
465 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
468 /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
469 #define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
471 /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
473 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
475 /* a close approximation: */
476 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
482 #define MEMCPY( DST, SRC, BYTES) \
483 memcpy( (char *) (DST), (char *) (SRC), (int) (BYTES) )
485 #define MEMCPY( DST, SRC, BYTES) \
486 memcpy( (void *) (DST), (void *) (SRC), (size_t) (BYTES) )
492 #define MEMSET( DST, VAL, N ) \
493 memset( (char *) (DST), (int) (VAL), (int) (N) )
495 #define MEMSET( DST, VAL, N ) \
496 memset( (void *) (DST), (int) (VAL), (size_t) (N) )
500 /* MACs and BeOS don't support static larger than 32kb, so... */
501 #if defined(macintosh) && !defined(__MRC__)
502 extern char *AGLAlloc(int size
);
503 extern void AGLFree(char* ptr
);
504 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)AGLAlloc(sizeof(TYPE)*(SIZE))
505 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])AGLAlloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
506 # define CHECKARRAY(NAME,CMD) do {if (!(NAME)) {CMD;}} while (0)
507 # define UNDEFARRAY(NAME) do {if ((NAME)) {AGLFree((char*)NAME);} }while (0)
508 #elif defined(__BEOS__)
509 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)malloc(sizeof(TYPE)*(SIZE))
510 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])malloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
511 # define CHECKARRAY(NAME,CMD) do {if (!(NAME)) {CMD;}} while (0)
512 # define UNDEFARRAY(NAME) do {if ((NAME)) {free((char*)NAME);} }while (0)
514 # define DEFARRAY(TYPE,NAME,SIZE) TYPE NAME[SIZE]
515 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE NAME[SIZE1][SIZE2]
516 # define CHECKARRAY(NAME,CMD) do {} while(0)
517 # define UNDEFARRAY(NAME)
521 /* Some compilers don't like some of Mesa's const usage */
532 #define M_PI (3.1415926)
536 /* Degrees to radians conversion: */
537 #define DEG2RAD (M_PI/180.0)