mesa: better debug messages in _mesa_test_texobj_completeness()
[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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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 in [-128,127] to GLfloat in [-1.0,1.0], texture/fb data */
58 #define BYTE_TO_FLOAT_TEX(B) ((B) == -128 ? -1.0F : (B) * (1.0F/127.0F))
59
60 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127], texture/fb data */
61 #define FLOAT_TO_BYTE_TEX(X) CLAMP( (GLint) (127.0F * (X)), -128, 127 )
62
63 /** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */
64 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
65
66 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
67 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
68
69
70 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
71 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
72
73 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */
74 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
75
76
77 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0], texture/fb data */
78 #define SHORT_TO_FLOAT_TEX(S) ((S) == -32768 ? -1.0F : (S) * (1.0F/32767.0F))
79
80 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767], texture/fb data */
81 #define FLOAT_TO_SHORT_TEX(X) ( (GLint) (32767.0F * (X)) )
82
83
84 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
85 #define UINT_TO_FLOAT(U) ((GLfloat) ((U) * (1.0F / 4294967295.0)))
86
87 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
88 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
89
90
91 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
92 #define INT_TO_FLOAT(I) ((GLfloat) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0)))
93
94 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
95 /* causes overflow:
96 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
97 */
98 /* a close approximation: */
99 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
100
101 /** Convert GLfloat in [-1.0,1.0] to GLint64 in [-(1<<63),(1 << 63) -1] */
102 #define FLOAT_TO_INT64(X) ( (GLint64) (9223372036854775807.0 * (double)(X)) )
103
104
105 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0], texture/fb data */
106 #define INT_TO_FLOAT_TEX(I) ((I) == -2147483648 ? -1.0F : (I) * (1.0F/2147483647.0))
107
108 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647], texture/fb data */
109 #define FLOAT_TO_INT_TEX(X) ( (GLint) (2147483647.0 * (X)) )
110
111
112 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
113 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
114 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
115 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
116 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
117
118
119 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
120 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
121 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
122 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
123 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
124 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
125 us = ( (GLushort) IROUND( CLAMP((f), 0.0F, 1.0F) * 65535.0F) )
126 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
127 us = ( (GLushort) IROUND( (f) * 65535.0F) )
128
129 #define UNCLAMPED_FLOAT_TO_SHORT(s, f) \
130 s = ( (GLshort) IROUND( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
131
132 /***
133 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
134 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
135 ***/
136 #if defined(USE_IEEE) && !defined(DEBUG)
137 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
138 /* This function/macro is sensitive to precision. Test very carefully
139 * if you change it!
140 */
141 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
142 do { \
143 fi_type __tmp; \
144 __tmp.f = (F); \
145 if (__tmp.i < 0) \
146 UB = (GLubyte) 0; \
147 else if (__tmp.i >= IEEE_0996) \
148 UB = (GLubyte) 255; \
149 else { \
150 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
151 UB = (GLubyte) __tmp.i; \
152 } \
153 } while (0)
154 #define CLAMPED_FLOAT_TO_UBYTE(UB, F) \
155 do { \
156 fi_type __tmp; \
157 __tmp.f = (F) * (255.0F/256.0F) + 32768.0F; \
158 UB = (GLubyte) __tmp.i; \
159 } while (0)
160 #else
161 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
162 ub = ((GLubyte) IROUND(CLAMP((f), 0.0F, 1.0F) * 255.0F))
163 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
164 ub = ((GLubyte) IROUND((f) * 255.0F))
165 #endif
166
167 /*@}*/
168
169
170 /** Stepping a GLfloat pointer by a byte stride */
171 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
172 /** Stepping a GLuint pointer by a byte stride */
173 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
174 /** Stepping a GLubyte[4] pointer by a byte stride */
175 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
176 /** Stepping a GLfloat[4] pointer by a byte stride */
177 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
178 /** Stepping a \p t pointer by a byte stride */
179 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
180
181
182 /**********************************************************************/
183 /** \name 4-element vector operations */
184 /*@{*/
185
186 /** Zero */
187 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
188
189 /** Test for equality */
190 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
191 (a)[1] == (b)[1] && \
192 (a)[2] == (b)[2] && \
193 (a)[3] == (b)[3])
194
195 /** Test for equality (unsigned bytes) */
196 #if defined(__i386__)
197 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
198 #else
199 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
200 #endif
201
202 /** Copy a 4-element vector */
203 #define COPY_4V( DST, SRC ) \
204 do { \
205 (DST)[0] = (SRC)[0]; \
206 (DST)[1] = (SRC)[1]; \
207 (DST)[2] = (SRC)[2]; \
208 (DST)[3] = (SRC)[3]; \
209 } while (0)
210
211 /** Copy a 4-element vector with cast */
212 #define COPY_4V_CAST( DST, SRC, CAST ) \
213 do { \
214 (DST)[0] = (CAST)(SRC)[0]; \
215 (DST)[1] = (CAST)(SRC)[1]; \
216 (DST)[2] = (CAST)(SRC)[2]; \
217 (DST)[3] = (CAST)(SRC)[3]; \
218 } while (0)
219
220 /** Copy a 4-element unsigned byte vector */
221 #if defined(__i386__)
222 #define COPY_4UBV(DST, SRC) \
223 do { \
224 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
225 } while (0)
226 #else
227 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
228 #define COPY_4UBV(DST, SRC) \
229 do { \
230 (DST)[0] = (SRC)[0]; \
231 (DST)[1] = (SRC)[1]; \
232 (DST)[2] = (SRC)[2]; \
233 (DST)[3] = (SRC)[3]; \
234 } while (0)
235 #endif
236
237 /**
238 * Copy a 4-element float vector
239 * memcpy seems to be most efficient
240 */
241 #define COPY_4FV( DST, SRC ) \
242 do { \
243 memcpy(DST, SRC, sizeof(GLfloat) * 4); \
244 } while (0)
245
246 /** Copy \p SZ elements into a 4-element vector */
247 #define COPY_SZ_4V(DST, SZ, SRC) \
248 do { \
249 switch (SZ) { \
250 case 4: (DST)[3] = (SRC)[3]; \
251 case 3: (DST)[2] = (SRC)[2]; \
252 case 2: (DST)[1] = (SRC)[1]; \
253 case 1: (DST)[0] = (SRC)[0]; \
254 } \
255 } while(0)
256
257 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
258 * default values to the remaining */
259 #define COPY_CLEAN_4V(DST, SZ, SRC) \
260 do { \
261 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
262 COPY_SZ_4V( DST, SZ, SRC ); \
263 } while (0)
264
265 /** Subtraction */
266 #define SUB_4V( DST, SRCA, SRCB ) \
267 do { \
268 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
269 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
270 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
271 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
272 } while (0)
273
274 /** Addition */
275 #define ADD_4V( DST, SRCA, SRCB ) \
276 do { \
277 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
278 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
279 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
280 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
281 } while (0)
282
283 /** Element-wise multiplication */
284 #define SCALE_4V( DST, SRCA, SRCB ) \
285 do { \
286 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
287 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
288 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
289 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
290 } while (0)
291
292 /** In-place addition */
293 #define ACC_4V( DST, SRC ) \
294 do { \
295 (DST)[0] += (SRC)[0]; \
296 (DST)[1] += (SRC)[1]; \
297 (DST)[2] += (SRC)[2]; \
298 (DST)[3] += (SRC)[3]; \
299 } while (0)
300
301 /** Element-wise multiplication and addition */
302 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
303 do { \
304 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
305 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
306 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
307 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
308 } while (0)
309
310 /** In-place scalar multiplication and addition */
311 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
312 do { \
313 (DST)[0] += S * (SRCB)[0]; \
314 (DST)[1] += S * (SRCB)[1]; \
315 (DST)[2] += S * (SRCB)[2]; \
316 (DST)[3] += S * (SRCB)[3]; \
317 } while (0)
318
319 /** Scalar multiplication */
320 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
321 do { \
322 (DST)[0] = S * (SRCB)[0]; \
323 (DST)[1] = S * (SRCB)[1]; \
324 (DST)[2] = S * (SRCB)[2]; \
325 (DST)[3] = S * (SRCB)[3]; \
326 } while (0)
327
328 /** In-place scalar multiplication */
329 #define SELF_SCALE_SCALAR_4V( DST, S ) \
330 do { \
331 (DST)[0] *= S; \
332 (DST)[1] *= S; \
333 (DST)[2] *= S; \
334 (DST)[3] *= S; \
335 } while (0)
336
337 /** Assignment */
338 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
339 do { \
340 V[0] = V0; \
341 V[1] = V1; \
342 V[2] = V2; \
343 V[3] = V3; \
344 } while(0)
345
346 /*@}*/
347
348
349 /**********************************************************************/
350 /** \name 3-element vector operations*/
351 /*@{*/
352
353 /** Zero */
354 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
355
356 /** Test for equality */
357 #define TEST_EQ_3V(a,b) \
358 ((a)[0] == (b)[0] && \
359 (a)[1] == (b)[1] && \
360 (a)[2] == (b)[2])
361
362 /** Copy a 3-element vector */
363 #define COPY_3V( DST, SRC ) \
364 do { \
365 (DST)[0] = (SRC)[0]; \
366 (DST)[1] = (SRC)[1]; \
367 (DST)[2] = (SRC)[2]; \
368 } while (0)
369
370 /** Copy a 3-element vector with cast */
371 #define COPY_3V_CAST( DST, SRC, CAST ) \
372 do { \
373 (DST)[0] = (CAST)(SRC)[0]; \
374 (DST)[1] = (CAST)(SRC)[1]; \
375 (DST)[2] = (CAST)(SRC)[2]; \
376 } while (0)
377
378 /** Copy a 3-element float vector */
379 #define COPY_3FV( DST, SRC ) \
380 do { \
381 const GLfloat *_tmp = (SRC); \
382 (DST)[0] = _tmp[0]; \
383 (DST)[1] = _tmp[1]; \
384 (DST)[2] = _tmp[2]; \
385 } while (0)
386
387 /** Subtraction */
388 #define SUB_3V( DST, SRCA, SRCB ) \
389 do { \
390 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
391 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
392 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
393 } while (0)
394
395 /** Addition */
396 #define ADD_3V( DST, SRCA, SRCB ) \
397 do { \
398 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
399 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
400 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
401 } while (0)
402
403 /** In-place scalar multiplication */
404 #define SCALE_3V( DST, SRCA, SRCB ) \
405 do { \
406 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
407 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
408 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
409 } while (0)
410
411 /** In-place element-wise multiplication */
412 #define SELF_SCALE_3V( DST, SRC ) \
413 do { \
414 (DST)[0] *= (SRC)[0]; \
415 (DST)[1] *= (SRC)[1]; \
416 (DST)[2] *= (SRC)[2]; \
417 } while (0)
418
419 /** In-place addition */
420 #define ACC_3V( DST, SRC ) \
421 do { \
422 (DST)[0] += (SRC)[0]; \
423 (DST)[1] += (SRC)[1]; \
424 (DST)[2] += (SRC)[2]; \
425 } while (0)
426
427 /** Element-wise multiplication and addition */
428 #define ACC_SCALE_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 /** Scalar multiplication */
436 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
437 do { \
438 (DST)[0] = S * (SRCB)[0]; \
439 (DST)[1] = S * (SRCB)[1]; \
440 (DST)[2] = S * (SRCB)[2]; \
441 } while (0)
442
443 /** In-place scalar multiplication and addition */
444 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
445 do { \
446 (DST)[0] += S * (SRCB)[0]; \
447 (DST)[1] += S * (SRCB)[1]; \
448 (DST)[2] += S * (SRCB)[2]; \
449 } while (0)
450
451 /** In-place scalar multiplication */
452 #define SELF_SCALE_SCALAR_3V( DST, S ) \
453 do { \
454 (DST)[0] *= S; \
455 (DST)[1] *= S; \
456 (DST)[2] *= S; \
457 } while (0)
458
459 /** In-place scalar addition */
460 #define ACC_SCALAR_3V( DST, S ) \
461 do { \
462 (DST)[0] += S; \
463 (DST)[1] += S; \
464 (DST)[2] += S; \
465 } while (0)
466
467 /** Assignment */
468 #define ASSIGN_3V( V, V0, V1, V2 ) \
469 do { \
470 V[0] = V0; \
471 V[1] = V1; \
472 V[2] = V2; \
473 } while(0)
474
475 /*@}*/
476
477
478 /**********************************************************************/
479 /** \name 2-element vector operations*/
480 /*@{*/
481
482 /** Zero */
483 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
484
485 /** Copy a 2-element vector */
486 #define COPY_2V( DST, SRC ) \
487 do { \
488 (DST)[0] = (SRC)[0]; \
489 (DST)[1] = (SRC)[1]; \
490 } while (0)
491
492 /** Copy a 2-element vector with cast */
493 #define COPY_2V_CAST( DST, SRC, CAST ) \
494 do { \
495 (DST)[0] = (CAST)(SRC)[0]; \
496 (DST)[1] = (CAST)(SRC)[1]; \
497 } while (0)
498
499 /** Copy a 2-element float vector */
500 #define COPY_2FV( DST, SRC ) \
501 do { \
502 const GLfloat *_tmp = (SRC); \
503 (DST)[0] = _tmp[0]; \
504 (DST)[1] = _tmp[1]; \
505 } while (0)
506
507 /** Subtraction */
508 #define SUB_2V( DST, SRCA, SRCB ) \
509 do { \
510 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
511 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
512 } while (0)
513
514 /** Addition */
515 #define ADD_2V( DST, SRCA, SRCB ) \
516 do { \
517 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
518 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
519 } while (0)
520
521 /** In-place scalar multiplication */
522 #define SCALE_2V( DST, SRCA, SRCB ) \
523 do { \
524 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
525 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
526 } while (0)
527
528 /** In-place addition */
529 #define ACC_2V( DST, SRC ) \
530 do { \
531 (DST)[0] += (SRC)[0]; \
532 (DST)[1] += (SRC)[1]; \
533 } while (0)
534
535 /** Element-wise multiplication and addition */
536 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
537 do { \
538 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
539 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
540 } while (0)
541
542 /** Scalar multiplication */
543 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
544 do { \
545 (DST)[0] = S * (SRCB)[0]; \
546 (DST)[1] = S * (SRCB)[1]; \
547 } while (0)
548
549 /** In-place scalar multiplication and addition */
550 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
551 do { \
552 (DST)[0] += S * (SRCB)[0]; \
553 (DST)[1] += S * (SRCB)[1]; \
554 } while (0)
555
556 /** In-place scalar multiplication */
557 #define SELF_SCALE_SCALAR_2V( DST, S ) \
558 do { \
559 (DST)[0] *= S; \
560 (DST)[1] *= S; \
561 } while (0)
562
563 /** In-place scalar addition */
564 #define ACC_SCALAR_2V( DST, S ) \
565 do { \
566 (DST)[0] += S; \
567 (DST)[1] += S; \
568 } while (0)
569
570 /** Assign scalers to short vectors */
571 #define ASSIGN_2V( V, V0, V1 ) \
572 do { \
573 V[0] = V0; \
574 V[1] = V1; \
575 } while(0)
576
577 /*@}*/
578
579
580 /** \name Linear interpolation macros */
581 /*@{*/
582
583 /**
584 * Linear interpolation
585 *
586 * \note \p OUT argument is evaluated twice!
587 * \note Be wary of using *coord++ as an argument to any of these macros!
588 */
589 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
590
591 /* Can do better with integer math
592 */
593 #define INTERP_UB( t, dstub, outub, inub ) \
594 do { \
595 GLfloat inf = UBYTE_TO_FLOAT( inub ); \
596 GLfloat outf = UBYTE_TO_FLOAT( outub ); \
597 GLfloat dstf = LINTERP( t, outf, inf ); \
598 UNCLAMPED_FLOAT_TO_UBYTE( dstub, dstf ); \
599 } while (0)
600
601 #define INTERP_UI( t, dstui, outui, inui ) \
602 dstui = (GLuint) (GLint) LINTERP( (t), (GLfloat) (outui), (GLfloat) (inui) )
603
604 #define INTERP_F( t, dstf, outf, inf ) \
605 dstf = LINTERP( t, outf, inf )
606
607 #define INTERP_4F( t, dst, out, in ) \
608 do { \
609 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
610 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
611 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
612 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
613 } while (0)
614
615 #define INTERP_3F( t, dst, out, in ) \
616 do { \
617 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
618 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
619 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
620 } while (0)
621
622 #define INTERP_SZ( t, vec, to, out, in, sz ) \
623 do { \
624 switch (sz) { \
625 case 4: vec[to][3] = LINTERP( (t), (vec)[out][3], (vec)[in][3] ); \
626 case 3: vec[to][2] = LINTERP( (t), (vec)[out][2], (vec)[in][2] ); \
627 case 2: vec[to][1] = LINTERP( (t), (vec)[out][1], (vec)[in][1] ); \
628 case 1: vec[to][0] = LINTERP( (t), (vec)[out][0], (vec)[in][0] ); \
629 } \
630 } while(0)
631
632 /*@}*/
633
634
635
636 /** Clamp X to [MIN,MAX] */
637 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
638
639 /** Minimum of two values: */
640 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
641
642 /** Maximum of two values: */
643 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
644
645 /** Minimum and maximum of three values: */
646 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
647 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
648
649 /** Dot product of two 2-element vectors */
650 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
651
652 /** Dot product of two 3-element vectors */
653 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
654
655 /** Dot product of two 4-element vectors */
656 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
657 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
658
659 /** Dot product of two 4-element vectors */
660 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
661
662
663 /** Cross product of two 3-element vectors */
664 #define CROSS3(n, u, v) \
665 do { \
666 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
667 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
668 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
669 } while (0)
670
671
672 /* Normalize a 3-element vector to unit length. */
673 #define NORMALIZE_3FV( V ) \
674 do { \
675 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
676 if (len) { \
677 len = INV_SQRTF(len); \
678 (V)[0] = (GLfloat) ((V)[0] * len); \
679 (V)[1] = (GLfloat) ((V)[1] * len); \
680 (V)[2] = (GLfloat) ((V)[2] * len); \
681 } \
682 } while(0)
683
684 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
685 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
686
687 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
688 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])
689
690
691 /** casts to silence warnings with some compilers */
692 #define ENUM_TO_INT(E) ((GLint)(E))
693 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
694 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
695 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
696
697
698 #endif