main: Add entry points for ClearNamedBuffer[Sub]Data.
[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 fi_type UINT_AS_UNION(GLuint u)
175 {
176 fi_type tmp;
177 tmp.u = u;
178 return tmp;
179 }
180
181 static inline fi_type INT_AS_UNION(GLint i)
182 {
183 fi_type tmp;
184 tmp.i = i;
185 return tmp;
186 }
187
188 static inline fi_type FLOAT_AS_UNION(GLfloat f)
189 {
190 fi_type tmp;
191 tmp.f = f;
192 return tmp;
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_UNION(fi_type dst[4], int sz, const fi_type src[4],
608 GLenum type)
609 {
610 switch (type) {
611 case GL_FLOAT:
612 ASSIGN_4V(dst, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
613 FLOAT_AS_UNION(0), FLOAT_AS_UNION(1));
614 break;
615 case GL_INT:
616 ASSIGN_4V(dst, INT_AS_UNION(0), INT_AS_UNION(0),
617 INT_AS_UNION(0), INT_AS_UNION(1));
618 break;
619 case GL_UNSIGNED_INT:
620 ASSIGN_4V(dst, UINT_AS_UNION(0), UINT_AS_UNION(0),
621 UINT_AS_UNION(0), UINT_AS_UNION(1));
622 break;
623 default:
624 ASSIGN_4V(dst, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
625 FLOAT_AS_UNION(0), FLOAT_AS_UNION(1)); /* silence warnings */
626 assert(!"Unexpected type in COPY_CLEAN_4V_TYPE_AS_UNION macro");
627 }
628 COPY_SZ_4V(dst, sz, src);
629 }
630
631 /** \name Linear interpolation functions */
632 /*@{*/
633
634 static inline GLfloat
635 LINTERP(GLfloat t, GLfloat out, GLfloat in)
636 {
637 return out + t * (in - out);
638 }
639
640 static inline void
641 INTERP_3F(GLfloat t, GLfloat dst[3], const GLfloat out[3], const GLfloat in[3])
642 {
643 dst[0] = LINTERP( t, out[0], in[0] );
644 dst[1] = LINTERP( t, out[1], in[1] );
645 dst[2] = LINTERP( t, out[2], in[2] );
646 }
647
648 static inline void
649 INTERP_4F(GLfloat t, GLfloat dst[4], const GLfloat out[4], const GLfloat in[4])
650 {
651 dst[0] = LINTERP( t, out[0], in[0] );
652 dst[1] = LINTERP( t, out[1], in[1] );
653 dst[2] = LINTERP( t, out[2], in[2] );
654 dst[3] = LINTERP( t, out[3], in[3] );
655 }
656
657 /*@}*/
658
659
660
661 /** Clamp X to [MIN,MAX] */
662 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
663
664 /** Minimum of two values: */
665 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
666
667 /** Maximum of two values: */
668 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
669
670 /** Minimum and maximum of three values: */
671 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
672 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
673
674 static inline unsigned
675 minify(unsigned value, unsigned levels)
676 {
677 return MAX2(1, value >> levels);
678 }
679
680 /**
681 * Return true if the given value is a power of two.
682 *
683 * Note that this considers 0 a power of two.
684 */
685 static inline bool
686 is_power_of_two(unsigned value)
687 {
688 return (value & (value - 1)) == 0;
689 }
690
691 /**
692 * Align a value up to an alignment value
693 *
694 * If \c value is not already aligned to the requested alignment value, it
695 * will be rounded up.
696 *
697 * \param value Value to be rounded
698 * \param alignment Alignment value to be used. This must be a power of two.
699 *
700 * \sa ROUND_DOWN_TO()
701 */
702 #define ALIGN(value, alignment) (((value) + (alignment) - 1) & ~((alignment) - 1))
703
704 /**
705 * Align a value down to an alignment value
706 *
707 * If \c value is not already aligned to the requested alignment value, it
708 * will be rounded down.
709 *
710 * \param value Value to be rounded
711 * \param alignment Alignment value to be used. This must be a power of two.
712 *
713 * \sa ALIGN()
714 */
715 #define ROUND_DOWN_TO(value, alignment) ((value) & ~(alignment - 1))
716
717
718 /** Cross product of two 3-element vectors */
719 static inline void
720 CROSS3(GLfloat n[3], const GLfloat u[3], const GLfloat v[3])
721 {
722 n[0] = u[1] * v[2] - u[2] * v[1];
723 n[1] = u[2] * v[0] - u[0] * v[2];
724 n[2] = u[0] * v[1] - u[1] * v[0];
725 }
726
727
728 /** Dot product of two 2-element vectors */
729 static inline GLfloat
730 DOT2(const GLfloat a[2], const GLfloat b[2])
731 {
732 return a[0] * b[0] + a[1] * b[1];
733 }
734
735 static inline GLfloat
736 DOT3(const GLfloat a[3], const GLfloat b[3])
737 {
738 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
739 }
740
741 static inline GLfloat
742 DOT4(const GLfloat a[4], const GLfloat b[4])
743 {
744 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
745 }
746
747
748 static inline GLfloat
749 LEN_SQUARED_3FV(const GLfloat v[3])
750 {
751 return DOT3(v, v);
752 }
753
754 static inline GLfloat
755 LEN_SQUARED_2FV(const GLfloat v[2])
756 {
757 return DOT2(v, v);
758 }
759
760
761 static inline GLfloat
762 LEN_3FV(const GLfloat v[3])
763 {
764 return sqrtf(LEN_SQUARED_3FV(v));
765 }
766
767 static inline GLfloat
768 LEN_2FV(const GLfloat v[2])
769 {
770 return sqrtf(LEN_SQUARED_2FV(v));
771 }
772
773
774 /* Normalize a 3-element vector to unit length. */
775 static inline void
776 NORMALIZE_3FV(GLfloat v[3])
777 {
778 GLfloat len = (GLfloat) LEN_SQUARED_3FV(v);
779 if (len) {
780 len = 1.0f / sqrtf(len);
781 v[0] *= len;
782 v[1] *= len;
783 v[2] *= len;
784 }
785 }
786
787
788 /** Test two floats have opposite signs */
789 static inline GLboolean
790 DIFFERENT_SIGNS(GLfloat x, GLfloat y)
791 {
792 return signbit(x) != signbit(y);
793 }
794
795
796 /** Compute ceiling of integer quotient of A divided by B. */
797 #define DIV_ROUND_UP( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
798
799
800 /** casts to silence warnings with some compilers */
801 #define ENUM_TO_INT(E) ((GLint)(E))
802 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
803 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
804 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
805
806
807 /* Stringify */
808 #define STRINGIFY(x) #x
809
810 #endif