blob: 70f7b82e8af0bc95ae0881935a2b498b24edbdb9 [file] [log] [blame]
Brian Paul1c56fdc2000-09-17 21:56:07 +00001/* $Id: macros.h,v 1.9 2000/09/17 21:56:07 brianp Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paulfbd8f211999-11-11 01:22:25 +00005 * Version: 3.3
jtgafb833d1999-08-19 00:55:39 +00006 *
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
jtgafb833d1999-08-19 00:55:39 +000028/*
29 * A collection of useful macros.
30 */
31
32
33#ifndef MACROS_H
34#define MACROS_H
35
Brian Paulfbd8f211999-11-11 01:22:25 +000036
37#include "glheader.h"
jtgafb833d1999-08-19 00:55:39 +000038
39
40#ifdef DEBUG
jtgafb833d1999-08-19 00:55:39 +000041# define ASSERT(X) assert(X)
42#else
43# define ASSERT(X)
44#endif
45
46
Keith Whitwell485f0401999-10-08 09:27:09 +000047#if defined(__GNUC__)
jtgafb833d1999-08-19 00:55:39 +000048#define INLINE __inline__
49#elif defined(__MSC__)
50#define INLINE __inline
51#else
52#define INLINE
53#endif
54
55
Brian Paul414b6e71999-11-22 18:57:56 +000056/* Limits: */
57#define MAX_GLUSHORT 0xffff
58#define MAX_GLUINT 0xffffffff
59
60
61/* Some compilers don't like some of Mesa's const usage */
62#ifdef NO_CONST
63# define CONST
64#else
65# define CONST const
66#endif
67
68
69/* Pi */
70#ifndef M_PI
71#define M_PI (3.1415926)
72#endif
73
74
75/* Degrees to radians conversion: */
76#define DEG2RAD (M_PI/180.0)
77
78
79#ifndef NULL
80#define NULL 0
81#endif
82
83
84
85/*
86 * Bitmask helpers
87 */
88#define SET_BITS(WORD, BITS) (WORD) |= (BITS)
89#define CLEAR_BITS(WORD, BITS) (WORD) &= ~(BITS)
90#define TEST_BITS(WORD, BITS) ((WORD) & (BITS))
91
92
jtgafb833d1999-08-19 00:55:39 +000093/* Stepping a GLfloat pointer by a byte stride
94 */
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))
98
99
jtgafb833d1999-08-19 00:55:39 +0000100#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
103
104
105/* Copy short vectors: */
106#define COPY_2V( DST, SRC ) \
107do { \
108 (DST)[0] = (SRC)[0]; \
109 (DST)[1] = (SRC)[1]; \
110} while (0)
111
jtgafb833d1999-08-19 00:55:39 +0000112#define COPY_3V( DST, SRC ) \
113do { \
114 (DST)[0] = (SRC)[0]; \
115 (DST)[1] = (SRC)[1]; \
116 (DST)[2] = (SRC)[2]; \
117} while (0)
118
119#define COPY_4V( DST, SRC ) \
120do { \
121 (DST)[0] = (SRC)[0]; \
122 (DST)[1] = (SRC)[1]; \
123 (DST)[2] = (SRC)[2]; \
124 (DST)[3] = (SRC)[3]; \
125} while (0)
126
127
128#define COPY_2FV( DST, SRC ) \
129do { \
130 const GLfloat *_tmp = (SRC); \
131 (DST)[0] = _tmp[0]; \
132 (DST)[1] = _tmp[1]; \
133} while (0)
134
jtgafb833d1999-08-19 00:55:39 +0000135#define COPY_3FV( DST, SRC ) \
136do { \
137 const GLfloat *_tmp = (SRC); \
138 (DST)[0] = _tmp[0]; \
139 (DST)[1] = _tmp[1]; \
140 (DST)[2] = _tmp[2]; \
141} while (0)
142
143#define COPY_4FV( DST, SRC ) \
144do { \
145 const GLfloat *_tmp = (SRC); \
146 (DST)[0] = _tmp[0]; \
147 (DST)[1] = _tmp[1]; \
148 (DST)[2] = _tmp[2]; \
149 (DST)[3] = _tmp[3]; \
150} while (0)
151
152
153
154#define COPY_SZ_4V(DST, SZ, SRC) \
155do { \
156 switch (SZ) { \
157 case 4: (DST)[3] = (SRC)[3]; \
158 case 3: (DST)[2] = (SRC)[2]; \
159 case 2: (DST)[1] = (SRC)[1]; \
160 case 1: (DST)[0] = (SRC)[0]; \
161 } \
162} while(0)
163
164#define SUB_4V( DST, SRCA, SRCB ) \
165do { \
166 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
167 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
168 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
169 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
170} while (0)
171
172#define ADD_4V( DST, SRCA, SRCB ) \
173do { \
174 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
175 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
176 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
177 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
178} while (0)
179
180#define SCALE_4V( DST, SRCA, SRCB ) \
181do { \
182 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
183 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
184 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
185 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
186} while (0)
187
188#define ACC_4V( DST, SRC ) \
189do { \
Brian Paulfbd8f211999-11-11 01:22:25 +0000190 (DST)[0] += (SRC)[0]; \
191 (DST)[1] += (SRC)[1]; \
192 (DST)[2] += (SRC)[2]; \
193 (DST)[3] += (SRC)[3]; \
jtgafb833d1999-08-19 00:55:39 +0000194} while (0)
195
196#define ACC_SCALE_4V( DST, SRCA, SRCB ) \
197do { \
Brian Paulfbd8f211999-11-11 01:22:25 +0000198 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
199 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
200 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
201 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
jtgafb833d1999-08-19 00:55:39 +0000202} while (0)
203
204#define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
205do { \
Brian Paulfbd8f211999-11-11 01:22:25 +0000206 (DST)[0] += S * (SRCB)[0]; \
207 (DST)[1] += S * (SRCB)[1]; \
208 (DST)[2] += S * (SRCB)[2]; \
209 (DST)[3] += S * (SRCB)[3]; \
jtgafb833d1999-08-19 00:55:39 +0000210} while (0)
211
212#define SCALE_SCALAR_4V( DST, S, SRCB ) \
213do { \
214 (DST)[0] = S * (SRCB)[0]; \
215 (DST)[1] = S * (SRCB)[1]; \
216 (DST)[2] = S * (SRCB)[2]; \
217 (DST)[3] = S * (SRCB)[3]; \
218} while (0)
219
220
221#define SELF_SCALE_SCALAR_4V( DST, S ) \
222do { \
223 (DST)[0] *= S; \
224 (DST)[1] *= S; \
225 (DST)[2] *= S; \
226 (DST)[3] *= S; \
227} while (0)
228
229
230/*
231 * Similarly for 3-vectors.
232 */
233#define SUB_3V( DST, SRCA, SRCB ) \
234do { \
235 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
236 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
237 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
238} while (0)
239
240#define ADD_3V( DST, SRCA, SRCB ) \
241do { \
242 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
243 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
244 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
245} while (0)
246
247#define SCALE_3V( DST, SRCA, SRCB ) \
248do { \
249 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
250 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
251 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
252} while (0)
253
254#define ACC_3V( DST, SRC ) \
255do { \
256 (DST)[0] += (SRC)[0]; \
257 (DST)[1] += (SRC)[1]; \
258 (DST)[2] += (SRC)[2]; \
259} while (0)
260
261#define ACC_SCALE_3V( DST, SRCA, SRCB ) \
262do { \
263 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
264 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
265 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
266} while (0)
267
268#define SCALE_SCALAR_3V( DST, S, SRCB ) \
269do { \
270 (DST)[0] = S * (SRCB)[0]; \
271 (DST)[1] = S * (SRCB)[1]; \
272 (DST)[2] = S * (SRCB)[2]; \
273} while (0)
274
275#define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
276do { \
277 (DST)[0] += S * (SRCB)[0]; \
278 (DST)[1] += S * (SRCB)[1]; \
279 (DST)[2] += S * (SRCB)[2]; \
280} while (0)
281
282#define SELF_SCALE_SCALAR_3V( DST, S ) \
283do { \
284 (DST)[0] *= S; \
285 (DST)[1] *= S; \
286 (DST)[2] *= S; \
287} while (0)
288
289#define ACC_SCALAR_3V( DST, S ) \
290do { \
291 (DST)[0] += S; \
292 (DST)[1] += S; \
293 (DST)[2] += S; \
294} while (0)
295
296/* And also for 2-vectors
297 */
298#define SUB_2V( DST, SRCA, SRCB ) \
299do { \
300 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
301 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
302} while (0)
303
304#define ADD_2V( DST, SRCA, SRCB ) \
305do { \
306 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
307 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
308} while (0)
309
310#define SCALE_2V( DST, SRCA, SRCB ) \
311do { \
312 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
313 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
314} while (0)
315
316#define ACC_2V( DST, SRC ) \
317do { \
318 (DST)[0] += (SRC)[0]; \
319 (DST)[1] += (SRC)[1]; \
320} while (0)
321
322#define ACC_SCALE_2V( DST, SRCA, SRCB ) \
323do { \
324 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
325 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
326} while (0)
327
328#define SCALE_SCALAR_2V( DST, S, SRCB ) \
329do { \
330 (DST)[0] = S * (SRCB)[0]; \
331 (DST)[1] = S * (SRCB)[1]; \
332} while (0)
333
334#define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
335do { \
336 (DST)[0] += S * (SRCB)[0]; \
337 (DST)[1] += S * (SRCB)[1]; \
338} while (0)
339
340#define SELF_SCALE_SCALAR_2V( DST, S ) \
341do { \
342 (DST)[0] *= S; \
343 (DST)[1] *= S; \
344} while (0)
345
346#define ACC_SCALAR_2V( DST, S ) \
347do { \
348 (DST)[0] += S; \
349 (DST)[1] += S; \
350} while (0)
351
352
353
354/*
355 * Copy a vector of 4 GLubytes from SRC to DST.
356 */
357#define COPY_4UBV(DST, SRC) \
358do { \
359 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
360 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
361 } \
362 else { \
363 (DST)[0] = (SRC)[0]; \
364 (DST)[1] = (SRC)[1]; \
365 (DST)[2] = (SRC)[2]; \
366 (DST)[3] = (SRC)[3]; \
367 } \
368} while (0)
369
370
371/* Assign scalers to short vectors: */
Brian Paul1c56fdc2000-09-17 21:56:07 +0000372#define ASSIGN_2V( V, V0, V1 ) \
373do { \
374 V[0] = V0; \
375 V[1] = V1; \
376} while(0)
jtgafb833d1999-08-19 00:55:39 +0000377
Brian Paul1c56fdc2000-09-17 21:56:07 +0000378#define ASSIGN_3V( V, V0, V1, V2 ) \
379do { \
380 V[0] = V0; \
381 V[1] = V1; \
382 V[2] = V2; \
383} while(0)
jtgafb833d1999-08-19 00:55:39 +0000384
385#define ASSIGN_4V( V, V0, V1, V2, V3 ) \
386do { \
387 V[0] = V0; \
388 V[1] = V1; \
389 V[2] = V2; \
390 V[3] = V3; \
391} while(0)
392
393
394
395
396/* Absolute value (for Int, Float, Double): */
397#define ABSI(X) ((X) < 0 ? -(X) : (X))
398#define ABSF(X) ((X) < 0.0F ? -(X) : (X))
399#define ABSD(X) ((X) < 0.0 ? -(X) : (X))
400
401
402
403/* Round a floating-point value to the nearest integer: */
404#define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
405
406
407/* Compute ceiling of integer quotient of A divided by B: */
408#define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
409
410
411/* Clamp X to [MIN,MAX]: */
412#define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
413
414/* Assign X to CLAMP(X, MIN, MAX) */
415#define CLAMP_SELF(x, mn, mx) \
416 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
417
418
419
420/* Min of two values: */
421#define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
422
jtgafb833d1999-08-19 00:55:39 +0000423/* MAX of two values: */
424#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
425
426/* Dot product of two 2-element vectors */
427#define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
428
429/* Dot product of two 3-element vectors */
430#define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
431
jtgafb833d1999-08-19 00:55:39 +0000432/* Dot product of two 4-element vectors */
433#define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
434 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
435
436#define DOT4V(v,a,b,c,d) (v[0]*a + v[1]*b + v[2]*c + v[3]*d)
437
438
439#define CROSS3(n, u, v) \
440do { \
441 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
442 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
443 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
444} while (0)
445
446
447/*
448 * Integer / float conversion for colors, normals, etc.
449 */
450
jtgafb833d1999-08-19 00:55:39 +0000451#define BYTE_TO_UBYTE(b) (b < 0 ? 0 : (GLubyte) b)
452#define SHORT_TO_UBYTE(s) (s < 0 ? 0 : (GLubyte) (s >> 7))
453#define USHORT_TO_UBYTE(s) (GLubyte) (s >> 8)
454#define INT_TO_UBYTE(i) (i < 0 ? 0 : (GLubyte) (i >> 23))
455#define UINT_TO_UBYTE(i) (GLubyte) (i >> 24)
456
jtgafb833d1999-08-19 00:55:39 +0000457/* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
458#define UBYTE_TO_FLOAT(B) ((GLfloat) (B) * (1.0F / 255.0F))
459
460/* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
461#define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F))
462
463
464/* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
465#define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
466
467/* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
468#define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
469
470
471/* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
472#define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
473
474/* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
475#define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
476
477
478/* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
479#define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
480
481/* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
482#define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
483
484
485/* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
486#define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F))
487
488/* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
489#define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
490
491
492/* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
493#define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
494
495/* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
496/* causes overflow:
497#define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
498*/
499/* a close approximation: */
500#define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
501
502
Brian Paul1c56fdc2000-09-17 21:56:07 +0000503#endif