macros.h revision ec6478fd322646ae4f6ae20eed8d9c14ea503dfc
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) */
203static inline GLboolean
204TEST_EQ_4UBV(const GLubyte a[4], const GLubyte b[4])
205{
206#if defined(__i386__)
207   return *((const GLuint *) a) == *((const GLuint *) b);
208#else
209   return TEST_EQ_4V(a, b);
210#endif
211}
212
213
214/** Copy a 4-element vector */
215#define COPY_4V( DST, SRC )         \
216do {                                \
217   (DST)[0] = (SRC)[0];             \
218   (DST)[1] = (SRC)[1];             \
219   (DST)[2] = (SRC)[2];             \
220   (DST)[3] = (SRC)[3];             \
221} while (0)
222
223/** Copy a 4-element unsigned byte vector */
224static inline void
225COPY_4UBV(GLubyte dst[4], const GLubyte src[4])
226{
227#if defined(__i386__)
228   *((GLuint *) dst) = *((GLuint *) src);
229#else
230   /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
231   COPY_4V(dst, src);
232#endif
233}
234
235/** Copy a 4-element float vector */
236static inline void
237COPY_4FV(GLfloat dst[4], const GLfloat src[4])
238{
239   /* memcpy seems to be most efficient */
240   memcpy(dst, src, sizeof(GLfloat) * 4);
241}
242
243/** Copy \p SZ elements into a 4-element vector */
244#define COPY_SZ_4V(DST, SZ, SRC)  \
245do {                              \
246   switch (SZ) {                  \
247   case 4: (DST)[3] = (SRC)[3];   \
248   case 3: (DST)[2] = (SRC)[2];   \
249   case 2: (DST)[1] = (SRC)[1];   \
250   case 1: (DST)[0] = (SRC)[0];   \
251   }                              \
252} while(0)
253
254/** Copy \p SZ elements into a homegeneous (4-element) vector, giving
255 * default values to the remaining */
256#define COPY_CLEAN_4V(DST, SZ, SRC)  \
257do {                                 \
258      ASSIGN_4V( DST, 0, 0, 0, 1 );  \
259      COPY_SZ_4V( DST, SZ, SRC );    \
260} while (0)
261
262/** Subtraction */
263#define SUB_4V( DST, SRCA, SRCB )           \
264do {                                        \
265      (DST)[0] = (SRCA)[0] - (SRCB)[0];     \
266      (DST)[1] = (SRCA)[1] - (SRCB)[1];     \
267      (DST)[2] = (SRCA)[2] - (SRCB)[2];     \
268      (DST)[3] = (SRCA)[3] - (SRCB)[3];     \
269} while (0)
270
271/** Addition */
272#define ADD_4V( DST, SRCA, SRCB )           \
273do {                                        \
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/** Element-wise multiplication */
281#define SCALE_4V( DST, SRCA, SRCB )         \
282do {                                        \
283      (DST)[0] = (SRCA)[0] * (SRCB)[0];     \
284      (DST)[1] = (SRCA)[1] * (SRCB)[1];     \
285      (DST)[2] = (SRCA)[2] * (SRCB)[2];     \
286      (DST)[3] = (SRCA)[3] * (SRCB)[3];     \
287} while (0)
288
289/** In-place addition */
290#define ACC_4V( DST, SRC )          \
291do {                                \
292      (DST)[0] += (SRC)[0];         \
293      (DST)[1] += (SRC)[1];         \
294      (DST)[2] += (SRC)[2];         \
295      (DST)[3] += (SRC)[3];         \
296} while (0)
297
298/** Element-wise multiplication and addition */
299#define ACC_SCALE_4V( DST, SRCA, SRCB )     \
300do {                                        \
301      (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
302      (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
303      (DST)[2] += (SRCA)[2] * (SRCB)[2];    \
304      (DST)[3] += (SRCA)[3] * (SRCB)[3];    \
305} while (0)
306
307/** In-place scalar multiplication and addition */
308#define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
309do {                                        \
310      (DST)[0] += S * (SRCB)[0];            \
311      (DST)[1] += S * (SRCB)[1];            \
312      (DST)[2] += S * (SRCB)[2];            \
313      (DST)[3] += S * (SRCB)[3];            \
314} while (0)
315
316/** Scalar multiplication */
317#define SCALE_SCALAR_4V( DST, S, SRCB ) \
318do {                                    \
319      (DST)[0] = S * (SRCB)[0];         \
320      (DST)[1] = S * (SRCB)[1];         \
321      (DST)[2] = S * (SRCB)[2];         \
322      (DST)[3] = S * (SRCB)[3];         \
323} while (0)
324
325/** In-place scalar multiplication */
326#define SELF_SCALE_SCALAR_4V( DST, S ) \
327do {                                   \
328      (DST)[0] *= S;                   \
329      (DST)[1] *= S;                   \
330      (DST)[2] *= S;                   \
331      (DST)[3] *= S;                   \
332} while (0)
333
334/** Assignment */
335#define ASSIGN_4V( V, V0, V1, V2, V3 )  \
336do {                                    \
337    V[0] = V0;                          \
338    V[1] = V1;                          \
339    V[2] = V2;                          \
340    V[3] = V3;                          \
341} while(0)
342
343/*@}*/
344
345
346/**********************************************************************/
347/** \name 3-element vector operations*/
348/*@{*/
349
350/** Zero */
351#define ZERO_3V( DST )  (DST)[0] = (DST)[1] = (DST)[2] = 0
352
353/** Test for equality */
354#define TEST_EQ_3V(a,b)  \
355   ((a)[0] == (b)[0] &&  \
356    (a)[1] == (b)[1] &&  \
357    (a)[2] == (b)[2])
358
359/** Copy a 3-element vector */
360#define COPY_3V( DST, SRC )         \
361do {                                \
362   (DST)[0] = (SRC)[0];             \
363   (DST)[1] = (SRC)[1];             \
364   (DST)[2] = (SRC)[2];             \
365} while (0)
366
367/** Copy a 3-element vector with cast */
368#define COPY_3V_CAST( DST, SRC, CAST )  \
369do {                                    \
370   (DST)[0] = (CAST)(SRC)[0];           \
371   (DST)[1] = (CAST)(SRC)[1];           \
372   (DST)[2] = (CAST)(SRC)[2];           \
373} while (0)
374
375/** Copy a 3-element float vector */
376#define COPY_3FV( DST, SRC )        \
377do {                                \
378   const GLfloat *_tmp = (SRC);     \
379   (DST)[0] = _tmp[0];              \
380   (DST)[1] = _tmp[1];              \
381   (DST)[2] = _tmp[2];              \
382} while (0)
383
384/** Subtraction */
385#define SUB_3V( DST, SRCA, SRCB )        \
386do {                                     \
387      (DST)[0] = (SRCA)[0] - (SRCB)[0];  \
388      (DST)[1] = (SRCA)[1] - (SRCB)[1];  \
389      (DST)[2] = (SRCA)[2] - (SRCB)[2];  \
390} while (0)
391
392/** Addition */
393#define ADD_3V( DST, SRCA, SRCB )       \
394do {                                    \
395      (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
396      (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
397      (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
398} while (0)
399
400/** In-place scalar multiplication */
401#define SCALE_3V( DST, SRCA, SRCB )     \
402do {                                    \
403      (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
404      (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
405      (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
406} while (0)
407
408/** In-place element-wise multiplication */
409#define SELF_SCALE_3V( DST, SRC )   \
410do {                                \
411      (DST)[0] *= (SRC)[0];         \
412      (DST)[1] *= (SRC)[1];         \
413      (DST)[2] *= (SRC)[2];         \
414} while (0)
415
416/** In-place addition */
417#define ACC_3V( DST, SRC )          \
418do {                                \
419      (DST)[0] += (SRC)[0];         \
420      (DST)[1] += (SRC)[1];         \
421      (DST)[2] += (SRC)[2];         \
422} while (0)
423
424/** Element-wise multiplication and addition */
425#define ACC_SCALE_3V( DST, SRCA, SRCB )     \
426do {                                        \
427      (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
428      (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
429      (DST)[2] += (SRCA)[2] * (SRCB)[2];    \
430} while (0)
431
432/** Scalar multiplication */
433#define SCALE_SCALAR_3V( DST, S, SRCB ) \
434do {                                    \
435      (DST)[0] = S * (SRCB)[0];         \
436      (DST)[1] = S * (SRCB)[1];         \
437      (DST)[2] = S * (SRCB)[2];         \
438} while (0)
439
440/** In-place scalar multiplication and addition */
441#define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
442do {                                        \
443      (DST)[0] += S * (SRCB)[0];            \
444      (DST)[1] += S * (SRCB)[1];            \
445      (DST)[2] += S * (SRCB)[2];            \
446} while (0)
447
448/** In-place scalar multiplication */
449#define SELF_SCALE_SCALAR_3V( DST, S ) \
450do {                                   \
451      (DST)[0] *= S;                   \
452      (DST)[1] *= S;                   \
453      (DST)[2] *= S;                   \
454} while (0)
455
456/** In-place scalar addition */
457#define ACC_SCALAR_3V( DST, S )     \
458do {                                \
459      (DST)[0] += S;                \
460      (DST)[1] += S;                \
461      (DST)[2] += S;                \
462} while (0)
463
464/** Assignment */
465#define ASSIGN_3V( V, V0, V1, V2 )  \
466do {                                \
467    V[0] = V0;                      \
468    V[1] = V1;                      \
469    V[2] = V2;                      \
470} while(0)
471
472/*@}*/
473
474
475/**********************************************************************/
476/** \name 2-element vector operations*/
477/*@{*/
478
479/** Zero */
480#define ZERO_2V( DST )  (DST)[0] = (DST)[1] = 0
481
482/** Copy a 2-element vector */
483#define COPY_2V( DST, SRC )         \
484do {                        \
485   (DST)[0] = (SRC)[0];             \
486   (DST)[1] = (SRC)[1];             \
487} while (0)
488
489/** Copy a 2-element vector with cast */
490#define COPY_2V_CAST( DST, SRC, CAST )      \
491do {                        \
492   (DST)[0] = (CAST)(SRC)[0];           \
493   (DST)[1] = (CAST)(SRC)[1];           \
494} while (0)
495
496/** Copy a 2-element float vector */
497#define COPY_2FV( DST, SRC )            \
498do {                        \
499   const GLfloat *_tmp = (SRC);         \
500   (DST)[0] = _tmp[0];              \
501   (DST)[1] = _tmp[1];              \
502} while (0)
503
504/** Subtraction */
505#define SUB_2V( DST, SRCA, SRCB )       \
506do {                        \
507      (DST)[0] = (SRCA)[0] - (SRCB)[0];     \
508      (DST)[1] = (SRCA)[1] - (SRCB)[1];     \
509} while (0)
510
511/** Addition */
512#define ADD_2V( DST, SRCA, SRCB )       \
513do {                        \
514      (DST)[0] = (SRCA)[0] + (SRCB)[0];     \
515      (DST)[1] = (SRCA)[1] + (SRCB)[1];     \
516} while (0)
517
518/** In-place scalar multiplication */
519#define SCALE_2V( DST, SRCA, SRCB )     \
520do {                        \
521      (DST)[0] = (SRCA)[0] * (SRCB)[0];     \
522      (DST)[1] = (SRCA)[1] * (SRCB)[1];     \
523} while (0)
524
525/** In-place addition */
526#define ACC_2V( DST, SRC )          \
527do {                        \
528      (DST)[0] += (SRC)[0];         \
529      (DST)[1] += (SRC)[1];         \
530} while (0)
531
532/** Element-wise multiplication and addition */
533#define ACC_SCALE_2V( DST, SRCA, SRCB )     \
534do {                        \
535      (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
536      (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
537} while (0)
538
539/** Scalar multiplication */
540#define SCALE_SCALAR_2V( DST, S, SRCB )     \
541do {                        \
542      (DST)[0] = S * (SRCB)[0];         \
543      (DST)[1] = S * (SRCB)[1];         \
544} while (0)
545
546/** In-place scalar multiplication and addition */
547#define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
548do {                        \
549      (DST)[0] += S * (SRCB)[0];        \
550      (DST)[1] += S * (SRCB)[1];        \
551} while (0)
552
553/** In-place scalar multiplication */
554#define SELF_SCALE_SCALAR_2V( DST, S )      \
555do {                        \
556      (DST)[0] *= S;                \
557      (DST)[1] *= S;                \
558} while (0)
559
560/** In-place scalar addition */
561#define ACC_SCALAR_2V( DST, S )         \
562do {                        \
563      (DST)[0] += S;                \
564      (DST)[1] += S;                \
565} while (0)
566
567/** Assign scalers to short vectors */
568#define ASSIGN_2V( V, V0, V1 )	\
569do {				\
570    V[0] = V0;			\
571    V[1] = V1;			\
572} while(0)
573
574/*@}*/
575
576
577/** \name Linear interpolation functions */
578/*@{*/
579
580static inline GLfloat
581LINTERP(GLfloat t, GLfloat out, GLfloat in)
582{
583   return out + t * (in - out);
584}
585
586static inline void
587INTERP_3F(GLfloat t, GLfloat dst[3], const GLfloat out[3], const GLfloat in[3])
588{
589   dst[0] = LINTERP( t, out[0], in[0] );
590   dst[1] = LINTERP( t, out[1], in[1] );
591   dst[2] = LINTERP( t, out[2], in[2] );
592}
593
594static inline void
595INTERP_4F(GLfloat t, GLfloat dst[4], const GLfloat out[4], const GLfloat in[4])
596{
597   dst[0] = LINTERP( t, out[0], in[0] );
598   dst[1] = LINTERP( t, out[1], in[1] );
599   dst[2] = LINTERP( t, out[2], in[2] );
600   dst[3] = LINTERP( t, out[3], in[3] );
601}
602
603/*@}*/
604
605
606
607/** Clamp X to [MIN,MAX] */
608#define CLAMP( X, MIN, MAX )  ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
609
610/** Minimum of two values: */
611#define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
612
613/** Maximum of two values: */
614#define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
615
616/** Minimum and maximum of three values: */
617#define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
618#define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
619
620
621
622/** Cross product of two 3-element vectors */
623static inline void
624CROSS3(GLfloat n[3], const GLfloat u[3], const GLfloat v[3])
625{
626   n[0] = u[1] * v[2] - u[2] * v[1];
627   n[1] = u[2] * v[0] - u[0] * v[2];
628   n[2] = u[0] * v[1] - u[1] * v[0];
629}
630
631
632/** Dot product of two 2-element vectors */
633static inline GLfloat
634DOT2(const GLfloat a[2], const GLfloat b[2])
635{
636   return a[0] * b[0] + a[1] * b[1];
637}
638
639static inline GLfloat
640DOT3(const GLfloat a[3], const GLfloat b[3])
641{
642   return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
643}
644
645static inline GLfloat
646DOT4(const GLfloat a[4], const GLfloat b[4])
647{
648   return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
649}
650
651
652static inline GLfloat
653LEN_SQUARED_3FV(const GLfloat v[3])
654{
655   return DOT3(v, v);
656}
657
658static inline GLfloat
659LEN_SQUARED_2FV(const GLfloat v[2])
660{
661   return DOT2(v, v);
662}
663
664
665static inline GLfloat
666LEN_3FV(const GLfloat v[3])
667{
668   return SQRTF(LEN_SQUARED_3FV(v));
669}
670
671static inline GLfloat
672LEN_2FV(const GLfloat v[2])
673{
674   return SQRTF(LEN_SQUARED_2FV(v));
675}
676
677
678/* Normalize a 3-element vector to unit length. */
679static inline void
680NORMALIZE_3FV(GLfloat v[3])
681{
682   GLfloat len = (GLfloat) LEN_SQUARED_3FV(v);
683   if (len) {
684      len = INV_SQRTF(len);
685      v[0] *= len;
686      v[1] *= len;
687      v[2] *= len;
688   }
689}
690
691
692/** Compute ceiling of integer quotient of A divided by B. */
693#define CEILING( A, B )  ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
694
695
696/** casts to silence warnings with some compilers */
697#define ENUM_TO_INT(E)     ((GLint)(E))
698#define ENUM_TO_FLOAT(E)   ((GLfloat)(GLint)(E))
699#define ENUM_TO_DOUBLE(E)  ((GLdouble)(GLint)(E))
700#define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
701
702
703#endif
704