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