1 /* $Id: macros.h,v 1.7 1999/11/11 01:22:27 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.
29 * A collection of useful macros.
41 # define ASSERT(X) assert(X)
48 #define INLINE __inline__
49 #elif defined(__MSC__)
50 #define INLINE __inline
56 /* Stepping a GLfloat pointer by a byte stride
58 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
59 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
60 #define STRIDE_T(p, t, i) (p = (t *)((GLubyte *)p + i))
64 #define MAX_GLUSHORT 0xffff
65 #define MAX_GLUINT 0xffffffff
68 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
69 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
70 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
73 /* Copy short vectors: */
74 #define COPY_2V( DST, SRC ) \
76 (DST)[0] = (SRC)[0]; \
77 (DST)[1] = (SRC)[1]; \
81 #define COPY_3V( DST, SRC ) \
83 (DST)[0] = (SRC)[0]; \
84 (DST)[1] = (SRC)[1]; \
85 (DST)[2] = (SRC)[2]; \
88 #define COPY_4V( DST, SRC ) \
90 (DST)[0] = (SRC)[0]; \
91 (DST)[1] = (SRC)[1]; \
92 (DST)[2] = (SRC)[2]; \
93 (DST)[3] = (SRC)[3]; \
97 #define COPY_2FV( DST, SRC ) \
99 const GLfloat *_tmp = (SRC); \
100 (DST)[0] = _tmp[0]; \
101 (DST)[1] = _tmp[1]; \
105 #define COPY_3FV( DST, SRC ) \
107 const GLfloat *_tmp = (SRC); \
108 (DST)[0] = _tmp[0]; \
109 (DST)[1] = _tmp[1]; \
110 (DST)[2] = _tmp[2]; \
113 #define COPY_4FV( DST, SRC ) \
115 const GLfloat *_tmp = (SRC); \
116 (DST)[0] = _tmp[0]; \
117 (DST)[1] = _tmp[1]; \
118 (DST)[2] = _tmp[2]; \
119 (DST)[3] = _tmp[3]; \
124 #define COPY_SZ_4V(DST, SZ, SRC) \
127 case 4: (DST)[3] = (SRC)[3]; \
128 case 3: (DST)[2] = (SRC)[2]; \
129 case 2: (DST)[1] = (SRC)[1]; \
130 case 1: (DST)[0] = (SRC)[0]; \
134 #define SUB_4V( DST, SRCA, SRCB ) \
136 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
137 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
138 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
139 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
142 #define ADD_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 SCALE_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 ACC_4V( DST, SRC ) \
160 (DST)[0] += (SRC)[0]; \
161 (DST)[1] += (SRC)[1]; \
162 (DST)[2] += (SRC)[2]; \
163 (DST)[3] += (SRC)[3]; \
166 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
168 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
169 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
170 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
171 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
174 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
176 (DST)[0] += S * (SRCB)[0]; \
177 (DST)[1] += S * (SRCB)[1]; \
178 (DST)[2] += S * (SRCB)[2]; \
179 (DST)[3] += S * (SRCB)[3]; \
182 #define 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]; \
191 #define SELF_SCALE_SCALAR_4V( DST, S ) \
201 * Similarly for 3-vectors.
203 #define SUB_3V( DST, SRCA, SRCB ) \
205 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
206 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
207 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
210 #define ADD_3V( DST, SRCA, SRCB ) \
212 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
213 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
214 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
217 #define SCALE_3V( DST, SRCA, SRCB ) \
219 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
220 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
221 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
224 #define ACC_3V( DST, SRC ) \
226 (DST)[0] += (SRC)[0]; \
227 (DST)[1] += (SRC)[1]; \
228 (DST)[2] += (SRC)[2]; \
231 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
233 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
234 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
235 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
238 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
240 (DST)[0] = S * (SRCB)[0]; \
241 (DST)[1] = S * (SRCB)[1]; \
242 (DST)[2] = S * (SRCB)[2]; \
245 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
247 (DST)[0] += S * (SRCB)[0]; \
248 (DST)[1] += S * (SRCB)[1]; \
249 (DST)[2] += S * (SRCB)[2]; \
252 #define SELF_SCALE_SCALAR_3V( DST, S ) \
259 #define ACC_SCALAR_3V( DST, S ) \
266 /* And also for 2-vectors
268 #define SUB_2V( DST, SRCA, SRCB ) \
270 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
271 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
274 #define ADD_2V( DST, SRCA, SRCB ) \
276 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
277 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
280 #define SCALE_2V( DST, SRCA, SRCB ) \
282 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
283 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
286 #define ACC_2V( DST, SRC ) \
288 (DST)[0] += (SRC)[0]; \
289 (DST)[1] += (SRC)[1]; \
292 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
294 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
295 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
298 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
300 (DST)[0] = S * (SRCB)[0]; \
301 (DST)[1] = S * (SRCB)[1]; \
304 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
306 (DST)[0] += S * (SRCB)[0]; \
307 (DST)[1] += S * (SRCB)[1]; \
310 #define SELF_SCALE_SCALAR_2V( DST, S ) \
316 #define ACC_SCALAR_2V( DST, S ) \
325 * Copy a vector of 4 GLubytes from SRC to DST.
327 #define COPY_4UBV(DST, SRC) \
329 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
330 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
333 (DST)[0] = (SRC)[0]; \
334 (DST)[1] = (SRC)[1]; \
335 (DST)[2] = (SRC)[2]; \
336 (DST)[3] = (SRC)[3]; \
341 /* Assign scalers to short vectors: */
342 #define ASSIGN_2V( V, V0, V1 ) \
343 do { V[0] = V0; V[1] = V1; } while(0)
345 #define ASSIGN_3V( V, V0, V1, V2 ) \
346 do { V[0] = V0; V[1] = V1; V[2] = V2; } while(0)
348 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
359 /* Absolute value (for Int, Float, Double): */
360 #define ABSI(X) ((X) < 0 ? -(X) : (X))
361 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
362 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
366 /* Round a floating-point value to the nearest integer: */
367 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
370 /* Compute ceiling of integer quotient of A divided by B: */
371 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
374 /* Clamp X to [MIN,MAX]: */
375 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
377 /* Assign X to CLAMP(X, MIN, MAX) */
378 #define CLAMP_SELF(x, mn, mx) \
379 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
383 /* Min of two values: */
384 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
387 /* MAX of two values: */
388 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
390 /* Dot product of two 2-element vectors */
391 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
393 /* Dot product of two 3-element vectors */
394 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
397 /* Dot product of two 4-element vectors */
398 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
399 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
401 #define DOT4V(v,a,b,c,d) (v[0]*a + v[1]*b + v[2]*c + v[3]*d)
404 #define CROSS3(n, u, v) \
406 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
407 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
408 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
413 * Integer / float conversion for colors, normals, etc.
419 #define BYTE_TO_UBYTE(b) (b < 0 ? 0 : (GLubyte) b)
420 #define SHORT_TO_UBYTE(s) (s < 0 ? 0 : (GLubyte) (s >> 7))
421 #define USHORT_TO_UBYTE(s) (GLubyte) (s >> 8)
422 #define INT_TO_UBYTE(i) (i < 0 ? 0 : (GLubyte) (i >> 23))
423 #define UINT_TO_UBYTE(i) (GLubyte) (i >> 24)
428 /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
429 #define UBYTE_TO_FLOAT(B) ((GLfloat) (B) * (1.0F / 255.0F))
431 /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
432 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F))
435 /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
436 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
438 /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
439 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
442 /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
443 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
445 /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
446 #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
449 /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
450 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
452 /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
453 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
456 /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
457 #define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F))
459 /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
460 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
463 /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
464 #define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
466 /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
468 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
470 /* a close approximation: */
471 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
475 /* Some compilers don't like some of Mesa's const usage */
486 #define M_PI (3.1415926)
490 /* Degrees to radians conversion: */
491 #define DEG2RAD (M_PI/180.0)