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/macros.h"
35 #include "util/u_math.h"
36 #include "util/rounding.h"
41 * \name Integer / float conversion for colors, normals, etc.
45 /** Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
46 extern GLfloat _mesa_ubyte_to_float_color_tab
[256];
47 #define UBYTE_TO_FLOAT(u) _mesa_ubyte_to_float_color_tab[(unsigned int)(u)]
49 /** Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
50 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) ((X) * 255.0F))
53 /** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
54 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
56 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
57 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
60 /** Convert GLbyte to GLfloat while preserving zero */
61 #define BYTE_TO_FLOATZ(B) ((B) == 0 ? 0.0F : BYTE_TO_FLOAT(B))
64 /** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0], texture/fb data */
65 #define BYTE_TO_FLOAT_TEX(B) ((B) == -128 ? -1.0F : (B) * (1.0F/127.0F))
67 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127], texture/fb data */
68 #define FLOAT_TO_BYTE_TEX(X) CLAMP( (GLint) (127.0F * (X)), -128, 127 )
70 /** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */
71 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
73 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
74 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
77 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
78 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
80 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */
81 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
83 /** Convert GLshort to GLfloat while preserving zero */
84 #define SHORT_TO_FLOATZ(S) ((S) == 0 ? 0.0F : SHORT_TO_FLOAT(S))
87 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0], texture/fb data */
88 #define SHORT_TO_FLOAT_TEX(S) ((S) == -32768 ? -1.0F : (S) * (1.0F/32767.0F))
90 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767], texture/fb data */
91 #define FLOAT_TO_SHORT_TEX(X) ( (GLint) (32767.0F * (X)) )
94 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
95 #define UINT_TO_FLOAT(U) ((GLfloat) ((U) * (1.0F / 4294967295.0)))
97 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
98 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
101 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
102 #define INT_TO_FLOAT(I) ((GLfloat) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0)))
104 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
106 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
108 /* a close approximation: */
109 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
111 /** Convert GLfloat in [-1.0,1.0] to GLint64 in [-(1<<63),(1 << 63) -1] */
112 #define FLOAT_TO_INT64(X) ( (GLint64) (9223372036854775807.0 * (double)(X)) )
115 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0], texture/fb data */
116 #define INT_TO_FLOAT_TEX(I) ((I) == -2147483648 ? -1.0F : (I) * (1.0F/2147483647.0))
118 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647], texture/fb data */
119 #define FLOAT_TO_INT_TEX(X) ( (GLint) (2147483647.0 * (X)) )
122 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
123 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
124 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
125 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
126 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
129 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
130 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
131 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
132 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
133 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
134 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
135 us = ( (GLushort) _mesa_lroundevenf( CLAMP((f), 0.0F, 1.0F) * 65535.0F) )
136 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
137 us = ( (GLushort) _mesa_lroundevenf( (f) * 65535.0F) )
139 #define UNCLAMPED_FLOAT_TO_SHORT(s, f) \
140 s = ( (GLshort) _mesa_lroundevenf( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
143 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
144 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
147 /* This function/macro is sensitive to precision. Test very carefully
150 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, FLT) \
156 else if (__tmp.i >= IEEE_ONE) \
157 UB = (GLubyte) 255; \
159 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
160 UB = (GLubyte) __tmp.i; \
163 #define CLAMPED_FLOAT_TO_UBYTE(UB, FLT) \
166 __tmp.f = (FLT) * (255.0F/256.0F) + 32768.0F; \
167 UB = (GLubyte) __tmp.i; \
170 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
171 ub = ((GLubyte) _mesa_lroundevenf(CLAMP((f), 0.0F, 1.0F) * 255.0F))
172 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
173 ub = ((GLubyte) _mesa_lroundevenf((f) * 255.0F))
176 static fi_type
UINT_AS_UNION(GLuint u
)
183 static inline fi_type
INT_AS_UNION(GLint i
)
190 static inline fi_type
FLOAT_AS_UNION(GLfloat f
)
198 * Convert a floating point value to an unsigned fixed point value.
200 * \param frac_bits The number of bits used to store the fractional part.
202 static inline uint32_t
203 U_FIXED(float value
, uint32_t frac_bits
)
205 value
*= (1 << frac_bits
);
206 return value
< 0.0f
? 0 : (uint32_t) value
;
210 * Convert a floating point value to an signed fixed point value.
212 * \param frac_bits The number of bits used to store the fractional part.
214 static inline int32_t
215 S_FIXED(float value
, uint32_t frac_bits
)
217 return (int32_t) (value
* (1 << frac_bits
));
222 /** Stepping a GLfloat pointer by a byte stride */
223 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
224 /** Stepping a GLuint pointer by a byte stride */
225 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
226 /** Stepping a GLubyte[4] pointer by a byte stride */
227 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
228 /** Stepping a GLfloat[4] pointer by a byte stride */
229 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
230 /** Stepping a \p t pointer by a byte stride */
231 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
234 /**********************************************************************/
235 /** \name 4-element vector operations */
239 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
241 /** Test for equality */
242 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
243 (a)[1] == (b)[1] && \
244 (a)[2] == (b)[2] && \
247 /** Test for equality (unsigned bytes) */
248 static inline GLboolean
249 TEST_EQ_4UBV(const GLubyte a
[4], const GLubyte b
[4])
251 #if defined(__i386__)
252 return *((const GLuint
*) a
) == *((const GLuint
*) b
);
254 return TEST_EQ_4V(a
, b
);
259 /** Copy a 4-element vector */
260 #define COPY_4V( DST, SRC ) \
262 (DST)[0] = (SRC)[0]; \
263 (DST)[1] = (SRC)[1]; \
264 (DST)[2] = (SRC)[2]; \
265 (DST)[3] = (SRC)[3]; \
268 /** Copy a 4-element unsigned byte vector */
270 COPY_4UBV(GLubyte dst
[4], const GLubyte src
[4])
272 #if defined(__i386__)
273 *((GLuint
*) dst
) = *((GLuint
*) src
);
275 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
280 /** Copy \p SZ elements into a 4-element vector */
281 #define COPY_SZ_4V(DST, SZ, SRC) \
284 case 4: (DST)[3] = (SRC)[3]; \
285 case 3: (DST)[2] = (SRC)[2]; \
286 case 2: (DST)[1] = (SRC)[1]; \
287 case 1: (DST)[0] = (SRC)[0]; \
291 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
292 * default values to the remaining */
293 #define COPY_CLEAN_4V(DST, SZ, SRC) \
295 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
296 COPY_SZ_4V( DST, SZ, SRC ); \
300 #define SUB_4V( DST, SRCA, SRCB ) \
302 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
303 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
304 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
305 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
309 #define ADD_4V( DST, SRCA, SRCB ) \
311 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
312 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
313 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
314 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
317 /** Element-wise multiplication */
318 #define SCALE_4V( DST, SRCA, SRCB ) \
320 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
321 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
322 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
323 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
326 /** In-place addition */
327 #define ACC_4V( DST, SRC ) \
329 (DST)[0] += (SRC)[0]; \
330 (DST)[1] += (SRC)[1]; \
331 (DST)[2] += (SRC)[2]; \
332 (DST)[3] += (SRC)[3]; \
335 /** Element-wise multiplication and addition */
336 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
338 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
339 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
340 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
341 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
344 /** In-place scalar multiplication and addition */
345 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
347 (DST)[0] += S * (SRCB)[0]; \
348 (DST)[1] += S * (SRCB)[1]; \
349 (DST)[2] += S * (SRCB)[2]; \
350 (DST)[3] += S * (SRCB)[3]; \
353 /** Scalar multiplication */
354 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
356 (DST)[0] = S * (SRCB)[0]; \
357 (DST)[1] = S * (SRCB)[1]; \
358 (DST)[2] = S * (SRCB)[2]; \
359 (DST)[3] = S * (SRCB)[3]; \
362 /** In-place scalar multiplication */
363 #define SELF_SCALE_SCALAR_4V( DST, S ) \
374 /**********************************************************************/
375 /** \name 3-element vector operations*/
379 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
381 /** Test for equality */
382 #define TEST_EQ_3V(a,b) \
383 ((a)[0] == (b)[0] && \
384 (a)[1] == (b)[1] && \
387 /** Copy a 3-element vector */
388 #define COPY_3V( DST, SRC ) \
390 (DST)[0] = (SRC)[0]; \
391 (DST)[1] = (SRC)[1]; \
392 (DST)[2] = (SRC)[2]; \
395 /** Copy a 3-element vector with cast */
396 #define COPY_3V_CAST( DST, SRC, CAST ) \
398 (DST)[0] = (CAST)(SRC)[0]; \
399 (DST)[1] = (CAST)(SRC)[1]; \
400 (DST)[2] = (CAST)(SRC)[2]; \
403 /** Copy a 3-element float vector */
404 #define COPY_3FV( DST, SRC ) \
406 const GLfloat *_tmp = (SRC); \
407 (DST)[0] = _tmp[0]; \
408 (DST)[1] = _tmp[1]; \
409 (DST)[2] = _tmp[2]; \
413 #define SUB_3V( DST, SRCA, SRCB ) \
415 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
416 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
417 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
421 #define ADD_3V( DST, SRCA, SRCB ) \
423 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
424 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
425 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
428 /** In-place scalar multiplication */
429 #define SCALE_3V( DST, SRCA, SRCB ) \
431 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
432 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
433 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
436 /** In-place element-wise multiplication */
437 #define SELF_SCALE_3V( DST, SRC ) \
439 (DST)[0] *= (SRC)[0]; \
440 (DST)[1] *= (SRC)[1]; \
441 (DST)[2] *= (SRC)[2]; \
444 /** In-place addition */
445 #define ACC_3V( DST, SRC ) \
447 (DST)[0] += (SRC)[0]; \
448 (DST)[1] += (SRC)[1]; \
449 (DST)[2] += (SRC)[2]; \
452 /** Element-wise multiplication and addition */
453 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
455 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
456 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
457 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
460 /** Scalar multiplication */
461 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
463 (DST)[0] = S * (SRCB)[0]; \
464 (DST)[1] = S * (SRCB)[1]; \
465 (DST)[2] = S * (SRCB)[2]; \
468 /** In-place scalar multiplication and addition */
469 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
471 (DST)[0] += S * (SRCB)[0]; \
472 (DST)[1] += S * (SRCB)[1]; \
473 (DST)[2] += S * (SRCB)[2]; \
476 /** In-place scalar multiplication */
477 #define SELF_SCALE_SCALAR_3V( DST, S ) \
484 /** In-place scalar addition */
485 #define ACC_SCALAR_3V( DST, S ) \
493 #define ASSIGN_3V( V, V0, V1, V2 ) \
503 /**********************************************************************/
504 /** \name 2-element vector operations*/
508 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
510 /** Copy a 2-element vector */
511 #define COPY_2V( DST, SRC ) \
513 (DST)[0] = (SRC)[0]; \
514 (DST)[1] = (SRC)[1]; \
517 /** Copy a 2-element vector with cast */
518 #define COPY_2V_CAST( DST, SRC, CAST ) \
520 (DST)[0] = (CAST)(SRC)[0]; \
521 (DST)[1] = (CAST)(SRC)[1]; \
524 /** Copy a 2-element float vector */
525 #define COPY_2FV( DST, SRC ) \
527 const GLfloat *_tmp = (SRC); \
528 (DST)[0] = _tmp[0]; \
529 (DST)[1] = _tmp[1]; \
533 #define SUB_2V( DST, SRCA, SRCB ) \
535 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
536 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
540 #define ADD_2V( DST, SRCA, SRCB ) \
542 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
543 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
546 /** In-place scalar multiplication */
547 #define SCALE_2V( DST, SRCA, SRCB ) \
549 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
550 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
553 /** In-place addition */
554 #define ACC_2V( DST, SRC ) \
556 (DST)[0] += (SRC)[0]; \
557 (DST)[1] += (SRC)[1]; \
560 /** Element-wise multiplication and addition */
561 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
563 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
564 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
567 /** Scalar multiplication */
568 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
570 (DST)[0] = S * (SRCB)[0]; \
571 (DST)[1] = S * (SRCB)[1]; \
574 /** In-place scalar multiplication and addition */
575 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
577 (DST)[0] += S * (SRCB)[0]; \
578 (DST)[1] += S * (SRCB)[1]; \
581 /** In-place scalar multiplication */
582 #define SELF_SCALE_SCALAR_2V( DST, S ) \
588 /** In-place scalar addition */
589 #define ACC_SCALAR_2V( DST, S ) \
595 /** Assign scalers to short vectors */
596 #define ASSIGN_2V( V, V0, V1 ) \
604 /** Copy \p sz elements into a homegeneous (4-element) vector, giving
605 * default values to the remaining components.
606 * The default values are chosen based on \p type.
609 COPY_CLEAN_4V_TYPE_AS_UNION(fi_type dst
[4], int sz
, const fi_type src
[4],
614 ASSIGN_4V(dst
, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
615 FLOAT_AS_UNION(0), FLOAT_AS_UNION(1));
618 ASSIGN_4V(dst
, INT_AS_UNION(0), INT_AS_UNION(0),
619 INT_AS_UNION(0), INT_AS_UNION(1));
621 case GL_UNSIGNED_INT
:
622 ASSIGN_4V(dst
, UINT_AS_UNION(0), UINT_AS_UNION(0),
623 UINT_AS_UNION(0), UINT_AS_UNION(1));
626 ASSIGN_4V(dst
, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
627 FLOAT_AS_UNION(0), FLOAT_AS_UNION(1)); /* silence warnings */
628 assert(!"Unexpected type in COPY_CLEAN_4V_TYPE_AS_UNION macro");
630 COPY_SZ_4V(dst
, sz
, src
);
633 /** \name Linear interpolation functions */
636 static inline GLfloat
637 LINTERP(GLfloat t
, GLfloat out
, GLfloat in
)
639 return out
+ t
* (in
- out
);
643 INTERP_3F(GLfloat t
, GLfloat dst
[3], const GLfloat out
[3], const GLfloat in
[3])
645 dst
[0] = LINTERP( t
, out
[0], in
[0] );
646 dst
[1] = LINTERP( t
, out
[1], in
[1] );
647 dst
[2] = LINTERP( t
, out
[2], in
[2] );
651 INTERP_4F(GLfloat t
, GLfloat dst
[4], const GLfloat out
[4], const GLfloat in
[4])
653 dst
[0] = LINTERP( t
, out
[0], in
[0] );
654 dst
[1] = LINTERP( t
, out
[1], in
[1] );
655 dst
[2] = LINTERP( t
, out
[2], in
[2] );
656 dst
[3] = LINTERP( t
, out
[3], in
[3] );
663 static inline unsigned
664 minify(unsigned value
, unsigned levels
)
666 return MAX2(1, value
>> levels
);
670 * Align a value up to an alignment value
672 * If \c value is not already aligned to the requested alignment value, it
673 * will be rounded up.
675 * \param value Value to be rounded
676 * \param alignment Alignment value to be used. This must be a power of two.
678 * \sa ROUND_DOWN_TO()
680 static inline uintptr_t
681 ALIGN(uintptr_t value
, int32_t alignment
)
683 assert((alignment
> 0) && _mesa_is_pow_two(alignment
));
684 return (((value
) + (alignment
) - 1) & ~((alignment
) - 1));
688 * Like ALIGN(), but works with a non-power-of-two alignment.
690 static inline uintptr_t
691 ALIGN_NPOT(uintptr_t value
, int32_t alignment
)
693 assert(alignment
> 0);
694 return (value
+ alignment
- 1) / alignment
* alignment
;
698 * Align a value down to an alignment value
700 * If \c value is not already aligned to the requested alignment value, it
701 * will be rounded down.
703 * \param value Value to be rounded
704 * \param alignment Alignment value to be used. This must be a power of two.
708 static inline uintptr_t
709 ROUND_DOWN_TO(uintptr_t value
, int32_t alignment
)
711 assert((alignment
> 0) && _mesa_is_pow_two(alignment
));
712 return ((value
) & ~(alignment
- 1));
716 /** Cross product of two 3-element vectors */
718 CROSS3(GLfloat n
[3], const GLfloat u
[3], const GLfloat v
[3])
720 n
[0] = u
[1] * v
[2] - u
[2] * v
[1];
721 n
[1] = u
[2] * v
[0] - u
[0] * v
[2];
722 n
[2] = u
[0] * v
[1] - u
[1] * v
[0];
726 /** Dot product of two 2-element vectors */
727 static inline GLfloat
728 DOT2(const GLfloat a
[2], const GLfloat b
[2])
730 return a
[0] * b
[0] + a
[1] * b
[1];
733 static inline GLfloat
734 DOT3(const GLfloat a
[3], const GLfloat b
[3])
736 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
739 static inline GLfloat
740 DOT4(const GLfloat a
[4], const GLfloat b
[4])
742 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
746 static inline GLfloat
747 LEN_SQUARED_3FV(const GLfloat v
[3])
752 static inline GLfloat
753 LEN_SQUARED_2FV(const GLfloat v
[2])
759 static inline GLfloat
760 LEN_3FV(const GLfloat v
[3])
762 return sqrtf(LEN_SQUARED_3FV(v
));
765 static inline GLfloat
766 LEN_2FV(const GLfloat v
[2])
768 return sqrtf(LEN_SQUARED_2FV(v
));
772 /* Normalize a 3-element vector to unit length. */
774 NORMALIZE_3FV(GLfloat v
[3])
776 GLfloat len
= (GLfloat
) LEN_SQUARED_3FV(v
);
778 len
= 1.0f
/ sqrtf(len
);
786 /** Test two floats have opposite signs */
787 static inline GLboolean
788 DIFFERENT_SIGNS(GLfloat x
, GLfloat y
)
791 #pragma warning( push )
792 #pragma warning( disable : 6334 ) /* sizeof operator applied to an expression with an operator may yield unexpected results */
794 return signbit(x
) != signbit(y
);
796 #pragma warning( pop )
801 /** casts to silence warnings with some compilers */
802 #define ENUM_TO_INT(E) ((GLint)(E))
803 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
804 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
805 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
809 #define STRINGIFY(x) #x