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"
37 #include "util/imports.h"
38 #include "main/glheader.h"
42 * \name Integer / float conversion for colors, normals, etc.
46 /** Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
47 extern GLfloat _mesa_ubyte_to_float_color_tab
[256];
48 #define UBYTE_TO_FLOAT(u) _mesa_ubyte_to_float_color_tab[(unsigned int)(u)]
50 /** Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
51 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) ((X) * 255.0F))
54 /** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
55 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
57 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
58 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
61 /** Convert GLbyte to GLfloat while preserving zero */
62 #define BYTE_TO_FLOATZ(B) ((B) == 0 ? 0.0F : BYTE_TO_FLOAT(B))
65 /** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0], texture/fb data */
66 #define BYTE_TO_FLOAT_TEX(B) ((B) == -128 ? -1.0F : (B) * (1.0F/127.0F))
68 /** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127], texture/fb data */
69 #define FLOAT_TO_BYTE_TEX(X) CLAMP( (GLint) (127.0F * (X)), -128, 127 )
71 /** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */
72 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
74 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
75 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
78 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
79 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
81 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */
82 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
84 /** Convert GLshort to GLfloat while preserving zero */
85 #define SHORT_TO_FLOATZ(S) ((S) == 0 ? 0.0F : SHORT_TO_FLOAT(S))
88 /** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0], texture/fb data */
89 #define SHORT_TO_FLOAT_TEX(S) ((S) == -32768 ? -1.0F : (S) * (1.0F/32767.0F))
91 /** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767], texture/fb data */
92 #define FLOAT_TO_SHORT_TEX(X) ( (GLint) (32767.0F * (X)) )
95 /** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
96 #define UINT_TO_FLOAT(U) ((GLfloat) ((U) * (1.0F / 4294967295.0)))
98 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
99 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
102 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
103 #define INT_TO_FLOAT(I) ((GLfloat) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0)))
105 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
107 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
109 /* a close approximation: */
110 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
112 /** Convert GLfloat in [-1.0,1.0] to GLint64 in [-(1<<63),(1 << 63) -1] */
113 #define FLOAT_TO_INT64(X) ( (GLint64) (9223372036854775807.0 * (double)(X)) )
116 /** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0], texture/fb data */
117 #define INT_TO_FLOAT_TEX(I) ((I) == -2147483648 ? -1.0F : (I) * (1.0F/2147483647.0))
119 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647], texture/fb data */
120 #define FLOAT_TO_INT_TEX(X) ( (GLint) (2147483647.0 * (X)) )
123 #define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b)))
124 #define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7)))
125 #define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8))
126 #define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23)))
127 #define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24))
130 #define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255)))
131 #define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b))
132 #define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767))))
133 #define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15)))
134 #define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16)))
135 #define UNCLAMPED_FLOAT_TO_USHORT(us, f) \
136 us = ( (GLushort) _mesa_lroundevenf( CLAMP((f), 0.0F, 1.0F) * 65535.0F) )
137 #define CLAMPED_FLOAT_TO_USHORT(us, f) \
138 us = ( (GLushort) _mesa_lroundevenf( (f) * 65535.0F) )
140 #define UNCLAMPED_FLOAT_TO_SHORT(s, f) \
141 s = ( (GLshort) _mesa_lroundevenf( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
144 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
145 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
148 /* This function/macro is sensitive to precision. Test very carefully
151 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, FLT) \
157 else if (__tmp.i >= IEEE_ONE) \
158 UB = (GLubyte) 255; \
160 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
161 UB = (GLubyte) __tmp.i; \
164 #define CLAMPED_FLOAT_TO_UBYTE(UB, FLT) \
167 __tmp.f = (FLT) * (255.0F/256.0F) + 32768.0F; \
168 UB = (GLubyte) __tmp.i; \
171 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
172 ub = ((GLubyte) _mesa_lroundevenf(CLAMP((f), 0.0F, 1.0F) * 255.0F))
173 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
174 ub = ((GLubyte) _mesa_lroundevenf((f) * 255.0F))
177 static fi_type
UINT_AS_UNION(GLuint u
)
184 static inline fi_type
INT_AS_UNION(GLint i
)
191 static inline fi_type
FLOAT_AS_UNION(GLfloat f
)
198 static inline uint64_t DOUBLE_AS_UINT64(double d
)
208 static inline double UINT64_AS_DOUBLE(uint64_t u
)
218 /* First sign-extend x, then return uint32_t. */
219 #define INT_AS_UINT(x) ((uint32_t)((int32_t)(x)))
220 #define FLOAT_AS_UINT(x) (FLOAT_AS_UNION(x).u)
223 * Convert a floating point value to an unsigned fixed point value.
225 * \param frac_bits The number of bits used to store the fractional part.
227 static inline uint32_t
228 U_FIXED(float value
, uint32_t frac_bits
)
230 value
*= (1 << frac_bits
);
231 return value
< 0.0f
? 0 : (uint32_t) value
;
235 * Convert a floating point value to an signed fixed point value.
237 * \param frac_bits The number of bits used to store the fractional part.
239 static inline int32_t
240 S_FIXED(float value
, uint32_t frac_bits
)
242 return (int32_t) (value
* (1 << frac_bits
));
247 /** Stepping a GLfloat pointer by a byte stride */
248 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
249 /** Stepping a GLuint pointer by a byte stride */
250 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
251 /** Stepping a GLubyte[4] pointer by a byte stride */
252 #define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i))
253 /** Stepping a GLfloat[4] pointer by a byte stride */
254 #define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i))
255 /** Stepping a \p t pointer by a byte stride */
256 #define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i))
259 /**********************************************************************/
260 /** \name 4-element vector operations */
264 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
266 /** Test for equality */
267 #define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \
268 (a)[1] == (b)[1] && \
269 (a)[2] == (b)[2] && \
272 /** Test for equality (unsigned bytes) */
273 static inline GLboolean
274 TEST_EQ_4UBV(const GLubyte a
[4], const GLubyte b
[4])
276 #if defined(__i386__)
277 return *((const GLuint
*) a
) == *((const GLuint
*) b
);
279 return TEST_EQ_4V(a
, b
);
284 /** Copy a 4-element vector */
285 #define COPY_4V( DST, SRC ) \
287 (DST)[0] = (SRC)[0]; \
288 (DST)[1] = (SRC)[1]; \
289 (DST)[2] = (SRC)[2]; \
290 (DST)[3] = (SRC)[3]; \
293 /** Copy a 4-element unsigned byte vector */
295 COPY_4UBV(GLubyte dst
[4], const GLubyte src
[4])
297 #if defined(__i386__)
298 *((GLuint
*) dst
) = *((GLuint
*) src
);
300 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
305 /** Copy \p SZ elements into a 4-element vector */
306 #define COPY_SZ_4V(DST, SZ, SRC) \
309 case 4: (DST)[3] = (SRC)[3]; \
310 case 3: (DST)[2] = (SRC)[2]; \
311 case 2: (DST)[1] = (SRC)[1]; \
312 case 1: (DST)[0] = (SRC)[0]; \
316 /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
317 * default values to the remaining */
318 #define COPY_CLEAN_4V(DST, SZ, SRC) \
320 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
321 COPY_SZ_4V( DST, SZ, SRC ); \
325 #define SUB_4V( DST, SRCA, SRCB ) \
327 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
328 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
329 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
330 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
334 #define ADD_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 /** Element-wise multiplication */
343 #define SCALE_4V( DST, SRCA, SRCB ) \
345 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
346 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
347 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
348 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
351 /** In-place addition */
352 #define ACC_4V( DST, SRC ) \
354 (DST)[0] += (SRC)[0]; \
355 (DST)[1] += (SRC)[1]; \
356 (DST)[2] += (SRC)[2]; \
357 (DST)[3] += (SRC)[3]; \
360 /** Element-wise multiplication and addition */
361 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
363 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
364 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
365 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
366 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
369 /** In-place scalar multiplication and addition */
370 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
372 (DST)[0] += S * (SRCB)[0]; \
373 (DST)[1] += S * (SRCB)[1]; \
374 (DST)[2] += S * (SRCB)[2]; \
375 (DST)[3] += S * (SRCB)[3]; \
378 /** Scalar multiplication */
379 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
381 (DST)[0] = S * (SRCB)[0]; \
382 (DST)[1] = S * (SRCB)[1]; \
383 (DST)[2] = S * (SRCB)[2]; \
384 (DST)[3] = S * (SRCB)[3]; \
387 /** In-place scalar multiplication */
388 #define SELF_SCALE_SCALAR_4V( DST, S ) \
399 /**********************************************************************/
400 /** \name 3-element vector operations*/
404 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
406 /** Test for equality */
407 #define TEST_EQ_3V(a,b) \
408 ((a)[0] == (b)[0] && \
409 (a)[1] == (b)[1] && \
412 /** Copy a 3-element vector */
413 #define COPY_3V( DST, SRC ) \
415 (DST)[0] = (SRC)[0]; \
416 (DST)[1] = (SRC)[1]; \
417 (DST)[2] = (SRC)[2]; \
420 /** Copy a 3-element vector with cast */
421 #define COPY_3V_CAST( DST, SRC, CAST ) \
423 (DST)[0] = (CAST)(SRC)[0]; \
424 (DST)[1] = (CAST)(SRC)[1]; \
425 (DST)[2] = (CAST)(SRC)[2]; \
428 /** Copy a 3-element float vector */
429 #define COPY_3FV( DST, SRC ) \
431 const GLfloat *_tmp = (SRC); \
432 (DST)[0] = _tmp[0]; \
433 (DST)[1] = _tmp[1]; \
434 (DST)[2] = _tmp[2]; \
438 #define SUB_3V( DST, SRCA, SRCB ) \
440 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
441 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
442 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
446 #define ADD_3V( DST, SRCA, SRCB ) \
448 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
449 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
450 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
453 /** In-place scalar multiplication */
454 #define SCALE_3V( DST, SRCA, SRCB ) \
456 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
457 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
458 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
461 /** In-place element-wise multiplication */
462 #define SELF_SCALE_3V( DST, SRC ) \
464 (DST)[0] *= (SRC)[0]; \
465 (DST)[1] *= (SRC)[1]; \
466 (DST)[2] *= (SRC)[2]; \
469 /** In-place addition */
470 #define ACC_3V( DST, SRC ) \
472 (DST)[0] += (SRC)[0]; \
473 (DST)[1] += (SRC)[1]; \
474 (DST)[2] += (SRC)[2]; \
477 /** Element-wise multiplication and addition */
478 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
480 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
481 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
482 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
485 /** Scalar multiplication */
486 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
488 (DST)[0] = S * (SRCB)[0]; \
489 (DST)[1] = S * (SRCB)[1]; \
490 (DST)[2] = S * (SRCB)[2]; \
493 /** In-place scalar multiplication and addition */
494 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
496 (DST)[0] += S * (SRCB)[0]; \
497 (DST)[1] += S * (SRCB)[1]; \
498 (DST)[2] += S * (SRCB)[2]; \
501 /** In-place scalar multiplication */
502 #define SELF_SCALE_SCALAR_3V( DST, S ) \
509 /** In-place scalar addition */
510 #define ACC_SCALAR_3V( DST, S ) \
518 #define ASSIGN_3V( V, V0, V1, V2 ) \
528 /**********************************************************************/
529 /** \name 2-element vector operations*/
533 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
535 /** Copy a 2-element vector */
536 #define COPY_2V( DST, SRC ) \
538 (DST)[0] = (SRC)[0]; \
539 (DST)[1] = (SRC)[1]; \
542 /** Copy a 2-element vector with cast */
543 #define COPY_2V_CAST( DST, SRC, CAST ) \
545 (DST)[0] = (CAST)(SRC)[0]; \
546 (DST)[1] = (CAST)(SRC)[1]; \
549 /** Copy a 2-element float vector */
550 #define COPY_2FV( DST, SRC ) \
552 const GLfloat *_tmp = (SRC); \
553 (DST)[0] = _tmp[0]; \
554 (DST)[1] = _tmp[1]; \
558 #define SUB_2V( DST, SRCA, SRCB ) \
560 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
561 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
565 #define ADD_2V( DST, SRCA, SRCB ) \
567 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
568 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
571 /** In-place scalar multiplication */
572 #define SCALE_2V( DST, SRCA, SRCB ) \
574 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
575 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
578 /** In-place addition */
579 #define ACC_2V( DST, SRC ) \
581 (DST)[0] += (SRC)[0]; \
582 (DST)[1] += (SRC)[1]; \
585 /** Element-wise multiplication and addition */
586 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
588 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
589 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
592 /** Scalar multiplication */
593 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
595 (DST)[0] = S * (SRCB)[0]; \
596 (DST)[1] = S * (SRCB)[1]; \
599 /** In-place scalar multiplication and addition */
600 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
602 (DST)[0] += S * (SRCB)[0]; \
603 (DST)[1] += S * (SRCB)[1]; \
606 /** In-place scalar multiplication */
607 #define SELF_SCALE_SCALAR_2V( DST, S ) \
613 /** In-place scalar addition */
614 #define ACC_SCALAR_2V( DST, S ) \
620 /** Assign scalers to short vectors */
621 #define ASSIGN_2V( V, V0, V1 ) \
629 /** Copy \p sz elements into a homegeneous (4-element) vector, giving
630 * default values to the remaining components.
631 * The default values are chosen based on \p type.
634 COPY_CLEAN_4V_TYPE_AS_UNION(fi_type dst
[4], int sz
, const fi_type src
[4],
639 ASSIGN_4V(dst
, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
640 FLOAT_AS_UNION(0), FLOAT_AS_UNION(1));
643 ASSIGN_4V(dst
, INT_AS_UNION(0), INT_AS_UNION(0),
644 INT_AS_UNION(0), INT_AS_UNION(1));
646 case GL_UNSIGNED_INT
:
647 ASSIGN_4V(dst
, UINT_AS_UNION(0), UINT_AS_UNION(0),
648 UINT_AS_UNION(0), UINT_AS_UNION(1));
651 ASSIGN_4V(dst
, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
652 FLOAT_AS_UNION(0), FLOAT_AS_UNION(1)); /* silence warnings */
653 assert(!"Unexpected type in COPY_CLEAN_4V_TYPE_AS_UNION macro");
655 COPY_SZ_4V(dst
, sz
, src
);
658 /** \name Linear interpolation functions */
661 static inline GLfloat
662 LINTERP(GLfloat t
, GLfloat out
, GLfloat in
)
664 return out
+ t
* (in
- out
);
668 INTERP_3F(GLfloat t
, GLfloat dst
[3], const GLfloat out
[3], const GLfloat in
[3])
670 dst
[0] = LINTERP( t
, out
[0], in
[0] );
671 dst
[1] = LINTERP( t
, out
[1], in
[1] );
672 dst
[2] = LINTERP( t
, out
[2], in
[2] );
676 INTERP_4F(GLfloat t
, GLfloat dst
[4], const GLfloat out
[4], const GLfloat in
[4])
678 dst
[0] = LINTERP( t
, out
[0], in
[0] );
679 dst
[1] = LINTERP( t
, out
[1], in
[1] );
680 dst
[2] = LINTERP( t
, out
[2], in
[2] );
681 dst
[3] = LINTERP( t
, out
[3], in
[3] );
688 static inline unsigned
689 minify(unsigned value
, unsigned levels
)
691 return MAX2(1, value
>> levels
);
695 /** Cross product of two 3-element vectors */
697 CROSS3(GLfloat n
[3], const GLfloat u
[3], const GLfloat v
[3])
699 n
[0] = u
[1] * v
[2] - u
[2] * v
[1];
700 n
[1] = u
[2] * v
[0] - u
[0] * v
[2];
701 n
[2] = u
[0] * v
[1] - u
[1] * v
[0];
705 /** Dot product of two 2-element vectors */
706 static inline GLfloat
707 DOT2(const GLfloat a
[2], const GLfloat b
[2])
709 return a
[0] * b
[0] + a
[1] * b
[1];
712 static inline GLfloat
713 DOT3(const GLfloat a
[3], const GLfloat b
[3])
715 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2];
718 static inline GLfloat
719 DOT4(const GLfloat a
[4], const GLfloat b
[4])
721 return a
[0] * b
[0] + a
[1] * b
[1] + a
[2] * b
[2] + a
[3] * b
[3];
725 static inline GLfloat
726 LEN_SQUARED_3FV(const GLfloat v
[3])
731 static inline GLfloat
732 LEN_SQUARED_2FV(const GLfloat v
[2])
738 static inline GLfloat
739 LEN_3FV(const GLfloat v
[3])
741 return sqrtf(LEN_SQUARED_3FV(v
));
744 static inline GLfloat
745 LEN_2FV(const GLfloat v
[2])
747 return sqrtf(LEN_SQUARED_2FV(v
));
751 /* Normalize a 3-element vector to unit length. */
753 NORMALIZE_3FV(GLfloat v
[3])
755 GLfloat len
= (GLfloat
) LEN_SQUARED_3FV(v
);
757 len
= 1.0f
/ sqrtf(len
);
765 /** Test two floats have opposite signs */
766 static inline GLboolean
767 DIFFERENT_SIGNS(GLfloat x
, GLfloat y
)
770 #pragma warning( push )
771 #pragma warning( disable : 6334 ) /* sizeof operator applied to an expression with an operator may yield unexpected results */
773 return signbit(x
) != signbit(y
);
775 #pragma warning( pop )
780 /** casts to silence warnings with some compilers */
781 #define ENUM_TO_INT(E) ((GLint)(E))
782 #define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E))
783 #define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E))
784 #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
788 #define STRINGIFY(x) #x