3 * A collection of useful macros.
7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2006 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 * 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.
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]
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
149 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
155 else if (__tmp.i >= IEEE_0996) \
156 UB = (GLubyte) 255; \
158 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
159 UB = (GLubyte) __tmp.i; \
162 #define CLAMPED_FLOAT_TO_UBYTE(UB, F) \
165 __tmp.f = (F) * (255.0F/256.0F) + 32768.0F; \
166 UB = (GLubyte) __tmp.i; \
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))
175 static inline GLfloat
INT_AS_FLT(GLint i
)
182 static inline GLfloat
UINT_AS_FLT(GLuint u
)
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))
204 /**********************************************************************/
205 /** \name 4-element vector operations */
209 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
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] && \
217 /** Test for equality (unsigned bytes) */
218 static inline GLboolean
219 TEST_EQ_4UBV(const GLubyte a
[4], const GLubyte b
[4])
221 #if defined(__i386__)
222 return *((const GLuint
*) a
) == *((const GLuint
*) b
);
224 return TEST_EQ_4V(a
, b
);
229 /** Copy a 4-element vector */
230 #define COPY_4V( DST, SRC ) \
232 (DST)[0] = (SRC)[0]; \
233 (DST)[1] = (SRC)[1]; \
234 (DST)[2] = (SRC)[2]; \
235 (DST)[3] = (SRC)[3]; \
238 /** Copy a 4-element unsigned byte vector */
240 COPY_4UBV(GLubyte dst
[4], const GLubyte src
[4])
242 #if defined(__i386__)
243 *((GLuint
*) dst
) = *((GLuint
*) src
);
245 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
250 /** Copy a 4-element float vector */
252 COPY_4FV(GLfloat dst
[4], const GLfloat src
[4])
254 /* memcpy seems to be most efficient */
255 memcpy(dst
, src
, sizeof(GLfloat
) * 4);
258 /** Copy \p SZ elements into a 4-element vector */
259 #define COPY_SZ_4V(DST, SZ, SRC) \
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]; \
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) \
273 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
274 COPY_SZ_4V( DST, SZ, SRC ); \
278 #define SUB_4V( DST, SRCA, SRCB ) \
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]; \
287 #define ADD_4V( DST, SRCA, SRCB ) \
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]; \
295 /** Element-wise multiplication */
296 #define SCALE_4V( DST, SRCA, SRCB ) \
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]; \
304 /** In-place addition */
305 #define ACC_4V( DST, SRC ) \
307 (DST)[0] += (SRC)[0]; \
308 (DST)[1] += (SRC)[1]; \
309 (DST)[2] += (SRC)[2]; \
310 (DST)[3] += (SRC)[3]; \
313 /** Element-wise multiplication and addition */
314 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
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]; \
322 /** In-place scalar multiplication and addition */
323 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
325 (DST)[0] += S * (SRCB)[0]; \
326 (DST)[1] += S * (SRCB)[1]; \
327 (DST)[2] += S * (SRCB)[2]; \
328 (DST)[3] += S * (SRCB)[3]; \
331 /** Scalar multiplication */
332 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
334 (DST)[0] = S * (SRCB)[0]; \
335 (DST)[1] = S * (SRCB)[1]; \
336 (DST)[2] = S * (SRCB)[2]; \
337 (DST)[3] = S * (SRCB)[3]; \
340 /** In-place scalar multiplication */
341 #define SELF_SCALE_SCALAR_4V( DST, S ) \
350 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
361 /**********************************************************************/
362 /** \name 3-element vector operations*/
366 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
368 /** Test for equality */
369 #define TEST_EQ_3V(a,b) \
370 ((a)[0] == (b)[0] && \
371 (a)[1] == (b)[1] && \
374 /** Copy a 3-element vector */
375 #define COPY_3V( DST, SRC ) \
377 (DST)[0] = (SRC)[0]; \
378 (DST)[1] = (SRC)[1]; \
379 (DST)[2] = (SRC)[2]; \
382 /** Copy a 3-element vector with cast */
383 #define COPY_3V_CAST( DST, SRC, CAST ) \
385 (DST)[0] = (CAST)(SRC)[0]; \
386 (DST)[1] = (CAST)(SRC)[1]; \
387 (DST)[2] = (CAST)(SRC)[2]; \
390 /** Copy a 3-element float vector */
391 #define COPY_3FV( DST, SRC ) \
393 const GLfloat *_tmp = (SRC); \
394 (DST)[0] = _tmp[0]; \
395 (DST)[1] = _tmp[1]; \
396 (DST)[2] = _tmp[2]; \
400 #define SUB_3V( DST, SRCA, SRCB ) \
402 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
403 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
404 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
408 #define ADD_3V( DST, SRCA, SRCB ) \
410 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
411 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
412 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
415 /** In-place scalar multiplication */
416 #define SCALE_3V( DST, SRCA, SRCB ) \
418 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
419 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
420 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
423 /** In-place element-wise multiplication */
424 #define SELF_SCALE_3V( DST, SRC ) \
426 (DST)[0] *= (SRC)[0]; \
427 (DST)[1] *= (SRC)[1]; \
428 (DST)[2] *= (SRC)[2]; \
431 /** In-place addition */
432 #define ACC_3V( DST, SRC ) \
434 (DST)[0] += (SRC)[0]; \
435 (DST)[1] += (SRC)[1]; \
436 (DST)[2] += (SRC)[2]; \
439 /** Element-wise multiplication and addition */
440 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
442 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
443 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
444 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
447 /** Scalar multiplication */
448 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
450 (DST)[0] = S * (SRCB)[0]; \
451 (DST)[1] = S * (SRCB)[1]; \
452 (DST)[2] = S * (SRCB)[2]; \
455 /** In-place scalar multiplication and addition */
456 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
458 (DST)[0] += S * (SRCB)[0]; \
459 (DST)[1] += S * (SRCB)[1]; \
460 (DST)[2] += S * (SRCB)[2]; \
463 /** In-place scalar multiplication */
464 #define SELF_SCALE_SCALAR_3V( DST, S ) \
471 /** In-place scalar addition */
472 #define ACC_SCALAR_3V( DST, S ) \
480 #define ASSIGN_3V( V, V0, V1, V2 ) \
490 /**********************************************************************/
491 /** \name 2-element vector operations*/
495 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
497 /** Copy a 2-element vector */
498 #define COPY_2V( DST, SRC ) \
500 (DST)[0] = (SRC)[0]; \
501 (DST)[1] = (SRC)[1]; \
504 /** Copy a 2-element vector with cast */
505 #define COPY_2V_CAST( DST, SRC, CAST ) \
507 (DST)[0] = (CAST)(SRC)[0]; \
508 (DST)[1] = (CAST)(SRC)[1]; \
511 /** Copy a 2-element float vector */
512 #define COPY_2FV( DST, SRC ) \
514 const GLfloat *_tmp = (SRC); \
515 (DST)[0] = _tmp[0]; \
516 (DST)[1] = _tmp[1]; \
520 #define SUB_2V( DST, SRCA, SRCB ) \
522 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
523 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
527 #define ADD_2V( DST, SRCA, SRCB ) \
529 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
530 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
533 /** In-place scalar multiplication */
534 #define SCALE_2V( DST, SRCA, SRCB ) \
536 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
537 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
540 /** In-place addition */
541 #define ACC_2V( DST, SRC ) \
543 (DST)[0] += (SRC)[0]; \
544 (DST)[1] += (SRC)[1]; \
547 /** Element-wise multiplication and addition */
548 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
550 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
551 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
554 /** Scalar multiplication */
555 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
557 (DST)[0] = S * (SRCB)[0]; \
558 (DST)[1] = S * (SRCB)[1]; \
561 /** In-place scalar multiplication and addition */
562 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
564 (DST)[0] += S * (SRCB)[0]; \
565 (DST)[1] += S * (SRCB)[1]; \
568 /** In-place scalar multiplication */
569 #define SELF_SCALE_SCALAR_2V( DST, S ) \
575 /** In-place scalar addition */
576 #define ACC_SCALAR_2V( DST, S ) \
582 /** Assign scalers to short vectors */
583 #define ASSIGN_2V( V, V0, V1 ) \
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.
596 COPY_CLEAN_4V_TYPE_AS_FLOAT(GLfloat dst
[4], int sz
, const GLfloat src
[4],
601 ASSIGN_4V(dst
, 0, 0, 0, 1);
604 ASSIGN_4V(dst
, INT_AS_FLT(0), INT_AS_FLT(0),
605 INT_AS_FLT(0), INT_AS_FLT(1));
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));
614 COPY_SZ_4V(dst
, sz
, src
);
617 /** \name Linear interpolation functions */
620 static inline GLfloat
621 LINTERP(GLfloat t
, GLfloat out
, GLfloat in
)
623 return out
+ t
* (in
- out
);
627 INTERP_3F(GLfloat t
, GLfloat dst
[3], const GLfloat out
[3], const GLfloat in
[3])
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] );
635 INTERP_4F(GLfloat t
, GLfloat dst
[4], const GLfloat out
[4], const GLfloat in
[4])
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] );
647 /** Clamp X to [MIN,MAX] */
648 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
650 /** Minimum of two values: */
651 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
653 /** Maximum of two values: */
654 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
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))
660 static inline unsigned
661 minify(unsigned value
, unsigned levels
)
663 return MAX2(1, value
>> levels
);
667 * Align a value up to an alignment value
669 * If \c value is not already aligned to the requested alignment value, it
670 * will be rounded up.
672 * \param value Value to be rounded
673 * \param alignment Alignment value to be used. This must be a power of two.
675 * \sa ROUND_DOWN_TO()
677 #define ALIGN(value, alignment) (((value) + alignment - 1) & ~(alignment - 1))
681 /** Cross product of two 3-element vectors */
683 CROSS3(GLfloat n
[3], const GLfloat u
[3], const GLfloat v
[3])
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];
691 /** Dot product of two 2-element vectors */
692 static inline GLfloat
693 DOT2(const GLfloat a
[2], const GLfloat b
[2])
695 return a
[0] * b
[0] + a
[1] * b
[1];
698 static inline GLfloat
699 DOT3(const GLfloat a
[3], const GLfloat b
[3])
701 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
704 static inline GLfloat
705 DOT4(const GLfloat a
[4], const GLfloat b
[4])
707 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
711 static inline GLfloat
712 LEN_SQUARED_3FV(const GLfloat v
[3])
717 static inline GLfloat
718 LEN_SQUARED_2FV(const GLfloat v
[2])
724 static inline GLfloat
725 LEN_3FV(const GLfloat v
[3])
727 return sqrtf(LEN_SQUARED_3FV(v
));
730 static inline GLfloat
731 LEN_2FV(const GLfloat v
[2])
733 return sqrtf(LEN_SQUARED_2FV(v
));
737 /* Normalize a 3-element vector to unit length. */
739 NORMALIZE_3FV(GLfloat v
[3])
741 GLfloat len
= (GLfloat
) LEN_SQUARED_3FV(v
);
743 len
= INV_SQRTF(len
);
751 /** Is float value negative? */
752 static inline GLboolean
755 return signbit(x
) != 0;
758 /** Test two floats have opposite signs */
759 static inline GLboolean
760 DIFFERENT_SIGNS(GLfloat x
, GLfloat y
)
762 return signbit(x
) != signbit(y
);
766 /** Compute ceiling of integer quotient of A divided by B. */
767 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
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)
776 /* Compute the size of an array */
777 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))