Merge remote branch 'main/master' into radeon-rewrite
[mesa.git] / src / mesa / main / macros.h
1 /**
2 * \file macros.h
3 * A collection of useful macros.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 6.5.2
9 *
10 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #ifndef MACROS_H
32 #define MACROS_H
33
34 #include "imports.h"
35
36
37 /**
38 * \name Integer / float conversion for colors, normals, etc.
39 */
40 /*@{*/
41
42 /** Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
43 extern GLfloat _mesa_ubyte_to_float_color_tab[256];
44 #define UBYTE_TO_FLOAT(u) _mesa_ubyte_to_float_color_tab[(unsigned int)(u)]
45
46 /** Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
47 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) ((X) * 255.0F))
48
49
50 /** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
51 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
52
53 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
54 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
55
56
57 /** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */
58 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
59
60 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
61 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0))
62
63 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
64 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
65
66 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */
67 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
68
69
70 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
71 #define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F))
72
73 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
74 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
75
76
77 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
78 #define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
79
80 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
81 /* causes overflow:
82 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
83 */
84 /* a close approximation: */
85 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
86
87
88 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
89 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
90 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
91 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
92 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
93
94
95 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
96 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
97 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
98 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
99 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
100 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
101 us = ( (GLushort) IROUND( CLAMP((f), 0.0, 1.0) * 65535.0F) )
102 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
103 us = ( (GLushort) IROUND( (f) * 65535.0F) )
104
105 /*@}*/
106
107
108 /** Stepping a GLfloat pointer by a byte stride */
109 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
110 /** Stepping a GLuint pointer by a byte stride */
111 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
112 /** Stepping a GLubyte[4] pointer by a byte stride */
113 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
114 /** Stepping a GLfloat[4] pointer by a byte stride */
115 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
116 /** Stepping a GLchan[4] pointer by a byte stride */
117 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
118 /** Stepping a GLchan pointer by a byte stride */
119 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
120 /** Stepping a \p t pointer by a byte stride */
121 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
122
123
124 /**********************************************************************/
125 /** \name 4-element vector operations */
126 /*@{*/
127
128 /** Zero */
129 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
130
131 /** Test for equality */
132 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
133 (a)[1] == (b)[1] && \
134 (a)[2] == (b)[2] && \
135 (a)[3] == (b)[3])
136
137 /** Test for equality (unsigned bytes) */
138 #if defined(__i386__)
139 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
140 #else
141 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
142 #endif
143
144 /** Copy a 4-element vector */
145 #define COPY_4V( DST, SRC ) \
146 do { \
147 (DST)[0] = (SRC)[0]; \
148 (DST)[1] = (SRC)[1]; \
149 (DST)[2] = (SRC)[2]; \
150 (DST)[3] = (SRC)[3]; \
151 } while (0)
152
153 /** Copy a 4-element vector with cast */
154 #define COPY_4V_CAST( DST, SRC, CAST ) \
155 do { \
156 (DST)[0] = (CAST)(SRC)[0]; \
157 (DST)[1] = (CAST)(SRC)[1]; \
158 (DST)[2] = (CAST)(SRC)[2]; \
159 (DST)[3] = (CAST)(SRC)[3]; \
160 } while (0)
161
162 /** Copy a 4-element unsigned byte vector */
163 #if defined(__i386__)
164 #define COPY_4UBV(DST, SRC) \
165 do { \
166 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
167 } while (0)
168 #else
169 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
170 #define COPY_4UBV(DST, SRC) \
171 do { \
172 (DST)[0] = (SRC)[0]; \
173 (DST)[1] = (SRC)[1]; \
174 (DST)[2] = (SRC)[2]; \
175 (DST)[3] = (SRC)[3]; \
176 } while (0)
177 #endif
178
179 /**
180 * Copy a 4-element float vector (avoid using FPU registers)
181 * XXX Could use two 64-bit moves on 64-bit systems
182 */
183 #define COPY_4FV( DST, SRC ) \
184 do { \
185 const GLuint *_s = (const GLuint *) (SRC); \
186 GLuint *_d = (GLuint *) (DST); \
187 _d[0] = _s[0]; \
188 _d[1] = _s[1]; \
189 _d[2] = _s[2]; \
190 _d[3] = _s[3]; \
191 } while (0)
192
193 /** Copy \p SZ elements into a 4-element vector */
194 #define COPY_SZ_4V(DST, SZ, SRC) \
195 do { \
196 switch (SZ) { \
197 case 4: (DST)[3] = (SRC)[3]; \
198 case 3: (DST)[2] = (SRC)[2]; \
199 case 2: (DST)[1] = (SRC)[1]; \
200 case 1: (DST)[0] = (SRC)[0]; \
201 } \
202 } while(0)
203
204 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
205 * default values to the remaining */
206 #define COPY_CLEAN_4V(DST, SZ, SRC) \
207 do { \
208 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
209 COPY_SZ_4V( DST, SZ, SRC ); \
210 } while (0)
211
212 /** Subtraction */
213 #define SUB_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 /** Addition */
222 #define ADD_4V( 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 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
228 } while (0)
229
230 /** Element-wise multiplication */
231 #define SCALE_4V( DST, SRCA, SRCB ) \
232 do { \
233 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
234 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
235 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
236 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
237 } while (0)
238
239 /** In-place addition */
240 #define ACC_4V( DST, SRC ) \
241 do { \
242 (DST)[0] += (SRC)[0]; \
243 (DST)[1] += (SRC)[1]; \
244 (DST)[2] += (SRC)[2]; \
245 (DST)[3] += (SRC)[3]; \
246 } while (0)
247
248 /** Element-wise multiplication and addition */
249 #define ACC_SCALE_4V( 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 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
255 } while (0)
256
257 /** In-place scalar multiplication and addition */
258 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
259 do { \
260 (DST)[0] += S * (SRCB)[0]; \
261 (DST)[1] += S * (SRCB)[1]; \
262 (DST)[2] += S * (SRCB)[2]; \
263 (DST)[3] += S * (SRCB)[3]; \
264 } while (0)
265
266 /** Scalar multiplication */
267 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
268 do { \
269 (DST)[0] = S * (SRCB)[0]; \
270 (DST)[1] = S * (SRCB)[1]; \
271 (DST)[2] = S * (SRCB)[2]; \
272 (DST)[3] = S * (SRCB)[3]; \
273 } while (0)
274
275 /** In-place scalar multiplication */
276 #define SELF_SCALE_SCALAR_4V( DST, S ) \
277 do { \
278 (DST)[0] *= S; \
279 (DST)[1] *= S; \
280 (DST)[2] *= S; \
281 (DST)[3] *= S; \
282 } while (0)
283
284 /** Assignment */
285 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
286 do { \
287 V[0] = V0; \
288 V[1] = V1; \
289 V[2] = V2; \
290 V[3] = V3; \
291 } while(0)
292
293 /*@}*/
294
295
296 /**********************************************************************/
297 /** \name 3-element vector operations*/
298 /*@{*/
299
300 /** Zero */
301 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
302
303 /** Test for equality */
304 #define TEST_EQ_3V(a,b) \
305 ((a)[0] == (b)[0] && \
306 (a)[1] == (b)[1] && \
307 (a)[2] == (b)[2])
308
309 /** Copy a 3-element vector */
310 #define COPY_3V( DST, SRC ) \
311 do { \
312 (DST)[0] = (SRC)[0]; \
313 (DST)[1] = (SRC)[1]; \
314 (DST)[2] = (SRC)[2]; \
315 } while (0)
316
317 /** Copy a 3-element vector with cast */
318 #define COPY_3V_CAST( DST, SRC, CAST ) \
319 do { \
320 (DST)[0] = (CAST)(SRC)[0]; \
321 (DST)[1] = (CAST)(SRC)[1]; \
322 (DST)[2] = (CAST)(SRC)[2]; \
323 } while (0)
324
325 /** Copy a 3-element float vector */
326 #define COPY_3FV( DST, SRC ) \
327 do { \
328 const GLfloat *_tmp = (SRC); \
329 (DST)[0] = _tmp[0]; \
330 (DST)[1] = _tmp[1]; \
331 (DST)[2] = _tmp[2]; \
332 } while (0)
333
334 /** Subtraction */
335 #define SUB_3V( DST, SRCA, SRCB ) \
336 do { \
337 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
338 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
339 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
340 } while (0)
341
342 /** Addition */
343 #define ADD_3V( DST, SRCA, SRCB ) \
344 do { \
345 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
346 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
347 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
348 } while (0)
349
350 /** In-place scalar multiplication */
351 #define SCALE_3V( DST, SRCA, SRCB ) \
352 do { \
353 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
354 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
355 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
356 } while (0)
357
358 /** In-place element-wise multiplication */
359 #define SELF_SCALE_3V( DST, SRC ) \
360 do { \
361 (DST)[0] *= (SRC)[0]; \
362 (DST)[1] *= (SRC)[1]; \
363 (DST)[2] *= (SRC)[2]; \
364 } while (0)
365
366 /** In-place addition */
367 #define ACC_3V( DST, SRC ) \
368 do { \
369 (DST)[0] += (SRC)[0]; \
370 (DST)[1] += (SRC)[1]; \
371 (DST)[2] += (SRC)[2]; \
372 } while (0)
373
374 /** Element-wise multiplication and addition */
375 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
376 do { \
377 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
378 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
379 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
380 } while (0)
381
382 /** Scalar multiplication */
383 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
384 do { \
385 (DST)[0] = S * (SRCB)[0]; \
386 (DST)[1] = S * (SRCB)[1]; \
387 (DST)[2] = S * (SRCB)[2]; \
388 } while (0)
389
390 /** In-place scalar multiplication and addition */
391 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
392 do { \
393 (DST)[0] += S * (SRCB)[0]; \
394 (DST)[1] += S * (SRCB)[1]; \
395 (DST)[2] += S * (SRCB)[2]; \
396 } while (0)
397
398 /** In-place scalar multiplication */
399 #define SELF_SCALE_SCALAR_3V( DST, S ) \
400 do { \
401 (DST)[0] *= S; \
402 (DST)[1] *= S; \
403 (DST)[2] *= S; \
404 } while (0)
405
406 /** In-place scalar addition */
407 #define ACC_SCALAR_3V( DST, S ) \
408 do { \
409 (DST)[0] += S; \
410 (DST)[1] += S; \
411 (DST)[2] += S; \
412 } while (0)
413
414 /** Assignment */
415 #define ASSIGN_3V( V, V0, V1, V2 ) \
416 do { \
417 V[0] = V0; \
418 V[1] = V1; \
419 V[2] = V2; \
420 } while(0)
421
422 /*@}*/
423
424
425 /**********************************************************************/
426 /** \name 2-element vector operations*/
427 /*@{*/
428
429 /** Zero */
430 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
431
432 /** Copy a 2-element vector */
433 #define COPY_2V( DST, SRC ) \
434 do { \
435 (DST)[0] = (SRC)[0]; \
436 (DST)[1] = (SRC)[1]; \
437 } while (0)
438
439 /** Copy a 2-element vector with cast */
440 #define COPY_2V_CAST( DST, SRC, CAST ) \
441 do { \
442 (DST)[0] = (CAST)(SRC)[0]; \
443 (DST)[1] = (CAST)(SRC)[1]; \
444 } while (0)
445
446 /** Copy a 2-element float vector */
447 #define COPY_2FV( DST, SRC ) \
448 do { \
449 const GLfloat *_tmp = (SRC); \
450 (DST)[0] = _tmp[0]; \
451 (DST)[1] = _tmp[1]; \
452 } while (0)
453
454 /** Subtraction */
455 #define SUB_2V( DST, SRCA, SRCB ) \
456 do { \
457 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
458 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
459 } while (0)
460
461 /** Addition */
462 #define ADD_2V( DST, SRCA, SRCB ) \
463 do { \
464 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
465 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
466 } while (0)
467
468 /** In-place scalar multiplication */
469 #define SCALE_2V( DST, SRCA, SRCB ) \
470 do { \
471 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
472 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
473 } while (0)
474
475 /** In-place addition */
476 #define ACC_2V( DST, SRC ) \
477 do { \
478 (DST)[0] += (SRC)[0]; \
479 (DST)[1] += (SRC)[1]; \
480 } while (0)
481
482 /** Element-wise multiplication and addition */
483 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
484 do { \
485 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
486 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
487 } while (0)
488
489 /** Scalar multiplication */
490 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
491 do { \
492 (DST)[0] = S * (SRCB)[0]; \
493 (DST)[1] = S * (SRCB)[1]; \
494 } while (0)
495
496 /** In-place scalar multiplication and addition */
497 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
498 do { \
499 (DST)[0] += S * (SRCB)[0]; \
500 (DST)[1] += S * (SRCB)[1]; \
501 } while (0)
502
503 /** In-place scalar multiplication */
504 #define SELF_SCALE_SCALAR_2V( DST, S ) \
505 do { \
506 (DST)[0] *= S; \
507 (DST)[1] *= S; \
508 } while (0)
509
510 /** In-place scalar addition */
511 #define ACC_SCALAR_2V( DST, S ) \
512 do { \
513 (DST)[0] += S; \
514 (DST)[1] += S; \
515 } while (0)
516
517 /** Assign scalers to short vectors */
518 #define ASSIGN_2V( V, V0, V1 ) \
519 do { \
520 V[0] = V0; \
521 V[1] = V1; \
522 } while(0)
523
524 /*@}*/
525
526
527 /** \name Linear interpolation macros */
528 /*@{*/
529
530 /**
531 * Linear interpolation
532 *
533 * \note \p OUT argument is evaluated twice!
534 * \note Be wary of using *coord++ as an argument to any of these macros!
535 */
536 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
537
538 /* Can do better with integer math
539 */
540 #define INTERP_UB( t, dstub, outub, inub ) \
541 do { \
542 GLfloat inf = UBYTE_TO_FLOAT( inub ); \
543 GLfloat outf = UBYTE_TO_FLOAT( outub ); \
544 GLfloat dstf = LINTERP( t, outf, inf ); \
545 UNCLAMPED_FLOAT_TO_UBYTE( dstub, dstf ); \
546 } while (0)
547
548 #define INTERP_CHAN( t, dstc, outc, inc ) \
549 do { \
550 GLfloat inf = CHAN_TO_FLOAT( inc ); \
551 GLfloat outf = CHAN_TO_FLOAT( outc ); \
552 GLfloat dstf = LINTERP( t, outf, inf ); \
553 UNCLAMPED_FLOAT_TO_CHAN( dstc, dstf ); \
554 } while (0)
555
556 #define INTERP_UI( t, dstui, outui, inui ) \
557 dstui = (GLuint) (GLint) LINTERP( (t), (GLfloat) (outui), (GLfloat) (inui) )
558
559 #define INTERP_F( t, dstf, outf, inf ) \
560 dstf = LINTERP( t, outf, inf )
561
562 #define INTERP_4F( t, dst, out, in ) \
563 do { \
564 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
565 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
566 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
567 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
568 } while (0)
569
570 #define INTERP_3F( t, dst, out, in ) \
571 do { \
572 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
573 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
574 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
575 } while (0)
576
577 #define INTERP_4CHAN( t, dst, out, in ) \
578 do { \
579 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
580 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
581 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
582 INTERP_CHAN( (t), (dst)[3], (out)[3], (in)[3] ); \
583 } while (0)
584
585 #define INTERP_3CHAN( t, dst, out, in ) \
586 do { \
587 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
588 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
589 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
590 } while (0)
591
592 #define INTERP_SZ( t, vec, to, out, in, sz ) \
593 do { \
594 switch (sz) { \
595 case 4: vec[to][3] = LINTERP( (t), (vec)[out][3], (vec)[in][3] ); \
596 case 3: vec[to][2] = LINTERP( (t), (vec)[out][2], (vec)[in][2] ); \
597 case 2: vec[to][1] = LINTERP( (t), (vec)[out][1], (vec)[in][1] ); \
598 case 1: vec[to][0] = LINTERP( (t), (vec)[out][0], (vec)[in][0] ); \
599 } \
600 } while(0)
601
602 /*@}*/
603
604
605
606 /** Clamp X to [MIN,MAX] */
607 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
608
609 /** Assign X to CLAMP(X, MIN, MAX) */
610 #define CLAMP_SELF(x, mn, mx) \
611 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
612
613
614
615 /** Minimum of two values: */
616 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
617
618 /** Maximum of two values: */
619 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
620
621 /** Dot product of two 2-element vectors */
622 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
623
624 /** Dot product of two 3-element vectors */
625 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
626
627 /** Dot product of two 4-element vectors */
628 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
629 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
630
631 /** Dot product of two 4-element vectors */
632 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
633
634
635 /** Cross product of two 3-element vectors */
636 #define CROSS3(n, u, v) \
637 do { \
638 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
639 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
640 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
641 } while (0)
642
643
644 /* Normalize a 3-element vector to unit length. */
645 #define NORMALIZE_3FV( V ) \
646 do { \
647 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
648 if (len) { \
649 len = INV_SQRTF(len); \
650 (V)[0] = (GLfloat) ((V)[0] * len); \
651 (V)[1] = (GLfloat) ((V)[1] * len); \
652 (V)[2] = (GLfloat) ((V)[2] * len); \
653 } \
654 } while(0)
655
656 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
657 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
658
659 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
660 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
661
662
663 /** casts to silence warnings with some compilers */
664 #define ENUM_TO_INT(E) ((GLint)(E))
665 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
666 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
667 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
668
669
670 #endif