e449ee3462573b246fcd6bd83d38c4123150c150
[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: 4.0.3
9 *
10 * Copyright (C) 1999-2002 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,65536] 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,65536] */
61 #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
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 [0.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 /** Stepping a GLfloat pointer by a byte stride */
107 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
108 /** Stepping a GLuint pointer by a byte stride */
109 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
110 /** Stepping a GLubyte[4] pointer by a byte stride */
111 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
112 /** Stepping a GLchan[4] pointer by a byte stride */
113 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
114 /** Stepping a GLchan pointer by a byte stride */
115 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
116 /** Stepping a \p t pointer by a byte stride */
117 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
118
119
120 /**********************************************************************/
121 /** \name 4-element vector operations */
122 /*@{*/
123
124 /** Zero */
125 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
126
127 /** Test for equality */
128 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
129 (a)[1] == (b)[1] && \
130 (a)[2] == (b)[2] && \
131 (a)[3] == (b)[3])
132
133 /** Test for equality (unsigned bytes) */
134 #if defined(__i386__)
135 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
136 #else
137 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
138 #endif
139
140 /** Copy a 4-element vector */
141 #define COPY_4V( DST, SRC ) \
142 do { \
143 (DST)[0] = (SRC)[0]; \
144 (DST)[1] = (SRC)[1]; \
145 (DST)[2] = (SRC)[2]; \
146 (DST)[3] = (SRC)[3]; \
147 } while (0)
148
149 /** Copy a 4-element vector with cast */
150 #define COPY_4V_CAST( DST, SRC, CAST ) \
151 do { \
152 (DST)[0] = (CAST)(SRC)[0]; \
153 (DST)[1] = (CAST)(SRC)[1]; \
154 (DST)[2] = (CAST)(SRC)[2]; \
155 (DST)[3] = (CAST)(SRC)[3]; \
156 } while (0)
157
158 /** Copy a 4-element unsigned byte vector */
159 #if defined(__i386__)
160 #define COPY_4UBV(DST, SRC) \
161 do { \
162 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
163 } while (0)
164 #else
165 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
166 #define COPY_4UBV(DST, SRC) \
167 do { \
168 (DST)[0] = (SRC)[0]; \
169 (DST)[1] = (SRC)[1]; \
170 (DST)[2] = (SRC)[2]; \
171 (DST)[3] = (SRC)[3]; \
172 } while (0)
173 #endif
174
175 /** Copy a 4-element float vector (Use COPY_FLOAT to avoid loading FPU) */
176 #define COPY_4FV( DST, SRC ) \
177 do { \
178 COPY_FLOAT((DST)[0], (SRC)[0]); \
179 COPY_FLOAT((DST)[1], (SRC)[1]); \
180 COPY_FLOAT((DST)[2], (SRC)[2]); \
181 COPY_FLOAT((DST)[3], (SRC)[3]); \
182 } while (0)
183
184
185 /** Copy \p SZ elements into a 4-element vector */
186 #define COPY_SZ_4V(DST, SZ, SRC) \
187 do { \
188 switch (SZ) { \
189 case 4: (DST)[3] = (SRC)[3]; \
190 case 3: (DST)[2] = (SRC)[2]; \
191 case 2: (DST)[1] = (SRC)[1]; \
192 case 1: (DST)[0] = (SRC)[0]; \
193 } \
194 } while(0)
195
196 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
197 * default values to the remaining */
198 #define COPY_CLEAN_4V(DST, SZ, SRC) \
199 do { \
200 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
201 COPY_SZ_4V( DST, SZ, SRC ); \
202 } while (0)
203
204 /** Subtraction */
205 #define SUB_4V( DST, SRCA, SRCB ) \
206 do { \
207 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
208 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
209 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
210 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
211 } while (0)
212
213 /** Addition */
214 #define ADD_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 /** Element-wise multiplication */
223 #define SCALE_4V( DST, SRCA, SRCB ) \
224 do { \
225 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
226 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
227 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
228 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
229 } while (0)
230
231 /** In-place addition */
232 #define ACC_4V( DST, SRC ) \
233 do { \
234 (DST)[0] += (SRC)[0]; \
235 (DST)[1] += (SRC)[1]; \
236 (DST)[2] += (SRC)[2]; \
237 (DST)[3] += (SRC)[3]; \
238 } while (0)
239
240 /** Element-wise multiplication and addition */
241 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
242 do { \
243 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
244 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
245 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
246 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
247 } while (0)
248
249 /** In-place scalar multiplication and addition */
250 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
251 do { \
252 (DST)[0] += S * (SRCB)[0]; \
253 (DST)[1] += S * (SRCB)[1]; \
254 (DST)[2] += S * (SRCB)[2]; \
255 (DST)[3] += S * (SRCB)[3]; \
256 } while (0)
257
258 /** Scalar multiplication */
259 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
260 do { \
261 (DST)[0] = S * (SRCB)[0]; \
262 (DST)[1] = S * (SRCB)[1]; \
263 (DST)[2] = S * (SRCB)[2]; \
264 (DST)[3] = S * (SRCB)[3]; \
265 } while (0)
266
267 /** In-place scalar multiplication */
268 #define SELF_SCALE_SCALAR_4V( DST, S ) \
269 do { \
270 (DST)[0] *= S; \
271 (DST)[1] *= S; \
272 (DST)[2] *= S; \
273 (DST)[3] *= S; \
274 } while (0)
275
276 /** Assignment */
277 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
278 do { \
279 V[0] = V0; \
280 V[1] = V1; \
281 V[2] = V2; \
282 V[3] = V3; \
283 } while(0)
284
285 /*@}*/
286
287
288 /**********************************************************************/
289 /** \name 3-element vector operations*/
290 /*@{*/
291
292 /** Zero */
293 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
294
295 /** Test for equality */
296 #define TEST_EQ_3V(a,b) ((a)[0] == (b)[0] && \
297 (a)[1] == (b)[1] && \
298 (a)[2] == (b)[2])
299
300 /** Copy a 3-element vector */
301 #define COPY_3V( DST, SRC ) \
302 do { \
303 (DST)[0] = (SRC)[0]; \
304 (DST)[1] = (SRC)[1]; \
305 (DST)[2] = (SRC)[2]; \
306 } while (0)
307
308 /** Copy a 3-element vector with cast */
309 #define COPY_3V_CAST( DST, SRC, CAST ) \
310 do { \
311 (DST)[0] = (CAST)(SRC)[0]; \
312 (DST)[1] = (CAST)(SRC)[1]; \
313 (DST)[2] = (CAST)(SRC)[2]; \
314 } while (0)
315
316 /** Copy a 3-element float vector */
317 #define COPY_3FV( DST, SRC ) \
318 do { \
319 const GLfloat *_tmp = (SRC); \
320 (DST)[0] = _tmp[0]; \
321 (DST)[1] = _tmp[1]; \
322 (DST)[2] = _tmp[2]; \
323 } while (0)
324
325 /** Subtraction */
326 #define SUB_3V( DST, SRCA, SRCB ) \
327 do { \
328 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
329 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
330 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
331 } while (0)
332
333 /** Addition */
334 #define ADD_3V( DST, SRCA, SRCB ) \
335 do { \
336 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
337 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
338 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
339 } while (0)
340
341 /** In-place scalar multiplication */
342 #define SCALE_3V( DST, SRCA, SRCB ) \
343 do { \
344 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
345 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
346 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
347 } while (0)
348
349 /** In-place element-wise multiplication */
350 #define SELF_SCALE_3V( DST, SRC ) \
351 do { \
352 (DST)[0] *= (SRC)[0]; \
353 (DST)[1] *= (SRC)[1]; \
354 (DST)[2] *= (SRC)[2]; \
355 } while (0)
356
357 /** In-place addition */
358 #define ACC_3V( DST, SRC ) \
359 do { \
360 (DST)[0] += (SRC)[0]; \
361 (DST)[1] += (SRC)[1]; \
362 (DST)[2] += (SRC)[2]; \
363 } while (0)
364
365 /** Element-wise multiplication and addition */
366 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
367 do { \
368 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
369 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
370 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
371 } while (0)
372
373 /** Scalar multiplication */
374 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
375 do { \
376 (DST)[0] = S * (SRCB)[0]; \
377 (DST)[1] = S * (SRCB)[1]; \
378 (DST)[2] = S * (SRCB)[2]; \
379 } while (0)
380
381 /** In-place scalar multiplication and addition */
382 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
383 do { \
384 (DST)[0] += S * (SRCB)[0]; \
385 (DST)[1] += S * (SRCB)[1]; \
386 (DST)[2] += S * (SRCB)[2]; \
387 } while (0)
388
389 /** In-place scalar multiplication */
390 #define SELF_SCALE_SCALAR_3V( DST, S ) \
391 do { \
392 (DST)[0] *= S; \
393 (DST)[1] *= S; \
394 (DST)[2] *= S; \
395 } while (0)
396
397 /** In-place scalar addition */
398 #define ACC_SCALAR_3V( DST, S ) \
399 do { \
400 (DST)[0] += S; \
401 (DST)[1] += S; \
402 (DST)[2] += S; \
403 } while (0)
404
405 /** Assignment */
406 #define ASSIGN_3V( V, V0, V1, V2 ) \
407 do { \
408 V[0] = V0; \
409 V[1] = V1; \
410 V[2] = V2; \
411 } while(0)
412
413 /*@}*/
414
415
416 /**********************************************************************/
417 /** \name 2-element vector operations*/
418 /*@{*/
419
420 /** Zero */
421 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
422
423 /** Copy a 2-element vector */
424 #define COPY_2V( DST, SRC ) \
425 do { \
426 (DST)[0] = (SRC)[0]; \
427 (DST)[1] = (SRC)[1]; \
428 } while (0)
429
430 /** Copy a 2-element vector with cast */
431 #define COPY_2V_CAST( DST, SRC, CAST ) \
432 do { \
433 (DST)[0] = (CAST)(SRC)[0]; \
434 (DST)[1] = (CAST)(SRC)[1]; \
435 } while (0)
436
437 /** Copy a 2-element float vector */
438 #define COPY_2FV( DST, SRC ) \
439 do { \
440 const GLfloat *_tmp = (SRC); \
441 (DST)[0] = _tmp[0]; \
442 (DST)[1] = _tmp[1]; \
443 } while (0)
444
445 /** Subtraction */
446 #define SUB_2V( DST, SRCA, SRCB ) \
447 do { \
448 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
449 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
450 } while (0)
451
452 /** Addition */
453 #define ADD_2V( DST, SRCA, SRCB ) \
454 do { \
455 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
456 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
457 } while (0)
458
459 /** In-place scalar multiplication */
460 #define SCALE_2V( DST, SRCA, SRCB ) \
461 do { \
462 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
463 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
464 } while (0)
465
466 /** In-place addition */
467 #define ACC_2V( DST, SRC ) \
468 do { \
469 (DST)[0] += (SRC)[0]; \
470 (DST)[1] += (SRC)[1]; \
471 } while (0)
472
473 /** Element-wise multiplication and addition */
474 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
475 do { \
476 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
477 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
478 } while (0)
479
480 /** Scalar multiplication */
481 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
482 do { \
483 (DST)[0] = S * (SRCB)[0]; \
484 (DST)[1] = S * (SRCB)[1]; \
485 } while (0)
486
487 /** In-place scalar multiplication and addition */
488 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
489 do { \
490 (DST)[0] += S * (SRCB)[0]; \
491 (DST)[1] += S * (SRCB)[1]; \
492 } while (0)
493
494 /** In-place scalar multiplication */
495 #define SELF_SCALE_SCALAR_2V( DST, S ) \
496 do { \
497 (DST)[0] *= S; \
498 (DST)[1] *= S; \
499 } while (0)
500
501 /** In-place scalar addition */
502 #define ACC_SCALAR_2V( DST, S ) \
503 do { \
504 (DST)[0] += S; \
505 (DST)[1] += S; \
506 } while (0)
507
508
509
510 /**
511 * Linear interpolation
512 *
513 * \note \p OUT argument is evaluated twice!
514 * \note Be wary of using *coord++ as an argument to any of these macros!
515 */
516 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
517
518 /* Can do better with integer math
519 */
520 #define INTERP_UB( t, dstub, outub, inub ) \
521 do { \
522 GLfloat inf = UBYTE_TO_FLOAT( inub ); \
523 GLfloat outf = UBYTE_TO_FLOAT( outub ); \
524 GLfloat dstf = LINTERP( t, outf, inf ); \
525 UNCLAMPED_FLOAT_TO_UBYTE( dstub, dstf ); \
526 } while (0)
527
528 #define INTERP_CHAN( t, dstc, outc, inc ) \
529 do { \
530 GLfloat inf = CHAN_TO_FLOAT( inc ); \
531 GLfloat outf = CHAN_TO_FLOAT( outc ); \
532 GLfloat dstf = LINTERP( t, outf, inf ); \
533 UNCLAMPED_FLOAT_TO_CHAN( dstc, dstf ); \
534 } while (0)
535
536 #define INTERP_UI( t, dstui, outui, inui ) \
537 dstui = (GLuint) (GLint) LINTERP( (t), (GLfloat) (outui), (GLfloat) (inui) )
538
539 #define INTERP_F( t, dstf, outf, inf ) \
540 dstf = LINTERP( t, outf, inf )
541
542 #define INTERP_4F( t, dst, out, in ) \
543 do { \
544 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
545 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
546 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
547 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
548 } while (0)
549
550 #define INTERP_3F( t, dst, out, in ) \
551 do { \
552 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
553 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
554 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
555 } while (0)
556
557 #define INTERP_4CHAN( t, dst, out, in ) \
558 do { \
559 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
560 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
561 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
562 INTERP_CHAN( (t), (dst)[3], (out)[3], (in)[3] ); \
563 } while (0)
564
565 #define INTERP_3CHAN( t, dst, out, in ) \
566 do { \
567 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
568 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
569 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
570 } while (0)
571
572 #define INTERP_SZ( t, vec, to, out, in, sz ) \
573 do { \
574 switch (sz) { \
575 case 4: vec[to][3] = LINTERP( (t), (vec)[out][3], (vec)[in][3] ); \
576 case 3: vec[to][2] = LINTERP( (t), (vec)[out][2], (vec)[in][2] ); \
577 case 2: vec[to][1] = LINTERP( (t), (vec)[out][1], (vec)[in][1] ); \
578 case 1: vec[to][0] = LINTERP( (t), (vec)[out][0], (vec)[in][0] ); \
579 } \
580 } while(0)
581
582
583
584 /** Assign scalers to short vectors */
585 #define ASSIGN_2V( V, V0, V1 ) \
586 do { \
587 V[0] = V0; \
588 V[1] = V1; \
589 } while(0)
590
591 /*@}*/
592
593
594
595 /** Clamp X to [MIN,MAX] */
596 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
597
598 /** Assign X to CLAMP(X, MIN, MAX) */
599 #define CLAMP_SELF(x, mn, mx) \
600 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
601
602
603
604 /** Minimum of two values: */
605 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
606
607 /** Maximum of two values: */
608 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
609
610 /** Dot product of two 2-element vectors */
611 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
612
613 /** Dot product of two 3-element vectors */
614 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
615
616 /** Dot product of two 4-element vectors */
617 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
618 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
619
620 /** Dot product of two 4-element vectors */
621 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
622
623
624 /** Cross product of two 3-element vectors */
625 #define CROSS3(n, u, v) \
626 do { \
627 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
628 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
629 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
630 } while (0)
631
632
633 /* Normalize a 3-element vector to unit length. */
634 #define NORMALIZE_3FV( V ) \
635 do { \
636 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
637 if (len) { \
638 len = INV_SQRTF(len); \
639 (V)[0] = (GLfloat) ((V)[0] * len); \
640 (V)[1] = (GLfloat) ((V)[1] * len); \
641 (V)[2] = (GLfloat) ((V)[2] * len); \
642 } \
643 } while(0)
644
645 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
646 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
647
648 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
649 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
650
651
652 /*@}*/
653
654
655 #endif