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