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