macros.h revision 848b8605
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 "imports.h" 35 36 37/** 38 * \name Integer / float conversion for colors, normals, etc. 39 */ 40/*@{*/ 41 42/** Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */ 43extern GLfloat _mesa_ubyte_to_float_color_tab[256]; 44#define UBYTE_TO_FLOAT(u) _mesa_ubyte_to_float_color_tab[(unsigned int)(u)] 45 46/** Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */ 47#define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) ((X) * 255.0F)) 48 49 50/** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */ 51#define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F)) 52 53/** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */ 54#define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 ) 55 56 57/** Convert GLbyte to GLfloat while preserving zero */ 58#define BYTE_TO_FLOATZ(B) ((B) == 0 ? 0.0F : BYTE_TO_FLOAT(B)) 59 60 61/** Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0], texture/fb data */ 62#define BYTE_TO_FLOAT_TEX(B) ((B) == -128 ? -1.0F : (B) * (1.0F/127.0F)) 63 64/** Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127], texture/fb data */ 65#define FLOAT_TO_BYTE_TEX(X) CLAMP( (GLint) (127.0F * (X)), -128, 127 ) 66 67/** Convert GLushort in [0,65535] to GLfloat in [0.0,1.0] */ 68#define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F)) 69 70/** Convert GLfloat in [0.0,1.0] to GLushort in [0, 65535] */ 71#define FLOAT_TO_USHORT(X) ((GLuint) ((X) * 65535.0F)) 72 73 74/** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */ 75#define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)) 76 77/** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767] */ 78#define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 ) 79 80/** Convert GLshort to GLfloat while preserving zero */ 81#define SHORT_TO_FLOATZ(S) ((S) == 0 ? 0.0F : SHORT_TO_FLOAT(S)) 82 83 84/** Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0], texture/fb data */ 85#define SHORT_TO_FLOAT_TEX(S) ((S) == -32768 ? -1.0F : (S) * (1.0F/32767.0F)) 86 87/** Convert GLfloat in [-1.0,1.0] to GLshort in [-32768,32767], texture/fb data */ 88#define FLOAT_TO_SHORT_TEX(X) ( (GLint) (32767.0F * (X)) ) 89 90 91/** Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */ 92#define UINT_TO_FLOAT(U) ((GLfloat) ((U) * (1.0F / 4294967295.0))) 93 94/** Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */ 95#define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0)) 96 97 98/** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */ 99#define INT_TO_FLOAT(I) ((GLfloat) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0))) 100 101/** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */ 102/* causes overflow: 103#define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0 * (X))) - 1) / 2 ) 104*/ 105/* a close approximation: */ 106#define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) ) 107 108/** Convert GLfloat in [-1.0,1.0] to GLint64 in [-(1<<63),(1 << 63) -1] */ 109#define FLOAT_TO_INT64(X) ( (GLint64) (9223372036854775807.0 * (double)(X)) ) 110 111 112/** Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0], texture/fb data */ 113#define INT_TO_FLOAT_TEX(I) ((I) == -2147483648 ? -1.0F : (I) * (1.0F/2147483647.0)) 114 115/** Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647], texture/fb data */ 116#define FLOAT_TO_INT_TEX(X) ( (GLint) (2147483647.0 * (X)) ) 117 118 119#define BYTE_TO_UBYTE(b) ((GLubyte) ((b) < 0 ? 0 : (GLubyte) (b))) 120#define SHORT_TO_UBYTE(s) ((GLubyte) ((s) < 0 ? 0 : (GLubyte) ((s) >> 7))) 121#define USHORT_TO_UBYTE(s) ((GLubyte) ((s) >> 8)) 122#define INT_TO_UBYTE(i) ((GLubyte) ((i) < 0 ? 0 : (GLubyte) ((i) >> 23))) 123#define UINT_TO_UBYTE(i) ((GLubyte) ((i) >> 24)) 124 125 126#define BYTE_TO_USHORT(b) ((b) < 0 ? 0 : ((GLushort) (((b) * 65535) / 255))) 127#define UBYTE_TO_USHORT(b) (((GLushort) (b) << 8) | (GLushort) (b)) 128#define SHORT_TO_USHORT(s) ((s) < 0 ? 0 : ((GLushort) (((s) * 65535 / 32767)))) 129#define INT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 15))) 130#define UINT_TO_USHORT(i) ((i) < 0 ? 0 : ((GLushort) ((i) >> 16))) 131#define UNCLAMPED_FLOAT_TO_USHORT(us, f) \ 132 us = ( (GLushort) F_TO_I( CLAMP((f), 0.0F, 1.0F) * 65535.0F) ) 133#define CLAMPED_FLOAT_TO_USHORT(us, f) \ 134 us = ( (GLushort) F_TO_I( (f) * 65535.0F) ) 135 136#define UNCLAMPED_FLOAT_TO_SHORT(s, f) \ 137 s = ( (GLshort) F_TO_I( CLAMP((f), -1.0F, 1.0F) * 32767.0F) ) 138 139/*** 140 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255] 141 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255] 142 ***/ 143#ifndef DEBUG 144/* This function/macro is sensitive to precision. Test very carefully 145 * if you change it! 146 */ 147#define UNCLAMPED_FLOAT_TO_UBYTE(UB, FLT) \ 148 do { \ 149 fi_type __tmp; \ 150 __tmp.f = (FLT); \ 151 if (__tmp.i < 0) \ 152 UB = (GLubyte) 0; \ 153 else if (__tmp.i >= IEEE_ONE) \ 154 UB = (GLubyte) 255; \ 155 else { \ 156 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \ 157 UB = (GLubyte) __tmp.i; \ 158 } \ 159 } while (0) 160#define CLAMPED_FLOAT_TO_UBYTE(UB, FLT) \ 161 do { \ 162 fi_type __tmp; \ 163 __tmp.f = (FLT) * (255.0F/256.0F) + 32768.0F; \ 164 UB = (GLubyte) __tmp.i; \ 165 } while (0) 166#else 167#define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \ 168 ub = ((GLubyte) F_TO_I(CLAMP((f), 0.0F, 1.0F) * 255.0F)) 169#define CLAMPED_FLOAT_TO_UBYTE(ub, f) \ 170 ub = ((GLubyte) F_TO_I((f) * 255.0F)) 171#endif 172 173static inline GLfloat INT_AS_FLT(GLint i) 174{ 175 fi_type tmp; 176 tmp.i = i; 177 return tmp.f; 178} 179 180static inline GLfloat UINT_AS_FLT(GLuint u) 181{ 182 fi_type tmp; 183 tmp.u = u; 184 return tmp.f; 185} 186 187static inline unsigned FLT_AS_UINT(float f) 188{ 189 fi_type tmp; 190 tmp.f = f; 191 return tmp.u; 192} 193 194/** 195 * Convert a floating point value to an unsigned fixed point value. 196 * 197 * \param frac_bits The number of bits used to store the fractional part. 198 */ 199static INLINE uint32_t 200U_FIXED(float value, uint32_t frac_bits) 201{ 202 value *= (1 << frac_bits); 203 return value < 0.0f ? 0 : (uint32_t) value; 204} 205 206/** 207 * Convert a floating point value to an signed fixed point value. 208 * 209 * \param frac_bits The number of bits used to store the fractional part. 210 */ 211static INLINE int32_t 212S_FIXED(float value, uint32_t frac_bits) 213{ 214 return (int32_t) (value * (1 << frac_bits)); 215} 216/*@}*/ 217 218 219/** Stepping a GLfloat pointer by a byte stride */ 220#define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i)) 221/** Stepping a GLuint pointer by a byte stride */ 222#define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i)) 223/** Stepping a GLubyte[4] pointer by a byte stride */ 224#define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i)) 225/** Stepping a GLfloat[4] pointer by a byte stride */ 226#define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i)) 227/** Stepping a \p t pointer by a byte stride */ 228#define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i)) 229 230 231/**********************************************************************/ 232/** \name 4-element vector operations */ 233/*@{*/ 234 235/** Zero */ 236#define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0 237 238/** Test for equality */ 239#define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \ 240 (a)[1] == (b)[1] && \ 241 (a)[2] == (b)[2] && \ 242 (a)[3] == (b)[3]) 243 244/** Test for equality (unsigned bytes) */ 245static inline GLboolean 246TEST_EQ_4UBV(const GLubyte a[4], const GLubyte b[4]) 247{ 248#if defined(__i386__) 249 return *((const GLuint *) a) == *((const GLuint *) b); 250#else 251 return TEST_EQ_4V(a, b); 252#endif 253} 254 255 256/** Copy a 4-element vector */ 257#define COPY_4V( DST, SRC ) \ 258do { \ 259 (DST)[0] = (SRC)[0]; \ 260 (DST)[1] = (SRC)[1]; \ 261 (DST)[2] = (SRC)[2]; \ 262 (DST)[3] = (SRC)[3]; \ 263} while (0) 264 265/** Copy a 4-element unsigned byte vector */ 266static inline void 267COPY_4UBV(GLubyte dst[4], const GLubyte src[4]) 268{ 269#if defined(__i386__) 270 *((GLuint *) dst) = *((GLuint *) src); 271#else 272 /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */ 273 COPY_4V(dst, src); 274#endif 275} 276 277/** Copy a 4-element float vector */ 278static inline void 279COPY_4FV(GLfloat dst[4], const GLfloat src[4]) 280{ 281 /* memcpy seems to be most efficient */ 282 memcpy(dst, src, sizeof(GLfloat) * 4); 283} 284 285/** Copy \p SZ elements into a 4-element vector */ 286#define COPY_SZ_4V(DST, SZ, SRC) \ 287do { \ 288 switch (SZ) { \ 289 case 4: (DST)[3] = (SRC)[3]; \ 290 case 3: (DST)[2] = (SRC)[2]; \ 291 case 2: (DST)[1] = (SRC)[1]; \ 292 case 1: (DST)[0] = (SRC)[0]; \ 293 } \ 294} while(0) 295 296/** Copy \p SZ elements into a homegeneous (4-element) vector, giving 297 * default values to the remaining */ 298#define COPY_CLEAN_4V(DST, SZ, SRC) \ 299do { \ 300 ASSIGN_4V( DST, 0, 0, 0, 1 ); \ 301 COPY_SZ_4V( DST, SZ, SRC ); \ 302} while (0) 303 304/** Subtraction */ 305#define SUB_4V( DST, SRCA, SRCB ) \ 306do { \ 307 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \ 308 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \ 309 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \ 310 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \ 311} while (0) 312 313/** Addition */ 314#define ADD_4V( DST, SRCA, SRCB ) \ 315do { \ 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]; \ 320} while (0) 321 322/** Element-wise multiplication */ 323#define SCALE_4V( DST, SRCA, SRCB ) \ 324do { \ 325 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \ 326 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \ 327 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \ 328 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \ 329} while (0) 330 331/** In-place addition */ 332#define ACC_4V( DST, SRC ) \ 333do { \ 334 (DST)[0] += (SRC)[0]; \ 335 (DST)[1] += (SRC)[1]; \ 336 (DST)[2] += (SRC)[2]; \ 337 (DST)[3] += (SRC)[3]; \ 338} while (0) 339 340/** Element-wise multiplication and addition */ 341#define ACC_SCALE_4V( DST, SRCA, SRCB ) \ 342do { \ 343 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \ 344 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \ 345 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \ 346 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \ 347} while (0) 348 349/** In-place scalar multiplication and addition */ 350#define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \ 351do { \ 352 (DST)[0] += S * (SRCB)[0]; \ 353 (DST)[1] += S * (SRCB)[1]; \ 354 (DST)[2] += S * (SRCB)[2]; \ 355 (DST)[3] += S * (SRCB)[3]; \ 356} while (0) 357 358/** Scalar multiplication */ 359#define SCALE_SCALAR_4V( DST, S, SRCB ) \ 360do { \ 361 (DST)[0] = S * (SRCB)[0]; \ 362 (DST)[1] = S * (SRCB)[1]; \ 363 (DST)[2] = S * (SRCB)[2]; \ 364 (DST)[3] = S * (SRCB)[3]; \ 365} while (0) 366 367/** In-place scalar multiplication */ 368#define SELF_SCALE_SCALAR_4V( DST, S ) \ 369do { \ 370 (DST)[0] *= S; \ 371 (DST)[1] *= S; \ 372 (DST)[2] *= S; \ 373 (DST)[3] *= S; \ 374} while (0) 375 376/** Assignment */ 377#define ASSIGN_4V( V, V0, V1, V2, V3 ) \ 378do { \ 379 V[0] = V0; \ 380 V[1] = V1; \ 381 V[2] = V2; \ 382 V[3] = V3; \ 383} while(0) 384 385/*@}*/ 386 387 388/**********************************************************************/ 389/** \name 3-element vector operations*/ 390/*@{*/ 391 392/** Zero */ 393#define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0 394 395/** Test for equality */ 396#define TEST_EQ_3V(a,b) \ 397 ((a)[0] == (b)[0] && \ 398 (a)[1] == (b)[1] && \ 399 (a)[2] == (b)[2]) 400 401/** Copy a 3-element vector */ 402#define COPY_3V( DST, SRC ) \ 403do { \ 404 (DST)[0] = (SRC)[0]; \ 405 (DST)[1] = (SRC)[1]; \ 406 (DST)[2] = (SRC)[2]; \ 407} while (0) 408 409/** Copy a 3-element vector with cast */ 410#define COPY_3V_CAST( DST, SRC, CAST ) \ 411do { \ 412 (DST)[0] = (CAST)(SRC)[0]; \ 413 (DST)[1] = (CAST)(SRC)[1]; \ 414 (DST)[2] = (CAST)(SRC)[2]; \ 415} while (0) 416 417/** Copy a 3-element float vector */ 418#define COPY_3FV( DST, SRC ) \ 419do { \ 420 const GLfloat *_tmp = (SRC); \ 421 (DST)[0] = _tmp[0]; \ 422 (DST)[1] = _tmp[1]; \ 423 (DST)[2] = _tmp[2]; \ 424} while (0) 425 426/** Subtraction */ 427#define SUB_3V( DST, SRCA, SRCB ) \ 428do { \ 429 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \ 430 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \ 431 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \ 432} while (0) 433 434/** Addition */ 435#define ADD_3V( DST, SRCA, SRCB ) \ 436do { \ 437 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \ 438 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \ 439 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \ 440} while (0) 441 442/** In-place scalar multiplication */ 443#define SCALE_3V( DST, SRCA, SRCB ) \ 444do { \ 445 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \ 446 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \ 447 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \ 448} while (0) 449 450/** In-place element-wise multiplication */ 451#define SELF_SCALE_3V( DST, SRC ) \ 452do { \ 453 (DST)[0] *= (SRC)[0]; \ 454 (DST)[1] *= (SRC)[1]; \ 455 (DST)[2] *= (SRC)[2]; \ 456} while (0) 457 458/** In-place addition */ 459#define ACC_3V( DST, SRC ) \ 460do { \ 461 (DST)[0] += (SRC)[0]; \ 462 (DST)[1] += (SRC)[1]; \ 463 (DST)[2] += (SRC)[2]; \ 464} while (0) 465 466/** Element-wise multiplication and addition */ 467#define ACC_SCALE_3V( DST, SRCA, SRCB ) \ 468do { \ 469 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \ 470 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \ 471 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \ 472} while (0) 473 474/** Scalar multiplication */ 475#define SCALE_SCALAR_3V( DST, S, SRCB ) \ 476do { \ 477 (DST)[0] = S * (SRCB)[0]; \ 478 (DST)[1] = S * (SRCB)[1]; \ 479 (DST)[2] = S * (SRCB)[2]; \ 480} while (0) 481 482/** In-place scalar multiplication and addition */ 483#define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \ 484do { \ 485 (DST)[0] += S * (SRCB)[0]; \ 486 (DST)[1] += S * (SRCB)[1]; \ 487 (DST)[2] += S * (SRCB)[2]; \ 488} while (0) 489 490/** In-place scalar multiplication */ 491#define SELF_SCALE_SCALAR_3V( DST, S ) \ 492do { \ 493 (DST)[0] *= S; \ 494 (DST)[1] *= S; \ 495 (DST)[2] *= S; \ 496} while (0) 497 498/** In-place scalar addition */ 499#define ACC_SCALAR_3V( DST, S ) \ 500do { \ 501 (DST)[0] += S; \ 502 (DST)[1] += S; \ 503 (DST)[2] += S; \ 504} while (0) 505 506/** Assignment */ 507#define ASSIGN_3V( V, V0, V1, V2 ) \ 508do { \ 509 V[0] = V0; \ 510 V[1] = V1; \ 511 V[2] = V2; \ 512} while(0) 513 514/*@}*/ 515 516 517/**********************************************************************/ 518/** \name 2-element vector operations*/ 519/*@{*/ 520 521/** Zero */ 522#define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0 523 524/** Copy a 2-element vector */ 525#define COPY_2V( DST, SRC ) \ 526do { \ 527 (DST)[0] = (SRC)[0]; \ 528 (DST)[1] = (SRC)[1]; \ 529} while (0) 530 531/** Copy a 2-element vector with cast */ 532#define COPY_2V_CAST( DST, SRC, CAST ) \ 533do { \ 534 (DST)[0] = (CAST)(SRC)[0]; \ 535 (DST)[1] = (CAST)(SRC)[1]; \ 536} while (0) 537 538/** Copy a 2-element float vector */ 539#define COPY_2FV( DST, SRC ) \ 540do { \ 541 const GLfloat *_tmp = (SRC); \ 542 (DST)[0] = _tmp[0]; \ 543 (DST)[1] = _tmp[1]; \ 544} while (0) 545 546/** Subtraction */ 547#define SUB_2V( DST, SRCA, SRCB ) \ 548do { \ 549 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \ 550 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \ 551} while (0) 552 553/** Addition */ 554#define ADD_2V( DST, SRCA, SRCB ) \ 555do { \ 556 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \ 557 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \ 558} while (0) 559 560/** In-place scalar multiplication */ 561#define SCALE_2V( DST, SRCA, SRCB ) \ 562do { \ 563 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \ 564 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \ 565} while (0) 566 567/** In-place addition */ 568#define ACC_2V( DST, SRC ) \ 569do { \ 570 (DST)[0] += (SRC)[0]; \ 571 (DST)[1] += (SRC)[1]; \ 572} while (0) 573 574/** Element-wise multiplication and addition */ 575#define ACC_SCALE_2V( DST, SRCA, SRCB ) \ 576do { \ 577 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \ 578 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \ 579} while (0) 580 581/** Scalar multiplication */ 582#define SCALE_SCALAR_2V( DST, S, SRCB ) \ 583do { \ 584 (DST)[0] = S * (SRCB)[0]; \ 585 (DST)[1] = S * (SRCB)[1]; \ 586} while (0) 587 588/** In-place scalar multiplication and addition */ 589#define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \ 590do { \ 591 (DST)[0] += S * (SRCB)[0]; \ 592 (DST)[1] += S * (SRCB)[1]; \ 593} while (0) 594 595/** In-place scalar multiplication */ 596#define SELF_SCALE_SCALAR_2V( DST, S ) \ 597do { \ 598 (DST)[0] *= S; \ 599 (DST)[1] *= S; \ 600} while (0) 601 602/** In-place scalar addition */ 603#define ACC_SCALAR_2V( DST, S ) \ 604do { \ 605 (DST)[0] += S; \ 606 (DST)[1] += S; \ 607} while (0) 608 609/** Assign scalers to short vectors */ 610#define ASSIGN_2V( V, V0, V1 ) \ 611do { \ 612 V[0] = V0; \ 613 V[1] = V1; \ 614} while(0) 615 616/*@}*/ 617 618/** Copy \p sz elements into a homegeneous (4-element) vector, giving 619 * default values to the remaining components. 620 * The default values are chosen based on \p type. 621 */ 622static inline void 623COPY_CLEAN_4V_TYPE_AS_FLOAT(GLfloat dst[4], int sz, const GLfloat src[4], 624 GLenum type) 625{ 626 switch (type) { 627 case GL_FLOAT: 628 ASSIGN_4V(dst, 0, 0, 0, 1); 629 break; 630 case GL_INT: 631 ASSIGN_4V(dst, INT_AS_FLT(0), INT_AS_FLT(0), 632 INT_AS_FLT(0), INT_AS_FLT(1)); 633 break; 634 case GL_UNSIGNED_INT: 635 ASSIGN_4V(dst, UINT_AS_FLT(0), UINT_AS_FLT(0), 636 UINT_AS_FLT(0), UINT_AS_FLT(1)); 637 break; 638 default: 639 ASSIGN_4V(dst, 0.0f, 0.0f, 0.0f, 1.0f); /* silence warnings */ 640 ASSERT(!"Unexpected type in COPY_CLEAN_4V_TYPE_AS_FLOAT macro"); 641 } 642 COPY_SZ_4V(dst, sz, src); 643} 644 645/** \name Linear interpolation functions */ 646/*@{*/ 647 648static inline GLfloat 649LINTERP(GLfloat t, GLfloat out, GLfloat in) 650{ 651 return out + t * (in - out); 652} 653 654static inline void 655INTERP_3F(GLfloat t, GLfloat dst[3], const GLfloat out[3], const GLfloat in[3]) 656{ 657 dst[0] = LINTERP( t, out[0], in[0] ); 658 dst[1] = LINTERP( t, out[1], in[1] ); 659 dst[2] = LINTERP( t, out[2], in[2] ); 660} 661 662static inline void 663INTERP_4F(GLfloat t, GLfloat dst[4], const GLfloat out[4], const GLfloat in[4]) 664{ 665 dst[0] = LINTERP( t, out[0], in[0] ); 666 dst[1] = LINTERP( t, out[1], in[1] ); 667 dst[2] = LINTERP( t, out[2], in[2] ); 668 dst[3] = LINTERP( t, out[3], in[3] ); 669} 670 671/*@}*/ 672 673 674 675/** Clamp X to [MIN,MAX] */ 676#define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) ) 677 678/** Minimum of two values: */ 679#define MIN2( A, B ) ( (A)<(B) ? (A) : (B) ) 680 681/** Maximum of two values: */ 682#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) ) 683 684/** Minimum and maximum of three values: */ 685#define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C)) 686#define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C)) 687 688static inline unsigned 689minify(unsigned value, unsigned levels) 690{ 691 return MAX2(1, value >> levels); 692} 693 694/** 695 * Return true if the given value is a power of two. 696 * 697 * Note that this considers 0 a power of two. 698 */ 699static inline bool 700is_power_of_two(unsigned value) 701{ 702 return (value & (value - 1)) == 0; 703} 704 705/** 706 * Align a value up to an alignment value 707 * 708 * If \c value is not already aligned to the requested alignment value, it 709 * will be rounded up. 710 * 711 * \param value Value to be rounded 712 * \param alignment Alignment value to be used. This must be a power of two. 713 * 714 * \sa ROUND_DOWN_TO() 715 */ 716#define ALIGN(value, alignment) (((value) + (alignment) - 1) & ~((alignment) - 1)) 717 718/** 719 * Align a value down to an alignment value 720 * 721 * If \c value is not already aligned to the requested alignment value, it 722 * will be rounded down. 723 * 724 * \param value Value to be rounded 725 * \param alignment Alignment value to be used. This must be a power of two. 726 * 727 * \sa ALIGN() 728 */ 729#define ROUND_DOWN_TO(value, alignment) ((value) & ~(alignment - 1)) 730 731 732/** Cross product of two 3-element vectors */ 733static inline void 734CROSS3(GLfloat n[3], const GLfloat u[3], const GLfloat v[3]) 735{ 736 n[0] = u[1] * v[2] - u[2] * v[1]; 737 n[1] = u[2] * v[0] - u[0] * v[2]; 738 n[2] = u[0] * v[1] - u[1] * v[0]; 739} 740 741 742/** Dot product of two 2-element vectors */ 743static inline GLfloat 744DOT2(const GLfloat a[2], const GLfloat b[2]) 745{ 746 return a[0] * b[0] + a[1] * b[1]; 747} 748 749static inline GLfloat 750DOT3(const GLfloat a[3], const GLfloat b[3]) 751{ 752 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; 753} 754 755static inline GLfloat 756DOT4(const GLfloat a[4], const GLfloat b[4]) 757{ 758 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; 759} 760 761 762static inline GLfloat 763LEN_SQUARED_3FV(const GLfloat v[3]) 764{ 765 return DOT3(v, v); 766} 767 768static inline GLfloat 769LEN_SQUARED_2FV(const GLfloat v[2]) 770{ 771 return DOT2(v, v); 772} 773 774 775static inline GLfloat 776LEN_3FV(const GLfloat v[3]) 777{ 778 return sqrtf(LEN_SQUARED_3FV(v)); 779} 780 781static inline GLfloat 782LEN_2FV(const GLfloat v[2]) 783{ 784 return sqrtf(LEN_SQUARED_2FV(v)); 785} 786 787 788/* Normalize a 3-element vector to unit length. */ 789static inline void 790NORMALIZE_3FV(GLfloat v[3]) 791{ 792 GLfloat len = (GLfloat) LEN_SQUARED_3FV(v); 793 if (len) { 794 len = INV_SQRTF(len); 795 v[0] *= len; 796 v[1] *= len; 797 v[2] *= len; 798 } 799} 800 801 802/** Is float value negative? */ 803static inline GLboolean 804IS_NEGATIVE(float x) 805{ 806 return signbit(x) != 0; 807} 808 809/** Test two floats have opposite signs */ 810static inline GLboolean 811DIFFERENT_SIGNS(GLfloat x, GLfloat y) 812{ 813 return signbit(x) != signbit(y); 814} 815 816 817/** Compute ceiling of integer quotient of A divided by B. */ 818#define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 ) 819 820 821/** casts to silence warnings with some compilers */ 822#define ENUM_TO_INT(E) ((GLint)(E)) 823#define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E)) 824#define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E)) 825#define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE) 826 827/* Compute the size of an array */ 828#ifndef ARRAY_SIZE 829# define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) 830#endif 831 832/* Stringify */ 833#define STRINGIFY(x) #x 834 835#endif 836