bring over Michel Daenzer's DRI changes
[mesa.git] / src / mesa / main / macros.h
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 3.5
5 *
6 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /*
28 * A collection of useful macros.
29 */
30
31
32 #ifndef MACROS_H
33 #define MACROS_H
34
35
36 #include "glheader.h"
37 /* Do not reference mtypes.h from this file.
38 */
39
40
41 /* Limits: */
42 #define MAX_GLUSHORT 0xffff
43 #define MAX_GLUINT 0xffffffff
44
45
46 /* Pi */
47 #ifndef M_PI
48 #define M_PI (3.1415926)
49 #endif
50
51
52 /* Degrees to radians conversion: */
53 #define DEG2RAD (M_PI/180.0)
54
55
56 #ifndef NULL
57 #define NULL 0
58 #endif
59
60
61
62 /*
63 * Bitmask helpers
64 */
65 #define SET_BITS(WORD, BITS) (WORD) |= (BITS)
66 #define CLEAR_BITS(WORD, BITS) (WORD) &= ~(BITS)
67 #define TEST_BITS(WORD, BITS) ((WORD) & (BITS))
68
69
70 /* Stepping a GLfloat pointer by a byte stride
71 */
72 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
73 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
74 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
75 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
76 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)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 #if defined(__i386__)
125 #define COPY_4UBV(DST, SRC) \
126 do { \
127 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
128 } while (0)
129 #else
130 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
131 #define COPY_4UBV(DST, SRC) \
132 do { \
133 (DST)[0] = (SRC)[0]; \
134 (DST)[1] = (SRC)[1]; \
135 (DST)[2] = (SRC)[2]; \
136 (DST)[3] = (SRC)[3]; \
137 } while (0)
138 #endif
139
140 #define COPY_2FV( DST, SRC ) \
141 do { \
142 const GLfloat *_tmp = (SRC); \
143 (DST)[0] = _tmp[0]; \
144 (DST)[1] = _tmp[1]; \
145 } while (0)
146
147 #define COPY_3FV( DST, SRC ) \
148 do { \
149 const GLfloat *_tmp = (SRC); \
150 (DST)[0] = _tmp[0]; \
151 (DST)[1] = _tmp[1]; \
152 (DST)[2] = _tmp[2]; \
153 } while (0)
154
155 #define COPY_4FV( DST, SRC ) \
156 do { \
157 const GLfloat *_tmp = (SRC); \
158 (DST)[0] = _tmp[0]; \
159 (DST)[1] = _tmp[1]; \
160 (DST)[2] = _tmp[2]; \
161 (DST)[3] = _tmp[3]; \
162 } while (0)
163
164
165
166 #define COPY_SZ_4V(DST, SZ, SRC) \
167 do { \
168 switch (SZ) { \
169 case 4: (DST)[3] = (SRC)[3]; \
170 case 3: (DST)[2] = (SRC)[2]; \
171 case 2: (DST)[1] = (SRC)[1]; \
172 case 1: (DST)[0] = (SRC)[0]; \
173 } \
174 } while(0)
175
176 #define COPY_CLEAN_4V(DST, SZ, SRC) \
177 do { \
178 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
179 COPY_SZ_4V( DST, SZ, SRC ); \
180 } while (0)
181
182 #define SUB_4V( DST, SRCA, SRCB ) \
183 do { \
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]; \
188 } while (0)
189
190 #define ADD_4V( DST, SRCA, SRCB ) \
191 do { \
192 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
193 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
194 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
195 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
196 } while (0)
197
198 #define SCALE_4V( DST, SRCA, SRCB ) \
199 do { \
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]; \
204 } while (0)
205
206 #define ACC_4V( DST, SRC ) \
207 do { \
208 (DST)[0] += (SRC)[0]; \
209 (DST)[1] += (SRC)[1]; \
210 (DST)[2] += (SRC)[2]; \
211 (DST)[3] += (SRC)[3]; \
212 } while (0)
213
214 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
215 do { \
216 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
217 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
218 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
219 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
220 } while (0)
221
222 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
223 do { \
224 (DST)[0] += S * (SRCB)[0]; \
225 (DST)[1] += S * (SRCB)[1]; \
226 (DST)[2] += S * (SRCB)[2]; \
227 (DST)[3] += S * (SRCB)[3]; \
228 } while (0)
229
230 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
231 do { \
232 (DST)[0] = S * (SRCB)[0]; \
233 (DST)[1] = S * (SRCB)[1]; \
234 (DST)[2] = S * (SRCB)[2]; \
235 (DST)[3] = S * (SRCB)[3]; \
236 } while (0)
237
238
239 #define SELF_SCALE_SCALAR_4V( DST, S ) \
240 do { \
241 (DST)[0] *= S; \
242 (DST)[1] *= S; \
243 (DST)[2] *= S; \
244 (DST)[3] *= S; \
245 } while (0)
246
247
248 /*
249 * Similarly for 3-vectors.
250 */
251 #define SUB_3V( DST, SRCA, SRCB ) \
252 do { \
253 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
254 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
255 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
256 } while (0)
257
258 #define ADD_3V( DST, SRCA, SRCB ) \
259 do { \
260 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
261 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
262 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
263 } while (0)
264
265 #define SCALE_3V( DST, SRCA, SRCB ) \
266 do { \
267 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
268 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
269 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
270 } while (0)
271
272 #define SELF_SCALE_3V( DST, SRC ) \
273 do { \
274 (DST)[0] *= (SRC)[0]; \
275 (DST)[1] *= (SRC)[1]; \
276 (DST)[2] *= (SRC)[2]; \
277 } while (0)
278
279 #define ACC_3V( DST, SRC ) \
280 do { \
281 (DST)[0] += (SRC)[0]; \
282 (DST)[1] += (SRC)[1]; \
283 (DST)[2] += (SRC)[2]; \
284 } while (0)
285
286 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
287 do { \
288 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
289 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
290 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
291 } while (0)
292
293 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
294 do { \
295 (DST)[0] = S * (SRCB)[0]; \
296 (DST)[1] = S * (SRCB)[1]; \
297 (DST)[2] = S * (SRCB)[2]; \
298 } while (0)
299
300 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
301 do { \
302 (DST)[0] += S * (SRCB)[0]; \
303 (DST)[1] += S * (SRCB)[1]; \
304 (DST)[2] += S * (SRCB)[2]; \
305 } while (0)
306
307 #define SELF_SCALE_SCALAR_3V( DST, S ) \
308 do { \
309 (DST)[0] *= S; \
310 (DST)[1] *= S; \
311 (DST)[2] *= S; \
312 } while (0)
313
314 #define ACC_SCALAR_3V( DST, S ) \
315 do { \
316 (DST)[0] += S; \
317 (DST)[1] += S; \
318 (DST)[2] += S; \
319 } while (0)
320
321 /* And also for 2-vectors
322 */
323 #define SUB_2V( DST, SRCA, SRCB ) \
324 do { \
325 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
326 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
327 } while (0)
328
329 #define ADD_2V( DST, SRCA, SRCB ) \
330 do { \
331 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
332 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
333 } while (0)
334
335 #define SCALE_2V( DST, SRCA, SRCB ) \
336 do { \
337 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
338 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
339 } while (0)
340
341 #define ACC_2V( DST, SRC ) \
342 do { \
343 (DST)[0] += (SRC)[0]; \
344 (DST)[1] += (SRC)[1]; \
345 } while (0)
346
347 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
348 do { \
349 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
350 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
351 } while (0)
352
353 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
354 do { \
355 (DST)[0] = S * (SRCB)[0]; \
356 (DST)[1] = S * (SRCB)[1]; \
357 } while (0)
358
359 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
360 do { \
361 (DST)[0] += S * (SRCB)[0]; \
362 (DST)[1] += S * (SRCB)[1]; \
363 } while (0)
364
365 #define SELF_SCALE_SCALAR_2V( DST, S ) \
366 do { \
367 (DST)[0] *= S; \
368 (DST)[1] *= S; \
369 } while (0)
370
371 #define ACC_SCALAR_2V( DST, S ) \
372 do { \
373 (DST)[0] += S; \
374 (DST)[1] += S; \
375 } while (0)
376
377
378
379 /* Assign scalers to short vectors: */
380 #define ASSIGN_2V( V, V0, V1 ) \
381 do { \
382 V[0] = V0; \
383 V[1] = V1; \
384 } while(0)
385
386 #define ASSIGN_3V( V, V0, V1, V2 ) \
387 do { \
388 V[0] = V0; \
389 V[1] = V1; \
390 V[2] = V2; \
391 } while(0)
392
393 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
394 do { \
395 V[0] = V0; \
396 V[1] = V1; \
397 V[2] = V2; \
398 V[3] = V3; \
399 } while(0)
400
401
402
403
404 /* Absolute value (for Int, Float, Double): */
405 #define ABSI(X) ((X) < 0 ? -(X) : (X))
406 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
407 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
408
409
410
411 /* Round a floating-point value to the nearest integer: */
412 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
413
414
415 /* Compute ceiling of integer quotient of A divided by B: */
416 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
417
418
419 /* Clamp X to [MIN,MAX]: */
420 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
421
422 /* Assign X to CLAMP(X, MIN, MAX) */
423 #define CLAMP_SELF(x, mn, mx) \
424 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
425
426
427
428 /* Min of two values: */
429 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
430
431 /* MAX of two values: */
432 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
433
434 /* Dot product of two 2-element vectors */
435 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
436
437 /* Dot product of two 3-element vectors */
438 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
439
440 /* Dot product of two 4-element vectors */
441 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
442 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
443
444 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
445
446
447 #define CROSS3(n, u, v) \
448 do { \
449 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
450 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
451 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
452 } while (0)
453
454
455
456 /* Byte swapping
457 */
458
459 #ifdef __BIG_ENDIAN
460 #include <byteswap.h>
461 #define CPU_TO_LE32( x ) bswap_32( x )
462 #else
463 #define CPU_TO_LE32( x ) ( x )
464 #endif
465
466 #define LE32_TO_CPU( x ) CPU_TO_LE32( x )
467
468
469 /* Generic color packing macros
470 */
471
472 #define PACK_COLOR_8888( a, b, c, d ) \
473 (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
474
475 #define PACK_COLOR_888( a, b, c ) \
476 (((a) << 16) | ((b) << 8) | (c))
477
478 #define PACK_COLOR_565( a, b, c ) \
479 ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
480
481 #define PACK_COLOR_1555( a, b, c, d ) \
482 ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) | \
483 ((a) ? 0x8000 : 0))
484
485 #define PACK_COLOR_4444( a, b, c, d ) \
486 ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
487
488 #define PACK_COLOR_88( a, b ) \
489 (((a) << 8) | (b))
490
491 #define PACK_COLOR_332( a, b, c ) \
492 (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
493
494
495 #ifdef __BIG_ENDIAN
496
497 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( d, c, b, a )
498
499 #define PACK_COLOR_565_LE( a, b, c ) \
500 (((a) & 0xf8) | (((b) & 0xe0) >> 5) | (((b) & 0x1c) << 11) | \
501 (((c) & 0xf8) << 5))
502
503 #define PACK_COLOR_1555_LE( a, b, c, d ) \
504 ((((b) & 0xf8) >> 1) | (((c) & 0xc0) >> 6) | (((c) & 0x38) << 10) | \
505 (((d) & 0xf8) << 5) | ((a) ? 0x80 : 0))
506
507 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( c, d, a, b )
508
509 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( b, a )
510
511 #else /* little endian */
512
513 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( a, b, c, d )
514
515 #define PACK_COLOR_565_LE( a, b, c ) PACK_COLOR_565( a, b, c )
516
517 #define PACK_COLOR_1555_LE( a, b, c, d ) PACK_COLOR_1555( a, b, c, d )
518
519 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( a, b, c, d )
520
521 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( a, b )
522
523 #endif /* endianness */
524
525
526 #endif