fix for gl_ztrick bug (Ove Kaaven)
[mesa.git] / src / mesa / main / macros.h
1 /* $Id: macros.h,v 1.20 2001/03/18 08:53:49 gareth 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])((GLchan *)p + i))
77 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
78
79
80 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
81 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
82 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
83
84
85 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
86 (a)[1] == (b)[1] && \
87 (a)[2] == (b)[2] && \
88 (a)[3] == (b)[3])
89
90 #define TEST_EQ_3V(a,b) ((a)[0] == (b)[0] && \
91 (a)[1] == (b)[1] && \
92 (a)[2] == (b)[2])
93
94 #if defined(__i386__)
95 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
96 #else
97 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
98 #endif
99
100
101
102 /* Copy short vectors: */
103 #define COPY_2V( DST, SRC ) \
104 do { \
105 (DST)[0] = (SRC)[0]; \
106 (DST)[1] = (SRC)[1]; \
107 } while (0)
108
109 #define COPY_3V( DST, SRC ) \
110 do { \
111 (DST)[0] = (SRC)[0]; \
112 (DST)[1] = (SRC)[1]; \
113 (DST)[2] = (SRC)[2]; \
114 } while (0)
115
116 #define COPY_4V( DST, SRC ) \
117 do { \
118 (DST)[0] = (SRC)[0]; \
119 (DST)[1] = (SRC)[1]; \
120 (DST)[2] = (SRC)[2]; \
121 (DST)[3] = (SRC)[3]; \
122 } while (0)
123
124 #define COPY_4UBV(DST, SRC) \
125 do { \
126 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
127 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
128 } \
129 else { \
130 (DST)[0] = (SRC)[0]; \
131 (DST)[1] = (SRC)[1]; \
132 (DST)[2] = (SRC)[2]; \
133 (DST)[3] = (SRC)[3]; \
134 } \
135 } while (0)
136
137
138 #define COPY_2FV( DST, SRC ) \
139 do { \
140 const GLfloat *_tmp = (SRC); \
141 (DST)[0] = _tmp[0]; \
142 (DST)[1] = _tmp[1]; \
143 } while (0)
144
145 #define COPY_3FV( DST, SRC ) \
146 do { \
147 const GLfloat *_tmp = (SRC); \
148 (DST)[0] = _tmp[0]; \
149 (DST)[1] = _tmp[1]; \
150 (DST)[2] = _tmp[2]; \
151 } while (0)
152
153 #define COPY_4FV( DST, SRC ) \
154 do { \
155 const GLfloat *_tmp = (SRC); \
156 (DST)[0] = _tmp[0]; \
157 (DST)[1] = _tmp[1]; \
158 (DST)[2] = _tmp[2]; \
159 (DST)[3] = _tmp[3]; \
160 } while (0)
161
162
163
164 #define COPY_SZ_4V(DST, SZ, SRC) \
165 do { \
166 switch (SZ) { \
167 case 4: (DST)[3] = (SRC)[3]; \
168 case 3: (DST)[2] = (SRC)[2]; \
169 case 2: (DST)[1] = (SRC)[1]; \
170 case 1: (DST)[0] = (SRC)[0]; \
171 } \
172 } while(0)
173
174 #define COPY_CLEAN_4V(DST, SZ, SRC) \
175 do { \
176 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
177 COPY_SZ_4V( DST, SZ, SRC ); \
178 } while (0)
179
180 #define SUB_4V( DST, SRCA, SRCB ) \
181 do { \
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 ADD_4V( DST, SRCA, SRCB ) \
189 do { \
190 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
191 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
192 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
193 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
194 } while (0)
195
196 #define SCALE_4V( DST, SRCA, SRCB ) \
197 do { \
198 (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]; \
202 } while (0)
203
204 #define ACC_4V( DST, SRC ) \
205 do { \
206 (DST)[0] += (SRC)[0]; \
207 (DST)[1] += (SRC)[1]; \
208 (DST)[2] += (SRC)[2]; \
209 (DST)[3] += (SRC)[3]; \
210 } while (0)
211
212 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
213 do { \
214 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
215 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
216 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
217 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
218 } while (0)
219
220 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
221 do { \
222 (DST)[0] += S * (SRCB)[0]; \
223 (DST)[1] += S * (SRCB)[1]; \
224 (DST)[2] += S * (SRCB)[2]; \
225 (DST)[3] += S * (SRCB)[3]; \
226 } while (0)
227
228 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
229 do { \
230 (DST)[0] = S * (SRCB)[0]; \
231 (DST)[1] = S * (SRCB)[1]; \
232 (DST)[2] = S * (SRCB)[2]; \
233 (DST)[3] = S * (SRCB)[3]; \
234 } while (0)
235
236
237 #define SELF_SCALE_SCALAR_4V( DST, S ) \
238 do { \
239 (DST)[0] *= S; \
240 (DST)[1] *= S; \
241 (DST)[2] *= S; \
242 (DST)[3] *= S; \
243 } while (0)
244
245
246 /*
247 * Similarly for 3-vectors.
248 */
249 #define SUB_3V( DST, SRCA, SRCB ) \
250 do { \
251 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
252 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
253 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
254 } while (0)
255
256 #define ADD_3V( DST, SRCA, SRCB ) \
257 do { \
258 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
259 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
260 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
261 } while (0)
262
263 #define SCALE_3V( DST, SRCA, SRCB ) \
264 do { \
265 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
266 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
267 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
268 } while (0)
269
270 #define ACC_3V( DST, SRC ) \
271 do { \
272 (DST)[0] += (SRC)[0]; \
273 (DST)[1] += (SRC)[1]; \
274 (DST)[2] += (SRC)[2]; \
275 } while (0)
276
277 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
278 do { \
279 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
280 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
281 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
282 } while (0)
283
284 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
285 do { \
286 (DST)[0] = S * (SRCB)[0]; \
287 (DST)[1] = S * (SRCB)[1]; \
288 (DST)[2] = S * (SRCB)[2]; \
289 } while (0)
290
291 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
292 do { \
293 (DST)[0] += S * (SRCB)[0]; \
294 (DST)[1] += S * (SRCB)[1]; \
295 (DST)[2] += S * (SRCB)[2]; \
296 } while (0)
297
298 #define SELF_SCALE_SCALAR_3V( DST, S ) \
299 do { \
300 (DST)[0] *= S; \
301 (DST)[1] *= S; \
302 (DST)[2] *= S; \
303 } while (0)
304
305 #define ACC_SCALAR_3V( DST, S ) \
306 do { \
307 (DST)[0] += S; \
308 (DST)[1] += S; \
309 (DST)[2] += S; \
310 } while (0)
311
312 /* And also for 2-vectors
313 */
314 #define SUB_2V( DST, SRCA, SRCB ) \
315 do { \
316 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
317 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
318 } while (0)
319
320 #define ADD_2V( DST, SRCA, SRCB ) \
321 do { \
322 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
323 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
324 } while (0)
325
326 #define SCALE_2V( DST, SRCA, SRCB ) \
327 do { \
328 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
329 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
330 } while (0)
331
332 #define ACC_2V( DST, SRC ) \
333 do { \
334 (DST)[0] += (SRC)[0]; \
335 (DST)[1] += (SRC)[1]; \
336 } while (0)
337
338 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
339 do { \
340 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
341 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
342 } while (0)
343
344 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
345 do { \
346 (DST)[0] = S * (SRCB)[0]; \
347 (DST)[1] = S * (SRCB)[1]; \
348 } while (0)
349
350 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
351 do { \
352 (DST)[0] += S * (SRCB)[0]; \
353 (DST)[1] += S * (SRCB)[1]; \
354 } while (0)
355
356 #define SELF_SCALE_SCALAR_2V( DST, S ) \
357 do { \
358 (DST)[0] *= S; \
359 (DST)[1] *= S; \
360 } while (0)
361
362 #define ACC_SCALAR_2V( DST, S ) \
363 do { \
364 (DST)[0] += S; \
365 (DST)[1] += S; \
366 } while (0)
367
368
369
370 /* Assign scalers to short vectors: */
371 #define ASSIGN_2V( V, V0, V1 ) \
372 do { \
373 V[0] = V0; \
374 V[1] = V1; \
375 } while(0)
376
377 #define ASSIGN_3V( V, V0, V1, V2 ) \
378 do { \
379 V[0] = V0; \
380 V[1] = V1; \
381 V[2] = V2; \
382 } while(0)
383
384 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
385 do { \
386 V[0] = V0; \
387 V[1] = V1; \
388 V[2] = V2; \
389 V[3] = V3; \
390 } while(0)
391
392
393
394
395 /* Absolute value (for Int, Float, Double): */
396 #define ABSI(X) ((X) < 0 ? -(X) : (X))
397 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
398 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
399
400
401
402 /* Round a floating-point value to the nearest integer: */
403 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
404
405
406 /* Compute ceiling of integer quotient of A divided by B: */
407 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
408
409
410 /* Clamp X to [MIN,MAX]: */
411 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
412
413 /* Assign X to CLAMP(X, MIN, MAX) */
414 #define CLAMP_SELF(x, mn, mx) \
415 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
416
417
418
419 /* Min of two values: */
420 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
421
422 /* MAX of two values: */
423 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
424
425 /* Dot product of two 2-element vectors */
426 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
427
428 /* Dot product of two 3-element vectors */
429 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
430
431 /* Dot product of two 4-element vectors */
432 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
433 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
434
435 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
436
437
438 #define CROSS3(n, u, v) \
439 do { \
440 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
441 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
442 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
443 } while (0)
444
445
446
447 /* Generic color packing macros
448 */
449
450 #define PACK_COLOR_8888( a, b, c, d ) \
451 (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
452
453 #define PACK_COLOR_888( a, b, c ) \
454 (((a) << 16) | ((b) << 8) | (c))
455
456 #define PACK_COLOR_565( a, b, c ) \
457 ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
458
459 #define PACK_COLOR_1555( a, b, c, d ) \
460 ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) | \
461 ((a) ? 0x8000 : 0))
462
463 #define PACK_COLOR_4444( a, b, c, d ) \
464 ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
465
466 #define PACK_COLOR_88( a, b ) \
467 (((a) << 8) | (b))
468
469 #define PACK_COLOR_332( a, b, c ) \
470 (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
471
472
473 #endif