Initial revision
[mesa.git] / src / mesa / main / macros.h
1 /* $Id: macros.h,v 1.1 1999/08/19 00:55:41 jtg Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.1
6 *
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
28
29
30
31 /*
32 * A collection of useful macros.
33 */
34
35
36 #ifndef MACROS_H
37 #define MACROS_H
38
39
40 #include <math.h>
41 #include <string.h>
42
43
44 #ifdef DEBUG
45 # include <assert.h>
46 # define ASSERT(X) assert(X)
47 #else
48 # define ASSERT(X)
49 #endif
50
51
52 #if defined(__GNUC__) || defined(__MWERKS__)
53 #define INLINE __inline__
54 #elif defined(__MSC__)
55 #define INLINE __inline
56 #else
57 #define INLINE
58 #endif
59
60
61 /* Stepping a GLfloat pointer by a byte stride
62 */
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))
66
67
68 /* Limits: */
69 #define MAX_GLUSHORT 0xffff
70 #define MAX_GLUINT 0xffffffff
71
72
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
76
77
78 /* Copy short vectors: */
79 #define COPY_2V( DST, SRC ) \
80 do { \
81 (DST)[0] = (SRC)[0]; \
82 (DST)[1] = (SRC)[1]; \
83 } while (0)
84
85
86 #define COPY_3V( DST, SRC ) \
87 do { \
88 (DST)[0] = (SRC)[0]; \
89 (DST)[1] = (SRC)[1]; \
90 (DST)[2] = (SRC)[2]; \
91 } while (0)
92
93 #define COPY_4V( DST, SRC ) \
94 do { \
95 (DST)[0] = (SRC)[0]; \
96 (DST)[1] = (SRC)[1]; \
97 (DST)[2] = (SRC)[2]; \
98 (DST)[3] = (SRC)[3]; \
99 } while (0)
100
101
102 #define COPY_2FV( DST, SRC ) \
103 do { \
104 const GLfloat *_tmp = (SRC); \
105 (DST)[0] = _tmp[0]; \
106 (DST)[1] = _tmp[1]; \
107 } while (0)
108
109
110 #define COPY_3FV( DST, SRC ) \
111 do { \
112 const GLfloat *_tmp = (SRC); \
113 (DST)[0] = _tmp[0]; \
114 (DST)[1] = _tmp[1]; \
115 (DST)[2] = _tmp[2]; \
116 } while (0)
117
118 #define COPY_4FV( DST, SRC ) \
119 do { \
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]; \
125 } while (0)
126
127
128
129 #define COPY_SZ_4V(DST, SZ, SRC) \
130 do { \
131 switch (SZ) { \
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]; \
136 } \
137 } while(0)
138
139 #define SUB_4V( DST, SRCA, SRCB ) \
140 do { \
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]; \
145 } while (0)
146
147 #define ADD_4V( DST, SRCA, SRCB ) \
148 do { \
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]; \
153 } while (0)
154
155 #define SCALE_4V( DST, SRCA, SRCB ) \
156 do { \
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]; \
161 } while (0)
162
163 #define ACC_4V( DST, SRC ) \
164 do { \
165 (DST)[0] += (SRC)[0]; \
166 (DST)[1] += (SRC)[1]; \
167 (DST)[2] += (SRC)[2]; \
168 (DST)[3] += (SRC)[3]; \
169 } while (0)
170
171 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
172 do { \
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]; \
177 } while (0)
178
179 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
180 do { \
181 (DST)[0] += S * (SRCB)[0]; \
182 (DST)[1] += S * (SRCB)[1]; \
183 (DST)[2] += S * (SRCB)[2]; \
184 (DST)[3] += S * (SRCB)[3]; \
185 } while (0)
186
187 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
188 do { \
189 (DST)[0] = S * (SRCB)[0]; \
190 (DST)[1] = S * (SRCB)[1]; \
191 (DST)[2] = S * (SRCB)[2]; \
192 (DST)[3] = S * (SRCB)[3]; \
193 } while (0)
194
195
196 #define SELF_SCALE_SCALAR_4V( DST, S ) \
197 do { \
198 (DST)[0] *= S; \
199 (DST)[1] *= S; \
200 (DST)[2] *= S; \
201 (DST)[3] *= S; \
202 } while (0)
203
204
205 /*
206 * Similarly for 3-vectors.
207 */
208 #define SUB_3V( DST, SRCA, SRCB ) \
209 do { \
210 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
211 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
212 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
213 } while (0)
214
215 #define ADD_3V( DST, SRCA, SRCB ) \
216 do { \
217 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
218 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
219 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
220 } while (0)
221
222 #define SCALE_3V( DST, SRCA, SRCB ) \
223 do { \
224 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
225 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
226 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
227 } while (0)
228
229 #define ACC_3V( DST, SRC ) \
230 do { \
231 (DST)[0] += (SRC)[0]; \
232 (DST)[1] += (SRC)[1]; \
233 (DST)[2] += (SRC)[2]; \
234 } while (0)
235
236 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
237 do { \
238 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
239 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
240 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
241 } while (0)
242
243 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
244 do { \
245 (DST)[0] = S * (SRCB)[0]; \
246 (DST)[1] = S * (SRCB)[1]; \
247 (DST)[2] = S * (SRCB)[2]; \
248 } while (0)
249
250 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
251 do { \
252 (DST)[0] += S * (SRCB)[0]; \
253 (DST)[1] += S * (SRCB)[1]; \
254 (DST)[2] += S * (SRCB)[2]; \
255 } while (0)
256
257 #define SELF_SCALE_SCALAR_3V( DST, S ) \
258 do { \
259 (DST)[0] *= S; \
260 (DST)[1] *= S; \
261 (DST)[2] *= S; \
262 } while (0)
263
264 #define ACC_SCALAR_3V( DST, S ) \
265 do { \
266 (DST)[0] += S; \
267 (DST)[1] += S; \
268 (DST)[2] += S; \
269 } while (0)
270
271 /* And also for 2-vectors
272 */
273 #define SUB_2V( DST, SRCA, SRCB ) \
274 do { \
275 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
276 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
277 } while (0)
278
279 #define ADD_2V( DST, SRCA, SRCB ) \
280 do { \
281 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
282 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
283 } while (0)
284
285 #define SCALE_2V( DST, SRCA, SRCB ) \
286 do { \
287 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
288 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
289 } while (0)
290
291 #define ACC_2V( DST, SRC ) \
292 do { \
293 (DST)[0] += (SRC)[0]; \
294 (DST)[1] += (SRC)[1]; \
295 } while (0)
296
297 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
298 do { \
299 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
300 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
301 } while (0)
302
303 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
304 do { \
305 (DST)[0] = S * (SRCB)[0]; \
306 (DST)[1] = S * (SRCB)[1]; \
307 } while (0)
308
309 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
310 do { \
311 (DST)[0] += S * (SRCB)[0]; \
312 (DST)[1] += S * (SRCB)[1]; \
313 } while (0)
314
315 #define SELF_SCALE_SCALAR_2V( DST, S ) \
316 do { \
317 (DST)[0] *= S; \
318 (DST)[1] *= S; \
319 } while (0)
320
321 #define ACC_SCALAR_2V( DST, S ) \
322 do { \
323 (DST)[0] += S; \
324 (DST)[1] += S; \
325 } while (0)
326
327
328
329 /*
330 * Copy a vector of 4 GLubytes from SRC to DST.
331 */
332 #define COPY_4UBV(DST, SRC) \
333 do { \
334 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
335 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
336 } \
337 else { \
338 (DST)[0] = (SRC)[0]; \
339 (DST)[1] = (SRC)[1]; \
340 (DST)[2] = (SRC)[2]; \
341 (DST)[3] = (SRC)[3]; \
342 } \
343 } while (0)
344
345
346 /* Assign scalers to short vectors: */
347 #define ASSIGN_2V( V, V0, V1 ) \
348 do { V[0] = V0; V[1] = V1; } while(0)
349
350 #define ASSIGN_3V( V, V0, V1, V2 ) \
351 do { V[0] = V0; V[1] = V1; V[2] = V2; } while(0)
352
353 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
354 do { \
355 V[0] = V0; \
356 V[1] = V1; \
357 V[2] = V2; \
358 V[3] = V3; \
359 } while(0)
360
361
362
363
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))
368
369
370
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)) )
373
374
375 /* Compute ceiling of integer quotient of A divided by B: */
376 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
377
378
379 /* Clamp X to [MIN,MAX]: */
380 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
381
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)) )
385
386
387
388 /* Min of two values: */
389 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
390
391
392 /* MAX of two values: */
393 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
394
395 /* Dot product of two 2-element vectors */
396 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
397
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] )
400
401
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] )
405
406 #define DOT4V(v,a,b,c,d) (v[0]*a + v[1]*b + v[2]*c + v[3]*d)
407
408
409 #define CROSS3(n, u, v) \
410 do { \
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]; \
414 } while (0)
415
416
417 /*
418 * Integer / float conversion for colors, normals, etc.
419 */
420
421
422
423
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)
429
430
431
432
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))
435
436 /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
437 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F))
438
439
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))
442
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 )
445
446
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))
449
450 /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
451 #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
452
453
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))
456
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 )
459
460
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))
463
464 /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
465 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
466
467
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))
470
471 /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
472 /* causes overflow:
473 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
474 */
475 /* a close approximation: */
476 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
477
478
479
480 /* Memory copy: */
481 #ifdef SUNOS4
482 #define MEMCPY( DST, SRC, BYTES) \
483 memcpy( (char *) (DST), (char *) (SRC), (int) (BYTES) )
484 #else
485 #define MEMCPY( DST, SRC, BYTES) \
486 memcpy( (void *) (DST), (void *) (SRC), (size_t) (BYTES) )
487 #endif
488
489
490 /* Memory set: */
491 #ifdef SUNOS4
492 #define MEMSET( DST, VAL, N ) \
493 memset( (char *) (DST), (int) (VAL), (int) (N) )
494 #else
495 #define MEMSET( DST, VAL, N ) \
496 memset( (void *) (DST), (int) (VAL), (size_t) (N) )
497 #endif
498
499
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)
513 #else
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)
518 #endif
519
520
521 /* Some compilers don't like some of Mesa's const usage */
522 #ifdef NO_CONST
523 # define CONST
524 #else
525 # define CONST const
526 #endif
527
528
529
530 /* Pi */
531 #ifndef M_PI
532 #define M_PI (3.1415926)
533 #endif
534
535
536 /* Degrees to radians conversion: */
537 #define DEG2RAD (M_PI/180.0)
538
539
540 #ifndef NULL
541 #define NULL 0
542 #endif
543
544
545
546 #endif /*MACROS_H*/