3 * A collection of useful macros.
7 * Mesa 3-D graphics library
9 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
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.
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 GLbyte to GLfloat while preserving zero */
58 #define BYTE_TO_FLOATZ(B) ((B) == 0 ? 0.0F : BYTE_TO_FLOAT(B))
61 /** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0], texture/fb data */
62 #define BYTE_TO_FLOAT_TEX(B) ((B) == -128 ? -1.0F : (B) * (1.0F/127.0F))
64 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127], texture/fb data */
65 #define FLOAT_TO_BYTE_TEX(X) CLAMP( (GLint) (127.0F * (X)), -128, 127 )
67 /** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */
68 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
70 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
71 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
74 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
75 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
77 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */
78 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
80 /** Convert GLshort to GLfloat while preserving zero */
81 #define SHORT_TO_FLOATZ(S) ((S) == 0 ? 0.0F : SHORT_TO_FLOAT(S))
84 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0], texture/fb data */
85 #define SHORT_TO_FLOAT_TEX(S) ((S) == -32768 ? -1.0F : (S) * (1.0F/32767.0F))
87 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767], texture/fb data */
88 #define FLOAT_TO_SHORT_TEX(X) ( (GLint) (32767.0F * (X)) )
91 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
92 #define UINT_TO_FLOAT(U) ((GLfloat) ((U) * (1.0F / 4294967295.0)))
94 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
95 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
98 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
99 #define INT_TO_FLOAT(I) ((GLfloat) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0)))
101 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
103 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
105 /* a close approximation: */
106 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
108 /** Convert GLfloat in [-1.0,1.0] to GLint64 in [-(1<<63),(1 << 63) -1] */
109 #define FLOAT_TO_INT64(X) ( (GLint64) (9223372036854775807.0 * (double)(X)) )
112 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0], texture/fb data */
113 #define INT_TO_FLOAT_TEX(I) ((I) == -2147483648 ? -1.0F : (I) * (1.0F/2147483647.0))
115 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647], texture/fb data */
116 #define FLOAT_TO_INT_TEX(X) ( (GLint) (2147483647.0 * (X)) )
119 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
120 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
121 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
122 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
123 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
126 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
127 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
128 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
129 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
130 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
131 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
132 us = ( (GLushort) F_TO_I( CLAMP((f), 0.0F, 1.0F) * 65535.0F) )
133 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
134 us = ( (GLushort) F_TO_I( (f) * 65535.0F) )
136 #define UNCLAMPED_FLOAT_TO_SHORT(s, f) \
137 s = ( (GLshort) F_TO_I( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
140 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
141 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
143 #if defined(USE_IEEE) && !defined(DEBUG)
144 /* This function/macro is sensitive to precision. Test very carefully
147 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
153 else if (__tmp.i >= IEEE_ONE) \
154 UB = (GLubyte) 255; \
156 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
157 UB = (GLubyte) __tmp.i; \
160 #define CLAMPED_FLOAT_TO_UBYTE(UB, F) \
163 __tmp.f = (F) * (255.0F/256.0F) + 32768.0F; \
164 UB = (GLubyte) __tmp.i; \
167 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
168 ub = ((GLubyte) F_TO_I(CLAMP((f), 0.0F, 1.0F) * 255.0F))
169 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
170 ub = ((GLubyte) F_TO_I((f) * 255.0F))
173 static inline GLfloat
INT_AS_FLT(GLint i
)
180 static inline GLfloat
UINT_AS_FLT(GLuint u
)
188 * Convert a floating point value to an unsigned fixed point value.
190 * \param frac_bits The number of bits used to store the fractional part.
192 static INLINE
uint32_t
193 U_FIXED(float value
, uint32_t frac_bits
)
195 value
*= (1 << frac_bits
);
196 return value
< 0.0f
? 0 : (uint32_t) value
;
200 * Convert a floating point value to an signed fixed point value.
202 * \param frac_bits The number of bits used to store the fractional part.
204 static INLINE
int32_t
205 S_FIXED(float value
, uint32_t frac_bits
)
207 return (int32_t) (value
* (1 << frac_bits
));
212 /** Stepping a GLfloat pointer by a byte stride */
213 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
214 /** Stepping a GLuint pointer by a byte stride */
215 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
216 /** Stepping a GLubyte[4] pointer by a byte stride */
217 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
218 /** Stepping a GLfloat[4] pointer by a byte stride */
219 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
220 /** Stepping a \p t pointer by a byte stride */
221 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
224 /**********************************************************************/
225 /** \name 4-element vector operations */
229 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
231 /** Test for equality */
232 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
233 (a)[1] == (b)[1] && \
234 (a)[2] == (b)[2] && \
237 /** Test for equality (unsigned bytes) */
238 static inline GLboolean
239 TEST_EQ_4UBV(const GLubyte a
[4], const GLubyte b
[4])
241 #if defined(__i386__)
242 return *((const GLuint
*) a
) == *((const GLuint
*) b
);
244 return TEST_EQ_4V(a
, b
);
249 /** Copy a 4-element vector */
250 #define COPY_4V( DST, SRC ) \
252 (DST)[0] = (SRC)[0]; \
253 (DST)[1] = (SRC)[1]; \
254 (DST)[2] = (SRC)[2]; \
255 (DST)[3] = (SRC)[3]; \
258 /** Copy a 4-element unsigned byte vector */
260 COPY_4UBV(GLubyte dst
[4], const GLubyte src
[4])
262 #if defined(__i386__)
263 *((GLuint
*) dst
) = *((GLuint
*) src
);
265 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
270 /** Copy a 4-element float vector */
272 COPY_4FV(GLfloat dst
[4], const GLfloat src
[4])
274 /* memcpy seems to be most efficient */
275 memcpy(dst
, src
, sizeof(GLfloat
) * 4);
278 /** Copy \p SZ elements into a 4-element vector */
279 #define COPY_SZ_4V(DST, SZ, SRC) \
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]; \
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) \
293 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
294 COPY_SZ_4V( DST, SZ, SRC ); \
298 #define SUB_4V( DST, SRCA, SRCB ) \
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]; \
307 #define ADD_4V( DST, SRCA, SRCB ) \
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]; \
315 /** Element-wise multiplication */
316 #define SCALE_4V( DST, SRCA, SRCB ) \
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]; \
324 /** In-place addition */
325 #define ACC_4V( DST, SRC ) \
327 (DST)[0] += (SRC)[0]; \
328 (DST)[1] += (SRC)[1]; \
329 (DST)[2] += (SRC)[2]; \
330 (DST)[3] += (SRC)[3]; \
333 /** Element-wise multiplication and addition */
334 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
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]; \
342 /** In-place scalar multiplication and addition */
343 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
345 (DST)[0] += S * (SRCB)[0]; \
346 (DST)[1] += S * (SRCB)[1]; \
347 (DST)[2] += S * (SRCB)[2]; \
348 (DST)[3] += S * (SRCB)[3]; \
351 /** Scalar multiplication */
352 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
354 (DST)[0] = S * (SRCB)[0]; \
355 (DST)[1] = S * (SRCB)[1]; \
356 (DST)[2] = S * (SRCB)[2]; \
357 (DST)[3] = S * (SRCB)[3]; \
360 /** In-place scalar multiplication */
361 #define SELF_SCALE_SCALAR_4V( DST, S ) \
370 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
381 /**********************************************************************/
382 /** \name 3-element vector operations*/
386 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
388 /** Test for equality */
389 #define TEST_EQ_3V(a,b) \
390 ((a)[0] == (b)[0] && \
391 (a)[1] == (b)[1] && \
394 /** Copy a 3-element vector */
395 #define COPY_3V( DST, SRC ) \
397 (DST)[0] = (SRC)[0]; \
398 (DST)[1] = (SRC)[1]; \
399 (DST)[2] = (SRC)[2]; \
402 /** Copy a 3-element vector with cast */
403 #define COPY_3V_CAST( DST, SRC, CAST ) \
405 (DST)[0] = (CAST)(SRC)[0]; \
406 (DST)[1] = (CAST)(SRC)[1]; \
407 (DST)[2] = (CAST)(SRC)[2]; \
410 /** Copy a 3-element float vector */
411 #define COPY_3FV( DST, SRC ) \
413 const GLfloat *_tmp = (SRC); \
414 (DST)[0] = _tmp[0]; \
415 (DST)[1] = _tmp[1]; \
416 (DST)[2] = _tmp[2]; \
420 #define SUB_3V( DST, SRCA, SRCB ) \
422 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
423 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
424 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
428 #define ADD_3V( DST, SRCA, SRCB ) \
430 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
431 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
432 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
435 /** In-place scalar multiplication */
436 #define SCALE_3V( DST, SRCA, SRCB ) \
438 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
439 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
440 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
443 /** In-place element-wise multiplication */
444 #define SELF_SCALE_3V( DST, SRC ) \
446 (DST)[0] *= (SRC)[0]; \
447 (DST)[1] *= (SRC)[1]; \
448 (DST)[2] *= (SRC)[2]; \
451 /** In-place addition */
452 #define ACC_3V( DST, SRC ) \
454 (DST)[0] += (SRC)[0]; \
455 (DST)[1] += (SRC)[1]; \
456 (DST)[2] += (SRC)[2]; \
459 /** Element-wise multiplication and addition */
460 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
462 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
463 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
464 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
467 /** Scalar multiplication */
468 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
470 (DST)[0] = S * (SRCB)[0]; \
471 (DST)[1] = S * (SRCB)[1]; \
472 (DST)[2] = S * (SRCB)[2]; \
475 /** In-place scalar multiplication and addition */
476 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
478 (DST)[0] += S * (SRCB)[0]; \
479 (DST)[1] += S * (SRCB)[1]; \
480 (DST)[2] += S * (SRCB)[2]; \
483 /** In-place scalar multiplication */
484 #define SELF_SCALE_SCALAR_3V( DST, S ) \
491 /** In-place scalar addition */
492 #define ACC_SCALAR_3V( DST, S ) \
500 #define ASSIGN_3V( V, V0, V1, V2 ) \
510 /**********************************************************************/
511 /** \name 2-element vector operations*/
515 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
517 /** Copy a 2-element vector */
518 #define COPY_2V( DST, SRC ) \
520 (DST)[0] = (SRC)[0]; \
521 (DST)[1] = (SRC)[1]; \
524 /** Copy a 2-element vector with cast */
525 #define COPY_2V_CAST( DST, SRC, CAST ) \
527 (DST)[0] = (CAST)(SRC)[0]; \
528 (DST)[1] = (CAST)(SRC)[1]; \
531 /** Copy a 2-element float vector */
532 #define COPY_2FV( DST, SRC ) \
534 const GLfloat *_tmp = (SRC); \
535 (DST)[0] = _tmp[0]; \
536 (DST)[1] = _tmp[1]; \
540 #define SUB_2V( DST, SRCA, SRCB ) \
542 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
543 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
547 #define ADD_2V( DST, SRCA, SRCB ) \
549 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
550 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
553 /** In-place scalar multiplication */
554 #define SCALE_2V( DST, SRCA, SRCB ) \
556 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
557 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
560 /** In-place addition */
561 #define ACC_2V( DST, SRC ) \
563 (DST)[0] += (SRC)[0]; \
564 (DST)[1] += (SRC)[1]; \
567 /** Element-wise multiplication and addition */
568 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
570 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
571 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
574 /** Scalar multiplication */
575 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
577 (DST)[0] = S * (SRCB)[0]; \
578 (DST)[1] = S * (SRCB)[1]; \
581 /** In-place scalar multiplication and addition */
582 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
584 (DST)[0] += S * (SRCB)[0]; \
585 (DST)[1] += S * (SRCB)[1]; \
588 /** In-place scalar multiplication */
589 #define SELF_SCALE_SCALAR_2V( DST, S ) \
595 /** In-place scalar addition */
596 #define ACC_SCALAR_2V( DST, S ) \
602 /** Assign scalers to short vectors */
603 #define ASSIGN_2V( V, V0, V1 ) \
611 /** Copy \p sz elements into a homegeneous (4-element) vector, giving
612 * default values to the remaining components.
613 * The default values are chosen based on \p type.
616 COPY_CLEAN_4V_TYPE_AS_FLOAT(GLfloat dst
[4], int sz
, const GLfloat src
[4],
621 ASSIGN_4V(dst
, 0, 0, 0, 1);
624 ASSIGN_4V(dst
, INT_AS_FLT(0), INT_AS_FLT(0),
625 INT_AS_FLT(0), INT_AS_FLT(1));
627 case GL_UNSIGNED_INT
:
628 ASSIGN_4V(dst
, UINT_AS_FLT(0), UINT_AS_FLT(0),
629 UINT_AS_FLT(0), UINT_AS_FLT(1));
632 ASSIGN_4V(dst
, 0.0f
, 0.0f
, 0.0f
, 1.0f
); /* silence warnings */
633 ASSERT(!"Unexpected type in COPY_CLEAN_4V_TYPE_AS_FLOAT macro");
635 COPY_SZ_4V(dst
, sz
, src
);
638 /** \name Linear interpolation functions */
641 static inline GLfloat
642 LINTERP(GLfloat t
, GLfloat out
, GLfloat in
)
644 return out
+ t
* (in
- out
);
648 INTERP_3F(GLfloat t
, GLfloat dst
[3], const GLfloat out
[3], const GLfloat in
[3])
650 dst
[0] = LINTERP( t
, out
[0], in
[0] );
651 dst
[1] = LINTERP( t
, out
[1], in
[1] );
652 dst
[2] = LINTERP( t
, out
[2], in
[2] );
656 INTERP_4F(GLfloat t
, GLfloat dst
[4], const GLfloat out
[4], const GLfloat in
[4])
658 dst
[0] = LINTERP( t
, out
[0], in
[0] );
659 dst
[1] = LINTERP( t
, out
[1], in
[1] );
660 dst
[2] = LINTERP( t
, out
[2], in
[2] );
661 dst
[3] = LINTERP( t
, out
[3], in
[3] );
668 /** Clamp X to [MIN,MAX] */
669 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
671 /** Minimum of two values: */
672 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
674 /** Maximum of two values: */
675 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
677 /** Minimum and maximum of three values: */
678 #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
679 #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
681 static inline unsigned
682 minify(unsigned value
, unsigned levels
)
684 return MAX2(1, value
>> levels
);
688 * Align a value up to an alignment value
690 * If \c value is not already aligned to the requested alignment value, it
691 * will be rounded up.
693 * \param value Value to be rounded
694 * \param alignment Alignment value to be used. This must be a power of two.
696 * \sa ROUND_DOWN_TO()
698 #define ALIGN(value, alignment) (((value) + (alignment) - 1) & ~((alignment) - 1))
701 * Align a value down to an alignment value
703 * If \c value is not already aligned to the requested alignment value, it
704 * will be rounded down.
706 * \param value Value to be rounded
707 * \param alignment Alignment value to be used. This must be a power of two.
711 #define ROUND_DOWN_TO(value, alignment) ((value) & ~(alignment - 1))
714 /** Cross product of two 3-element vectors */
716 CROSS3(GLfloat n
[3], const GLfloat u
[3], const GLfloat v
[3])
718 n
[0] = u
[1] * v
[2] - u
[2] * v
[1];
719 n
[1] = u
[2] * v
[0] - u
[0] * v
[2];
720 n
[2] = u
[0] * v
[1] - u
[1] * v
[0];
724 /** Dot product of two 2-element vectors */
725 static inline GLfloat
726 DOT2(const GLfloat a
[2], const GLfloat b
[2])
728 return a
[0] * b
[0] + a
[1] * b
[1];
731 static inline GLfloat
732 DOT3(const GLfloat a
[3], const GLfloat b
[3])
734 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
737 static inline GLfloat
738 DOT4(const GLfloat a
[4], const GLfloat b
[4])
740 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
744 static inline GLfloat
745 LEN_SQUARED_3FV(const GLfloat v
[3])
750 static inline GLfloat
751 LEN_SQUARED_2FV(const GLfloat v
[2])
757 static inline GLfloat
758 LEN_3FV(const GLfloat v
[3])
760 return sqrtf(LEN_SQUARED_3FV(v
));
763 static inline GLfloat
764 LEN_2FV(const GLfloat v
[2])
766 return sqrtf(LEN_SQUARED_2FV(v
));
770 /* Normalize a 3-element vector to unit length. */
772 NORMALIZE_3FV(GLfloat v
[3])
774 GLfloat len
= (GLfloat
) LEN_SQUARED_3FV(v
);
776 len
= INV_SQRTF(len
);
784 /** Is float value negative? */
785 static inline GLboolean
788 return signbit(x
) != 0;
791 /** Test two floats have opposite signs */
792 static inline GLboolean
793 DIFFERENT_SIGNS(GLfloat x
, GLfloat y
)
795 return signbit(x
) != signbit(y
);
799 /** Compute ceiling of integer quotient of A divided by B. */
800 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
803 /** casts to silence warnings with some compilers */
804 #define ENUM_TO_INT(E) ((GLint)(E))
805 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
806 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
807 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
809 /* Compute the size of an array */
810 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))