efdb0a0957060015450035a52aad43a357065129
[mesa.git] / src / mesa / main / macros.h
1 /**
2 * \file macros.h
3 * A collection of useful macros.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 *
9 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
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.
28 */
29
30
31 #ifndef MACROS_H
32 #define MACROS_H
33
34 #include "util/macros.h"
35 #include "util/u_math.h"
36 #include "util/rounding.h"
37 #include "imports.h"
38
39
40 /**
41 * \name Integer / float conversion for colors, normals, etc.
42 */
43 /*@{*/
44
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)]
48
49 /** Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
50 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) ((X) * 255.0F))
51
52
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))
55
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 )
58
59
60 /** Convert GLbyte to GLfloat while preserving zero */
61 #define BYTE_TO_FLOATZ(B) ((B) == 0 ? 0.0F : BYTE_TO_FLOAT(B))
62
63
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))
66
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 )
69
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))
72
73 /** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */
74 #define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F))
75
76
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))
79
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 )
82
83 /** Convert GLshort to GLfloat while preserving zero */
84 #define SHORT_TO_FLOATZ(S) ((S) == 0 ? 0.0F : SHORT_TO_FLOAT(S))
85
86
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))
89
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)) )
92
93
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)))
96
97 /** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
98 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
99
100
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)))
103
104 /** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
105 /* causes overflow:
106 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 )
107 */
108 /* a close approximation: */
109 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
110
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)) )
113
114
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))
117
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)) )
120
121
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))
127
128
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) )
138
139 #define UNCLAMPED_FLOAT_TO_SHORT(s, f) \
140 s = ( (GLshort) _mesa_lroundevenf( CLAMP((f), -1.0F, 1.0F) * 32767.0F) )
141
142 /***
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]
145 ***/
146 #ifndef DEBUG
147 /* This function/macro is sensitive to precision. Test very carefully
148 * if you change it!
149 */
150 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, FLT) \
151 do { \
152 fi_type __tmp; \
153 __tmp.f = (FLT); \
154 if (__tmp.i < 0) \
155 UB = (GLubyte) 0; \
156 else if (__tmp.i >= IEEE_ONE) \
157 UB = (GLubyte) 255; \
158 else { \
159 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
160 UB = (GLubyte) __tmp.i; \
161 } \
162 } while (0)
163 #define CLAMPED_FLOAT_TO_UBYTE(UB, FLT) \
164 do { \
165 fi_type __tmp; \
166 __tmp.f = (FLT) * (255.0F/256.0F) + 32768.0F; \
167 UB = (GLubyte) __tmp.i; \
168 } while (0)
169 #else
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))
174 #endif
175
176 static fi_type UINT_AS_UNION(GLuint u)
177 {
178 fi_type tmp;
179 tmp.u = u;
180 return tmp;
181 }
182
183 static inline fi_type INT_AS_UNION(GLint i)
184 {
185 fi_type tmp;
186 tmp.i = i;
187 return tmp;
188 }
189
190 static inline fi_type FLOAT_AS_UNION(GLfloat f)
191 {
192 fi_type tmp;
193 tmp.f = f;
194 return tmp;
195 }
196
197 static inline uint64_t DOUBLE_AS_UINT64(double d)
198 {
199 union {
200 double d;
201 uint64_t u64;
202 } tmp;
203 tmp.d = d;
204 return tmp.u64;
205 }
206
207 static inline double UINT64_AS_DOUBLE(uint64_t u)
208 {
209 union {
210 double d;
211 uint64_t u64;
212 } tmp;
213 tmp.u64 = u;
214 return tmp.d;
215 }
216
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)
220
221 /**
222 * Convert a floating point value to an unsigned fixed point value.
223 *
224 * \param frac_bits The number of bits used to store the fractional part.
225 */
226 static inline uint32_t
227 U_FIXED(float value, uint32_t frac_bits)
228 {
229 value *= (1 << frac_bits);
230 return value < 0.0f ? 0 : (uint32_t) value;
231 }
232
233 /**
234 * Convert a floating point value to an signed fixed point value.
235 *
236 * \param frac_bits The number of bits used to store the fractional part.
237 */
238 static inline int32_t
239 S_FIXED(float value, uint32_t frac_bits)
240 {
241 return (int32_t) (value * (1 << frac_bits));
242 }
243 /*@}*/
244
245
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))
256
257
258 /**********************************************************************/
259 /** \name 4-element vector operations */
260 /*@{*/
261
262 /** Zero */
263 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
264
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] && \
269 (a)[3] == (b)[3])
270
271 /** Test for equality (unsigned bytes) */
272 static inline GLboolean
273 TEST_EQ_4UBV(const GLubyte a[4], const GLubyte b[4])
274 {
275 #if defined(__i386__)
276 return *((const GLuint *) a) == *((const GLuint *) b);
277 #else
278 return TEST_EQ_4V(a, b);
279 #endif
280 }
281
282
283 /** Copy a 4-element vector */
284 #define COPY_4V( DST, SRC ) \
285 do { \
286 (DST)[0] = (SRC)[0]; \
287 (DST)[1] = (SRC)[1]; \
288 (DST)[2] = (SRC)[2]; \
289 (DST)[3] = (SRC)[3]; \
290 } while (0)
291
292 /** Copy a 4-element unsigned byte vector */
293 static inline void
294 COPY_4UBV(GLubyte dst[4], const GLubyte src[4])
295 {
296 #if defined(__i386__)
297 *((GLuint *) dst) = *((GLuint *) src);
298 #else
299 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
300 COPY_4V(dst, src);
301 #endif
302 }
303
304 /** Copy \p SZ elements into a 4-element vector */
305 #define COPY_SZ_4V(DST, SZ, SRC) \
306 do { \
307 switch (SZ) { \
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]; \
312 } \
313 } while(0)
314
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) \
318 do { \
319 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
320 COPY_SZ_4V( DST, SZ, SRC ); \
321 } while (0)
322
323 /** Subtraction */
324 #define SUB_4V( DST, SRCA, SRCB ) \
325 do { \
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]; \
330 } while (0)
331
332 /** Addition */
333 #define ADD_4V( DST, SRCA, SRCB ) \
334 do { \
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]; \
339 } while (0)
340
341 /** Element-wise multiplication */
342 #define SCALE_4V( DST, SRCA, SRCB ) \
343 do { \
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]; \
348 } while (0)
349
350 /** In-place addition */
351 #define ACC_4V( DST, SRC ) \
352 do { \
353 (DST)[0] += (SRC)[0]; \
354 (DST)[1] += (SRC)[1]; \
355 (DST)[2] += (SRC)[2]; \
356 (DST)[3] += (SRC)[3]; \
357 } while (0)
358
359 /** Element-wise multiplication and addition */
360 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
361 do { \
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]; \
366 } while (0)
367
368 /** In-place scalar multiplication and addition */
369 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
370 do { \
371 (DST)[0] += S * (SRCB)[0]; \
372 (DST)[1] += S * (SRCB)[1]; \
373 (DST)[2] += S * (SRCB)[2]; \
374 (DST)[3] += S * (SRCB)[3]; \
375 } while (0)
376
377 /** Scalar multiplication */
378 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
379 do { \
380 (DST)[0] = S * (SRCB)[0]; \
381 (DST)[1] = S * (SRCB)[1]; \
382 (DST)[2] = S * (SRCB)[2]; \
383 (DST)[3] = S * (SRCB)[3]; \
384 } while (0)
385
386 /** In-place scalar multiplication */
387 #define SELF_SCALE_SCALAR_4V( DST, S ) \
388 do { \
389 (DST)[0] *= S; \
390 (DST)[1] *= S; \
391 (DST)[2] *= S; \
392 (DST)[3] *= S; \
393 } while (0)
394
395 /*@}*/
396
397
398 /**********************************************************************/
399 /** \name 3-element vector operations*/
400 /*@{*/
401
402 /** Zero */
403 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
404
405 /** Test for equality */
406 #define TEST_EQ_3V(a,b) \
407 ((a)[0] == (b)[0] && \
408 (a)[1] == (b)[1] && \
409 (a)[2] == (b)[2])
410
411 /** Copy a 3-element vector */
412 #define COPY_3V( DST, SRC ) \
413 do { \
414 (DST)[0] = (SRC)[0]; \
415 (DST)[1] = (SRC)[1]; \
416 (DST)[2] = (SRC)[2]; \
417 } while (0)
418
419 /** Copy a 3-element vector with cast */
420 #define COPY_3V_CAST( DST, SRC, CAST ) \
421 do { \
422 (DST)[0] = (CAST)(SRC)[0]; \
423 (DST)[1] = (CAST)(SRC)[1]; \
424 (DST)[2] = (CAST)(SRC)[2]; \
425 } while (0)
426
427 /** Copy a 3-element float vector */
428 #define COPY_3FV( DST, SRC ) \
429 do { \
430 const GLfloat *_tmp = (SRC); \
431 (DST)[0] = _tmp[0]; \
432 (DST)[1] = _tmp[1]; \
433 (DST)[2] = _tmp[2]; \
434 } while (0)
435
436 /** Subtraction */
437 #define SUB_3V( DST, SRCA, SRCB ) \
438 do { \
439 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
440 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
441 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
442 } while (0)
443
444 /** Addition */
445 #define ADD_3V( DST, SRCA, SRCB ) \
446 do { \
447 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
448 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
449 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
450 } while (0)
451
452 /** In-place scalar multiplication */
453 #define SCALE_3V( DST, SRCA, SRCB ) \
454 do { \
455 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
456 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
457 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
458 } while (0)
459
460 /** In-place element-wise multiplication */
461 #define SELF_SCALE_3V( DST, SRC ) \
462 do { \
463 (DST)[0] *= (SRC)[0]; \
464 (DST)[1] *= (SRC)[1]; \
465 (DST)[2] *= (SRC)[2]; \
466 } while (0)
467
468 /** In-place addition */
469 #define ACC_3V( DST, SRC ) \
470 do { \
471 (DST)[0] += (SRC)[0]; \
472 (DST)[1] += (SRC)[1]; \
473 (DST)[2] += (SRC)[2]; \
474 } while (0)
475
476 /** Element-wise multiplication and addition */
477 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
478 do { \
479 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
480 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
481 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
482 } while (0)
483
484 /** Scalar multiplication */
485 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
486 do { \
487 (DST)[0] = S * (SRCB)[0]; \
488 (DST)[1] = S * (SRCB)[1]; \
489 (DST)[2] = S * (SRCB)[2]; \
490 } while (0)
491
492 /** In-place scalar multiplication and addition */
493 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
494 do { \
495 (DST)[0] += S * (SRCB)[0]; \
496 (DST)[1] += S * (SRCB)[1]; \
497 (DST)[2] += S * (SRCB)[2]; \
498 } while (0)
499
500 /** In-place scalar multiplication */
501 #define SELF_SCALE_SCALAR_3V( DST, S ) \
502 do { \
503 (DST)[0] *= S; \
504 (DST)[1] *= S; \
505 (DST)[2] *= S; \
506 } while (0)
507
508 /** In-place scalar addition */
509 #define ACC_SCALAR_3V( DST, S ) \
510 do { \
511 (DST)[0] += S; \
512 (DST)[1] += S; \
513 (DST)[2] += S; \
514 } while (0)
515
516 /** Assignment */
517 #define ASSIGN_3V( V, V0, V1, V2 ) \
518 do { \
519 V[0] = V0; \
520 V[1] = V1; \
521 V[2] = V2; \
522 } while(0)
523
524 /*@}*/
525
526
527 /**********************************************************************/
528 /** \name 2-element vector operations*/
529 /*@{*/
530
531 /** Zero */
532 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
533
534 /** Copy a 2-element vector */
535 #define COPY_2V( DST, SRC ) \
536 do { \
537 (DST)[0] = (SRC)[0]; \
538 (DST)[1] = (SRC)[1]; \
539 } while (0)
540
541 /** Copy a 2-element vector with cast */
542 #define COPY_2V_CAST( DST, SRC, CAST ) \
543 do { \
544 (DST)[0] = (CAST)(SRC)[0]; \
545 (DST)[1] = (CAST)(SRC)[1]; \
546 } while (0)
547
548 /** Copy a 2-element float vector */
549 #define COPY_2FV( DST, SRC ) \
550 do { \
551 const GLfloat *_tmp = (SRC); \
552 (DST)[0] = _tmp[0]; \
553 (DST)[1] = _tmp[1]; \
554 } while (0)
555
556 /** Subtraction */
557 #define SUB_2V( DST, SRCA, SRCB ) \
558 do { \
559 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
560 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
561 } while (0)
562
563 /** Addition */
564 #define ADD_2V( DST, SRCA, SRCB ) \
565 do { \
566 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
567 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
568 } while (0)
569
570 /** In-place scalar multiplication */
571 #define SCALE_2V( DST, SRCA, SRCB ) \
572 do { \
573 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
574 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
575 } while (0)
576
577 /** In-place addition */
578 #define ACC_2V( DST, SRC ) \
579 do { \
580 (DST)[0] += (SRC)[0]; \
581 (DST)[1] += (SRC)[1]; \
582 } while (0)
583
584 /** Element-wise multiplication and addition */
585 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
586 do { \
587 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
588 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
589 } while (0)
590
591 /** Scalar multiplication */
592 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
593 do { \
594 (DST)[0] = S * (SRCB)[0]; \
595 (DST)[1] = S * (SRCB)[1]; \
596 } while (0)
597
598 /** In-place scalar multiplication and addition */
599 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
600 do { \
601 (DST)[0] += S * (SRCB)[0]; \
602 (DST)[1] += S * (SRCB)[1]; \
603 } while (0)
604
605 /** In-place scalar multiplication */
606 #define SELF_SCALE_SCALAR_2V( DST, S ) \
607 do { \
608 (DST)[0] *= S; \
609 (DST)[1] *= S; \
610 } while (0)
611
612 /** In-place scalar addition */
613 #define ACC_SCALAR_2V( DST, S ) \
614 do { \
615 (DST)[0] += S; \
616 (DST)[1] += S; \
617 } while (0)
618
619 /** Assign scalers to short vectors */
620 #define ASSIGN_2V( V, V0, V1 ) \
621 do { \
622 V[0] = V0; \
623 V[1] = V1; \
624 } while(0)
625
626 /*@}*/
627
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.
631 */
632 static inline void
633 COPY_CLEAN_4V_TYPE_AS_UNION(fi_type dst[4], int sz, const fi_type src[4],
634 GLenum type)
635 {
636 switch (type) {
637 case GL_FLOAT:
638 ASSIGN_4V(dst, FLOAT_AS_UNION(0), FLOAT_AS_UNION(0),
639 FLOAT_AS_UNION(0), FLOAT_AS_UNION(1));
640 break;
641 case GL_INT:
642 ASSIGN_4V(dst, INT_AS_UNION(0), INT_AS_UNION(0),
643 INT_AS_UNION(0), INT_AS_UNION(1));
644 break;
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));
648 break;
649 default:
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");
653 }
654 COPY_SZ_4V(dst, sz, src);
655 }
656
657 /** \name Linear interpolation functions */
658 /*@{*/
659
660 static inline GLfloat
661 LINTERP(GLfloat t, GLfloat out, GLfloat in)
662 {
663 return out + t * (in - out);
664 }
665
666 static inline void
667 INTERP_3F(GLfloat t, GLfloat dst[3], const GLfloat out[3], const GLfloat in[3])
668 {
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] );
672 }
673
674 static inline void
675 INTERP_4F(GLfloat t, GLfloat dst[4], const GLfloat out[4], const GLfloat in[4])
676 {
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] );
681 }
682
683 /*@}*/
684
685
686
687 static inline unsigned
688 minify(unsigned value, unsigned levels)
689 {
690 return MAX2(1, value >> levels);
691 }
692
693 /**
694 * Align a value up to an alignment value
695 *
696 * If \c value is not already aligned to the requested alignment value, it
697 * will be rounded up.
698 *
699 * \param value Value to be rounded
700 * \param alignment Alignment value to be used. This must be a power of two.
701 *
702 * \sa ROUND_DOWN_TO()
703 */
704 static inline uintptr_t
705 ALIGN(uintptr_t value, int32_t alignment)
706 {
707 assert((alignment > 0) && _mesa_is_pow_two(alignment));
708 return (((value) + (alignment) - 1) & ~((alignment) - 1));
709 }
710
711 /**
712 * Like ALIGN(), but works with a non-power-of-two alignment.
713 */
714 static inline uintptr_t
715 ALIGN_NPOT(uintptr_t value, int32_t alignment)
716 {
717 assert(alignment > 0);
718 return (value + alignment - 1) / alignment * alignment;
719 }
720
721 /**
722 * Align a value down to an alignment value
723 *
724 * If \c value is not already aligned to the requested alignment value, it
725 * will be rounded down.
726 *
727 * \param value Value to be rounded
728 * \param alignment Alignment value to be used. This must be a power of two.
729 *
730 * \sa ALIGN()
731 */
732 static inline uintptr_t
733 ROUND_DOWN_TO(uintptr_t value, int32_t alignment)
734 {
735 assert((alignment > 0) && _mesa_is_pow_two(alignment));
736 return ((value) & ~(alignment - 1));
737 }
738
739
740 /** Cross product of two 3-element vectors */
741 static inline void
742 CROSS3(GLfloat n[3], const GLfloat u[3], const GLfloat v[3])
743 {
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];
747 }
748
749
750 /** Dot product of two 2-element vectors */
751 static inline GLfloat
752 DOT2(const GLfloat a[2], const GLfloat b[2])
753 {
754 return a[0] * b[0] + a[1] * b[1];
755 }
756
757 static inline GLfloat
758 DOT3(const GLfloat a[3], const GLfloat b[3])
759 {
760 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
761 }
762
763 static inline GLfloat
764 DOT4(const GLfloat a[4], const GLfloat b[4])
765 {
766 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
767 }
768
769
770 static inline GLfloat
771 LEN_SQUARED_3FV(const GLfloat v[3])
772 {
773 return DOT3(v, v);
774 }
775
776 static inline GLfloat
777 LEN_SQUARED_2FV(const GLfloat v[2])
778 {
779 return DOT2(v, v);
780 }
781
782
783 static inline GLfloat
784 LEN_3FV(const GLfloat v[3])
785 {
786 return sqrtf(LEN_SQUARED_3FV(v));
787 }
788
789 static inline GLfloat
790 LEN_2FV(const GLfloat v[2])
791 {
792 return sqrtf(LEN_SQUARED_2FV(v));
793 }
794
795
796 /* Normalize a 3-element vector to unit length. */
797 static inline void
798 NORMALIZE_3FV(GLfloat v[3])
799 {
800 GLfloat len = (GLfloat) LEN_SQUARED_3FV(v);
801 if (len) {
802 len = 1.0f / sqrtf(len);
803 v[0] *= len;
804 v[1] *= len;
805 v[2] *= len;
806 }
807 }
808
809
810 /** Test two floats have opposite signs */
811 static inline GLboolean
812 DIFFERENT_SIGNS(GLfloat x, GLfloat y)
813 {
814 #ifdef _MSC_VER
815 #pragma warning( push )
816 #pragma warning( disable : 6334 ) /* sizeof operator applied to an expression with an operator may yield unexpected results */
817 #endif
818 return signbit(x) != signbit(y);
819 #ifdef _MSC_VER
820 #pragma warning( pop )
821 #endif
822 }
823
824
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)
830
831
832 /* Stringify */
833 #define STRINGIFY(x) #x
834
835 #endif