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.
34 #include "util/u_math.h"
39 * \name Integer / float conversion for colors, normals, etc.
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)]
47 /** Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
48 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) ((X) * 255.0F))
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))
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 )
58 /** Convert GLbyte to GLfloat while preserving zero */
59 #define BYTE_TO_FLOATZ(B) ((B) == 0 ? 0.0F : BYTE_TO_FLOAT(B))
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))
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 )
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))
71 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
72 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
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))
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 )
81 /** Convert GLshort to GLfloat while preserving zero */
82 #define SHORT_TO_FLOATZ(S) ((S) == 0 ? 0.0F : SHORT_TO_FLOAT(S))
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))
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)) )
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)))
95 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
96 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
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)))
102 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
104 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
106 /* a close approximation: */
107 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
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)) )
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))
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)) )
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))
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) )
137 #define UNCLAMPED_FLOAT_TO_SHORT(s, f) \
138 s = ( (GLshort) F_TO_I( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
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]
145 /* This function/macro is sensitive to precision. Test very carefully
148 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, FLT) \
154 else if (__tmp.i >= IEEE_ONE) \
155 UB = (GLubyte) 255; \
157 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
158 UB = (GLubyte) __tmp.i; \
161 #define CLAMPED_FLOAT_TO_UBYTE(UB, FLT) \
164 __tmp.f = (FLT) * (255.0F/256.0F) + 32768.0F; \
165 UB = (GLubyte) __tmp.i; \
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))
174 static fi_type
UINT_AS_UNION(GLuint u
)
181 static inline fi_type
INT_AS_UNION(GLint i
)
188 static inline fi_type
FLOAT_AS_UNION(GLfloat f
)
196 * Convert a floating point value to an unsigned fixed point value.
198 * \param frac_bits The number of bits used to store the fractional part.
200 static inline uint32_t
201 U_FIXED(float value
, uint32_t frac_bits
)
203 value
*= (1 << frac_bits
);
204 return value
< 0.0f
? 0 : (uint32_t) value
;
208 * Convert a floating point value to an signed fixed point value.
210 * \param frac_bits The number of bits used to store the fractional part.
212 static inline int32_t
213 S_FIXED(float value
, uint32_t frac_bits
)
215 return (int32_t) (value
* (1 << frac_bits
));
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))
232 /**********************************************************************/
233 /** \name 4-element vector operations */
237 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
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] && \
245 /** Test for equality (unsigned bytes) */
246 static inline GLboolean
247 TEST_EQ_4UBV(const GLubyte a
[4], const GLubyte b
[4])
249 #if defined(__i386__)
250 return *((const GLuint
*) a
) == *((const GLuint
*) b
);
252 return TEST_EQ_4V(a
, b
);
257 /** Copy a 4-element vector */
258 #define COPY_4V( DST, SRC ) \
260 (DST)[0] = (SRC)[0]; \
261 (DST)[1] = (SRC)[1]; \
262 (DST)[2] = (SRC)[2]; \
263 (DST)[3] = (SRC)[3]; \
266 /** Copy a 4-element unsigned byte vector */
268 COPY_4UBV(GLubyte dst
[4], const GLubyte src
[4])
270 #if defined(__i386__)
271 *((GLuint
*) dst
) = *((GLuint
*) src
);
273 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
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 ) \
372 /**********************************************************************/
373 /** \name 3-element vector operations*/
377 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
379 /** Test for equality */
380 #define TEST_EQ_3V(a,b) \
381 ((a)[0] == (b)[0] && \
382 (a)[1] == (b)[1] && \
385 /** Copy a 3-element vector */
386 #define COPY_3V( DST, SRC ) \
388 (DST)[0] = (SRC)[0]; \
389 (DST)[1] = (SRC)[1]; \
390 (DST)[2] = (SRC)[2]; \
393 /** Copy a 3-element vector with cast */
394 #define COPY_3V_CAST( DST, SRC, CAST ) \
396 (DST)[0] = (CAST)(SRC)[0]; \
397 (DST)[1] = (CAST)(SRC)[1]; \
398 (DST)[2] = (CAST)(SRC)[2]; \
401 /** Copy a 3-element float vector */
402 #define COPY_3FV( DST, SRC ) \
404 const GLfloat *_tmp = (SRC); \
405 (DST)[0] = _tmp[0]; \
406 (DST)[1] = _tmp[1]; \
407 (DST)[2] = _tmp[2]; \
411 #define SUB_3V( DST, SRCA, SRCB ) \
413 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
414 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
415 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
419 #define ADD_3V( DST, SRCA, SRCB ) \
421 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
422 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
423 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
426 /** In-place scalar multiplication */
427 #define SCALE_3V( DST, SRCA, SRCB ) \
429 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
430 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
431 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
434 /** In-place element-wise multiplication */
435 #define SELF_SCALE_3V( DST, SRC ) \
437 (DST)[0] *= (SRC)[0]; \
438 (DST)[1] *= (SRC)[1]; \
439 (DST)[2] *= (SRC)[2]; \
442 /** In-place addition */
443 #define ACC_3V( DST, SRC ) \
445 (DST)[0] += (SRC)[0]; \
446 (DST)[1] += (SRC)[1]; \
447 (DST)[2] += (SRC)[2]; \
450 /** Element-wise multiplication and addition */
451 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
453 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
454 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
455 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
458 /** Scalar multiplication */
459 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
461 (DST)[0] = S * (SRCB)[0]; \
462 (DST)[1] = S * (SRCB)[1]; \
463 (DST)[2] = S * (SRCB)[2]; \
466 /** In-place scalar multiplication and addition */
467 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
469 (DST)[0] += S * (SRCB)[0]; \
470 (DST)[1] += S * (SRCB)[1]; \
471 (DST)[2] += S * (SRCB)[2]; \
474 /** In-place scalar multiplication */
475 #define SELF_SCALE_SCALAR_3V( DST, S ) \
482 /** In-place scalar addition */
483 #define ACC_SCALAR_3V( DST, S ) \
491 #define ASSIGN_3V( V, V0, V1, V2 ) \
501 /**********************************************************************/
502 /** \name 2-element vector operations*/
506 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
508 /** Copy a 2-element vector */
509 #define COPY_2V( DST, SRC ) \
511 (DST)[0] = (SRC)[0]; \
512 (DST)[1] = (SRC)[1]; \
515 /** Copy a 2-element vector with cast */
516 #define COPY_2V_CAST( DST, SRC, CAST ) \
518 (DST)[0] = (CAST)(SRC)[0]; \
519 (DST)[1] = (CAST)(SRC)[1]; \
522 /** Copy a 2-element float vector */
523 #define COPY_2FV( DST, SRC ) \
525 const GLfloat *_tmp = (SRC); \
526 (DST)[0] = _tmp[0]; \
527 (DST)[1] = _tmp[1]; \
531 #define SUB_2V( DST, SRCA, SRCB ) \
533 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
534 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
538 #define ADD_2V( DST, SRCA, SRCB ) \
540 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
541 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
544 /** In-place scalar multiplication */
545 #define SCALE_2V( DST, SRCA, SRCB ) \
547 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
548 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
551 /** In-place addition */
552 #define ACC_2V( DST, SRC ) \
554 (DST)[0] += (SRC)[0]; \
555 (DST)[1] += (SRC)[1]; \
558 /** Element-wise multiplication and addition */
559 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
561 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
562 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
565 /** Scalar multiplication */
566 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
568 (DST)[0] = S * (SRCB)[0]; \
569 (DST)[1] = S * (SRCB)[1]; \
572 /** In-place scalar multiplication and addition */
573 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
575 (DST)[0] += S * (SRCB)[0]; \
576 (DST)[1] += S * (SRCB)[1]; \
579 /** In-place scalar multiplication */
580 #define SELF_SCALE_SCALAR_2V( DST, S ) \
586 /** In-place scalar addition */
587 #define ACC_SCALAR_2V( DST, S ) \
593 /** Assign scalers to short vectors */
594 #define ASSIGN_2V( V, V0, V1 ) \
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.
607 COPY_CLEAN_4V_TYPE_AS_UNION(fi_type dst
[4], int sz
, const fi_type src
[4],
612 ASSIGN_4V(dst
, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
613 FLOAT_AS_UNION(0), FLOAT_AS_UNION(1));
616 ASSIGN_4V(dst
, INT_AS_UNION(0), INT_AS_UNION(0),
617 INT_AS_UNION(0), INT_AS_UNION(1));
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));
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");
628 COPY_SZ_4V(dst
, sz
, src
);
631 /** \name Linear interpolation functions */
634 static inline GLfloat
635 LINTERP(GLfloat t
, GLfloat out
, GLfloat in
)
637 return out
+ t
* (in
- out
);
641 INTERP_3F(GLfloat t
, GLfloat dst
[3], const GLfloat out
[3], const GLfloat in
[3])
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] );
649 INTERP_4F(GLfloat t
, GLfloat dst
[4], const GLfloat out
[4], const GLfloat in
[4])
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] );
661 /** Clamp X to [MIN,MAX] */
662 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
664 /** Minimum of two values: */
665 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
667 /** Maximum of two values: */
668 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
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))
674 static inline unsigned
675 minify(unsigned value
, unsigned levels
)
677 return MAX2(1, value
>> levels
);
681 * Return true if the given value is a power of two.
683 * Note that this considers 0 a power of two.
686 is_power_of_two(unsigned value
)
688 return (value
& (value
- 1)) == 0;
692 * Align a value up to an alignment value
694 * If \c value is not already aligned to the requested alignment value, it
695 * will be rounded up.
697 * \param value Value to be rounded
698 * \param alignment Alignment value to be used. This must be a power of two.
700 * \sa ROUND_DOWN_TO()
702 #define ALIGN(value, alignment) (((value) + (alignment) - 1) & ~((alignment) - 1))
705 * Align a value down to an alignment value
707 * If \c value is not already aligned to the requested alignment value, it
708 * will be rounded down.
710 * \param value Value to be rounded
711 * \param alignment Alignment value to be used. This must be a power of two.
715 #define ROUND_DOWN_TO(value, alignment) ((value) & ~(alignment - 1))
718 /** Cross product of two 3-element vectors */
720 CROSS3(GLfloat n
[3], const GLfloat u
[3], const GLfloat v
[3])
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];
728 /** Dot product of two 2-element vectors */
729 static inline GLfloat
730 DOT2(const GLfloat a
[2], const GLfloat b
[2])
732 return a
[0] * b
[0] + a
[1] * b
[1];
735 static inline GLfloat
736 DOT3(const GLfloat a
[3], const GLfloat b
[3])
738 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
741 static inline GLfloat
742 DOT4(const GLfloat a
[4], const GLfloat b
[4])
744 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
748 static inline GLfloat
749 LEN_SQUARED_3FV(const GLfloat v
[3])
754 static inline GLfloat
755 LEN_SQUARED_2FV(const GLfloat v
[2])
761 static inline GLfloat
762 LEN_3FV(const GLfloat v
[3])
764 return sqrtf(LEN_SQUARED_3FV(v
));
767 static inline GLfloat
768 LEN_2FV(const GLfloat v
[2])
770 return sqrtf(LEN_SQUARED_2FV(v
));
774 /* Normalize a 3-element vector to unit length. */
776 NORMALIZE_3FV(GLfloat v
[3])
778 GLfloat len
= (GLfloat
) LEN_SQUARED_3FV(v
);
780 len
= 1.0f
/ sqrtf(len
);
788 /** Test two floats have opposite signs */
789 static inline GLboolean
790 DIFFERENT_SIGNS(GLfloat x
, GLfloat y
)
792 return signbit(x
) != signbit(y
);
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 )
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)
808 #define STRINGIFY(x) #x