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