1/**
2 * \file macros.h
3 * A collection of useful macros.
4 */
5
6/*
7 * Mesa 3-D graphics library
8 *
9 * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31#ifndef MACROS_H
32#define MACROS_H
33
34#include "util/macros.h"
35#include "util/u_math.h"
36#include "util/rounding.h"
37#include "imports.h"
38
39
40/**
41 * \name Integer / float conversion for colors, normals, etc.
42 */
43/*@{*/
44
45/** Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
46extern GLfloat _mesa_ubyte_to_float_color_tab[256];
47#define UBYTE_TO_FLOAT(u) _mesa_ubyte_to_float_color_tab[(unsigned int)(u)]
48
49/** Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
50#define FLOAT_TO_UBYTE(X)   ((GLubyte) (GLint) ((X) * 255.0F))
51
52
53/** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
54#define BYTE_TO_FLOAT(B)    ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
55
56/** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
57#define FLOAT_TO_BYTE(X)    ( (((GLint) (255.0F * (X))) - 1) / 2 )
58
59
60/** Convert GLbyte to GLfloat while preserving zero */
61#define BYTE_TO_FLOATZ(B)   ((B) == 0 ? 0.0F : BYTE_TO_FLOAT(B))
62
63
64/** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0], texture/fb data */
65#define BYTE_TO_FLOAT_TEX(B)    ((B) == -128 ? -1.0F : (B) * (1.0F/127.0F))
66
67/** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127], texture/fb data */
68#define FLOAT_TO_BYTE_TEX(X)    CLAMP( (GLint) (127.0F * (X)), -128, 127 )
69
70/** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */
71#define USHORT_TO_FLOAT(S)  ((GLfloat) (S) * (1.0F / 65535.0F))
72
73/** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
74#define FLOAT_TO_USHORT(X)   ((GLuint) ((X) * 65535.0F))
75
76
77/** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
78#define SHORT_TO_FLOAT(S)   ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
79
80/** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */
81#define FLOAT_TO_SHORT(X)   ( (((GLint) (65535.0F * (X))) - 1) / 2 )
82
83/** Convert GLshort to GLfloat while preserving zero */
84#define SHORT_TO_FLOATZ(S)   ((S) == 0 ? 0.0F : SHORT_TO_FLOAT(S))
85
86
87/** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0], texture/fb data */
88#define SHORT_TO_FLOAT_TEX(S)    ((S) == -32768 ? -1.0F : (S) * (1.0F/32767.0F))
89
90/** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767], texture/fb data */
91#define FLOAT_TO_SHORT_TEX(X)    ( (GLint) (32767.0F * (X)) )
92
93
94/** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
95#define UINT_TO_FLOAT(U)    ((GLfloat) ((U) * (1.0F / 4294967295.0)))
96
97/** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
98#define FLOAT_TO_UINT(X)    ((GLuint) ((X) * 4294967295.0))
99
100
101/** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
102#define INT_TO_FLOAT(I)     ((GLfloat) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0)))
103
104/** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
105/* causes overflow:
106#define FLOAT_TO_INT(X)     ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
107*/
108/* a close approximation: */
109#define FLOAT_TO_INT(X)     ( (GLint) (2147483647.0 * (X)) )
110
111/** Convert GLfloat in [-1.0,1.0] to GLint64 in [-(1<<63),(1 << 63) -1] */
112#define FLOAT_TO_INT64(X)     ( (GLint64) (9223372036854775807.0 * (double)(X)) )
113
114
115/** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0], texture/fb data */
116#define INT_TO_FLOAT_TEX(I)    ((I) == -2147483648 ? -1.0F : (I) * (1.0F/2147483647.0))
117
118/** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647], texture/fb data */
119#define FLOAT_TO_INT_TEX(X)    ( (GLint) (2147483647.0 * (X)) )
120
121
122#define BYTE_TO_UBYTE(b)   ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
123#define SHORT_TO_UBYTE(s)  ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
124#define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
125#define INT_TO_UBYTE(i)    ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
126#define UINT_TO_UBYTE(i)   ((GLubyte) ((i) >> 24))
127
128
129#define BYTE_TO_USHORT(b)  ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
130#define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
131#define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
132#define INT_TO_USHORT(i)   ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
133#define UINT_TO_USHORT(i)  ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
134#define UNCLAMPED_FLOAT_TO_USHORT(us, f)  \
135        us = ( (GLushort) _mesa_lroundevenf( CLAMP((f), 0.0F, 1.0F) * 65535.0F) )
136#define CLAMPED_FLOAT_TO_USHORT(us, f)  \
137        us = ( (GLushort) _mesa_lroundevenf( (f) * 65535.0F) )
138
139#define UNCLAMPED_FLOAT_TO_SHORT(s, f)  \
140        s = ( (GLshort) _mesa_lroundevenf( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
141
142/***
143 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
144 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
145 ***/
146#ifndef DEBUG
147/* This function/macro is sensitive to precision.  Test very carefully
148 * if you change it!
149 */
150#define UNCLAMPED_FLOAT_TO_UBYTE(UB, FLT)				\
151        do {								\
152           fi_type __tmp;						\
153           __tmp.f = (FLT);						\
154           if (__tmp.i < 0)						\
155              UB = (GLubyte) 0;						\
156           else if (__tmp.i >= IEEE_ONE)				\
157              UB = (GLubyte) 255;					\
158           else {							\
159              __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F;		\
160              UB = (GLubyte) __tmp.i;					\
161           }								\
162        } while (0)
163#define CLAMPED_FLOAT_TO_UBYTE(UB, FLT)					\
164        do {								\
165           fi_type __tmp;						\
166           __tmp.f = (FLT) * (255.0F/256.0F) + 32768.0F;		\
167           UB = (GLubyte) __tmp.i;					\
168        } while (0)
169#else
170#define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
171	ub = ((GLubyte) _mesa_lroundevenf(CLAMP((f), 0.0F, 1.0F) * 255.0F))
172#define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
173	ub = ((GLubyte) _mesa_lroundevenf((f) * 255.0F))
174#endif
175
176static fi_type UINT_AS_UNION(GLuint u)
177{
178   fi_type tmp;
179   tmp.u = u;
180   return tmp;
181}
182
183static inline fi_type INT_AS_UNION(GLint i)
184{
185   fi_type tmp;
186   tmp.i = i;
187   return tmp;
188}
189
190static inline fi_type FLOAT_AS_UNION(GLfloat f)
191{
192   fi_type tmp;
193   tmp.f = f;
194   return tmp;
195}
196
197/**
198 * Convert a floating point value to an unsigned fixed point value.
199 *
200 * \param frac_bits   The number of bits used to store the fractional part.
201 */
202static inline uint32_t
203U_FIXED(float value, uint32_t frac_bits)
204{
205   value *= (1 << frac_bits);
206   return value < 0.0f ? 0 : (uint32_t) value;
207}
208
209/**
210 * Convert a floating point value to an signed fixed point value.
211 *
212 * \param frac_bits   The number of bits used to store the fractional part.
213 */
214static inline int32_t
215S_FIXED(float value, uint32_t frac_bits)
216{
217   return (int32_t) (value * (1 << frac_bits));
218}
219/*@}*/
220
221
222/** Stepping a GLfloat pointer by a byte stride */
223#define STRIDE_F(p, i)  (p = (GLfloat *)((GLubyte *)p + i))
224/** Stepping a GLuint pointer by a byte stride */
225#define STRIDE_UI(p, i)  (p = (GLuint *)((GLubyte *)p + i))
226/** Stepping a GLubyte[4] pointer by a byte stride */
227#define STRIDE_4UB(p, i)  (p = (GLubyte (*)[4])((GLubyte *)p + i))
228/** Stepping a GLfloat[4] pointer by a byte stride */
229#define STRIDE_4F(p, i)  (p = (GLfloat (*)[4])((GLubyte *)p + i))
230/** Stepping a \p t pointer by a byte stride */
231#define STRIDE_T(p, t, i)  (p = (t)((GLubyte *)p + i))
232
233
234/**********************************************************************/
235/** \name 4-element vector operations */
236/*@{*/
237
238/** Zero */
239#define ZERO_4V( DST )  (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
240
241/** Test for equality */
242#define TEST_EQ_4V(a,b)  ((a)[0] == (b)[0] &&   \
243              (a)[1] == (b)[1] &&   \
244              (a)[2] == (b)[2] &&   \
245              (a)[3] == (b)[3])
246
247/** Test for equality (unsigned bytes) */
248static inline GLboolean
249TEST_EQ_4UBV(const GLubyte a[4], const GLubyte b[4])
250{
251#if defined(__i386__)
252   return *((const GLuint *) a) == *((const GLuint *) b);
253#else
254   return TEST_EQ_4V(a, b);
255#endif
256}
257
258
259/** Copy a 4-element vector */
260#define COPY_4V( DST, SRC )         \
261do {                                \
262   (DST)[0] = (SRC)[0];             \
263   (DST)[1] = (SRC)[1];             \
264   (DST)[2] = (SRC)[2];             \
265   (DST)[3] = (SRC)[3];             \
266} while (0)
267
268/** Copy a 4-element unsigned byte vector */
269static inline void
270COPY_4UBV(GLubyte dst[4], const GLubyte src[4])
271{
272#if defined(__i386__)
273   *((GLuint *) dst) = *((GLuint *) src);
274#else
275   /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
276   COPY_4V(dst, src);
277#endif
278}
279
280/** Copy \p SZ elements into a 4-element vector */
281#define COPY_SZ_4V(DST, SZ, SRC)  \
282do {                              \
283   switch (SZ) {                  \
284   case 4: (DST)[3] = (SRC)[3];   \
285   case 3: (DST)[2] = (SRC)[2];   \
286   case 2: (DST)[1] = (SRC)[1];   \
287   case 1: (DST)[0] = (SRC)[0];   \
288   }                              \
289} while(0)
290
291/** Copy \p SZ elements into a homegeneous (4-element) vector, giving
292 * default values to the remaining */
293#define COPY_CLEAN_4V(DST, SZ, SRC)  \
294do {                                 \
295      ASSIGN_4V( DST, 0, 0, 0, 1 );  \
296      COPY_SZ_4V( DST, SZ, SRC );    \
297} while (0)
298
299/** Subtraction */
300#define SUB_4V( DST, SRCA, SRCB )           \
301do {                                        \
302      (DST)[0] = (SRCA)[0] - (SRCB)[0];     \
303      (DST)[1] = (SRCA)[1] - (SRCB)[1];     \
304      (DST)[2] = (SRCA)[2] - (SRCB)[2];     \
305      (DST)[3] = (SRCA)[3] - (SRCB)[3];     \
306} while (0)
307
308/** Addition */
309#define ADD_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/** Element-wise multiplication */
318#define SCALE_4V( DST, SRCA, SRCB )         \
319do {                                        \
320      (DST)[0] = (SRCA)[0] * (SRCB)[0];     \
321      (DST)[1] = (SRCA)[1] * (SRCB)[1];     \
322      (DST)[2] = (SRCA)[2] * (SRCB)[2];     \
323      (DST)[3] = (SRCA)[3] * (SRCB)[3];     \
324} while (0)
325
326/** In-place addition */
327#define ACC_4V( DST, SRC )          \
328do {                                \
329      (DST)[0] += (SRC)[0];         \
330      (DST)[1] += (SRC)[1];         \
331      (DST)[2] += (SRC)[2];         \
332      (DST)[3] += (SRC)[3];         \
333} while (0)
334
335/** Element-wise multiplication and addition */
336#define ACC_SCALE_4V( DST, SRCA, SRCB )     \
337do {                                        \
338      (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
339      (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
340      (DST)[2] += (SRCA)[2] * (SRCB)[2];    \
341      (DST)[3] += (SRCA)[3] * (SRCB)[3];    \
342} while (0)
343
344/** In-place scalar multiplication and addition */
345#define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
346do {                                        \
347      (DST)[0] += S * (SRCB)[0];            \
348      (DST)[1] += S * (SRCB)[1];            \
349      (DST)[2] += S * (SRCB)[2];            \
350      (DST)[3] += S * (SRCB)[3];            \
351} while (0)
352
353/** Scalar multiplication */
354#define SCALE_SCALAR_4V( DST, S, SRCB ) \
355do {                                    \
356      (DST)[0] = S * (SRCB)[0];         \
357      (DST)[1] = S * (SRCB)[1];         \
358      (DST)[2] = S * (SRCB)[2];         \
359      (DST)[3] = S * (SRCB)[3];         \
360} while (0)
361
362/** In-place scalar multiplication */
363#define SELF_SCALE_SCALAR_4V( DST, S ) \
364do {                                   \
365      (DST)[0] *= S;                   \
366      (DST)[1] *= S;                   \
367      (DST)[2] *= S;                   \
368      (DST)[3] *= S;                   \
369} while (0)
370
371/*@}*/
372
373
374/**********************************************************************/
375/** \name 3-element vector operations*/
376/*@{*/
377
378/** Zero */
379#define ZERO_3V( DST )  (DST)[0] = (DST)[1] = (DST)[2] = 0
380
381/** Test for equality */
382#define TEST_EQ_3V(a,b)  \
383   ((a)[0] == (b)[0] &&  \
384    (a)[1] == (b)[1] &&  \
385    (a)[2] == (b)[2])
386
387/** Copy a 3-element vector */
388#define COPY_3V( DST, SRC )         \
389do {                                \
390   (DST)[0] = (SRC)[0];             \
391   (DST)[1] = (SRC)[1];             \
392   (DST)[2] = (SRC)[2];             \
393} while (0)
394
395/** Copy a 3-element vector with cast */
396#define COPY_3V_CAST( DST, SRC, CAST )  \
397do {                                    \
398   (DST)[0] = (CAST)(SRC)[0];           \
399   (DST)[1] = (CAST)(SRC)[1];           \
400   (DST)[2] = (CAST)(SRC)[2];           \
401} while (0)
402
403/** Copy a 3-element float vector */
404#define COPY_3FV( DST, SRC )        \
405do {                                \
406   const GLfloat *_tmp = (SRC);     \
407   (DST)[0] = _tmp[0];              \
408   (DST)[1] = _tmp[1];              \
409   (DST)[2] = _tmp[2];              \
410} while (0)
411
412/** Subtraction */
413#define SUB_3V( DST, SRCA, SRCB )        \
414do {                                     \
415      (DST)[0] = (SRCA)[0] - (SRCB)[0];  \
416      (DST)[1] = (SRCA)[1] - (SRCB)[1];  \
417      (DST)[2] = (SRCA)[2] - (SRCB)[2];  \
418} while (0)
419
420/** Addition */
421#define ADD_3V( DST, SRCA, SRCB )       \
422do {                                    \
423      (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
424      (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
425      (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
426} while (0)
427
428/** In-place scalar multiplication */
429#define SCALE_3V( DST, SRCA, SRCB )     \
430do {                                    \
431      (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
432      (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
433      (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
434} while (0)
435
436/** In-place element-wise multiplication */
437#define SELF_SCALE_3V( DST, SRC )   \
438do {                                \
439      (DST)[0] *= (SRC)[0];         \
440      (DST)[1] *= (SRC)[1];         \
441      (DST)[2] *= (SRC)[2];         \
442} while (0)
443
444/** In-place addition */
445#define ACC_3V( DST, SRC )          \
446do {                                \
447      (DST)[0] += (SRC)[0];         \
448      (DST)[1] += (SRC)[1];         \
449      (DST)[2] += (SRC)[2];         \
450} while (0)
451
452/** Element-wise multiplication and addition */
453#define ACC_SCALE_3V( DST, SRCA, SRCB )     \
454do {                                        \
455      (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
456      (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
457      (DST)[2] += (SRCA)[2] * (SRCB)[2];    \
458} while (0)
459
460/** Scalar multiplication */
461#define SCALE_SCALAR_3V( DST, S, SRCB ) \
462do {                                    \
463      (DST)[0] = S * (SRCB)[0];         \
464      (DST)[1] = S * (SRCB)[1];         \
465      (DST)[2] = S * (SRCB)[2];         \
466} while (0)
467
468/** In-place scalar multiplication and addition */
469#define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
470do {                                        \
471      (DST)[0] += S * (SRCB)[0];            \
472      (DST)[1] += S * (SRCB)[1];            \
473      (DST)[2] += S * (SRCB)[2];            \
474} while (0)
475
476/** In-place scalar multiplication */
477#define SELF_SCALE_SCALAR_3V( DST, S ) \
478do {                                   \
479      (DST)[0] *= S;                   \
480      (DST)[1] *= S;                   \
481      (DST)[2] *= S;                   \
482} while (0)
483
484/** In-place scalar addition */
485#define ACC_SCALAR_3V( DST, S )     \
486do {                                \
487      (DST)[0] += S;                \
488      (DST)[1] += S;                \
489      (DST)[2] += S;                \
490} while (0)
491
492/** Assignment */
493#define ASSIGN_3V( V, V0, V1, V2 )  \
494do {                                \
495    V[0] = V0;                      \
496    V[1] = V1;                      \
497    V[2] = V2;                      \
498} while(0)
499
500/*@}*/
501
502
503/**********************************************************************/
504/** \name 2-element vector operations*/
505/*@{*/
506
507/** Zero */
508#define ZERO_2V( DST )  (DST)[0] = (DST)[1] = 0
509
510/** Copy a 2-element vector */
511#define COPY_2V( DST, SRC )         \
512do {                        \
513   (DST)[0] = (SRC)[0];             \
514   (DST)[1] = (SRC)[1];             \
515} while (0)
516
517/** Copy a 2-element vector with cast */
518#define COPY_2V_CAST( DST, SRC, CAST )      \
519do {                        \
520   (DST)[0] = (CAST)(SRC)[0];           \
521   (DST)[1] = (CAST)(SRC)[1];           \
522} while (0)
523
524/** Copy a 2-element float vector */
525#define COPY_2FV( DST, SRC )            \
526do {                        \
527   const GLfloat *_tmp = (SRC);         \
528   (DST)[0] = _tmp[0];              \
529   (DST)[1] = _tmp[1];              \
530} while (0)
531
532/** Subtraction */
533#define SUB_2V( DST, SRCA, SRCB )       \
534do {                        \
535      (DST)[0] = (SRCA)[0] - (SRCB)[0];     \
536      (DST)[1] = (SRCA)[1] - (SRCB)[1];     \
537} while (0)
538
539/** Addition */
540#define ADD_2V( DST, SRCA, SRCB )       \
541do {                        \
542      (DST)[0] = (SRCA)[0] + (SRCB)[0];     \
543      (DST)[1] = (SRCA)[1] + (SRCB)[1];     \
544} while (0)
545
546/** In-place scalar multiplication */
547#define SCALE_2V( DST, SRCA, SRCB )     \
548do {                        \
549      (DST)[0] = (SRCA)[0] * (SRCB)[0];     \
550      (DST)[1] = (SRCA)[1] * (SRCB)[1];     \
551} while (0)
552
553/** In-place addition */
554#define ACC_2V( DST, SRC )          \
555do {                        \
556      (DST)[0] += (SRC)[0];         \
557      (DST)[1] += (SRC)[1];         \
558} while (0)
559
560/** Element-wise multiplication and addition */
561#define ACC_SCALE_2V( DST, SRCA, SRCB )     \
562do {                        \
563      (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
564      (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
565} while (0)
566
567/** Scalar multiplication */
568#define SCALE_SCALAR_2V( DST, S, SRCB )     \
569do {                        \
570      (DST)[0] = S * (SRCB)[0];         \
571      (DST)[1] = S * (SRCB)[1];         \
572} while (0)
573
574/** In-place scalar multiplication and addition */
575#define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
576do {                        \
577      (DST)[0] += S * (SRCB)[0];        \
578      (DST)[1] += S * (SRCB)[1];        \
579} while (0)
580
581/** In-place scalar multiplication */
582#define SELF_SCALE_SCALAR_2V( DST, S )      \
583do {                        \
584      (DST)[0] *= S;                \
585      (DST)[1] *= S;                \
586} while (0)
587
588/** In-place scalar addition */
589#define ACC_SCALAR_2V( DST, S )         \
590do {                        \
591      (DST)[0] += S;                \
592      (DST)[1] += S;                \
593} while (0)
594
595/** Assign scalers to short vectors */
596#define ASSIGN_2V( V, V0, V1 )	\
597do {				\
598    V[0] = V0;			\
599    V[1] = V1;			\
600} while(0)
601
602/*@}*/
603
604/** Copy \p sz elements into a homegeneous (4-element) vector, giving
605 * default values to the remaining components.
606 * The default values are chosen based on \p type.
607 */
608static inline void
609COPY_CLEAN_4V_TYPE_AS_UNION(fi_type dst[4], int sz, const fi_type src[4],
610                            GLenum type)
611{
612   switch (type) {
613   case GL_FLOAT:
614      ASSIGN_4V(dst, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
615                FLOAT_AS_UNION(0), FLOAT_AS_UNION(1));
616      break;
617   case GL_INT:
618      ASSIGN_4V(dst, INT_AS_UNION(0), INT_AS_UNION(0),
619                INT_AS_UNION(0), INT_AS_UNION(1));
620      break;
621   case GL_UNSIGNED_INT:
622      ASSIGN_4V(dst, UINT_AS_UNION(0), UINT_AS_UNION(0),
623                UINT_AS_UNION(0), UINT_AS_UNION(1));
624      break;
625   default:
626      ASSIGN_4V(dst, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
627                FLOAT_AS_UNION(0), FLOAT_AS_UNION(1)); /* silence warnings */
628      assert(!"Unexpected type in COPY_CLEAN_4V_TYPE_AS_UNION macro");
629   }
630   COPY_SZ_4V(dst, sz, src);
631}
632
633/** \name Linear interpolation functions */
634/*@{*/
635
636static inline GLfloat
637LINTERP(GLfloat t, GLfloat out, GLfloat in)
638{
639   return out + t * (in - out);
640}
641
642static inline void
643INTERP_3F(GLfloat t, GLfloat dst[3], const GLfloat out[3], const GLfloat in[3])
644{
645   dst[0] = LINTERP( t, out[0], in[0] );
646   dst[1] = LINTERP( t, out[1], in[1] );
647   dst[2] = LINTERP( t, out[2], in[2] );
648}
649
650static inline void
651INTERP_4F(GLfloat t, GLfloat dst[4], const GLfloat out[4], const GLfloat in[4])
652{
653   dst[0] = LINTERP( t, out[0], in[0] );
654   dst[1] = LINTERP( t, out[1], in[1] );
655   dst[2] = LINTERP( t, out[2], in[2] );
656   dst[3] = LINTERP( t, out[3], in[3] );
657}
658
659/*@}*/
660
661
662
663static inline unsigned
664minify(unsigned value, unsigned levels)
665{
666    return MAX2(1, value >> levels);
667}
668
669/**
670 * Align a value up to an alignment value
671 *
672 * If \c value is not already aligned to the requested alignment value, it
673 * will be rounded up.
674 *
675 * \param value  Value to be rounded
676 * \param alignment  Alignment value to be used.  This must be a power of two.
677 *
678 * \sa ROUND_DOWN_TO()
679 */
680static inline uintptr_t
681ALIGN(uintptr_t value, int32_t alignment)
682{
683   assert((alignment > 0) && _mesa_is_pow_two(alignment));
684   return (((value) + (alignment) - 1) & ~((alignment) - 1));
685}
686
687/**
688 * Like ALIGN(), but works with a non-power-of-two alignment.
689 */
690static inline uintptr_t
691ALIGN_NPOT(uintptr_t value, int32_t alignment)
692{
693   assert(alignment > 0);
694   return (value + alignment - 1) / alignment * alignment;
695}
696
697/**
698 * Align a value down to an alignment value
699 *
700 * If \c value is not already aligned to the requested alignment value, it
701 * will be rounded down.
702 *
703 * \param value  Value to be rounded
704 * \param alignment  Alignment value to be used.  This must be a power of two.
705 *
706 * \sa ALIGN()
707 */
708static inline uintptr_t
709ROUND_DOWN_TO(uintptr_t value, int32_t alignment)
710{
711   assert((alignment > 0) && _mesa_is_pow_two(alignment));
712   return ((value) & ~(alignment - 1));
713}
714
715
716/** Cross product of two 3-element vectors */
717static inline void
718CROSS3(GLfloat n[3], const GLfloat u[3], const GLfloat v[3])
719{
720   n[0] = u[1] * v[2] - u[2] * v[1];
721   n[1] = u[2] * v[0] - u[0] * v[2];
722   n[2] = u[0] * v[1] - u[1] * v[0];
723}
724
725
726/** Dot product of two 2-element vectors */
727static inline GLfloat
728DOT2(const GLfloat a[2], const GLfloat b[2])
729{
730   return a[0] * b[0] + a[1] * b[1];
731}
732
733static inline GLfloat
734DOT3(const GLfloat a[3], const GLfloat b[3])
735{
736   return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
737}
738
739static inline GLfloat
740DOT4(const GLfloat a[4], const GLfloat b[4])
741{
742   return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
743}
744
745
746static inline GLfloat
747LEN_SQUARED_3FV(const GLfloat v[3])
748{
749   return DOT3(v, v);
750}
751
752static inline GLfloat
753LEN_SQUARED_2FV(const GLfloat v[2])
754{
755   return DOT2(v, v);
756}
757
758
759static inline GLfloat
760LEN_3FV(const GLfloat v[3])
761{
762   return sqrtf(LEN_SQUARED_3FV(v));
763}
764
765static inline GLfloat
766LEN_2FV(const GLfloat v[2])
767{
768   return sqrtf(LEN_SQUARED_2FV(v));
769}
770
771
772/* Normalize a 3-element vector to unit length. */
773static inline void
774NORMALIZE_3FV(GLfloat v[3])
775{
776   GLfloat len = (GLfloat) LEN_SQUARED_3FV(v);
777   if (len) {
778      len = 1.0f / sqrtf(len);
779      v[0] *= len;
780      v[1] *= len;
781      v[2] *= len;
782   }
783}
784
785
786/** Test two floats have opposite signs */
787static inline GLboolean
788DIFFERENT_SIGNS(GLfloat x, GLfloat y)
789{
790#ifdef _MSC_VER
791#pragma warning( push )
792#pragma warning( disable : 6334 ) /* sizeof operator applied to an expression with an operator may yield unexpected results */
793#endif
794   return signbit(x) != signbit(y);
795#ifdef _MSC_VER
796#pragma warning( pop )
797#endif
798}
799
800
801/** casts to silence warnings with some compilers */
802#define ENUM_TO_INT(E)     ((GLint)(E))
803#define ENUM_TO_FLOAT(E)   ((GLfloat)(GLint)(E))
804#define ENUM_TO_DOUBLE(E)  ((GLdouble)(GLint)(E))
805#define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
806
807
808/* Stringify */
809#define STRINGIFY(x) #x
810
811#endif
812