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