Merge branch 'mesa_7_7_branch'
[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 GLbyte in [-128,127] to GLfloat in [-1.0,1.0], texture/fb data */
58 #define BYTE_TO_FLOAT_TEX(B) ((B) == -128 ? -1.0F : (B) * (1.0F/127.0F))
59
60 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127], texture/fb data */
61 #define FLOAT_TO_BYTE_TEX(X) ( (GLint) (127.0F * (X)) )
62
63
64 /** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */
65 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
66
67 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
68 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
69
70
71 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
72 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
73
74 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */
75 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
76
77
78 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0], texture/fb data */
79 #define SHORT_TO_FLOAT_TEX(S) ((S) == -32768 ? -1.0F : (S) * (1.0F/32767.0F))
80
81 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767], texture/fb data */
82 #define FLOAT_TO_SHORT_TEX(X) ( (GLint) (32767.0F * (X)) )
83
84
85 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
86 #define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0))
87
88 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
89 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
90
91
92 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
93 #define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0))
94
95 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
96 /* causes overflow:
97 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
98 */
99 /* a close approximation: */
100 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
101
102 /** Convert GLfloat in [-1.0,1.0] to GLint64 in [-(1<<63),(1 << 63) -1] */
103 #define FLOAT_TO_INT64(X) ( (GLint64) (9223372036854775807.0 * (double)(X)) )
104
105
106 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0], texture/fb data */
107 #define INT_TO_FLOAT_TEX(I) ((I) == -2147483648 ? -1.0F : (I) * (1.0F/2147483647.0))
108
109 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647], texture/fb data */
110 #define FLOAT_TO_INT_TEX(X) ( (GLint) (2147483647.0 * (X)) )
111
112
113 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
114 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
115 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
116 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
117 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
118
119
120 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
121 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
122 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
123 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
124 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
125 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
126 us = ( (GLushort) IROUND( CLAMP((f), 0.0F, 1.0F) * 65535.0F) )
127 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
128 us = ( (GLushort) IROUND( (f) * 65535.0F) )
129
130 /*@}*/
131
132
133 /** Stepping a GLfloat pointer by a byte stride */
134 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
135 /** Stepping a GLuint pointer by a byte stride */
136 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
137 /** Stepping a GLubyte[4] pointer by a byte stride */
138 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
139 /** Stepping a GLfloat[4] pointer by a byte stride */
140 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
141 /** Stepping a GLchan[4] pointer by a byte stride */
142 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
143 /** Stepping a GLchan pointer by a byte stride */
144 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
145 /** Stepping a \p t pointer by a byte stride */
146 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
147
148
149 /**********************************************************************/
150 /** \name 4-element vector operations */
151 /*@{*/
152
153 /** Zero */
154 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
155
156 /** Test for equality */
157 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
158 (a)[1] == (b)[1] && \
159 (a)[2] == (b)[2] && \
160 (a)[3] == (b)[3])
161
162 /** Test for equality (unsigned bytes) */
163 #if defined(__i386__)
164 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
165 #else
166 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
167 #endif
168
169 /** Copy a 4-element vector */
170 #define COPY_4V( 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
178 /** Copy a 4-element vector with cast */
179 #define COPY_4V_CAST( DST, SRC, CAST ) \
180 do { \
181 (DST)[0] = (CAST)(SRC)[0]; \
182 (DST)[1] = (CAST)(SRC)[1]; \
183 (DST)[2] = (CAST)(SRC)[2]; \
184 (DST)[3] = (CAST)(SRC)[3]; \
185 } while (0)
186
187 /** Copy a 4-element unsigned byte vector */
188 #if defined(__i386__)
189 #define COPY_4UBV(DST, SRC) \
190 do { \
191 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
192 } while (0)
193 #else
194 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
195 #define COPY_4UBV(DST, SRC) \
196 do { \
197 (DST)[0] = (SRC)[0]; \
198 (DST)[1] = (SRC)[1]; \
199 (DST)[2] = (SRC)[2]; \
200 (DST)[3] = (SRC)[3]; \
201 } while (0)
202 #endif
203
204 /**
205 * Copy a 4-element float vector
206 * memcpy seems to be most efficient
207 */
208 #define COPY_4FV( DST, SRC ) \
209 do { \
210 _mesa_memcpy(DST, SRC, sizeof(GLfloat) * 4); \
211 } while (0)
212
213 /** Copy \p SZ elements into a 4-element vector */
214 #define COPY_SZ_4V(DST, SZ, SRC) \
215 do { \
216 switch (SZ) { \
217 case 4: (DST)[3] = (SRC)[3]; \
218 case 3: (DST)[2] = (SRC)[2]; \
219 case 2: (DST)[1] = (SRC)[1]; \
220 case 1: (DST)[0] = (SRC)[0]; \
221 } \
222 } while(0)
223
224 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
225 * default values to the remaining */
226 #define COPY_CLEAN_4V(DST, SZ, SRC) \
227 do { \
228 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
229 COPY_SZ_4V( DST, SZ, SRC ); \
230 } while (0)
231
232 /** Subtraction */
233 #define SUB_4V( DST, SRCA, SRCB ) \
234 do { \
235 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
236 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
237 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
238 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
239 } while (0)
240
241 /** Addition */
242 #define ADD_4V( DST, SRCA, SRCB ) \
243 do { \
244 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
245 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
246 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
247 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
248 } while (0)
249
250 /** Element-wise multiplication */
251 #define SCALE_4V( 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 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
257 } while (0)
258
259 /** In-place addition */
260 #define ACC_4V( DST, SRC ) \
261 do { \
262 (DST)[0] += (SRC)[0]; \
263 (DST)[1] += (SRC)[1]; \
264 (DST)[2] += (SRC)[2]; \
265 (DST)[3] += (SRC)[3]; \
266 } while (0)
267
268 /** Element-wise multiplication and addition */
269 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
270 do { \
271 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
272 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
273 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
274 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
275 } while (0)
276
277 /** In-place scalar multiplication and addition */
278 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
279 do { \
280 (DST)[0] += S * (SRCB)[0]; \
281 (DST)[1] += S * (SRCB)[1]; \
282 (DST)[2] += S * (SRCB)[2]; \
283 (DST)[3] += S * (SRCB)[3]; \
284 } while (0)
285
286 /** Scalar multiplication */
287 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
288 do { \
289 (DST)[0] = S * (SRCB)[0]; \
290 (DST)[1] = S * (SRCB)[1]; \
291 (DST)[2] = S * (SRCB)[2]; \
292 (DST)[3] = S * (SRCB)[3]; \
293 } while (0)
294
295 /** In-place scalar multiplication */
296 #define SELF_SCALE_SCALAR_4V( DST, S ) \
297 do { \
298 (DST)[0] *= S; \
299 (DST)[1] *= S; \
300 (DST)[2] *= S; \
301 (DST)[3] *= S; \
302 } while (0)
303
304 /** Assignment */
305 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
306 do { \
307 V[0] = V0; \
308 V[1] = V1; \
309 V[2] = V2; \
310 V[3] = V3; \
311 } while(0)
312
313 /*@}*/
314
315
316 /**********************************************************************/
317 /** \name 3-element vector operations*/
318 /*@{*/
319
320 /** Zero */
321 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
322
323 /** Test for equality */
324 #define TEST_EQ_3V(a,b) \
325 ((a)[0] == (b)[0] && \
326 (a)[1] == (b)[1] && \
327 (a)[2] == (b)[2])
328
329 /** Copy a 3-element vector */
330 #define COPY_3V( DST, SRC ) \
331 do { \
332 (DST)[0] = (SRC)[0]; \
333 (DST)[1] = (SRC)[1]; \
334 (DST)[2] = (SRC)[2]; \
335 } while (0)
336
337 /** Copy a 3-element vector with cast */
338 #define COPY_3V_CAST( DST, SRC, CAST ) \
339 do { \
340 (DST)[0] = (CAST)(SRC)[0]; \
341 (DST)[1] = (CAST)(SRC)[1]; \
342 (DST)[2] = (CAST)(SRC)[2]; \
343 } while (0)
344
345 /** Copy a 3-element float vector */
346 #define COPY_3FV( DST, SRC ) \
347 do { \
348 const GLfloat *_tmp = (SRC); \
349 (DST)[0] = _tmp[0]; \
350 (DST)[1] = _tmp[1]; \
351 (DST)[2] = _tmp[2]; \
352 } while (0)
353
354 /** Subtraction */
355 #define SUB_3V( DST, SRCA, SRCB ) \
356 do { \
357 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
358 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
359 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
360 } while (0)
361
362 /** Addition */
363 #define ADD_3V( DST, SRCA, SRCB ) \
364 do { \
365 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
366 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
367 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
368 } while (0)
369
370 /** In-place scalar multiplication */
371 #define SCALE_3V( DST, SRCA, SRCB ) \
372 do { \
373 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
374 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
375 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
376 } while (0)
377
378 /** In-place element-wise multiplication */
379 #define SELF_SCALE_3V( DST, SRC ) \
380 do { \
381 (DST)[0] *= (SRC)[0]; \
382 (DST)[1] *= (SRC)[1]; \
383 (DST)[2] *= (SRC)[2]; \
384 } while (0)
385
386 /** In-place addition */
387 #define ACC_3V( DST, SRC ) \
388 do { \
389 (DST)[0] += (SRC)[0]; \
390 (DST)[1] += (SRC)[1]; \
391 (DST)[2] += (SRC)[2]; \
392 } while (0)
393
394 /** Element-wise multiplication and addition */
395 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
396 do { \
397 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
398 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
399 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
400 } while (0)
401
402 /** Scalar multiplication */
403 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
404 do { \
405 (DST)[0] = S * (SRCB)[0]; \
406 (DST)[1] = S * (SRCB)[1]; \
407 (DST)[2] = S * (SRCB)[2]; \
408 } while (0)
409
410 /** In-place scalar multiplication and addition */
411 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
412 do { \
413 (DST)[0] += S * (SRCB)[0]; \
414 (DST)[1] += S * (SRCB)[1]; \
415 (DST)[2] += S * (SRCB)[2]; \
416 } while (0)
417
418 /** In-place scalar multiplication */
419 #define SELF_SCALE_SCALAR_3V( DST, S ) \
420 do { \
421 (DST)[0] *= S; \
422 (DST)[1] *= S; \
423 (DST)[2] *= S; \
424 } while (0)
425
426 /** In-place scalar addition */
427 #define ACC_SCALAR_3V( DST, S ) \
428 do { \
429 (DST)[0] += S; \
430 (DST)[1] += S; \
431 (DST)[2] += S; \
432 } while (0)
433
434 /** Assignment */
435 #define ASSIGN_3V( V, V0, V1, V2 ) \
436 do { \
437 V[0] = V0; \
438 V[1] = V1; \
439 V[2] = V2; \
440 } while(0)
441
442 /*@}*/
443
444
445 /**********************************************************************/
446 /** \name 2-element vector operations*/
447 /*@{*/
448
449 /** Zero */
450 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
451
452 /** Copy a 2-element vector */
453 #define COPY_2V( DST, SRC ) \
454 do { \
455 (DST)[0] = (SRC)[0]; \
456 (DST)[1] = (SRC)[1]; \
457 } while (0)
458
459 /** Copy a 2-element vector with cast */
460 #define COPY_2V_CAST( DST, SRC, CAST ) \
461 do { \
462 (DST)[0] = (CAST)(SRC)[0]; \
463 (DST)[1] = (CAST)(SRC)[1]; \
464 } while (0)
465
466 /** Copy a 2-element float vector */
467 #define COPY_2FV( DST, SRC ) \
468 do { \
469 const GLfloat *_tmp = (SRC); \
470 (DST)[0] = _tmp[0]; \
471 (DST)[1] = _tmp[1]; \
472 } while (0)
473
474 /** Subtraction */
475 #define SUB_2V( DST, SRCA, SRCB ) \
476 do { \
477 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
478 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
479 } while (0)
480
481 /** Addition */
482 #define ADD_2V( DST, SRCA, SRCB ) \
483 do { \
484 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
485 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
486 } while (0)
487
488 /** In-place scalar multiplication */
489 #define SCALE_2V( DST, SRCA, SRCB ) \
490 do { \
491 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
492 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
493 } while (0)
494
495 /** In-place addition */
496 #define ACC_2V( DST, SRC ) \
497 do { \
498 (DST)[0] += (SRC)[0]; \
499 (DST)[1] += (SRC)[1]; \
500 } while (0)
501
502 /** Element-wise multiplication and addition */
503 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
504 do { \
505 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
506 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
507 } while (0)
508
509 /** Scalar multiplication */
510 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
511 do { \
512 (DST)[0] = S * (SRCB)[0]; \
513 (DST)[1] = S * (SRCB)[1]; \
514 } while (0)
515
516 /** In-place scalar multiplication and addition */
517 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
518 do { \
519 (DST)[0] += S * (SRCB)[0]; \
520 (DST)[1] += S * (SRCB)[1]; \
521 } while (0)
522
523 /** In-place scalar multiplication */
524 #define SELF_SCALE_SCALAR_2V( DST, S ) \
525 do { \
526 (DST)[0] *= S; \
527 (DST)[1] *= S; \
528 } while (0)
529
530 /** In-place scalar addition */
531 #define ACC_SCALAR_2V( DST, S ) \
532 do { \
533 (DST)[0] += S; \
534 (DST)[1] += S; \
535 } while (0)
536
537 /** Assign scalers to short vectors */
538 #define ASSIGN_2V( V, V0, V1 ) \
539 do { \
540 V[0] = V0; \
541 V[1] = V1; \
542 } while(0)
543
544 /*@}*/
545
546
547 /** \name Linear interpolation macros */
548 /*@{*/
549
550 /**
551 * Linear interpolation
552 *
553 * \note \p OUT argument is evaluated twice!
554 * \note Be wary of using *coord++ as an argument to any of these macros!
555 */
556 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
557
558 /* Can do better with integer math
559 */
560 #define INTERP_UB( t, dstub, outub, inub ) \
561 do { \
562 GLfloat inf = UBYTE_TO_FLOAT( inub ); \
563 GLfloat outf = UBYTE_TO_FLOAT( outub ); \
564 GLfloat dstf = LINTERP( t, outf, inf ); \
565 UNCLAMPED_FLOAT_TO_UBYTE( dstub, dstf ); \
566 } while (0)
567
568 #define INTERP_CHAN( t, dstc, outc, inc ) \
569 do { \
570 GLfloat inf = CHAN_TO_FLOAT( inc ); \
571 GLfloat outf = CHAN_TO_FLOAT( outc ); \
572 GLfloat dstf = LINTERP( t, outf, inf ); \
573 UNCLAMPED_FLOAT_TO_CHAN( dstc, dstf ); \
574 } while (0)
575
576 #define INTERP_UI( t, dstui, outui, inui ) \
577 dstui = (GLuint) (GLint) LINTERP( (t), (GLfloat) (outui), (GLfloat) (inui) )
578
579 #define INTERP_F( t, dstf, outf, inf ) \
580 dstf = LINTERP( t, outf, inf )
581
582 #define INTERP_4F( t, dst, out, in ) \
583 do { \
584 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
585 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
586 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
587 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
588 } while (0)
589
590 #define INTERP_3F( t, dst, out, in ) \
591 do { \
592 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
593 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
594 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
595 } while (0)
596
597 #define INTERP_4CHAN( t, dst, out, in ) \
598 do { \
599 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
600 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
601 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
602 INTERP_CHAN( (t), (dst)[3], (out)[3], (in)[3] ); \
603 } while (0)
604
605 #define INTERP_3CHAN( t, dst, out, in ) \
606 do { \
607 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
608 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
609 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
610 } while (0)
611
612 #define INTERP_SZ( t, vec, to, out, in, sz ) \
613 do { \
614 switch (sz) { \
615 case 4: vec[to][3] = LINTERP( (t), (vec)[out][3], (vec)[in][3] ); \
616 case 3: vec[to][2] = LINTERP( (t), (vec)[out][2], (vec)[in][2] ); \
617 case 2: vec[to][1] = LINTERP( (t), (vec)[out][1], (vec)[in][1] ); \
618 case 1: vec[to][0] = LINTERP( (t), (vec)[out][0], (vec)[in][0] ); \
619 } \
620 } while(0)
621
622 /*@}*/
623
624
625
626 /** Clamp X to [MIN,MAX] */
627 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
628
629 /** Minimum of two values: */
630 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
631
632 /** Maximum of two values: */
633 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
634
635 /** Dot product of two 2-element vectors */
636 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
637
638 /** Dot product of two 3-element vectors */
639 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
640
641 /** Dot product of two 4-element vectors */
642 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
643 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
644
645 /** Dot product of two 4-element vectors */
646 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
647
648
649 /** Cross product of two 3-element vectors */
650 #define CROSS3(n, u, v) \
651 do { \
652 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
653 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
654 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
655 } while (0)
656
657
658 /* Normalize a 3-element vector to unit length. */
659 #define NORMALIZE_3FV( V ) \
660 do { \
661 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
662 if (len) { \
663 len = INV_SQRTF(len); \
664 (V)[0] = (GLfloat) ((V)[0] * len); \
665 (V)[1] = (GLfloat) ((V)[1] * len); \
666 (V)[2] = (GLfloat) ((V)[2] * len); \
667 } \
668 } while(0)
669
670 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
671 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
672
673 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
674 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
675
676
677 /** casts to silence warnings with some compilers */
678 #define ENUM_TO_INT(E) ((GLint)(E))
679 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
680 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
681 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
682
683
684 #endif