1 /* $Id: macros.h,v 1.8 1999/11/22 18:57:56 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
57 #define MAX_GLUSHORT 0xffff
58 #define MAX_GLUINT 0xffffffff
61 /* Some compilers don't like some of Mesa's const usage */
71 #define M_PI (3.1415926)
75 /* Degrees to radians conversion: */
76 #define DEG2RAD (M_PI/180.0)
88 #define SET_BITS(WORD, BITS) (WORD) |= (BITS)
89 #define CLEAR_BITS(WORD, BITS) (WORD) &= ~(BITS)
90 #define TEST_BITS(WORD, BITS) ((WORD) & (BITS))
93 /* Stepping a GLfloat pointer by a byte stride
95 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
96 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
97 #define STRIDE_T(p, t, i) (p = (t *)((GLubyte *)p + i))
100 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
101 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
102 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
105 /* Copy short vectors: */
106 #define COPY_2V( DST, SRC ) \
108 (DST)[0] = (SRC)[0]; \
109 (DST)[1] = (SRC)[1]; \
113 #define COPY_3V( DST, SRC ) \
115 (DST)[0] = (SRC)[0]; \
116 (DST)[1] = (SRC)[1]; \
117 (DST)[2] = (SRC)[2]; \
120 #define COPY_4V( DST, SRC ) \
122 (DST)[0] = (SRC)[0]; \
123 (DST)[1] = (SRC)[1]; \
124 (DST)[2] = (SRC)[2]; \
125 (DST)[3] = (SRC)[3]; \
129 #define COPY_2FV( DST, SRC ) \
131 const GLfloat *_tmp = (SRC); \
132 (DST)[0] = _tmp[0]; \
133 (DST)[1] = _tmp[1]; \
137 #define COPY_3FV( DST, SRC ) \
139 const GLfloat *_tmp = (SRC); \
140 (DST)[0] = _tmp[0]; \
141 (DST)[1] = _tmp[1]; \
142 (DST)[2] = _tmp[2]; \
145 #define COPY_4FV( DST, SRC ) \
147 const GLfloat *_tmp = (SRC); \
148 (DST)[0] = _tmp[0]; \
149 (DST)[1] = _tmp[1]; \
150 (DST)[2] = _tmp[2]; \
151 (DST)[3] = _tmp[3]; \
156 #define COPY_SZ_4V(DST, SZ, SRC) \
159 case 4: (DST)[3] = (SRC)[3]; \
160 case 3: (DST)[2] = (SRC)[2]; \
161 case 2: (DST)[1] = (SRC)[1]; \
162 case 1: (DST)[0] = (SRC)[0]; \
166 #define SUB_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 ADD_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 SCALE_4V( DST, SRCA, SRCB ) \
184 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
185 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
186 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
187 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
190 #define ACC_4V( DST, SRC ) \
192 (DST)[0] += (SRC)[0]; \
193 (DST)[1] += (SRC)[1]; \
194 (DST)[2] += (SRC)[2]; \
195 (DST)[3] += (SRC)[3]; \
198 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
200 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
201 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
202 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
203 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
206 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
208 (DST)[0] += S * (SRCB)[0]; \
209 (DST)[1] += S * (SRCB)[1]; \
210 (DST)[2] += S * (SRCB)[2]; \
211 (DST)[3] += S * (SRCB)[3]; \
214 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
216 (DST)[0] = S * (SRCB)[0]; \
217 (DST)[1] = S * (SRCB)[1]; \
218 (DST)[2] = S * (SRCB)[2]; \
219 (DST)[3] = S * (SRCB)[3]; \
223 #define SELF_SCALE_SCALAR_4V( DST, S ) \
233 * Similarly for 3-vectors.
235 #define SUB_3V( DST, SRCA, SRCB ) \
237 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
238 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
239 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
242 #define ADD_3V( DST, SRCA, SRCB ) \
244 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
245 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
246 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
249 #define SCALE_3V( DST, SRCA, SRCB ) \
251 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
252 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
253 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
256 #define ACC_3V( DST, SRC ) \
258 (DST)[0] += (SRC)[0]; \
259 (DST)[1] += (SRC)[1]; \
260 (DST)[2] += (SRC)[2]; \
263 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
265 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
266 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
267 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
270 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
272 (DST)[0] = S * (SRCB)[0]; \
273 (DST)[1] = S * (SRCB)[1]; \
274 (DST)[2] = S * (SRCB)[2]; \
277 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
279 (DST)[0] += S * (SRCB)[0]; \
280 (DST)[1] += S * (SRCB)[1]; \
281 (DST)[2] += S * (SRCB)[2]; \
284 #define SELF_SCALE_SCALAR_3V( DST, S ) \
291 #define ACC_SCALAR_3V( DST, S ) \
298 /* And also for 2-vectors
300 #define SUB_2V( DST, SRCA, SRCB ) \
302 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
303 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
306 #define ADD_2V( DST, SRCA, SRCB ) \
308 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
309 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
312 #define SCALE_2V( DST, SRCA, SRCB ) \
314 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
315 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
318 #define ACC_2V( DST, SRC ) \
320 (DST)[0] += (SRC)[0]; \
321 (DST)[1] += (SRC)[1]; \
324 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
326 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
327 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
330 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
332 (DST)[0] = S * (SRCB)[0]; \
333 (DST)[1] = S * (SRCB)[1]; \
336 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
338 (DST)[0] += S * (SRCB)[0]; \
339 (DST)[1] += S * (SRCB)[1]; \
342 #define SELF_SCALE_SCALAR_2V( DST, S ) \
348 #define ACC_SCALAR_2V( DST, S ) \
357 * Copy a vector of 4 GLubytes from SRC to DST.
359 #define COPY_4UBV(DST, SRC) \
361 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
362 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
365 (DST)[0] = (SRC)[0]; \
366 (DST)[1] = (SRC)[1]; \
367 (DST)[2] = (SRC)[2]; \
368 (DST)[3] = (SRC)[3]; \
373 /* Assign scalers to short vectors: */
374 #define ASSIGN_2V( V, V0, V1 ) \
375 do { V[0] = V0; V[1] = V1; } while(0)
377 #define ASSIGN_3V( V, V0, V1, V2 ) \
378 do { V[0] = V0; V[1] = V1; V[2] = V2; } while(0)
380 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
391 /* Absolute value (for Int, Float, Double): */
392 #define ABSI(X) ((X) < 0 ? -(X) : (X))
393 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
394 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
398 /* Round a floating-point value to the nearest integer: */
399 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
402 /* Compute ceiling of integer quotient of A divided by B: */
403 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
406 /* Clamp X to [MIN,MAX]: */
407 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
409 /* Assign X to CLAMP(X, MIN, MAX) */
410 #define CLAMP_SELF(x, mn, mx) \
411 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
415 /* Min of two values: */
416 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
419 /* MAX of two values: */
420 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
422 /* Dot product of two 2-element vectors */
423 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
425 /* Dot product of two 3-element vectors */
426 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
429 /* Dot product of two 4-element vectors */
430 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
431 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
433 #define DOT4V(v,a,b,c,d) (v[0]*a + v[1]*b + v[2]*c + v[3]*d)
436 #define CROSS3(n, u, v) \
438 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
439 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
440 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
445 * Integer / float conversion for colors, normals, etc.
448 #define BYTE_TO_UBYTE(b) (b < 0 ? 0 : (GLubyte) b)
449 #define SHORT_TO_UBYTE(s) (s < 0 ? 0 : (GLubyte) (s >> 7))
450 #define USHORT_TO_UBYTE(s) (GLubyte) (s >> 8)
451 #define INT_TO_UBYTE(i) (i < 0 ? 0 : (GLubyte) (i >> 23))
452 #define UINT_TO_UBYTE(i) (GLubyte) (i >> 24)
454 /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
455 #define UBYTE_TO_FLOAT(B) ((GLfloat) (B) * (1.0F / 255.0F))
457 /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
458 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F))
461 /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
462 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
464 /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
465 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
468 /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
469 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
471 /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
472 #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
475 /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
476 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
478 /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
479 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
482 /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
483 #define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F))
485 /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
486 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
489 /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
490 #define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
492 /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
494 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
496 /* a close approximation: */
497 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )