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
)
197 static inline uint64_t DOUBLE_AS_UINT64(double d
)
207 static inline double UINT64_AS_DOUBLE(uint64_t u
)
217 /* First sign-extend x, then return uint32_t. */
218 #define INT_AS_UINT(x) ((uint32_t)((int32_t)(x)))
219 #define FLOAT_AS_UINT(x) (FLOAT_AS_UNION(x).u)
222 * Convert a floating point value to an unsigned fixed point value.
224 * \param frac_bits The number of bits used to store the fractional part.
226 static inline uint32_t
227 U_FIXED(float value
, uint32_t frac_bits
)
229 value
*= (1 << frac_bits
);
230 return value
< 0.0f
? 0 : (uint32_t) value
;
234 * Convert a floating point value to an signed fixed point value.
236 * \param frac_bits The number of bits used to store the fractional part.
238 static inline int32_t
239 S_FIXED(float value
, uint32_t frac_bits
)
241 return (int32_t) (value
* (1 << frac_bits
));
246 /** Stepping a GLfloat pointer by a byte stride */
247 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
248 /** Stepping a GLuint pointer by a byte stride */
249 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
250 /** Stepping a GLubyte[4] pointer by a byte stride */
251 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
252 /** Stepping a GLfloat[4] pointer by a byte stride */
253 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
254 /** Stepping a \p t pointer by a byte stride */
255 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
258 /**********************************************************************/
259 /** \name 4-element vector operations */
263 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
265 /** Test for equality */
266 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
267 (a)[1] == (b)[1] && \
268 (a)[2] == (b)[2] && \
271 /** Test for equality (unsigned bytes) */
272 static inline GLboolean
273 TEST_EQ_4UBV(const GLubyte a
[4], const GLubyte b
[4])
275 #if defined(__i386__)
276 return *((const GLuint
*) a
) == *((const GLuint
*) b
);
278 return TEST_EQ_4V(a
, b
);
283 /** Copy a 4-element vector */
284 #define COPY_4V( DST, SRC ) \
286 (DST)[0] = (SRC)[0]; \
287 (DST)[1] = (SRC)[1]; \
288 (DST)[2] = (SRC)[2]; \
289 (DST)[3] = (SRC)[3]; \
292 /** Copy a 4-element unsigned byte vector */
294 COPY_4UBV(GLubyte dst
[4], const GLubyte src
[4])
296 #if defined(__i386__)
297 *((GLuint
*) dst
) = *((GLuint
*) src
);
299 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
304 /** Copy \p SZ elements into a 4-element vector */
305 #define COPY_SZ_4V(DST, SZ, SRC) \
308 case 4: (DST)[3] = (SRC)[3]; \
309 case 3: (DST)[2] = (SRC)[2]; \
310 case 2: (DST)[1] = (SRC)[1]; \
311 case 1: (DST)[0] = (SRC)[0]; \
315 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
316 * default values to the remaining */
317 #define COPY_CLEAN_4V(DST, SZ, SRC) \
319 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
320 COPY_SZ_4V( DST, SZ, SRC ); \
324 #define SUB_4V( DST, SRCA, SRCB ) \
326 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
327 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
328 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
329 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
333 #define ADD_4V( DST, SRCA, SRCB ) \
335 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
336 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
337 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
338 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
341 /** Element-wise multiplication */
342 #define SCALE_4V( DST, SRCA, SRCB ) \
344 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
345 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
346 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
347 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
350 /** In-place addition */
351 #define ACC_4V( DST, SRC ) \
353 (DST)[0] += (SRC)[0]; \
354 (DST)[1] += (SRC)[1]; \
355 (DST)[2] += (SRC)[2]; \
356 (DST)[3] += (SRC)[3]; \
359 /** Element-wise multiplication and addition */
360 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
362 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
363 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
364 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
365 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
368 /** In-place scalar multiplication and addition */
369 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
371 (DST)[0] += S * (SRCB)[0]; \
372 (DST)[1] += S * (SRCB)[1]; \
373 (DST)[2] += S * (SRCB)[2]; \
374 (DST)[3] += S * (SRCB)[3]; \
377 /** Scalar multiplication */
378 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
380 (DST)[0] = S * (SRCB)[0]; \
381 (DST)[1] = S * (SRCB)[1]; \
382 (DST)[2] = S * (SRCB)[2]; \
383 (DST)[3] = S * (SRCB)[3]; \
386 /** In-place scalar multiplication */
387 #define SELF_SCALE_SCALAR_4V( DST, S ) \
398 /**********************************************************************/
399 /** \name 3-element vector operations*/
403 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
405 /** Test for equality */
406 #define TEST_EQ_3V(a,b) \
407 ((a)[0] == (b)[0] && \
408 (a)[1] == (b)[1] && \
411 /** Copy a 3-element vector */
412 #define COPY_3V( DST, SRC ) \
414 (DST)[0] = (SRC)[0]; \
415 (DST)[1] = (SRC)[1]; \
416 (DST)[2] = (SRC)[2]; \
419 /** Copy a 3-element vector with cast */
420 #define COPY_3V_CAST( DST, SRC, CAST ) \
422 (DST)[0] = (CAST)(SRC)[0]; \
423 (DST)[1] = (CAST)(SRC)[1]; \
424 (DST)[2] = (CAST)(SRC)[2]; \
427 /** Copy a 3-element float vector */
428 #define COPY_3FV( DST, SRC ) \
430 const GLfloat *_tmp = (SRC); \
431 (DST)[0] = _tmp[0]; \
432 (DST)[1] = _tmp[1]; \
433 (DST)[2] = _tmp[2]; \
437 #define SUB_3V( DST, SRCA, SRCB ) \
439 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
440 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
441 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
445 #define ADD_3V( DST, SRCA, SRCB ) \
447 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
448 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
449 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
452 /** In-place scalar multiplication */
453 #define 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 /** In-place element-wise multiplication */
461 #define SELF_SCALE_3V( DST, SRC ) \
463 (DST)[0] *= (SRC)[0]; \
464 (DST)[1] *= (SRC)[1]; \
465 (DST)[2] *= (SRC)[2]; \
468 /** In-place addition */
469 #define ACC_3V( DST, SRC ) \
471 (DST)[0] += (SRC)[0]; \
472 (DST)[1] += (SRC)[1]; \
473 (DST)[2] += (SRC)[2]; \
476 /** Element-wise multiplication and addition */
477 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
479 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
480 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
481 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
484 /** Scalar multiplication */
485 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
487 (DST)[0] = S * (SRCB)[0]; \
488 (DST)[1] = S * (SRCB)[1]; \
489 (DST)[2] = S * (SRCB)[2]; \
492 /** In-place scalar multiplication and addition */
493 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
495 (DST)[0] += S * (SRCB)[0]; \
496 (DST)[1] += S * (SRCB)[1]; \
497 (DST)[2] += S * (SRCB)[2]; \
500 /** In-place scalar multiplication */
501 #define SELF_SCALE_SCALAR_3V( DST, S ) \
508 /** In-place scalar addition */
509 #define ACC_SCALAR_3V( DST, S ) \
517 #define ASSIGN_3V( V, V0, V1, V2 ) \
527 /**********************************************************************/
528 /** \name 2-element vector operations*/
532 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
534 /** Copy a 2-element vector */
535 #define COPY_2V( DST, SRC ) \
537 (DST)[0] = (SRC)[0]; \
538 (DST)[1] = (SRC)[1]; \
541 /** Copy a 2-element vector with cast */
542 #define COPY_2V_CAST( DST, SRC, CAST ) \
544 (DST)[0] = (CAST)(SRC)[0]; \
545 (DST)[1] = (CAST)(SRC)[1]; \
548 /** Copy a 2-element float vector */
549 #define COPY_2FV( DST, SRC ) \
551 const GLfloat *_tmp = (SRC); \
552 (DST)[0] = _tmp[0]; \
553 (DST)[1] = _tmp[1]; \
557 #define SUB_2V( DST, SRCA, SRCB ) \
559 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
560 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
564 #define ADD_2V( DST, SRCA, SRCB ) \
566 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
567 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
570 /** In-place scalar multiplication */
571 #define SCALE_2V( DST, SRCA, SRCB ) \
573 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
574 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
577 /** In-place addition */
578 #define ACC_2V( DST, SRC ) \
580 (DST)[0] += (SRC)[0]; \
581 (DST)[1] += (SRC)[1]; \
584 /** Element-wise multiplication and addition */
585 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
587 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
588 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
591 /** Scalar multiplication */
592 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
594 (DST)[0] = S * (SRCB)[0]; \
595 (DST)[1] = S * (SRCB)[1]; \
598 /** In-place scalar multiplication and addition */
599 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
601 (DST)[0] += S * (SRCB)[0]; \
602 (DST)[1] += S * (SRCB)[1]; \
605 /** In-place scalar multiplication */
606 #define SELF_SCALE_SCALAR_2V( DST, S ) \
612 /** In-place scalar addition */
613 #define ACC_SCALAR_2V( DST, S ) \
619 /** Assign scalers to short vectors */
620 #define ASSIGN_2V( V, V0, V1 ) \
628 /** Copy \p sz elements into a homegeneous (4-element) vector, giving
629 * default values to the remaining components.
630 * The default values are chosen based on \p type.
633 COPY_CLEAN_4V_TYPE_AS_UNION(fi_type dst
[4], int sz
, const fi_type src
[4],
638 ASSIGN_4V(dst
, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
639 FLOAT_AS_UNION(0), FLOAT_AS_UNION(1));
642 ASSIGN_4V(dst
, INT_AS_UNION(0), INT_AS_UNION(0),
643 INT_AS_UNION(0), INT_AS_UNION(1));
645 case GL_UNSIGNED_INT
:
646 ASSIGN_4V(dst
, UINT_AS_UNION(0), UINT_AS_UNION(0),
647 UINT_AS_UNION(0), UINT_AS_UNION(1));
650 ASSIGN_4V(dst
, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
651 FLOAT_AS_UNION(0), FLOAT_AS_UNION(1)); /* silence warnings */
652 assert(!"Unexpected type in COPY_CLEAN_4V_TYPE_AS_UNION macro");
654 COPY_SZ_4V(dst
, sz
, src
);
657 /** \name Linear interpolation functions */
660 static inline GLfloat
661 LINTERP(GLfloat t
, GLfloat out
, GLfloat in
)
663 return out
+ t
* (in
- out
);
667 INTERP_3F(GLfloat t
, GLfloat dst
[3], const GLfloat out
[3], const GLfloat in
[3])
669 dst
[0] = LINTERP( t
, out
[0], in
[0] );
670 dst
[1] = LINTERP( t
, out
[1], in
[1] );
671 dst
[2] = LINTERP( t
, out
[2], in
[2] );
675 INTERP_4F(GLfloat t
, GLfloat dst
[4], const GLfloat out
[4], const GLfloat in
[4])
677 dst
[0] = LINTERP( t
, out
[0], in
[0] );
678 dst
[1] = LINTERP( t
, out
[1], in
[1] );
679 dst
[2] = LINTERP( t
, out
[2], in
[2] );
680 dst
[3] = LINTERP( t
, out
[3], in
[3] );
687 static inline unsigned
688 minify(unsigned value
, unsigned levels
)
690 return MAX2(1, value
>> levels
);
694 * Align a value up to an alignment value
696 * If \c value is not already aligned to the requested alignment value, it
697 * will be rounded up.
699 * \param value Value to be rounded
700 * \param alignment Alignment value to be used. This must be a power of two.
702 * \sa ROUND_DOWN_TO()
704 static inline uintptr_t
705 ALIGN(uintptr_t value
, int32_t alignment
)
707 assert((alignment
> 0) && _mesa_is_pow_two(alignment
));
708 return (((value
) + (alignment
) - 1) & ~((alignment
) - 1));
712 * Like ALIGN(), but works with a non-power-of-two alignment.
714 static inline uintptr_t
715 ALIGN_NPOT(uintptr_t value
, int32_t alignment
)
717 assert(alignment
> 0);
718 return (value
+ alignment
- 1) / alignment
* alignment
;
722 * Align a value down to an alignment value
724 * If \c value is not already aligned to the requested alignment value, it
725 * will be rounded down.
727 * \param value Value to be rounded
728 * \param alignment Alignment value to be used. This must be a power of two.
732 static inline uintptr_t
733 ROUND_DOWN_TO(uintptr_t value
, int32_t alignment
)
735 assert((alignment
> 0) && _mesa_is_pow_two(alignment
));
736 return ((value
) & ~(alignment
- 1));
740 /** Cross product of two 3-element vectors */
742 CROSS3(GLfloat n
[3], const GLfloat u
[3], const GLfloat v
[3])
744 n
[0] = u
[1] * v
[2] - u
[2] * v
[1];
745 n
[1] = u
[2] * v
[0] - u
[0] * v
[2];
746 n
[2] = u
[0] * v
[1] - u
[1] * v
[0];
750 /** Dot product of two 2-element vectors */
751 static inline GLfloat
752 DOT2(const GLfloat a
[2], const GLfloat b
[2])
754 return a
[0] * b
[0] + a
[1] * b
[1];
757 static inline GLfloat
758 DOT3(const GLfloat a
[3], const GLfloat b
[3])
760 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
763 static inline GLfloat
764 DOT4(const GLfloat a
[4], const GLfloat b
[4])
766 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
770 static inline GLfloat
771 LEN_SQUARED_3FV(const GLfloat v
[3])
776 static inline GLfloat
777 LEN_SQUARED_2FV(const GLfloat v
[2])
783 static inline GLfloat
784 LEN_3FV(const GLfloat v
[3])
786 return sqrtf(LEN_SQUARED_3FV(v
));
789 static inline GLfloat
790 LEN_2FV(const GLfloat v
[2])
792 return sqrtf(LEN_SQUARED_2FV(v
));
796 /* Normalize a 3-element vector to unit length. */
798 NORMALIZE_3FV(GLfloat v
[3])
800 GLfloat len
= (GLfloat
) LEN_SQUARED_3FV(v
);
802 len
= 1.0f
/ sqrtf(len
);
810 /** Test two floats have opposite signs */
811 static inline GLboolean
812 DIFFERENT_SIGNS(GLfloat x
, GLfloat y
)
815 #pragma warning( push )
816 #pragma warning( disable : 6334 ) /* sizeof operator applied to an expression with an operator may yield unexpected results */
818 return signbit(x
) != signbit(y
);
820 #pragma warning( pop )
825 /** casts to silence warnings with some compilers */
826 #define ENUM_TO_INT(E) ((GLint)(E))
827 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
828 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
829 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
833 #define STRINGIFY(x) #x