util: Gather some common macros
[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 "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] */
43 extern 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 /* This function/macro is sensitive to precision. Test very carefully
145 * if you change it!
146 */
147 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
148 do { \
149 fi_type __tmp; \
150 __tmp.f = (F); \
151 if (__tmp.i < 0) \
152 UB = (GLubyte) 0; \
153 else if (__tmp.i >= IEEE_ONE) \
154 UB = (GLubyte) 255; \
155 else { \
156 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
157 UB = (GLubyte) __tmp.i; \
158 } \
159 } while (0)
160 #define CLAMPED_FLOAT_TO_UBYTE(UB, F) \
161 do { \
162 fi_type __tmp; \
163 __tmp.f = (F) * (255.0F/256.0F) + 32768.0F; \
164 UB = (GLubyte) __tmp.i; \
165 } while (0)
166 #else
167 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
168 ub = ((GLubyte) F_TO_I(CLAMP((f), 0.0F, 1.0F) * 255.0F))
169 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
170 ub = ((GLubyte) F_TO_I((f) * 255.0F))
171 #endif
172
173 static inline GLfloat INT_AS_FLT(GLint i)
174 {
175 fi_type tmp;
176 tmp.i = i;
177 return tmp.f;
178 }
179
180 static inline GLfloat UINT_AS_FLT(GLuint u)
181 {
182 fi_type tmp;
183 tmp.u = u;
184 return tmp.f;
185 }
186
187 /**
188 * Convert a floating point value to an unsigned fixed point value.
189 *
190 * \param frac_bits The number of bits used to store the fractional part.
191 */
192 static INLINE uint32_t
193 U_FIXED(float value, uint32_t frac_bits)
194 {
195 value *= (1 << frac_bits);
196 return value < 0.0f ? 0 : (uint32_t) value;
197 }
198
199 /**
200 * Convert a floating point value to an signed fixed point value.
201 *
202 * \param frac_bits The number of bits used to store the fractional part.
203 */
204 static INLINE int32_t
205 S_FIXED(float value, uint32_t frac_bits)
206 {
207 return (int32_t) (value * (1 << frac_bits));
208 }
209 /*@}*/
210
211
212 /** Stepping a GLfloat pointer by a byte stride */
213 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
214 /** Stepping a GLuint pointer by a byte stride */
215 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
216 /** Stepping a GLubyte[4] pointer by a byte stride */
217 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
218 /** Stepping a GLfloat[4] pointer by a byte stride */
219 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
220 /** Stepping a \p t pointer by a byte stride */
221 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
222
223
224 /**********************************************************************/
225 /** \name 4-element vector operations */
226 /*@{*/
227
228 /** Zero */
229 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
230
231 /** Test for equality */
232 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
233 (a)[1] == (b)[1] && \
234 (a)[2] == (b)[2] && \
235 (a)[3] == (b)[3])
236
237 /** Test for equality (unsigned bytes) */
238 static inline GLboolean
239 TEST_EQ_4UBV(const GLubyte a[4], const GLubyte b[4])
240 {
241 #if defined(__i386__)
242 return *((const GLuint *) a) == *((const GLuint *) b);
243 #else
244 return TEST_EQ_4V(a, b);
245 #endif
246 }
247
248
249 /** Copy a 4-element vector */
250 #define COPY_4V( DST, SRC ) \
251 do { \
252 (DST)[0] = (SRC)[0]; \
253 (DST)[1] = (SRC)[1]; \
254 (DST)[2] = (SRC)[2]; \
255 (DST)[3] = (SRC)[3]; \
256 } while (0)
257
258 /** Copy a 4-element unsigned byte vector */
259 static inline void
260 COPY_4UBV(GLubyte dst[4], const GLubyte src[4])
261 {
262 #if defined(__i386__)
263 *((GLuint *) dst) = *((GLuint *) src);
264 #else
265 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
266 COPY_4V(dst, src);
267 #endif
268 }
269
270 /** Copy a 4-element float vector */
271 static inline void
272 COPY_4FV(GLfloat dst[4], const GLfloat src[4])
273 {
274 /* memcpy seems to be most efficient */
275 memcpy(dst, src, sizeof(GLfloat) * 4);
276 }
277
278 /** Copy \p SZ elements into a 4-element vector */
279 #define COPY_SZ_4V(DST, SZ, SRC) \
280 do { \
281 switch (SZ) { \
282 case 4: (DST)[3] = (SRC)[3]; \
283 case 3: (DST)[2] = (SRC)[2]; \
284 case 2: (DST)[1] = (SRC)[1]; \
285 case 1: (DST)[0] = (SRC)[0]; \
286 } \
287 } while(0)
288
289 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
290 * default values to the remaining */
291 #define COPY_CLEAN_4V(DST, SZ, SRC) \
292 do { \
293 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
294 COPY_SZ_4V( DST, SZ, SRC ); \
295 } while (0)
296
297 /** Subtraction */
298 #define SUB_4V( DST, SRCA, SRCB ) \
299 do { \
300 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
301 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
302 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
303 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
304 } while (0)
305
306 /** Addition */
307 #define ADD_4V( DST, SRCA, SRCB ) \
308 do { \
309 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
310 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
311 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
312 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
313 } while (0)
314
315 /** Element-wise multiplication */
316 #define SCALE_4V( DST, SRCA, SRCB ) \
317 do { \
318 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
319 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
320 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
321 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
322 } while (0)
323
324 /** In-place addition */
325 #define ACC_4V( DST, SRC ) \
326 do { \
327 (DST)[0] += (SRC)[0]; \
328 (DST)[1] += (SRC)[1]; \
329 (DST)[2] += (SRC)[2]; \
330 (DST)[3] += (SRC)[3]; \
331 } while (0)
332
333 /** Element-wise multiplication and addition */
334 #define ACC_SCALE_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 /** In-place scalar multiplication and addition */
343 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
344 do { \
345 (DST)[0] += S * (SRCB)[0]; \
346 (DST)[1] += S * (SRCB)[1]; \
347 (DST)[2] += S * (SRCB)[2]; \
348 (DST)[3] += S * (SRCB)[3]; \
349 } while (0)
350
351 /** Scalar multiplication */
352 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
353 do { \
354 (DST)[0] = S * (SRCB)[0]; \
355 (DST)[1] = S * (SRCB)[1]; \
356 (DST)[2] = S * (SRCB)[2]; \
357 (DST)[3] = S * (SRCB)[3]; \
358 } while (0)
359
360 /** In-place scalar multiplication */
361 #define SELF_SCALE_SCALAR_4V( DST, S ) \
362 do { \
363 (DST)[0] *= S; \
364 (DST)[1] *= S; \
365 (DST)[2] *= S; \
366 (DST)[3] *= S; \
367 } while (0)
368
369 /** Assignment */
370 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
371 do { \
372 V[0] = V0; \
373 V[1] = V1; \
374 V[2] = V2; \
375 V[3] = V3; \
376 } while(0)
377
378 /*@}*/
379
380
381 /**********************************************************************/
382 /** \name 3-element vector operations*/
383 /*@{*/
384
385 /** Zero */
386 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
387
388 /** Test for equality */
389 #define TEST_EQ_3V(a,b) \
390 ((a)[0] == (b)[0] && \
391 (a)[1] == (b)[1] && \
392 (a)[2] == (b)[2])
393
394 /** Copy a 3-element vector */
395 #define COPY_3V( DST, SRC ) \
396 do { \
397 (DST)[0] = (SRC)[0]; \
398 (DST)[1] = (SRC)[1]; \
399 (DST)[2] = (SRC)[2]; \
400 } while (0)
401
402 /** Copy a 3-element vector with cast */
403 #define COPY_3V_CAST( DST, SRC, CAST ) \
404 do { \
405 (DST)[0] = (CAST)(SRC)[0]; \
406 (DST)[1] = (CAST)(SRC)[1]; \
407 (DST)[2] = (CAST)(SRC)[2]; \
408 } while (0)
409
410 /** Copy a 3-element float vector */
411 #define COPY_3FV( DST, SRC ) \
412 do { \
413 const GLfloat *_tmp = (SRC); \
414 (DST)[0] = _tmp[0]; \
415 (DST)[1] = _tmp[1]; \
416 (DST)[2] = _tmp[2]; \
417 } while (0)
418
419 /** Subtraction */
420 #define SUB_3V( DST, SRCA, SRCB ) \
421 do { \
422 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
423 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
424 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
425 } while (0)
426
427 /** Addition */
428 #define ADD_3V( DST, SRCA, SRCB ) \
429 do { \
430 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
431 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
432 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
433 } while (0)
434
435 /** In-place scalar multiplication */
436 #define SCALE_3V( DST, SRCA, SRCB ) \
437 do { \
438 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
439 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
440 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
441 } while (0)
442
443 /** In-place element-wise multiplication */
444 #define SELF_SCALE_3V( DST, SRC ) \
445 do { \
446 (DST)[0] *= (SRC)[0]; \
447 (DST)[1] *= (SRC)[1]; \
448 (DST)[2] *= (SRC)[2]; \
449 } while (0)
450
451 /** In-place addition */
452 #define ACC_3V( DST, SRC ) \
453 do { \
454 (DST)[0] += (SRC)[0]; \
455 (DST)[1] += (SRC)[1]; \
456 (DST)[2] += (SRC)[2]; \
457 } while (0)
458
459 /** Element-wise multiplication and addition */
460 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
461 do { \
462 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
463 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
464 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
465 } while (0)
466
467 /** Scalar multiplication */
468 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
469 do { \
470 (DST)[0] = S * (SRCB)[0]; \
471 (DST)[1] = S * (SRCB)[1]; \
472 (DST)[2] = S * (SRCB)[2]; \
473 } while (0)
474
475 /** In-place scalar multiplication and addition */
476 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
477 do { \
478 (DST)[0] += S * (SRCB)[0]; \
479 (DST)[1] += S * (SRCB)[1]; \
480 (DST)[2] += S * (SRCB)[2]; \
481 } while (0)
482
483 /** In-place scalar multiplication */
484 #define SELF_SCALE_SCALAR_3V( DST, S ) \
485 do { \
486 (DST)[0] *= S; \
487 (DST)[1] *= S; \
488 (DST)[2] *= S; \
489 } while (0)
490
491 /** In-place scalar addition */
492 #define ACC_SCALAR_3V( DST, S ) \
493 do { \
494 (DST)[0] += S; \
495 (DST)[1] += S; \
496 (DST)[2] += S; \
497 } while (0)
498
499 /** Assignment */
500 #define ASSIGN_3V( V, V0, V1, V2 ) \
501 do { \
502 V[0] = V0; \
503 V[1] = V1; \
504 V[2] = V2; \
505 } while(0)
506
507 /*@}*/
508
509
510 /**********************************************************************/
511 /** \name 2-element vector operations*/
512 /*@{*/
513
514 /** Zero */
515 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
516
517 /** Copy a 2-element vector */
518 #define COPY_2V( DST, SRC ) \
519 do { \
520 (DST)[0] = (SRC)[0]; \
521 (DST)[1] = (SRC)[1]; \
522 } while (0)
523
524 /** Copy a 2-element vector with cast */
525 #define COPY_2V_CAST( DST, SRC, CAST ) \
526 do { \
527 (DST)[0] = (CAST)(SRC)[0]; \
528 (DST)[1] = (CAST)(SRC)[1]; \
529 } while (0)
530
531 /** Copy a 2-element float vector */
532 #define COPY_2FV( DST, SRC ) \
533 do { \
534 const GLfloat *_tmp = (SRC); \
535 (DST)[0] = _tmp[0]; \
536 (DST)[1] = _tmp[1]; \
537 } while (0)
538
539 /** Subtraction */
540 #define SUB_2V( DST, SRCA, SRCB ) \
541 do { \
542 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
543 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
544 } while (0)
545
546 /** Addition */
547 #define ADD_2V( DST, SRCA, SRCB ) \
548 do { \
549 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
550 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
551 } while (0)
552
553 /** In-place scalar multiplication */
554 #define SCALE_2V( DST, SRCA, SRCB ) \
555 do { \
556 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
557 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
558 } while (0)
559
560 /** In-place addition */
561 #define ACC_2V( DST, SRC ) \
562 do { \
563 (DST)[0] += (SRC)[0]; \
564 (DST)[1] += (SRC)[1]; \
565 } while (0)
566
567 /** Element-wise multiplication and addition */
568 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
569 do { \
570 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
571 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
572 } while (0)
573
574 /** Scalar multiplication */
575 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
576 do { \
577 (DST)[0] = S * (SRCB)[0]; \
578 (DST)[1] = S * (SRCB)[1]; \
579 } while (0)
580
581 /** In-place scalar multiplication and addition */
582 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
583 do { \
584 (DST)[0] += S * (SRCB)[0]; \
585 (DST)[1] += S * (SRCB)[1]; \
586 } while (0)
587
588 /** In-place scalar multiplication */
589 #define SELF_SCALE_SCALAR_2V( DST, S ) \
590 do { \
591 (DST)[0] *= S; \
592 (DST)[1] *= S; \
593 } while (0)
594
595 /** In-place scalar addition */
596 #define ACC_SCALAR_2V( DST, S ) \
597 do { \
598 (DST)[0] += S; \
599 (DST)[1] += S; \
600 } while (0)
601
602 /** Assign scalers to short vectors */
603 #define ASSIGN_2V( V, V0, V1 ) \
604 do { \
605 V[0] = V0; \
606 V[1] = V1; \
607 } while(0)
608
609 /*@}*/
610
611 /** Copy \p sz elements into a homegeneous (4-element) vector, giving
612 * default values to the remaining components.
613 * The default values are chosen based on \p type.
614 */
615 static inline void
616 COPY_CLEAN_4V_TYPE_AS_FLOAT(GLfloat dst[4], int sz, const GLfloat src[4],
617 GLenum type)
618 {
619 switch (type) {
620 case GL_FLOAT:
621 ASSIGN_4V(dst, 0, 0, 0, 1);
622 break;
623 case GL_INT:
624 ASSIGN_4V(dst, INT_AS_FLT(0), INT_AS_FLT(0),
625 INT_AS_FLT(0), INT_AS_FLT(1));
626 break;
627 case GL_UNSIGNED_INT:
628 ASSIGN_4V(dst, UINT_AS_FLT(0), UINT_AS_FLT(0),
629 UINT_AS_FLT(0), UINT_AS_FLT(1));
630 break;
631 default:
632 ASSIGN_4V(dst, 0.0f, 0.0f, 0.0f, 1.0f); /* silence warnings */
633 ASSERT(!"Unexpected type in COPY_CLEAN_4V_TYPE_AS_FLOAT macro");
634 }
635 COPY_SZ_4V(dst, sz, src);
636 }
637
638 /** \name Linear interpolation functions */
639 /*@{*/
640
641 static inline GLfloat
642 LINTERP(GLfloat t, GLfloat out, GLfloat in)
643 {
644 return out + t * (in - out);
645 }
646
647 static inline void
648 INTERP_3F(GLfloat t, GLfloat dst[3], const GLfloat out[3], const GLfloat in[3])
649 {
650 dst[0] = LINTERP( t, out[0], in[0] );
651 dst[1] = LINTERP( t, out[1], in[1] );
652 dst[2] = LINTERP( t, out[2], in[2] );
653 }
654
655 static inline void
656 INTERP_4F(GLfloat t, GLfloat dst[4], const GLfloat out[4], const GLfloat in[4])
657 {
658 dst[0] = LINTERP( t, out[0], in[0] );
659 dst[1] = LINTERP( t, out[1], in[1] );
660 dst[2] = LINTERP( t, out[2], in[2] );
661 dst[3] = LINTERP( t, out[3], in[3] );
662 }
663
664 /*@}*/
665
666
667
668 /** Clamp X to [MIN,MAX] */
669 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
670
671 /** Minimum of two values: */
672 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
673
674 /** Maximum of two values: */
675 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
676
677 /** Minimum and maximum of three values: */
678 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
679 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
680
681 static inline unsigned
682 minify(unsigned value, unsigned levels)
683 {
684 return MAX2(1, value >> levels);
685 }
686
687 /**
688 * Return true if the given value is a power of two.
689 *
690 * Note that this considers 0 a power of two.
691 */
692 static inline bool
693 is_power_of_two(unsigned value)
694 {
695 return (value & (value - 1)) == 0;
696 }
697
698 /**
699 * Align a value up to an alignment value
700 *
701 * If \c value is not already aligned to the requested alignment value, it
702 * will be rounded up.
703 *
704 * \param value Value to be rounded
705 * \param alignment Alignment value to be used. This must be a power of two.
706 *
707 * \sa ROUND_DOWN_TO()
708 */
709 #define ALIGN(value, alignment) (((value) + (alignment) - 1) & ~((alignment) - 1))
710
711 /**
712 * Align a value down to an alignment value
713 *
714 * If \c value is not already aligned to the requested alignment value, it
715 * will be rounded down.
716 *
717 * \param value Value to be rounded
718 * \param alignment Alignment value to be used. This must be a power of two.
719 *
720 * \sa ALIGN()
721 */
722 #define ROUND_DOWN_TO(value, alignment) ((value) & ~(alignment - 1))
723
724
725 /** Cross product of two 3-element vectors */
726 static inline void
727 CROSS3(GLfloat n[3], const GLfloat u[3], const GLfloat v[3])
728 {
729 n[0] = u[1] * v[2] - u[2] * v[1];
730 n[1] = u[2] * v[0] - u[0] * v[2];
731 n[2] = u[0] * v[1] - u[1] * v[0];
732 }
733
734
735 /** Dot product of two 2-element vectors */
736 static inline GLfloat
737 DOT2(const GLfloat a[2], const GLfloat b[2])
738 {
739 return a[0] * b[0] + a[1] * b[1];
740 }
741
742 static inline GLfloat
743 DOT3(const GLfloat a[3], const GLfloat b[3])
744 {
745 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
746 }
747
748 static inline GLfloat
749 DOT4(const GLfloat a[4], const GLfloat b[4])
750 {
751 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
752 }
753
754
755 static inline GLfloat
756 LEN_SQUARED_3FV(const GLfloat v[3])
757 {
758 return DOT3(v, v);
759 }
760
761 static inline GLfloat
762 LEN_SQUARED_2FV(const GLfloat v[2])
763 {
764 return DOT2(v, v);
765 }
766
767
768 static inline GLfloat
769 LEN_3FV(const GLfloat v[3])
770 {
771 return sqrtf(LEN_SQUARED_3FV(v));
772 }
773
774 static inline GLfloat
775 LEN_2FV(const GLfloat v[2])
776 {
777 return sqrtf(LEN_SQUARED_2FV(v));
778 }
779
780
781 /* Normalize a 3-element vector to unit length. */
782 static inline void
783 NORMALIZE_3FV(GLfloat v[3])
784 {
785 GLfloat len = (GLfloat) LEN_SQUARED_3FV(v);
786 if (len) {
787 len = INV_SQRTF(len);
788 v[0] *= len;
789 v[1] *= len;
790 v[2] *= len;
791 }
792 }
793
794
795 /** Is float value negative? */
796 static inline GLboolean
797 IS_NEGATIVE(float x)
798 {
799 return signbit(x) != 0;
800 }
801
802 /** Test two floats have opposite signs */
803 static inline GLboolean
804 DIFFERENT_SIGNS(GLfloat x, GLfloat y)
805 {
806 return signbit(x) != signbit(y);
807 }
808
809
810 /** Compute ceiling of integer quotient of A divided by B. */
811 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
812
813
814 /** casts to silence warnings with some compilers */
815 #define ENUM_TO_INT(E) ((GLint)(E))
816 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
817 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
818 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
819
820 /* Compute the size of an array */
821 #ifndef ARRAY_SIZE
822 # define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
823 #endif
824
825 /* Stringify */
826 #define STRINGIFY(x) #x
827
828 #endif