improved solve_plane_chan() (Evgeny Kotsuba)
[mesa.git] / src / mesa / main / macros.h
1 /* $Id: macros.h,v 1.30 2002/10/18 17:02:00 kschultz Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.0.3
6 *
7 * Copyright (C) 1999-2002 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 /* Stepping a GLfloat pointer by a byte stride
63 */
64 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
65 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
66 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
67 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
68 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
69 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
70
71
72 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
73 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
74 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
75
76
77 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
78 (a)[1] == (b)[1] && \
79 (a)[2] == (b)[2] && \
80 (a)[3] == (b)[3])
81
82 #define TEST_EQ_3V(a,b) ((a)[0] == (b)[0] && \
83 (a)[1] == (b)[1] && \
84 (a)[2] == (b)[2])
85
86 #if defined(__i386__)
87 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
88 #else
89 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
90 #endif
91
92
93
94 /* Copy short vectors: */
95 #define COPY_2V( DST, SRC ) \
96 do { \
97 (DST)[0] = (SRC)[0]; \
98 (DST)[1] = (SRC)[1]; \
99 } while (0)
100
101 #define COPY_3V( DST, SRC ) \
102 do { \
103 (DST)[0] = (SRC)[0]; \
104 (DST)[1] = (SRC)[1]; \
105 (DST)[2] = (SRC)[2]; \
106 } while (0)
107
108 #define COPY_4V( DST, SRC ) \
109 do { \
110 (DST)[0] = (SRC)[0]; \
111 (DST)[1] = (SRC)[1]; \
112 (DST)[2] = (SRC)[2]; \
113 (DST)[3] = (SRC)[3]; \
114 } while (0)
115
116 #define COPY_2V_CAST( DST, SRC, CAST ) \
117 do { \
118 (DST)[0] = (CAST)(SRC)[0]; \
119 (DST)[1] = (CAST)(SRC)[1]; \
120 } while (0)
121
122 #define COPY_3V_CAST( DST, SRC, CAST ) \
123 do { \
124 (DST)[0] = (CAST)(SRC)[0]; \
125 (DST)[1] = (CAST)(SRC)[1]; \
126 (DST)[2] = (CAST)(SRC)[2]; \
127 } while (0)
128
129 #define COPY_4V_CAST( DST, SRC, CAST ) \
130 do { \
131 (DST)[0] = (CAST)(SRC)[0]; \
132 (DST)[1] = (CAST)(SRC)[1]; \
133 (DST)[2] = (CAST)(SRC)[2]; \
134 (DST)[3] = (CAST)(SRC)[3]; \
135 } while (0)
136
137 #if defined(__i386__)
138 #define COPY_4UBV(DST, SRC) \
139 do { \
140 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
141 } while (0)
142 #else
143 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
144 #define COPY_4UBV(DST, SRC) \
145 do { \
146 (DST)[0] = (SRC)[0]; \
147 (DST)[1] = (SRC)[1]; \
148 (DST)[2] = (SRC)[2]; \
149 (DST)[3] = (SRC)[3]; \
150 } while (0)
151 #endif
152
153 #define COPY_2FV( DST, SRC ) \
154 do { \
155 const GLfloat *_tmp = (SRC); \
156 (DST)[0] = _tmp[0]; \
157 (DST)[1] = _tmp[1]; \
158 } while (0)
159
160 #define COPY_3FV( DST, SRC ) \
161 do { \
162 const GLfloat *_tmp = (SRC); \
163 (DST)[0] = _tmp[0]; \
164 (DST)[1] = _tmp[1]; \
165 (DST)[2] = _tmp[2]; \
166 } while (0)
167
168 #define COPY_4FV( DST, SRC ) \
169 do { \
170 const GLfloat *_tmp = (SRC); \
171 (DST)[0] = _tmp[0]; \
172 (DST)[1] = _tmp[1]; \
173 (DST)[2] = _tmp[2]; \
174 (DST)[3] = _tmp[3]; \
175 } while (0)
176
177
178
179 #define COPY_SZ_4V(DST, SZ, SRC) \
180 do { \
181 switch (SZ) { \
182 case 4: (DST)[3] = (SRC)[3]; \
183 case 3: (DST)[2] = (SRC)[2]; \
184 case 2: (DST)[1] = (SRC)[1]; \
185 case 1: (DST)[0] = (SRC)[0]; \
186 } \
187 } while(0)
188
189 #define COPY_CLEAN_4V(DST, SZ, SRC) \
190 do { \
191 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
192 COPY_SZ_4V( DST, SZ, SRC ); \
193 } while (0)
194
195 #define SUB_4V( DST, SRCA, SRCB ) \
196 do { \
197 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
198 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
199 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
200 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
201 } while (0)
202
203 #define ADD_4V( DST, SRCA, SRCB ) \
204 do { \
205 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
206 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
207 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
208 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
209 } while (0)
210
211 #define SCALE_4V( DST, SRCA, SRCB ) \
212 do { \
213 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
214 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
215 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
216 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
217 } while (0)
218
219 #define ACC_4V( DST, SRC ) \
220 do { \
221 (DST)[0] += (SRC)[0]; \
222 (DST)[1] += (SRC)[1]; \
223 (DST)[2] += (SRC)[2]; \
224 (DST)[3] += (SRC)[3]; \
225 } while (0)
226
227 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
228 do { \
229 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
230 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
231 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
232 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
233 } while (0)
234
235 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
236 do { \
237 (DST)[0] += S * (SRCB)[0]; \
238 (DST)[1] += S * (SRCB)[1]; \
239 (DST)[2] += S * (SRCB)[2]; \
240 (DST)[3] += S * (SRCB)[3]; \
241 } while (0)
242
243 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
244 do { \
245 (DST)[0] = S * (SRCB)[0]; \
246 (DST)[1] = S * (SRCB)[1]; \
247 (DST)[2] = S * (SRCB)[2]; \
248 (DST)[3] = S * (SRCB)[3]; \
249 } while (0)
250
251
252 #define SELF_SCALE_SCALAR_4V( DST, S ) \
253 do { \
254 (DST)[0] *= S; \
255 (DST)[1] *= S; \
256 (DST)[2] *= S; \
257 (DST)[3] *= S; \
258 } while (0)
259
260
261 /*
262 * Similarly for 3-vectors.
263 */
264 #define SUB_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 ADD_3V( DST, SRCA, SRCB ) \
272 do { \
273 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
274 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
275 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
276 } while (0)
277
278 #define 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 SELF_SCALE_3V( DST, SRC ) \
286 do { \
287 (DST)[0] *= (SRC)[0]; \
288 (DST)[1] *= (SRC)[1]; \
289 (DST)[2] *= (SRC)[2]; \
290 } while (0)
291
292 #define ACC_3V( DST, SRC ) \
293 do { \
294 (DST)[0] += (SRC)[0]; \
295 (DST)[1] += (SRC)[1]; \
296 (DST)[2] += (SRC)[2]; \
297 } while (0)
298
299 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
300 do { \
301 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
302 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
303 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
304 } while (0)
305
306 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
307 do { \
308 (DST)[0] = S * (SRCB)[0]; \
309 (DST)[1] = S * (SRCB)[1]; \
310 (DST)[2] = S * (SRCB)[2]; \
311 } while (0)
312
313 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
314 do { \
315 (DST)[0] += S * (SRCB)[0]; \
316 (DST)[1] += S * (SRCB)[1]; \
317 (DST)[2] += S * (SRCB)[2]; \
318 } while (0)
319
320 #define SELF_SCALE_SCALAR_3V( DST, S ) \
321 do { \
322 (DST)[0] *= S; \
323 (DST)[1] *= S; \
324 (DST)[2] *= S; \
325 } while (0)
326
327 #define ACC_SCALAR_3V( DST, S ) \
328 do { \
329 (DST)[0] += S; \
330 (DST)[1] += S; \
331 (DST)[2] += S; \
332 } while (0)
333
334 /* And also for 2-vectors
335 */
336 #define SUB_2V( DST, SRCA, SRCB ) \
337 do { \
338 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
339 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
340 } while (0)
341
342 #define ADD_2V( DST, SRCA, SRCB ) \
343 do { \
344 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
345 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
346 } while (0)
347
348 #define SCALE_2V( DST, SRCA, SRCB ) \
349 do { \
350 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
351 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
352 } while (0)
353
354 #define ACC_2V( DST, SRC ) \
355 do { \
356 (DST)[0] += (SRC)[0]; \
357 (DST)[1] += (SRC)[1]; \
358 } while (0)
359
360 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
361 do { \
362 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
363 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
364 } while (0)
365
366 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
367 do { \
368 (DST)[0] = S * (SRCB)[0]; \
369 (DST)[1] = S * (SRCB)[1]; \
370 } while (0)
371
372 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
373 do { \
374 (DST)[0] += S * (SRCB)[0]; \
375 (DST)[1] += S * (SRCB)[1]; \
376 } while (0)
377
378 #define SELF_SCALE_SCALAR_2V( DST, S ) \
379 do { \
380 (DST)[0] *= S; \
381 (DST)[1] *= S; \
382 } while (0)
383
384 #define ACC_SCALAR_2V( DST, S ) \
385 do { \
386 (DST)[0] += S; \
387 (DST)[1] += S; \
388 } while (0)
389
390
391
392 /* Assign scalers to short vectors: */
393 #define ASSIGN_2V( V, V0, V1 ) \
394 do { \
395 V[0] = V0; \
396 V[1] = V1; \
397 } while(0)
398
399 #define ASSIGN_3V( V, V0, V1, V2 ) \
400 do { \
401 V[0] = V0; \
402 V[1] = V1; \
403 V[2] = V2; \
404 } while(0)
405
406 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
407 do { \
408 V[0] = V0; \
409 V[1] = V1; \
410 V[2] = V2; \
411 V[3] = V3; \
412 } while(0)
413
414
415
416
417 /* Absolute value (for Int, Float, Double): */
418 #define ABSI(X) ((X) < 0 ? -(X) : (X))
419 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
420 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
421
422
423
424 /* Round a floating-point value to the nearest integer: */
425 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
426
427
428 /* Compute ceiling of integer quotient of A divided by B: */
429 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
430
431
432 /* Clamp X to [MIN,MAX]: */
433 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
434
435 /* Assign X to CLAMP(X, MIN, MAX) */
436 #define CLAMP_SELF(x, mn, mx) \
437 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
438
439
440
441 /* Min of two values: */
442 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
443
444 /* MAX of two values: */
445 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
446
447 /* Dot product of two 2-element vectors */
448 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
449
450 /* Dot product of two 3-element vectors */
451 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
452
453 /* Dot product of two 4-element vectors */
454 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
455 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
456
457 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
458
459
460 #define CROSS3(n, u, v) \
461 do { \
462 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
463 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
464 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
465 } while (0)
466
467
468
469 /* Generic color packing macros
470 * XXX We may move these into texutil.h at some point.
471 */
472
473 #define PACK_COLOR_8888( a, b, c, d ) \
474 (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
475
476 #define PACK_COLOR_888( a, b, c ) \
477 (((a) << 16) | ((b) << 8) | (c))
478
479 #define PACK_COLOR_565( a, b, c ) \
480 ((((a) & 0xf8) << 8) | (((b) & 0xfc) << 3) | (((c) & 0xf8) >> 3))
481
482 #define PACK_COLOR_1555( a, b, c, d ) \
483 ((((b) & 0xf8) << 7) | (((c) & 0xf8) << 2) | (((d) & 0xf8) >> 3) | \
484 ((a) ? 0x8000 : 0))
485
486 #define PACK_COLOR_4444( a, b, c, d ) \
487 ((((a) & 0xf0) << 8) | (((b) & 0xf0) << 4) | ((c) & 0xf0) | ((d) >> 4))
488
489 #define PACK_COLOR_88( a, b ) \
490 (((a) << 8) | (b))
491
492 #define PACK_COLOR_332( a, b, c ) \
493 (((a) & 0xe0) | (((b) & 0xe0) >> 3) | (((c) & 0xc0) >> 6))
494
495
496 #ifdef MESA_BIG_ENDIAN
497
498 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( d, c, b, a )
499
500 #define PACK_COLOR_565_LE( a, b, c ) \
501 (((a) & 0xf8) | (((b) & 0xe0) >> 5) | (((b) & 0x1c) << 11) | \
502 (((c) & 0xf8) << 5))
503
504 #define PACK_COLOR_1555_LE( a, b, c, d ) \
505 ((((b) & 0xf8) >> 1) | (((c) & 0xc0) >> 6) | (((c) & 0x38) << 10) | \
506 (((d) & 0xf8) << 5) | ((a) ? 0x80 : 0))
507
508 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( c, d, a, b )
509
510 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( b, a )
511
512 #else /* little endian */
513
514 #define PACK_COLOR_8888_LE( a, b, c, d ) PACK_COLOR_8888( a, b, c, d )
515
516 #define PACK_COLOR_565_LE( a, b, c ) PACK_COLOR_565( a, b, c )
517
518 #define PACK_COLOR_1555_LE( a, b, c, d ) PACK_COLOR_1555( a, b, c, d )
519
520 #define PACK_COLOR_4444_LE( a, b, c, d ) PACK_COLOR_4444( a, b, c, d )
521
522 #define PACK_COLOR_88_LE( a, b ) PACK_COLOR_88( a, b )
523
524 #endif /* endianness */
525
526
527 #endif