mesa: Use u_math.h from macros.h
[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/u_math.h"
35 #include "imports.h"
36
37
38 /**
39 * \name Integer / float conversion for colors, normals, etc.
40 */
41 /*@{*/
42
43 /** Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
44 extern GLfloat _mesa_ubyte_to_float_color_tab[256];
45 #define UBYTE_TO_FLOAT(u) _mesa_ubyte_to_float_color_tab[(unsigned int)(u)]
46
47 /** Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
48 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) ((X) * 255.0F))
49
50
51 /** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
52 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
53
54 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
55 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
56
57
58 /** Convert GLbyte to GLfloat while preserving zero */
59 #define BYTE_TO_FLOATZ(B) ((B) == 0 ? 0.0F : BYTE_TO_FLOAT(B))
60
61
62 /** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0], texture/fb data */
63 #define BYTE_TO_FLOAT_TEX(B) ((B) == -128 ? -1.0F : (B) * (1.0F/127.0F))
64
65 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127], texture/fb data */
66 #define FLOAT_TO_BYTE_TEX(X) CLAMP( (GLint) (127.0F * (X)), -128, 127 )
67
68 /** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */
69 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
70
71 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
72 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
73
74
75 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
76 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
77
78 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */
79 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
80
81 /** Convert GLshort to GLfloat while preserving zero */
82 #define SHORT_TO_FLOATZ(S) ((S) == 0 ? 0.0F : SHORT_TO_FLOAT(S))
83
84
85 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0], texture/fb data */
86 #define SHORT_TO_FLOAT_TEX(S) ((S) == -32768 ? -1.0F : (S) * (1.0F/32767.0F))
87
88 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767], texture/fb data */
89 #define FLOAT_TO_SHORT_TEX(X) ( (GLint) (32767.0F * (X)) )
90
91
92 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
93 #define UINT_TO_FLOAT(U) ((GLfloat) ((U) * (1.0F / 4294967295.0)))
94
95 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
96 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
97
98
99 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
100 #define INT_TO_FLOAT(I) ((GLfloat) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0)))
101
102 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
103 /* causes overflow:
104 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
105 */
106 /* a close approximation: */
107 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
108
109 /** Convert GLfloat in [-1.0,1.0] to GLint64 in [-(1<<63),(1 << 63) -1] */
110 #define FLOAT_TO_INT64(X) ( (GLint64) (9223372036854775807.0 * (double)(X)) )
111
112
113 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0], texture/fb data */
114 #define INT_TO_FLOAT_TEX(I) ((I) == -2147483648 ? -1.0F : (I) * (1.0F/2147483647.0))
115
116 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647], texture/fb data */
117 #define FLOAT_TO_INT_TEX(X) ( (GLint) (2147483647.0 * (X)) )
118
119
120 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
121 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
122 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
123 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
124 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
125
126
127 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
128 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
129 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
130 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
131 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
132 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
133 us = ( (GLushort) F_TO_I( CLAMP((f), 0.0F, 1.0F) * 65535.0F) )
134 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
135 us = ( (GLushort) F_TO_I( (f) * 65535.0F) )
136
137 #define UNCLAMPED_FLOAT_TO_SHORT(s, f) \
138 s = ( (GLshort) F_TO_I( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
139
140 /***
141 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
142 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
143 ***/
144 #ifndef DEBUG
145 /* This function/macro is sensitive to precision. Test very carefully
146 * if you change it!
147 */
148 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, FLT) \
149 do { \
150 fi_type __tmp; \
151 __tmp.f = (FLT); \
152 if (__tmp.i < 0) \
153 UB = (GLubyte) 0; \
154 else if (__tmp.i >= IEEE_ONE) \
155 UB = (GLubyte) 255; \
156 else { \
157 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
158 UB = (GLubyte) __tmp.i; \
159 } \
160 } while (0)
161 #define CLAMPED_FLOAT_TO_UBYTE(UB, FLT) \
162 do { \
163 fi_type __tmp; \
164 __tmp.f = (FLT) * (255.0F/256.0F) + 32768.0F; \
165 UB = (GLubyte) __tmp.i; \
166 } while (0)
167 #else
168 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
169 ub = ((GLubyte) F_TO_I(CLAMP((f), 0.0F, 1.0F) * 255.0F))
170 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
171 ub = ((GLubyte) F_TO_I((f) * 255.0F))
172 #endif
173
174 static inline GLfloat INT_AS_FLT(GLint i)
175 {
176 fi_type tmp;
177 tmp.i = i;
178 return tmp.f;
179 }
180
181 static inline GLfloat UINT_AS_FLT(GLuint u)
182 {
183 fi_type tmp;
184 tmp.u = u;
185 return tmp.f;
186 }
187
188 static inline unsigned FLT_AS_UINT(float f)
189 {
190 fi_type tmp;
191 tmp.f = f;
192 return tmp.u;
193 }
194
195 /**
196 * Convert a floating point value to an unsigned fixed point value.
197 *
198 * \param frac_bits The number of bits used to store the fractional part.
199 */
200 static inline uint32_t
201 U_FIXED(float value, uint32_t frac_bits)
202 {
203 value *= (1 << frac_bits);
204 return value < 0.0f ? 0 : (uint32_t) value;
205 }
206
207 /**
208 * Convert a floating point value to an signed fixed point value.
209 *
210 * \param frac_bits The number of bits used to store the fractional part.
211 */
212 static inline int32_t
213 S_FIXED(float value, uint32_t frac_bits)
214 {
215 return (int32_t) (value * (1 << frac_bits));
216 }
217 /*@}*/
218
219
220 /** Stepping a GLfloat pointer by a byte stride */
221 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
222 /** Stepping a GLuint pointer by a byte stride */
223 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
224 /** Stepping a GLubyte[4] pointer by a byte stride */
225 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
226 /** Stepping a GLfloat[4] pointer by a byte stride */
227 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
228 /** Stepping a \p t pointer by a byte stride */
229 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
230
231
232 /**********************************************************************/
233 /** \name 4-element vector operations */
234 /*@{*/
235
236 /** Zero */
237 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
238
239 /** Test for equality */
240 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
241 (a)[1] == (b)[1] && \
242 (a)[2] == (b)[2] && \
243 (a)[3] == (b)[3])
244
245 /** Test for equality (unsigned bytes) */
246 static inline GLboolean
247 TEST_EQ_4UBV(const GLubyte a[4], const GLubyte b[4])
248 {
249 #if defined(__i386__)
250 return *((const GLuint *) a) == *((const GLuint *) b);
251 #else
252 return TEST_EQ_4V(a, b);
253 #endif
254 }
255
256
257 /** Copy a 4-element vector */
258 #define COPY_4V( DST, SRC ) \
259 do { \
260 (DST)[0] = (SRC)[0]; \
261 (DST)[1] = (SRC)[1]; \
262 (DST)[2] = (SRC)[2]; \
263 (DST)[3] = (SRC)[3]; \
264 } while (0)
265
266 /** Copy a 4-element unsigned byte vector */
267 static inline void
268 COPY_4UBV(GLubyte dst[4], const GLubyte src[4])
269 {
270 #if defined(__i386__)
271 *((GLuint *) dst) = *((GLuint *) src);
272 #else
273 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
274 COPY_4V(dst, src);
275 #endif
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 /*@}*/
370
371
372 /**********************************************************************/
373 /** \name 3-element vector operations*/
374 /*@{*/
375
376 /** Zero */
377 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
378
379 /** Test for equality */
380 #define TEST_EQ_3V(a,b) \
381 ((a)[0] == (b)[0] && \
382 (a)[1] == (b)[1] && \
383 (a)[2] == (b)[2])
384
385 /** Copy a 3-element vector */
386 #define COPY_3V( DST, SRC ) \
387 do { \
388 (DST)[0] = (SRC)[0]; \
389 (DST)[1] = (SRC)[1]; \
390 (DST)[2] = (SRC)[2]; \
391 } while (0)
392
393 /** Copy a 3-element vector with cast */
394 #define COPY_3V_CAST( DST, SRC, CAST ) \
395 do { \
396 (DST)[0] = (CAST)(SRC)[0]; \
397 (DST)[1] = (CAST)(SRC)[1]; \
398 (DST)[2] = (CAST)(SRC)[2]; \
399 } while (0)
400
401 /** Copy a 3-element float vector */
402 #define COPY_3FV( DST, SRC ) \
403 do { \
404 const GLfloat *_tmp = (SRC); \
405 (DST)[0] = _tmp[0]; \
406 (DST)[1] = _tmp[1]; \
407 (DST)[2] = _tmp[2]; \
408 } while (0)
409
410 /** Subtraction */
411 #define SUB_3V( DST, SRCA, SRCB ) \
412 do { \
413 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
414 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
415 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
416 } while (0)
417
418 /** Addition */
419 #define ADD_3V( DST, SRCA, SRCB ) \
420 do { \
421 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
422 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
423 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
424 } while (0)
425
426 /** In-place scalar multiplication */
427 #define SCALE_3V( DST, SRCA, SRCB ) \
428 do { \
429 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
430 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
431 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
432 } while (0)
433
434 /** In-place element-wise multiplication */
435 #define SELF_SCALE_3V( DST, SRC ) \
436 do { \
437 (DST)[0] *= (SRC)[0]; \
438 (DST)[1] *= (SRC)[1]; \
439 (DST)[2] *= (SRC)[2]; \
440 } while (0)
441
442 /** In-place addition */
443 #define ACC_3V( DST, SRC ) \
444 do { \
445 (DST)[0] += (SRC)[0]; \
446 (DST)[1] += (SRC)[1]; \
447 (DST)[2] += (SRC)[2]; \
448 } while (0)
449
450 /** Element-wise multiplication and addition */
451 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
452 do { \
453 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
454 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
455 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
456 } while (0)
457
458 /** Scalar multiplication */
459 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
460 do { \
461 (DST)[0] = S * (SRCB)[0]; \
462 (DST)[1] = S * (SRCB)[1]; \
463 (DST)[2] = S * (SRCB)[2]; \
464 } while (0)
465
466 /** In-place scalar multiplication and addition */
467 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
468 do { \
469 (DST)[0] += S * (SRCB)[0]; \
470 (DST)[1] += S * (SRCB)[1]; \
471 (DST)[2] += S * (SRCB)[2]; \
472 } while (0)
473
474 /** In-place scalar multiplication */
475 #define SELF_SCALE_SCALAR_3V( DST, S ) \
476 do { \
477 (DST)[0] *= S; \
478 (DST)[1] *= S; \
479 (DST)[2] *= S; \
480 } while (0)
481
482 /** In-place scalar addition */
483 #define ACC_SCALAR_3V( DST, S ) \
484 do { \
485 (DST)[0] += S; \
486 (DST)[1] += S; \
487 (DST)[2] += S; \
488 } while (0)
489
490 /** Assignment */
491 #define ASSIGN_3V( V, V0, V1, V2 ) \
492 do { \
493 V[0] = V0; \
494 V[1] = V1; \
495 V[2] = V2; \
496 } while(0)
497
498 /*@}*/
499
500
501 /**********************************************************************/
502 /** \name 2-element vector operations*/
503 /*@{*/
504
505 /** Zero */
506 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
507
508 /** Copy a 2-element vector */
509 #define COPY_2V( DST, SRC ) \
510 do { \
511 (DST)[0] = (SRC)[0]; \
512 (DST)[1] = (SRC)[1]; \
513 } while (0)
514
515 /** Copy a 2-element vector with cast */
516 #define COPY_2V_CAST( DST, SRC, CAST ) \
517 do { \
518 (DST)[0] = (CAST)(SRC)[0]; \
519 (DST)[1] = (CAST)(SRC)[1]; \
520 } while (0)
521
522 /** Copy a 2-element float vector */
523 #define COPY_2FV( DST, SRC ) \
524 do { \
525 const GLfloat *_tmp = (SRC); \
526 (DST)[0] = _tmp[0]; \
527 (DST)[1] = _tmp[1]; \
528 } while (0)
529
530 /** Subtraction */
531 #define SUB_2V( DST, SRCA, SRCB ) \
532 do { \
533 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
534 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
535 } while (0)
536
537 /** Addition */
538 #define ADD_2V( DST, SRCA, SRCB ) \
539 do { \
540 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
541 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
542 } while (0)
543
544 /** In-place scalar multiplication */
545 #define SCALE_2V( DST, SRCA, SRCB ) \
546 do { \
547 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
548 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
549 } while (0)
550
551 /** In-place addition */
552 #define ACC_2V( DST, SRC ) \
553 do { \
554 (DST)[0] += (SRC)[0]; \
555 (DST)[1] += (SRC)[1]; \
556 } while (0)
557
558 /** Element-wise multiplication and addition */
559 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
560 do { \
561 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
562 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
563 } while (0)
564
565 /** Scalar multiplication */
566 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
567 do { \
568 (DST)[0] = S * (SRCB)[0]; \
569 (DST)[1] = S * (SRCB)[1]; \
570 } while (0)
571
572 /** In-place scalar multiplication and addition */
573 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
574 do { \
575 (DST)[0] += S * (SRCB)[0]; \
576 (DST)[1] += S * (SRCB)[1]; \
577 } while (0)
578
579 /** In-place scalar multiplication */
580 #define SELF_SCALE_SCALAR_2V( DST, S ) \
581 do { \
582 (DST)[0] *= S; \
583 (DST)[1] *= S; \
584 } while (0)
585
586 /** In-place scalar addition */
587 #define ACC_SCALAR_2V( DST, S ) \
588 do { \
589 (DST)[0] += S; \
590 (DST)[1] += S; \
591 } while (0)
592
593 /** Assign scalers to short vectors */
594 #define ASSIGN_2V( V, V0, V1 ) \
595 do { \
596 V[0] = V0; \
597 V[1] = V1; \
598 } while(0)
599
600 /*@}*/
601
602 /** Copy \p sz elements into a homegeneous (4-element) vector, giving
603 * default values to the remaining components.
604 * The default values are chosen based on \p type.
605 */
606 static inline void
607 COPY_CLEAN_4V_TYPE_AS_FLOAT(GLfloat dst[4], int sz, const GLfloat src[4],
608 GLenum type)
609 {
610 switch (type) {
611 case GL_FLOAT:
612 ASSIGN_4V(dst, 0, 0, 0, 1);
613 break;
614 case GL_INT:
615 ASSIGN_4V(dst, INT_AS_FLT(0), INT_AS_FLT(0),
616 INT_AS_FLT(0), INT_AS_FLT(1));
617 break;
618 case GL_UNSIGNED_INT:
619 ASSIGN_4V(dst, UINT_AS_FLT(0), UINT_AS_FLT(0),
620 UINT_AS_FLT(0), UINT_AS_FLT(1));
621 break;
622 default:
623 ASSIGN_4V(dst, 0.0f, 0.0f, 0.0f, 1.0f); /* silence warnings */
624 ASSERT(!"Unexpected type in COPY_CLEAN_4V_TYPE_AS_FLOAT macro");
625 }
626 COPY_SZ_4V(dst, sz, src);
627 }
628
629 /** \name Linear interpolation functions */
630 /*@{*/
631
632 static inline GLfloat
633 LINTERP(GLfloat t, GLfloat out, GLfloat in)
634 {
635 return out + t * (in - out);
636 }
637
638 static inline void
639 INTERP_3F(GLfloat t, GLfloat dst[3], const GLfloat out[3], const GLfloat in[3])
640 {
641 dst[0] = LINTERP( t, out[0], in[0] );
642 dst[1] = LINTERP( t, out[1], in[1] );
643 dst[2] = LINTERP( t, out[2], in[2] );
644 }
645
646 static inline void
647 INTERP_4F(GLfloat t, GLfloat dst[4], const GLfloat out[4], const GLfloat in[4])
648 {
649 dst[0] = LINTERP( t, out[0], in[0] );
650 dst[1] = LINTERP( t, out[1], in[1] );
651 dst[2] = LINTERP( t, out[2], in[2] );
652 dst[3] = LINTERP( t, out[3], in[3] );
653 }
654
655 /*@}*/
656
657
658
659 /** Clamp X to [MIN,MAX] */
660 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
661
662 /** Minimum of two values: */
663 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
664
665 /** Maximum of two values: */
666 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
667
668 /** Minimum and maximum of three values: */
669 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
670 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
671
672 static inline unsigned
673 minify(unsigned value, unsigned levels)
674 {
675 return MAX2(1, value >> levels);
676 }
677
678 /**
679 * Return true if the given value is a power of two.
680 *
681 * Note that this considers 0 a power of two.
682 */
683 static inline bool
684 is_power_of_two(unsigned value)
685 {
686 return (value & (value - 1)) == 0;
687 }
688
689 /**
690 * Align a value up to an alignment value
691 *
692 * If \c value is not already aligned to the requested alignment value, it
693 * will be rounded up.
694 *
695 * \param value Value to be rounded
696 * \param alignment Alignment value to be used. This must be a power of two.
697 *
698 * \sa ROUND_DOWN_TO()
699 */
700 #define ALIGN(value, alignment) (((value) + (alignment) - 1) & ~((alignment) - 1))
701
702 /**
703 * Align a value down to an alignment value
704 *
705 * If \c value is not already aligned to the requested alignment value, it
706 * will be rounded down.
707 *
708 * \param value Value to be rounded
709 * \param alignment Alignment value to be used. This must be a power of two.
710 *
711 * \sa ALIGN()
712 */
713 #define ROUND_DOWN_TO(value, alignment) ((value) & ~(alignment - 1))
714
715
716 /** Cross product of two 3-element vectors */
717 static inline void
718 CROSS3(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 */
727 static inline GLfloat
728 DOT2(const GLfloat a[2], const GLfloat b[2])
729 {
730 return a[0] * b[0] + a[1] * b[1];
731 }
732
733 static inline GLfloat
734 DOT3(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
739 static inline GLfloat
740 DOT4(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
746 static inline GLfloat
747 LEN_SQUARED_3FV(const GLfloat v[3])
748 {
749 return DOT3(v, v);
750 }
751
752 static inline GLfloat
753 LEN_SQUARED_2FV(const GLfloat v[2])
754 {
755 return DOT2(v, v);
756 }
757
758
759 static inline GLfloat
760 LEN_3FV(const GLfloat v[3])
761 {
762 return sqrtf(LEN_SQUARED_3FV(v));
763 }
764
765 static inline GLfloat
766 LEN_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. */
773 static inline void
774 NORMALIZE_3FV(GLfloat v[3])
775 {
776 GLfloat len = (GLfloat) LEN_SQUARED_3FV(v);
777 if (len) {
778 len = INV_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 */
787 static inline GLboolean
788 DIFFERENT_SIGNS(GLfloat x, GLfloat y)
789 {
790 return signbit(x) != signbit(y);
791 }
792
793
794 /** Compute ceiling of integer quotient of A divided by B. */
795 #define DIV_ROUND_UP( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
796
797
798 /** casts to silence warnings with some compilers */
799 #define ENUM_TO_INT(E) ((GLint)(E))
800 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
801 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
802 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
803
804 /* Compute the size of an array */
805 #ifndef ARRAY_SIZE
806 # define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
807 #endif
808
809 /* Stringify */
810 #define STRINGIFY(x) #x
811
812 #endif