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