3 * A collection of useful macros.
7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
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:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
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.
38 * \name Integer / float conversion for colors, normals, etc.
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)]
46 /** Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
47 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) ((X) * 255.0F))
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))
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 )
57 /** Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
58 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
60 /** Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
61 #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
63 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
64 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
66 /** Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
67 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
70 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
71 #define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F))
73 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
74 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
77 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
78 #define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
80 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
82 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
84 /* a close approximation: */
85 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
88 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
89 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
90 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
91 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
92 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
95 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
96 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
97 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
98 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
99 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
100 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
101 us = ( (GLushort) IROUND( CLAMP((f), 0.0, 1.0) * 65535.0F) )
102 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
103 us = ( (GLushort) IROUND( (f) * 65535.0F) )
106 /** Stepping a GLfloat pointer by a byte stride */
107 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
108 /** Stepping a GLuint pointer by a byte stride */
109 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
110 /** Stepping a GLubyte[4] pointer by a byte stride */
111 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
112 /** Stepping a GLchan[4] pointer by a byte stride */
113 #define STRIDE_4CHAN(p, i) (p = (GLchan (*)[4])((GLubyte *)p + i))
114 /** Stepping a GLchan pointer by a byte stride */
115 #define STRIDE_CHAN(p, i) (p = (GLchan *)((GLubyte *)p + i))
116 /** Stepping a \p t pointer by a byte stride */
117 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
120 /**********************************************************************/
121 /** \name 4-element vector operations */
125 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
127 /** Test for equality */
128 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
129 (a)[1] == (b)[1] && \
130 (a)[2] == (b)[2] && \
133 /** Test for equality (unsigned bytes) */
134 #if defined(__i386__)
135 #define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC))
137 #define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC)
140 /** Copy a 4-element vector */
141 #define COPY_4V( DST, SRC ) \
143 (DST)[0] = (SRC)[0]; \
144 (DST)[1] = (SRC)[1]; \
145 (DST)[2] = (SRC)[2]; \
146 (DST)[3] = (SRC)[3]; \
149 /** Copy a 4-element unsigned byte vector */
150 #if defined(__i386__)
151 #define COPY_4UBV(DST, SRC) \
153 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
156 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
157 #define COPY_4UBV(DST, SRC) \
159 (DST)[0] = (SRC)[0]; \
160 (DST)[1] = (SRC)[1]; \
161 (DST)[2] = (SRC)[2]; \
162 (DST)[3] = (SRC)[3]; \
166 /** Copy a 4-element float vector */
167 #define COPY_4FV( DST, SRC ) \
169 const GLfloat *_tmp = (SRC); \
170 (DST)[0] = _tmp[0]; \
171 (DST)[1] = _tmp[1]; \
172 (DST)[2] = _tmp[2]; \
173 (DST)[3] = _tmp[3]; \
177 /** Copy \p SZ elements into a 4-element vector */
178 #define COPY_SZ_4V(DST, SZ, SRC) \
181 case 4: (DST)[3] = (SRC)[3]; \
182 case 3: (DST)[2] = (SRC)[2]; \
183 case 2: (DST)[1] = (SRC)[1]; \
184 case 1: (DST)[0] = (SRC)[0]; \
188 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
189 * default values to the remaining */
190 #define COPY_CLEAN_4V(DST, SZ, SRC) \
192 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
193 COPY_SZ_4V( DST, SZ, SRC ); \
197 #define SUB_4V( DST, SRCA, SRCB ) \
199 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
200 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
201 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
202 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
206 #define ADD_4V( DST, SRCA, SRCB ) \
208 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
209 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
210 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
211 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
214 /** Element-wise multiplication */
215 #define SCALE_4V( DST, SRCA, SRCB ) \
217 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
218 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
219 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
220 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
223 /** In-place addition */
224 #define ACC_4V( DST, SRC ) \
226 (DST)[0] += (SRC)[0]; \
227 (DST)[1] += (SRC)[1]; \
228 (DST)[2] += (SRC)[2]; \
229 (DST)[3] += (SRC)[3]; \
232 /** Element-wise multiplication and addition */
233 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
235 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
236 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
237 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
238 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
241 /** In-place scalar multiplication and addition */
242 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
244 (DST)[0] += S * (SRCB)[0]; \
245 (DST)[1] += S * (SRCB)[1]; \
246 (DST)[2] += S * (SRCB)[2]; \
247 (DST)[3] += S * (SRCB)[3]; \
250 /** Scalar multiplication */
251 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
253 (DST)[0] = S * (SRCB)[0]; \
254 (DST)[1] = S * (SRCB)[1]; \
255 (DST)[2] = S * (SRCB)[2]; \
256 (DST)[3] = S * (SRCB)[3]; \
259 /** In-place scalar multiplication */
260 #define SELF_SCALE_SCALAR_4V( DST, S ) \
269 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
280 /**********************************************************************/
281 /** \name 3-element vector operations*/
285 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
287 /** Test for equality */
288 #define TEST_EQ_3V(a,b) ((a)[0] == (b)[0] && \
289 (a)[1] == (b)[1] && \
292 /** Copy a 3-element vector */
293 #define COPY_3V( DST, SRC ) \
295 (DST)[0] = (SRC)[0]; \
296 (DST)[1] = (SRC)[1]; \
297 (DST)[2] = (SRC)[2]; \
300 /** Copy a 3-element vector with cast */
301 #define COPY_3V_CAST( DST, SRC, CAST ) \
303 (DST)[0] = (CAST)(SRC)[0]; \
304 (DST)[1] = (CAST)(SRC)[1]; \
305 (DST)[2] = (CAST)(SRC)[2]; \
308 /** Copy a 3-element float vector */
309 #define COPY_3FV( DST, SRC ) \
311 const GLfloat *_tmp = (SRC); \
312 (DST)[0] = _tmp[0]; \
313 (DST)[1] = _tmp[1]; \
314 (DST)[2] = _tmp[2]; \
318 #define SUB_3V( DST, SRCA, SRCB ) \
320 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
321 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
322 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
326 #define ADD_3V( DST, SRCA, SRCB ) \
328 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
329 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
330 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
333 /** In-place scalar multiplication */
334 #define SCALE_3V( DST, SRCA, SRCB ) \
336 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
337 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
338 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
341 /** In-place element-wise multiplication */
342 #define SELF_SCALE_3V( DST, SRC ) \
344 (DST)[0] *= (SRC)[0]; \
345 (DST)[1] *= (SRC)[1]; \
346 (DST)[2] *= (SRC)[2]; \
349 /** In-place addition */
350 #define ACC_3V( DST, SRC ) \
352 (DST)[0] += (SRC)[0]; \
353 (DST)[1] += (SRC)[1]; \
354 (DST)[2] += (SRC)[2]; \
357 /** Element-wise multiplication and addition */
358 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
360 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
361 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
362 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
365 /** Scalar multiplication */
366 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
368 (DST)[0] = S * (SRCB)[0]; \
369 (DST)[1] = S * (SRCB)[1]; \
370 (DST)[2] = S * (SRCB)[2]; \
373 /** In-place scalar multiplication and addition */
374 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
376 (DST)[0] += S * (SRCB)[0]; \
377 (DST)[1] += S * (SRCB)[1]; \
378 (DST)[2] += S * (SRCB)[2]; \
381 /** In-place scalar multiplication */
382 #define SELF_SCALE_SCALAR_3V( DST, S ) \
389 /** In-place scalar addition */
390 #define ACC_SCALAR_3V( DST, S ) \
398 #define ASSIGN_3V( V, V0, V1, V2 ) \
408 /**********************************************************************/
409 /** \name 2-element vector operations*/
413 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
415 /** Copy a 2-element vector */
416 #define COPY_2V( DST, SRC ) \
418 (DST)[0] = (SRC)[0]; \
419 (DST)[1] = (SRC)[1]; \
422 /** Copy a 2-element vector with cast */
423 #define COPY_2V_CAST( DST, SRC, CAST ) \
425 (DST)[0] = (CAST)(SRC)[0]; \
426 (DST)[1] = (CAST)(SRC)[1]; \
429 /** Copy a 2-element float vector */
430 #define COPY_2FV( DST, SRC ) \
432 const GLfloat *_tmp = (SRC); \
433 (DST)[0] = _tmp[0]; \
434 (DST)[1] = _tmp[1]; \
438 #define SUB_2V( DST, SRCA, SRCB ) \
440 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
441 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
445 #define ADD_2V( DST, SRCA, SRCB ) \
447 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
448 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
451 /** In-place scalar multiplication */
452 #define SCALE_2V( DST, SRCA, SRCB ) \
454 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
455 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
458 /** In-place addition */
459 #define ACC_2V( DST, SRC ) \
461 (DST)[0] += (SRC)[0]; \
462 (DST)[1] += (SRC)[1]; \
465 /** Element-wise multiplication and addition */
466 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
468 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
469 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
472 /** Scalar multiplication */
473 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
475 (DST)[0] = S * (SRCB)[0]; \
476 (DST)[1] = S * (SRCB)[1]; \
479 /** In-place scalar multiplication and addition */
480 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
482 (DST)[0] += S * (SRCB)[0]; \
483 (DST)[1] += S * (SRCB)[1]; \
486 /** In-place scalar multiplication */
487 #define SELF_SCALE_SCALAR_2V( DST, S ) \
493 /** In-place scalar addition */
494 #define ACC_SCALAR_2V( DST, S ) \
503 * Linear interpolation
505 * \note \p OUT argument is evaluated twice!
506 * \note Be wary of using *coord++ as an argument to any of these macros!
508 #define LINTERP(T, OUT, IN) ((OUT) + (T) * ((IN) - (OUT)))
510 /* Can do better with integer math
512 #define INTERP_UB( t, dstub, outub, inub ) \
514 GLfloat inf = UBYTE_TO_FLOAT( inub ); \
515 GLfloat outf = UBYTE_TO_FLOAT( outub ); \
516 GLfloat dstf = LINTERP( t, outf, inf ); \
517 UNCLAMPED_FLOAT_TO_UBYTE( dstub, dstf ); \
520 #define INTERP_CHAN( t, dstc, outc, inc ) \
522 GLfloat inf = CHAN_TO_FLOAT( inc ); \
523 GLfloat outf = CHAN_TO_FLOAT( outc ); \
524 GLfloat dstf = LINTERP( t, outf, inf ); \
525 UNCLAMPED_FLOAT_TO_CHAN( dstc, dstf ); \
528 #define INTERP_UI( t, dstui, outui, inui ) \
529 dstui = (GLuint) (GLint) LINTERP( (t), (GLfloat) (outui), (GLfloat) (inui) )
531 #define INTERP_F( t, dstf, outf, inf ) \
532 dstf = LINTERP( t, outf, inf )
534 #define INTERP_4F( t, dst, out, in ) \
536 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
537 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
538 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
539 dst[3] = LINTERP( (t), (out)[3], (in)[3] ); \
542 #define INTERP_3F( t, dst, out, in ) \
544 dst[0] = LINTERP( (t), (out)[0], (in)[0] ); \
545 dst[1] = LINTERP( (t), (out)[1], (in)[1] ); \
546 dst[2] = LINTERP( (t), (out)[2], (in)[2] ); \
549 #define INTERP_4CHAN( t, dst, out, in ) \
551 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
552 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
553 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
554 INTERP_CHAN( (t), (dst)[3], (out)[3], (in)[3] ); \
557 #define INTERP_3CHAN( t, dst, out, in ) \
559 INTERP_CHAN( (t), (dst)[0], (out)[0], (in)[0] ); \
560 INTERP_CHAN( (t), (dst)[1], (out)[1], (in)[1] ); \
561 INTERP_CHAN( (t), (dst)[2], (out)[2], (in)[2] ); \
564 #define INTERP_SZ( t, vec, to, out, in, sz ) \
567 case 4: vec[to][3] = LINTERP( (t), (vec)[out][3], (vec)[in][3] ); \
568 case 3: vec[to][2] = LINTERP( (t), (vec)[out][2], (vec)[in][2] ); \
569 case 2: vec[to][1] = LINTERP( (t), (vec)[out][1], (vec)[in][1] ); \
570 case 1: vec[to][0] = LINTERP( (t), (vec)[out][0], (vec)[in][0] ); \
576 /** Assign scalers to short vectors */
577 #define ASSIGN_2V( V, V0, V1 ) \
587 /** Clamp X to [MIN,MAX] */
588 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
590 /** Assign X to CLAMP(X, MIN, MAX) */
591 #define CLAMP_SELF(x, mn, mx) \
592 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
596 /** Minimum of two values: */
597 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
599 /** Maximum of two values: */
600 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
602 /** Dot product of two 2-element vectors */
603 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
605 /** Dot product of two 3-element vectors */
606 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
608 /** Dot product of two 4-element vectors */
609 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
610 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
612 /** Dot product of two 4-element vectors */
613 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
616 /** Cross product of two 3-element vectors */
617 #define CROSS3(n, u, v) \
619 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
620 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
621 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
625 /* Normalize a 3-element vector to unit length. */
626 #define NORMALIZE_3FV( V ) \
628 GLfloat len = (GLfloat) LEN_SQUARED_3FV(V); \
630 len = INV_SQRTF(len); \
631 (V)[0] = (GLfloat) ((V)[0] * len); \
632 (V)[1] = (GLfloat) ((V)[1] * len); \
633 (V)[2] = (GLfloat) ((V)[2] * len); \
637 #define LEN_3FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2]))
638 #define LEN_2FV( V ) (SQRTF((V)[0]*(V)[0]+(V)[1]*(V)[1]))
640 #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
641 #define LEN_SQUARED_2FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1])