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