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