Comment out __FUNCTION__ usage.
[mesa.git] / src / mesa / main / macros.h
1 /* $Id: macros.h,v 1.21 2001/04/19 12:22:09 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 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 * A collection of useful macros.
30 */
31
32
33 #ifndef MACROS_H
34 #define MACROS_H
35
36
37 #include "glheader.h"
38 /* Do not reference mtypes.h from this file.
39 */
40
41
42 /* Limits: */
43 #define MAX_GLUSHORT 0xffff
44 #define MAX_GLUINT 0xffffffff
45
46
47 /* Pi */
48 #ifndef M_PI
49 #define M_PI (3.1415926)
50 #endif
51
52
53 /* Degrees to radians conversion: */
54 #define DEG2RAD (M_PI/180.0)
55
56
57 #ifndef NULL
58 #define NULL 0
59 #endif
60
61
62
63 /*
64 * Bitmask helpers
65 */
66 #define SET_BITS(WORD, BITS) (WORD) |= (BITS)
67 #define CLEAR_BITS(WORD, BITS) (WORD) &= ~(BITS)
68 #define TEST_BITS(WORD, BITS) ((WORD) & (BITS))
69
70
71 /* Stepping a GLfloat pointer by a byte stride
72 */
73 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
74 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
75 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
76 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
77 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
78 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
79
80
81 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
82 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
83 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
84
85
86 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
87 (a)[1] == (b)[1] && \
88 (a)[2] == (b)[2] && \
89 (a)[3] == (b)[3])
90
91 #define TEST_EQ_3V(a,b) ((a)[0] == (b)[0] && \
92 (a)[1] == (b)[1] && \
93 (a)[2] == (b)[2])
94
95 #if defined(__i386__)
96 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
97 #else
98 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
99 #endif
100
101
102
103 /* Copy short vectors: */
104 #define COPY_2V( DST, SRC ) \
105 do { \
106 (DST)[0] = (SRC)[0]; \
107 (DST)[1] = (SRC)[1]; \
108 } while (0)
109
110 #define COPY_3V( DST, SRC ) \
111 do { \
112 (DST)[0] = (SRC)[0]; \
113 (DST)[1] = (SRC)[1]; \
114 (DST)[2] = (SRC)[2]; \
115 } while (0)
116
117 #define COPY_4V( DST, SRC ) \
118 do { \
119 (DST)[0] = (SRC)[0]; \
120 (DST)[1] = (SRC)[1]; \
121 (DST)[2] = (SRC)[2]; \
122 (DST)[3] = (SRC)[3]; \
123 } while (0)
124
125 #define COPY_4UBV(DST, SRC) \
126 do { \
127 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
128 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
129 } \
130 else { \
131 (DST)[0] = (SRC)[0]; \
132 (DST)[1] = (SRC)[1]; \
133 (DST)[2] = (SRC)[2]; \
134 (DST)[3] = (SRC)[3]; \
135 } \
136 } while (0)
137
138
139 #define COPY_2FV( DST, SRC ) \
140 do { \
141 const GLfloat *_tmp = (SRC); \
142 (DST)[0] = _tmp[0]; \
143 (DST)[1] = _tmp[1]; \
144 } while (0)
145
146 #define COPY_3FV( DST, SRC ) \
147 do { \
148 const GLfloat *_tmp = (SRC); \
149 (DST)[0] = _tmp[0]; \
150 (DST)[1] = _tmp[1]; \
151 (DST)[2] = _tmp[2]; \
152 } while (0)
153
154 #define COPY_4FV( DST, SRC ) \
155 do { \
156 const GLfloat *_tmp = (SRC); \
157 (DST)[0] = _tmp[0]; \
158 (DST)[1] = _tmp[1]; \
159 (DST)[2] = _tmp[2]; \
160 (DST)[3] = _tmp[3]; \
161 } while (0)
162
163
164
165 #define COPY_SZ_4V(DST, SZ, SRC) \
166 do { \
167 switch (SZ) { \
168 case 4: (DST)[3] = (SRC)[3]; \
169 case 3: (DST)[2] = (SRC)[2]; \
170 case 2: (DST)[1] = (SRC)[1]; \
171 case 1: (DST)[0] = (SRC)[0]; \
172 } \
173 } while(0)
174
175 #define COPY_CLEAN_4V(DST, SZ, SRC) \
176 do { \
177 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
178 COPY_SZ_4V( DST, SZ, SRC ); \
179 } while (0)
180
181 #define SUB_4V( DST, SRCA, SRCB ) \
182 do { \
183 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
184 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
185 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
186 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
187 } while (0)
188
189 #define ADD_4V( DST, SRCA, SRCB ) \
190 do { \
191 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
192 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
193 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
194 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
195 } while (0)
196
197 #define SCALE_4V( DST, SRCA, SRCB ) \
198 do { \
199 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
200 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
201 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
202 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
203 } while (0)
204
205 #define ACC_4V( DST, SRC ) \
206 do { \
207 (DST)[0] += (SRC)[0]; \
208 (DST)[1] += (SRC)[1]; \
209 (DST)[2] += (SRC)[2]; \
210 (DST)[3] += (SRC)[3]; \
211 } while (0)
212
213 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
214 do { \
215 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
216 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
217 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
218 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
219 } while (0)
220
221 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
222 do { \
223 (DST)[0] += S * (SRCB)[0]; \
224 (DST)[1] += S * (SRCB)[1]; \
225 (DST)[2] += S * (SRCB)[2]; \
226 (DST)[3] += S * (SRCB)[3]; \
227 } while (0)
228
229 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
230 do { \
231 (DST)[0] = S * (SRCB)[0]; \
232 (DST)[1] = S * (SRCB)[1]; \
233 (DST)[2] = S * (SRCB)[2]; \
234 (DST)[3] = S * (SRCB)[3]; \
235 } while (0)
236
237
238 #define SELF_SCALE_SCALAR_4V( DST, S ) \
239 do { \
240 (DST)[0] *= S; \
241 (DST)[1] *= S; \
242 (DST)[2] *= S; \
243 (DST)[3] *= S; \
244 } while (0)
245
246
247 /*
248 * Similarly for 3-vectors.
249 */
250 #define SUB_3V( DST, SRCA, SRCB ) \
251 do { \
252 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
253 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
254 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
255 } while (0)
256
257 #define ADD_3V( DST, SRCA, SRCB ) \
258 do { \
259 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
260 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
261 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
262 } while (0)
263
264 #define SCALE_3V( DST, SRCA, SRCB ) \
265 do { \
266 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
267 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
268 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
269 } while (0)
270
271 #define ACC_3V( DST, SRC ) \
272 do { \
273 (DST)[0] += (SRC)[0]; \
274 (DST)[1] += (SRC)[1]; \
275 (DST)[2] += (SRC)[2]; \
276 } while (0)
277
278 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
279 do { \
280 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
281 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
282 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
283 } while (0)
284
285 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
286 do { \
287 (DST)[0] = S * (SRCB)[0]; \
288 (DST)[1] = S * (SRCB)[1]; \
289 (DST)[2] = S * (SRCB)[2]; \
290 } while (0)
291
292 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
293 do { \
294 (DST)[0] += S * (SRCB)[0]; \
295 (DST)[1] += S * (SRCB)[1]; \
296 (DST)[2] += S * (SRCB)[2]; \
297 } while (0)
298
299 #define SELF_SCALE_SCALAR_3V( DST, S ) \
300 do { \
301 (DST)[0] *= S; \
302 (DST)[1] *= S; \
303 (DST)[2] *= S; \
304 } while (0)
305
306 #define ACC_SCALAR_3V( DST, S ) \
307 do { \
308 (DST)[0] += S; \
309 (DST)[1] += S; \
310 (DST)[2] += S; \
311 } while (0)
312
313 /* And also for 2-vectors
314 */
315 #define SUB_2V( DST, SRCA, SRCB ) \
316 do { \
317 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
318 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
319 } while (0)
320
321 #define ADD_2V( DST, SRCA, SRCB ) \
322 do { \
323 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
324 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
325 } while (0)
326
327 #define SCALE_2V( DST, SRCA, SRCB ) \
328 do { \
329 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
330 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
331 } while (0)
332
333 #define ACC_2V( DST, SRC ) \
334 do { \
335 (DST)[0] += (SRC)[0]; \
336 (DST)[1] += (SRC)[1]; \
337 } while (0)
338
339 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
340 do { \
341 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
342 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
343 } while (0)
344
345 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
346 do { \
347 (DST)[0] = S * (SRCB)[0]; \
348 (DST)[1] = S * (SRCB)[1]; \
349 } while (0)
350
351 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
352 do { \
353 (DST)[0] += S * (SRCB)[0]; \
354 (DST)[1] += S * (SRCB)[1]; \
355 } while (0)
356
357 #define SELF_SCALE_SCALAR_2V( DST, S ) \
358 do { \
359 (DST)[0] *= S; \
360 (DST)[1] *= S; \
361 } while (0)
362
363 #define ACC_SCALAR_2V( DST, S ) \
364 do { \
365 (DST)[0] += S; \
366 (DST)[1] += S; \
367 } while (0)
368
369
370
371 /* Assign scalers to short vectors: */
372 #define ASSIGN_2V( V, V0, V1 ) \
373 do { \
374 V[0] = V0; \
375 V[1] = V1; \
376 } while(0)
377
378 #define ASSIGN_3V( V, V0, V1, V2 ) \
379 do { \
380 V[0] = V0; \
381 V[1] = V1; \
382 V[2] = V2; \
383 } while(0)
384
385 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
386 do { \
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
423 /* 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
432 /* 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) \
440 do { \
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 /* Generic color packing macros
449 */
450
451 #define PACK_COLOR_8888( a, b, c, d ) \
452 (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
453
454 #define PACK_COLOR_888( a, b, c ) \
455 (((a) << 16) | ((b) << 8) | (c))
456
457 #define PACK_COLOR_565( a, b, c ) \
458 ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
459
460 #define PACK_COLOR_1555( a, b, c, d ) \
461 ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) | \
462 ((a) ? 0x8000 : 0))
463
464 #define PACK_COLOR_4444( a, b, c, d ) \
465 ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
466
467 #define PACK_COLOR_88( a, b ) \
468 (((a) << 8) | (b))
469
470 #define PACK_COLOR_332( a, b, c ) \
471 (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
472
473
474 #endif