macros.h revision cf41d7c63aab2289d739c0f9df116caf2cc410e3
1/** 2 * \file macros.h 3 * A collection of useful macros. 4 */ 5 6/* 7 * Mesa 3-D graphics library 8 * Version: 6.5.2 9 * 10 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved. 11 * 12 * Permission is hereby granted, free of charge, to any person obtaining a 13 * copy of this software and associated documentation files (the "Software"), 14 * to deal in the Software without restriction, including without limitation 15 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 16 * and/or sell copies of the Software, and to permit persons to whom the 17 * Software is furnished to do so, subject to the following conditions: 18 * 19 * The above copyright notice and this permission notice shall be included 20 * in all copies or substantial portions of the Software. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 27 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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#if defined(USE_IEEE) && !defined(DEBUG) 144#define IEEE_0996 0x3f7f0000 /* 0.996 or so */ 145/* This function/macro is sensitive to precision. Test very carefully 146 * if you change it! 147 */ 148#define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \ 149 do { \ 150 fi_type __tmp; \ 151 __tmp.f = (F); \ 152 if (__tmp.i < 0) \ 153 UB = (GLubyte) 0; \ 154 else if (__tmp.i >= IEEE_0996) \ 155 UB = (GLubyte) 255; \ 156 else { \ 157 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \ 158 UB = (GLubyte) __tmp.i; \ 159 } \ 160 } while (0) 161#define CLAMPED_FLOAT_TO_UBYTE(UB, F) \ 162 do { \ 163 fi_type __tmp; \ 164 __tmp.f = (F) * (255.0F/256.0F) + 32768.0F; \ 165 UB = (GLubyte) __tmp.i; \ 166 } while (0) 167#else 168#define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \ 169 ub = ((GLubyte) F_TO_I(CLAMP((f), 0.0F, 1.0F) * 255.0F)) 170#define CLAMPED_FLOAT_TO_UBYTE(ub, f) \ 171 ub = ((GLubyte) F_TO_I((f) * 255.0F)) 172#endif 173 174/*@}*/ 175 176 177/** Stepping a GLfloat pointer by a byte stride */ 178#define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i)) 179/** Stepping a GLuint pointer by a byte stride */ 180#define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i)) 181/** Stepping a GLubyte[4] pointer by a byte stride */ 182#define STRIDE_4UB(p, i) (p = (GLubyte (*)[4])((GLubyte *)p + i)) 183/** Stepping a GLfloat[4] pointer by a byte stride */ 184#define STRIDE_4F(p, i) (p = (GLfloat (*)[4])((GLubyte *)p + i)) 185/** Stepping a \p t pointer by a byte stride */ 186#define STRIDE_T(p, t, i) (p = (t)((GLubyte *)p + i)) 187 188 189/**********************************************************************/ 190/** \name 4-element vector operations */ 191/*@{*/ 192 193/** Zero */ 194#define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0 195 196/** Test for equality */ 197#define TEST_EQ_4V(a,b) ((a)[0] == (b)[0] && \ 198 (a)[1] == (b)[1] && \ 199 (a)[2] == (b)[2] && \ 200 (a)[3] == (b)[3]) 201 202/** Test for equality (unsigned bytes) */ 203#if defined(__i386__) 204#define TEST_EQ_4UBV(DST, SRC) *((GLuint*)(DST)) == *((GLuint*)(SRC)) 205#else 206#define TEST_EQ_4UBV(DST, SRC) TEST_EQ_4V(DST, SRC) 207#endif 208 209/** Copy a 4-element vector */ 210#define COPY_4V( DST, SRC ) \ 211do { \ 212 (DST)[0] = (SRC)[0]; \ 213 (DST)[1] = (SRC)[1]; \ 214 (DST)[2] = (SRC)[2]; \ 215 (DST)[3] = (SRC)[3]; \ 216} while (0) 217 218/** Copy a 4-element unsigned byte vector */ 219#if defined(__i386__) 220#define COPY_4UBV(DST, SRC) \ 221do { \ 222 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \ 223} while (0) 224#else 225/* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */ 226#define COPY_4UBV(DST, SRC) \ 227do { \ 228 (DST)[0] = (SRC)[0]; \ 229 (DST)[1] = (SRC)[1]; \ 230 (DST)[2] = (SRC)[2]; \ 231 (DST)[3] = (SRC)[3]; \ 232} while (0) 233#endif 234 235/** 236 * Copy a 4-element float vector 237 * memcpy seems to be most efficient 238 */ 239#define COPY_4FV( DST, SRC ) \ 240do { \ 241 memcpy(DST, SRC, sizeof(GLfloat) * 4); \ 242} while (0) 243 244/** Copy \p SZ elements into a 4-element vector */ 245#define COPY_SZ_4V(DST, SZ, SRC) \ 246do { \ 247 switch (SZ) { \ 248 case 4: (DST)[3] = (SRC)[3]; \ 249 case 3: (DST)[2] = (SRC)[2]; \ 250 case 2: (DST)[1] = (SRC)[1]; \ 251 case 1: (DST)[0] = (SRC)[0]; \ 252 } \ 253} while(0) 254 255/** Copy \p SZ elements into a homegeneous (4-element) vector, giving 256 * default values to the remaining */ 257#define COPY_CLEAN_4V(DST, SZ, SRC) \ 258do { \ 259 ASSIGN_4V( DST, 0, 0, 0, 1 ); \ 260 COPY_SZ_4V( DST, SZ, SRC ); \ 261} while (0) 262 263/** Subtraction */ 264#define SUB_4V( DST, SRCA, SRCB ) \ 265do { \ 266 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \ 267 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \ 268 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \ 269 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \ 270} while (0) 271 272/** Addition */ 273#define ADD_4V( DST, SRCA, SRCB ) \ 274do { \ 275 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \ 276 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \ 277 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \ 278 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \ 279} while (0) 280 281/** Element-wise multiplication */ 282#define SCALE_4V( DST, SRCA, SRCB ) \ 283do { \ 284 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \ 285 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \ 286 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \ 287 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \ 288} while (0) 289 290/** In-place addition */ 291#define ACC_4V( DST, SRC ) \ 292do { \ 293 (DST)[0] += (SRC)[0]; \ 294 (DST)[1] += (SRC)[1]; \ 295 (DST)[2] += (SRC)[2]; \ 296 (DST)[3] += (SRC)[3]; \ 297} while (0) 298 299/** Element-wise multiplication and addition */ 300#define ACC_SCALE_4V( DST, SRCA, SRCB ) \ 301do { \ 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]; \ 306} while (0) 307 308/** In-place scalar multiplication and addition */ 309#define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \ 310do { \ 311 (DST)[0] += S * (SRCB)[0]; \ 312 (DST)[1] += S * (SRCB)[1]; \ 313 (DST)[2] += S * (SRCB)[2]; \ 314 (DST)[3] += S * (SRCB)[3]; \ 315} while (0) 316 317/** Scalar multiplication */ 318#define SCALE_SCALAR_4V( DST, S, SRCB ) \ 319do { \ 320 (DST)[0] = S * (SRCB)[0]; \ 321 (DST)[1] = S * (SRCB)[1]; \ 322 (DST)[2] = S * (SRCB)[2]; \ 323 (DST)[3] = S * (SRCB)[3]; \ 324} while (0) 325 326/** In-place scalar multiplication */ 327#define SELF_SCALE_SCALAR_4V( DST, S ) \ 328do { \ 329 (DST)[0] *= S; \ 330 (DST)[1] *= S; \ 331 (DST)[2] *= S; \ 332 (DST)[3] *= S; \ 333} while (0) 334 335/** Assignment */ 336#define ASSIGN_4V( V, V0, V1, V2, V3 ) \ 337do { \ 338 V[0] = V0; \ 339 V[1] = V1; \ 340 V[2] = V2; \ 341 V[3] = V3; \ 342} while(0) 343 344/*@}*/ 345 346 347/**********************************************************************/ 348/** \name 3-element vector operations*/ 349/*@{*/ 350 351/** Zero */ 352#define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0 353 354/** Test for equality */ 355#define TEST_EQ_3V(a,b) \ 356 ((a)[0] == (b)[0] && \ 357 (a)[1] == (b)[1] && \ 358 (a)[2] == (b)[2]) 359 360/** Copy a 3-element vector */ 361#define COPY_3V( DST, SRC ) \ 362do { \ 363 (DST)[0] = (SRC)[0]; \ 364 (DST)[1] = (SRC)[1]; \ 365 (DST)[2] = (SRC)[2]; \ 366} while (0) 367 368/** Copy a 3-element vector with cast */ 369#define COPY_3V_CAST( DST, SRC, CAST ) \ 370do { \ 371 (DST)[0] = (CAST)(SRC)[0]; \ 372 (DST)[1] = (CAST)(SRC)[1]; \ 373 (DST)[2] = (CAST)(SRC)[2]; \ 374} while (0) 375 376/** Copy a 3-element float vector */ 377#define COPY_3FV( DST, SRC ) \ 378do { \ 379 const GLfloat *_tmp = (SRC); \ 380 (DST)[0] = _tmp[0]; \ 381 (DST)[1] = _tmp[1]; \ 382 (DST)[2] = _tmp[2]; \ 383} while (0) 384 385/** Subtraction */ 386#define SUB_3V( DST, SRCA, SRCB ) \ 387do { \ 388 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \ 389 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \ 390 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \ 391} while (0) 392 393/** Addition */ 394#define ADD_3V( DST, SRCA, SRCB ) \ 395do { \ 396 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \ 397 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \ 398 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \ 399} while (0) 400 401/** In-place scalar multiplication */ 402#define SCALE_3V( DST, SRCA, SRCB ) \ 403do { \ 404 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \ 405 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \ 406 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \ 407} while (0) 408 409/** In-place element-wise multiplication */ 410#define SELF_SCALE_3V( DST, SRC ) \ 411do { \ 412 (DST)[0] *= (SRC)[0]; \ 413 (DST)[1] *= (SRC)[1]; \ 414 (DST)[2] *= (SRC)[2]; \ 415} while (0) 416 417/** In-place addition */ 418#define ACC_3V( DST, SRC ) \ 419do { \ 420 (DST)[0] += (SRC)[0]; \ 421 (DST)[1] += (SRC)[1]; \ 422 (DST)[2] += (SRC)[2]; \ 423} while (0) 424 425/** Element-wise multiplication and addition */ 426#define ACC_SCALE_3V( DST, SRCA, SRCB ) \ 427do { \ 428 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \ 429 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \ 430 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \ 431} while (0) 432 433/** Scalar multiplication */ 434#define SCALE_SCALAR_3V( DST, S, SRCB ) \ 435do { \ 436 (DST)[0] = S * (SRCB)[0]; \ 437 (DST)[1] = S * (SRCB)[1]; \ 438 (DST)[2] = S * (SRCB)[2]; \ 439} while (0) 440 441/** In-place scalar multiplication and addition */ 442#define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \ 443do { \ 444 (DST)[0] += S * (SRCB)[0]; \ 445 (DST)[1] += S * (SRCB)[1]; \ 446 (DST)[2] += S * (SRCB)[2]; \ 447} while (0) 448 449/** In-place scalar multiplication */ 450#define SELF_SCALE_SCALAR_3V( DST, S ) \ 451do { \ 452 (DST)[0] *= S; \ 453 (DST)[1] *= S; \ 454 (DST)[2] *= S; \ 455} while (0) 456 457/** In-place scalar addition */ 458#define ACC_SCALAR_3V( DST, S ) \ 459do { \ 460 (DST)[0] += S; \ 461 (DST)[1] += S; \ 462 (DST)[2] += S; \ 463} while (0) 464 465/** Assignment */ 466#define ASSIGN_3V( V, V0, V1, V2 ) \ 467do { \ 468 V[0] = V0; \ 469 V[1] = V1; \ 470 V[2] = V2; \ 471} while(0) 472 473/*@}*/ 474 475 476/**********************************************************************/ 477/** \name 2-element vector operations*/ 478/*@{*/ 479 480/** Zero */ 481#define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0 482 483/** Copy a 2-element vector */ 484#define COPY_2V( DST, SRC ) \ 485do { \ 486 (DST)[0] = (SRC)[0]; \ 487 (DST)[1] = (SRC)[1]; \ 488} while (0) 489 490/** Copy a 2-element vector with cast */ 491#define COPY_2V_CAST( DST, SRC, CAST ) \ 492do { \ 493 (DST)[0] = (CAST)(SRC)[0]; \ 494 (DST)[1] = (CAST)(SRC)[1]; \ 495} while (0) 496 497/** Copy a 2-element float vector */ 498#define COPY_2FV( DST, SRC ) \ 499do { \ 500 const GLfloat *_tmp = (SRC); \ 501 (DST)[0] = _tmp[0]; \ 502 (DST)[1] = _tmp[1]; \ 503} while (0) 504 505/** Subtraction */ 506#define SUB_2V( DST, SRCA, SRCB ) \ 507do { \ 508 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \ 509 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \ 510} while (0) 511 512/** Addition */ 513#define ADD_2V( DST, SRCA, SRCB ) \ 514do { \ 515 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \ 516 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \ 517} while (0) 518 519/** In-place scalar multiplication */ 520#define SCALE_2V( DST, SRCA, SRCB ) \ 521do { \ 522 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \ 523 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \ 524} while (0) 525 526/** In-place addition */ 527#define ACC_2V( DST, SRC ) \ 528do { \ 529 (DST)[0] += (SRC)[0]; \ 530 (DST)[1] += (SRC)[1]; \ 531} while (0) 532 533/** Element-wise multiplication and addition */ 534#define ACC_SCALE_2V( DST, SRCA, SRCB ) \ 535do { \ 536 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \ 537 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \ 538} while (0) 539 540/** Scalar multiplication */ 541#define SCALE_SCALAR_2V( DST, S, SRCB ) \ 542do { \ 543 (DST)[0] = S * (SRCB)[0]; \ 544 (DST)[1] = S * (SRCB)[1]; \ 545} while (0) 546 547/** In-place scalar multiplication and addition */ 548#define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \ 549do { \ 550 (DST)[0] += S * (SRCB)[0]; \ 551 (DST)[1] += S * (SRCB)[1]; \ 552} while (0) 553 554/** In-place scalar multiplication */ 555#define SELF_SCALE_SCALAR_2V( DST, S ) \ 556do { \ 557 (DST)[0] *= S; \ 558 (DST)[1] *= S; \ 559} while (0) 560 561/** In-place scalar addition */ 562#define ACC_SCALAR_2V( DST, S ) \ 563do { \ 564 (DST)[0] += S; \ 565 (DST)[1] += S; \ 566} while (0) 567 568/** Assign scalers to short vectors */ 569#define ASSIGN_2V( V, V0, V1 ) \ 570do { \ 571 V[0] = V0; \ 572 V[1] = V1; \ 573} while(0) 574 575/*@}*/ 576 577 578/** \name Linear interpolation functions */ 579/*@{*/ 580 581static inline GLfloat 582LINTERP(GLfloat t, GLfloat out, GLfloat in) 583{ 584 return out + t * (in - out); 585} 586 587static inline void 588INTERP_3F(GLfloat t, GLfloat dst[3], const GLfloat out[3], const GLfloat in[3]) 589{ 590 dst[0] = LINTERP( t, out[0], in[0] ); 591 dst[1] = LINTERP( t, out[1], in[1] ); 592 dst[2] = LINTERP( t, out[2], in[2] ); 593} 594 595static inline void 596INTERP_4F(GLfloat t, GLfloat dst[4], const GLfloat out[4], const GLfloat in[4]) 597{ 598 dst[0] = LINTERP( t, out[0], in[0] ); 599 dst[1] = LINTERP( t, out[1], in[1] ); 600 dst[2] = LINTERP( t, out[2], in[2] ); 601 dst[3] = LINTERP( t, out[3], in[3] ); 602} 603 604/*@}*/ 605 606 607 608/** Clamp X to [MIN,MAX] */ 609#define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) ) 610 611/** Minimum of two values: */ 612#define MIN2( A, B ) ( (A)<(B) ? (A) : (B) ) 613 614/** Maximum of two values: */ 615#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) ) 616 617/** Minimum and maximum of three values: */ 618#define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C)) 619#define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C)) 620 621 622 623/** Cross product of two 3-element vectors */ 624static inline void 625CROSS3(GLfloat n[3], const GLfloat u[3], const GLfloat v[3]) 626{ 627 n[0] = u[1] * v[2] - u[2] * v[1]; 628 n[1] = u[2] * v[0] - u[0] * v[2]; 629 n[2] = u[0] * v[1] - u[1] * v[0]; 630} 631 632 633/** Dot product of two 2-element vectors */ 634static inline GLfloat 635DOT2(const GLfloat a[2], const GLfloat b[2]) 636{ 637 return a[0] * b[0] + a[1] * b[1]; 638} 639 640static inline GLfloat 641DOT3(const GLfloat a[3], const GLfloat b[3]) 642{ 643 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; 644} 645 646static inline GLfloat 647DOT4(const GLfloat a[4], const GLfloat b[4]) 648{ 649 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; 650} 651 652 653static inline GLfloat 654LEN_SQUARED_3FV(const GLfloat v[3]) 655{ 656 return DOT3(v, v); 657} 658 659static inline GLfloat 660LEN_SQUARED_2FV(const GLfloat v[2]) 661{ 662 return DOT2(v, v); 663} 664 665 666static inline GLfloat 667LEN_3FV(const GLfloat v[3]) 668{ 669 return SQRTF(LEN_SQUARED_3FV(v)); 670} 671 672static inline GLfloat 673LEN_2FV(const GLfloat v[2]) 674{ 675 return SQRTF(LEN_SQUARED_2FV(v)); 676} 677 678 679/* Normalize a 3-element vector to unit length. */ 680static inline void 681NORMALIZE_3FV(GLfloat v[3]) 682{ 683 GLfloat len = (GLfloat) LEN_SQUARED_3FV(v); 684 if (len) { 685 len = INV_SQRTF(len); 686 v[0] *= len; 687 v[1] *= len; 688 v[2] *= len; 689 } 690} 691 692 693/** Compute ceiling of integer quotient of A divided by B. */ 694#define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 ) 695 696 697/** casts to silence warnings with some compilers */ 698#define ENUM_TO_INT(E) ((GLint)(E)) 699#define ENUM_TO_FLOAT(E) ((GLfloat)(GLint)(E)) 700#define ENUM_TO_DOUBLE(E) ((GLdouble)(GLint)(E)) 701#define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE) 702 703 704#endif 705