Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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] */
  43. extern 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. /* This function/macro is sensitive to precision.  Test very carefully
  145.  * if you change it!
  146.  */
  147. #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F)                                 \
  148.         do {                                                            \
  149.            fi_type __tmp;                                               \
  150.            __tmp.f = (F);                                               \
  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, F)                                   \
  161.         do {                                                            \
  162.            fi_type __tmp;                                               \
  163.            __tmp.f = (F) * (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.  
  173. static inline GLfloat INT_AS_FLT(GLint i)
  174. {
  175.    fi_type tmp;
  176.    tmp.i = i;
  177.    return tmp.f;
  178. }
  179.  
  180. static inline GLfloat UINT_AS_FLT(GLuint u)
  181. {
  182.    fi_type tmp;
  183.    tmp.u = u;
  184.    return tmp.f;
  185. }
  186.  
  187. /*@}*/
  188.  
  189.  
  190. /** Stepping a GLfloat pointer by a byte stride */
  191. #define STRIDE_F(p, i)  (p = (GLfloat *)((GLubyte *)p + i))
  192. /** Stepping a GLuint pointer by a byte stride */
  193. #define STRIDE_UI(p, i)  (p = (GLuint *)((GLubyte *)p + i))
  194. /** Stepping a GLubyte[4] pointer by a byte stride */
  195. #define STRIDE_4UB(p, i)  (p = (GLubyte (*)[4])((GLubyte *)p + i))
  196. /** Stepping a GLfloat[4] pointer by a byte stride */
  197. #define STRIDE_4F(p, i)  (p = (GLfloat (*)[4])((GLubyte *)p + i))
  198. /** Stepping a \p t pointer by a byte stride */
  199. #define STRIDE_T(p, t, i)  (p = (t)((GLubyte *)p + i))
  200.  
  201.  
  202. /**********************************************************************/
  203. /** \name 4-element vector operations */
  204. /*@{*/
  205.  
  206. /** Zero */
  207. #define ZERO_4V( DST )  (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
  208.  
  209. /** Test for equality */
  210. #define TEST_EQ_4V(a,b)  ((a)[0] == (b)[0] &&   \
  211.               (a)[1] == (b)[1] &&   \
  212.               (a)[2] == (b)[2] &&   \
  213.               (a)[3] == (b)[3])
  214.  
  215. /** Test for equality (unsigned bytes) */
  216. static inline GLboolean
  217. TEST_EQ_4UBV(const GLubyte a[4], const GLubyte b[4])
  218. {
  219. #if defined(__i386__)
  220.    return *((const GLuint *) a) == *((const GLuint *) b);
  221. #else
  222.    return TEST_EQ_4V(a, b);
  223. #endif
  224. }
  225.  
  226.  
  227. /** Copy a 4-element vector */
  228. #define COPY_4V( DST, SRC )         \
  229. do {                                \
  230.    (DST)[0] = (SRC)[0];             \
  231.    (DST)[1] = (SRC)[1];             \
  232.    (DST)[2] = (SRC)[2];             \
  233.    (DST)[3] = (SRC)[3];             \
  234. } while (0)
  235.  
  236. /** Copy a 4-element unsigned byte vector */
  237. static inline void
  238. COPY_4UBV(GLubyte dst[4], const GLubyte src[4])
  239. {
  240. #if defined(__i386__)
  241.    *((GLuint *) dst) = *((GLuint *) src);
  242. #else
  243.    /* The GLuint cast might fail if DST or SRC are not dword-aligned (RISC) */
  244.    COPY_4V(dst, src);
  245. #endif
  246. }
  247.  
  248. /** Copy a 4-element float vector */
  249. static inline void
  250. COPY_4FV(GLfloat dst[4], const GLfloat src[4])
  251. {
  252.    /* memcpy seems to be most efficient */
  253.    memcpy(dst, src, sizeof(GLfloat) * 4);
  254. }
  255.  
  256. /** Copy \p SZ elements into a 4-element vector */
  257. #define COPY_SZ_4V(DST, SZ, SRC)  \
  258. do {                              \
  259.    switch (SZ) {                  \
  260.    case 4: (DST)[3] = (SRC)[3];   \
  261.    case 3: (DST)[2] = (SRC)[2];   \
  262.    case 2: (DST)[1] = (SRC)[1];   \
  263.    case 1: (DST)[0] = (SRC)[0];   \
  264.    }                              \
  265. } while(0)
  266.  
  267. /** Copy \p SZ elements into a homegeneous (4-element) vector, giving
  268.  * default values to the remaining */
  269. #define COPY_CLEAN_4V(DST, SZ, SRC)  \
  270. do {                                 \
  271.       ASSIGN_4V( DST, 0, 0, 0, 1 );  \
  272.       COPY_SZ_4V( DST, SZ, SRC );    \
  273. } while (0)
  274.  
  275. /** Subtraction */
  276. #define SUB_4V( DST, SRCA, SRCB )           \
  277. do {                                        \
  278.       (DST)[0] = (SRCA)[0] - (SRCB)[0];     \
  279.       (DST)[1] = (SRCA)[1] - (SRCB)[1];     \
  280.       (DST)[2] = (SRCA)[2] - (SRCB)[2];     \
  281.       (DST)[3] = (SRCA)[3] - (SRCB)[3];     \
  282. } while (0)
  283.  
  284. /** Addition */
  285. #define ADD_4V( DST, SRCA, SRCB )           \
  286. do {                                        \
  287.       (DST)[0] = (SRCA)[0] + (SRCB)[0];     \
  288.       (DST)[1] = (SRCA)[1] + (SRCB)[1];     \
  289.       (DST)[2] = (SRCA)[2] + (SRCB)[2];     \
  290.       (DST)[3] = (SRCA)[3] + (SRCB)[3];     \
  291. } while (0)
  292.  
  293. /** Element-wise multiplication */
  294. #define SCALE_4V( DST, SRCA, SRCB )         \
  295. do {                                        \
  296.       (DST)[0] = (SRCA)[0] * (SRCB)[0];     \
  297.       (DST)[1] = (SRCA)[1] * (SRCB)[1];     \
  298.       (DST)[2] = (SRCA)[2] * (SRCB)[2];     \
  299.       (DST)[3] = (SRCA)[3] * (SRCB)[3];     \
  300. } while (0)
  301.  
  302. /** In-place addition */
  303. #define ACC_4V( DST, SRC )          \
  304. do {                                \
  305.       (DST)[0] += (SRC)[0];         \
  306.       (DST)[1] += (SRC)[1];         \
  307.       (DST)[2] += (SRC)[2];         \
  308.       (DST)[3] += (SRC)[3];         \
  309. } while (0)
  310.  
  311. /** Element-wise multiplication and addition */
  312. #define ACC_SCALE_4V( DST, SRCA, SRCB )     \
  313. do {                                        \
  314.       (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
  315.       (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
  316.       (DST)[2] += (SRCA)[2] * (SRCB)[2];    \
  317.       (DST)[3] += (SRCA)[3] * (SRCB)[3];    \
  318. } while (0)
  319.  
  320. /** In-place scalar multiplication and addition */
  321. #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
  322. do {                                        \
  323.       (DST)[0] += S * (SRCB)[0];            \
  324.       (DST)[1] += S * (SRCB)[1];            \
  325.       (DST)[2] += S * (SRCB)[2];            \
  326.       (DST)[3] += S * (SRCB)[3];            \
  327. } while (0)
  328.  
  329. /** Scalar multiplication */
  330. #define SCALE_SCALAR_4V( DST, S, SRCB ) \
  331. do {                                    \
  332.       (DST)[0] = S * (SRCB)[0];         \
  333.       (DST)[1] = S * (SRCB)[1];         \
  334.       (DST)[2] = S * (SRCB)[2];         \
  335.       (DST)[3] = S * (SRCB)[3];         \
  336. } while (0)
  337.  
  338. /** In-place scalar multiplication */
  339. #define SELF_SCALE_SCALAR_4V( DST, S ) \
  340. do {                                   \
  341.       (DST)[0] *= S;                   \
  342.       (DST)[1] *= S;                   \
  343.       (DST)[2] *= S;                   \
  344.       (DST)[3] *= S;                   \
  345. } while (0)
  346.  
  347. /** Assignment */
  348. #define ASSIGN_4V( V, V0, V1, V2, V3 )  \
  349. do {                                    \
  350.     V[0] = V0;                          \
  351.     V[1] = V1;                          \
  352.     V[2] = V2;                          \
  353.     V[3] = V3;                          \
  354. } while(0)
  355.  
  356. /*@}*/
  357.  
  358.  
  359. /**********************************************************************/
  360. /** \name 3-element vector operations*/
  361. /*@{*/
  362.  
  363. /** Zero */
  364. #define ZERO_3V( DST )  (DST)[0] = (DST)[1] = (DST)[2] = 0
  365.  
  366. /** Test for equality */
  367. #define TEST_EQ_3V(a,b)  \
  368.    ((a)[0] == (b)[0] &&  \
  369.     (a)[1] == (b)[1] &&  \
  370.     (a)[2] == (b)[2])
  371.  
  372. /** Copy a 3-element vector */
  373. #define COPY_3V( DST, SRC )         \
  374. do {                                \
  375.    (DST)[0] = (SRC)[0];             \
  376.    (DST)[1] = (SRC)[1];             \
  377.    (DST)[2] = (SRC)[2];             \
  378. } while (0)
  379.  
  380. /** Copy a 3-element vector with cast */
  381. #define COPY_3V_CAST( DST, SRC, CAST )  \
  382. do {                                    \
  383.    (DST)[0] = (CAST)(SRC)[0];           \
  384.    (DST)[1] = (CAST)(SRC)[1];           \
  385.    (DST)[2] = (CAST)(SRC)[2];           \
  386. } while (0)
  387.  
  388. /** Copy a 3-element float vector */
  389. #define COPY_3FV( DST, SRC )        \
  390. do {                                \
  391.    const GLfloat *_tmp = (SRC);     \
  392.    (DST)[0] = _tmp[0];              \
  393.    (DST)[1] = _tmp[1];              \
  394.    (DST)[2] = _tmp[2];              \
  395. } while (0)
  396.  
  397. /** Subtraction */
  398. #define SUB_3V( DST, SRCA, SRCB )        \
  399. do {                                     \
  400.       (DST)[0] = (SRCA)[0] - (SRCB)[0];  \
  401.       (DST)[1] = (SRCA)[1] - (SRCB)[1];  \
  402.       (DST)[2] = (SRCA)[2] - (SRCB)[2];  \
  403. } while (0)
  404.  
  405. /** Addition */
  406. #define ADD_3V( DST, SRCA, SRCB )       \
  407. do {                                    \
  408.       (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
  409.       (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
  410.       (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
  411. } while (0)
  412.  
  413. /** In-place scalar multiplication */
  414. #define SCALE_3V( DST, SRCA, SRCB )     \
  415. do {                                    \
  416.       (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
  417.       (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
  418.       (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
  419. } while (0)
  420.  
  421. /** In-place element-wise multiplication */
  422. #define SELF_SCALE_3V( DST, SRC )   \
  423. do {                                \
  424.       (DST)[0] *= (SRC)[0];         \
  425.       (DST)[1] *= (SRC)[1];         \
  426.       (DST)[2] *= (SRC)[2];         \
  427. } while (0)
  428.  
  429. /** In-place addition */
  430. #define ACC_3V( DST, SRC )          \
  431. do {                                \
  432.       (DST)[0] += (SRC)[0];         \
  433.       (DST)[1] += (SRC)[1];         \
  434.       (DST)[2] += (SRC)[2];         \
  435. } while (0)
  436.  
  437. /** Element-wise multiplication and addition */
  438. #define ACC_SCALE_3V( DST, SRCA, SRCB )     \
  439. do {                                        \
  440.       (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
  441.       (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
  442.       (DST)[2] += (SRCA)[2] * (SRCB)[2];    \
  443. } while (0)
  444.  
  445. /** Scalar multiplication */
  446. #define SCALE_SCALAR_3V( DST, S, SRCB ) \
  447. do {                                    \
  448.       (DST)[0] = S * (SRCB)[0];         \
  449.       (DST)[1] = S * (SRCB)[1];         \
  450.       (DST)[2] = S * (SRCB)[2];         \
  451. } while (0)
  452.  
  453. /** In-place scalar multiplication and addition */
  454. #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
  455. do {                                        \
  456.       (DST)[0] += S * (SRCB)[0];            \
  457.       (DST)[1] += S * (SRCB)[1];            \
  458.       (DST)[2] += S * (SRCB)[2];            \
  459. } while (0)
  460.  
  461. /** In-place scalar multiplication */
  462. #define SELF_SCALE_SCALAR_3V( DST, S ) \
  463. do {                                   \
  464.       (DST)[0] *= S;                   \
  465.       (DST)[1] *= S;                   \
  466.       (DST)[2] *= S;                   \
  467. } while (0)
  468.  
  469. /** In-place scalar addition */
  470. #define ACC_SCALAR_3V( DST, S )     \
  471. do {                                \
  472.       (DST)[0] += S;                \
  473.       (DST)[1] += S;                \
  474.       (DST)[2] += S;                \
  475. } while (0)
  476.  
  477. /** Assignment */
  478. #define ASSIGN_3V( V, V0, V1, V2 )  \
  479. do {                                \
  480.     V[0] = V0;                      \
  481.     V[1] = V1;                      \
  482.     V[2] = V2;                      \
  483. } while(0)
  484.  
  485. /*@}*/
  486.  
  487.  
  488. /**********************************************************************/
  489. /** \name 2-element vector operations*/
  490. /*@{*/
  491.  
  492. /** Zero */
  493. #define ZERO_2V( DST )  (DST)[0] = (DST)[1] = 0
  494.  
  495. /** Copy a 2-element vector */
  496. #define COPY_2V( DST, SRC )         \
  497. do {                        \
  498.    (DST)[0] = (SRC)[0];             \
  499.    (DST)[1] = (SRC)[1];             \
  500. } while (0)
  501.  
  502. /** Copy a 2-element vector with cast */
  503. #define COPY_2V_CAST( DST, SRC, CAST )      \
  504. do {                        \
  505.    (DST)[0] = (CAST)(SRC)[0];           \
  506.    (DST)[1] = (CAST)(SRC)[1];           \
  507. } while (0)
  508.  
  509. /** Copy a 2-element float vector */
  510. #define COPY_2FV( DST, SRC )            \
  511. do {                        \
  512.    const GLfloat *_tmp = (SRC);         \
  513.    (DST)[0] = _tmp[0];              \
  514.    (DST)[1] = _tmp[1];              \
  515. } while (0)
  516.  
  517. /** Subtraction */
  518. #define SUB_2V( DST, SRCA, SRCB )       \
  519. do {                        \
  520.       (DST)[0] = (SRCA)[0] - (SRCB)[0];     \
  521.       (DST)[1] = (SRCA)[1] - (SRCB)[1];     \
  522. } while (0)
  523.  
  524. /** Addition */
  525. #define ADD_2V( DST, SRCA, SRCB )       \
  526. do {                        \
  527.       (DST)[0] = (SRCA)[0] + (SRCB)[0];     \
  528.       (DST)[1] = (SRCA)[1] + (SRCB)[1];     \
  529. } while (0)
  530.  
  531. /** In-place scalar multiplication */
  532. #define SCALE_2V( DST, SRCA, SRCB )     \
  533. do {                        \
  534.       (DST)[0] = (SRCA)[0] * (SRCB)[0];     \
  535.       (DST)[1] = (SRCA)[1] * (SRCB)[1];     \
  536. } while (0)
  537.  
  538. /** In-place addition */
  539. #define ACC_2V( DST, SRC )          \
  540. do {                        \
  541.       (DST)[0] += (SRC)[0];         \
  542.       (DST)[1] += (SRC)[1];         \
  543. } while (0)
  544.  
  545. /** Element-wise multiplication and addition */
  546. #define ACC_SCALE_2V( DST, SRCA, SRCB )     \
  547. do {                        \
  548.       (DST)[0] += (SRCA)[0] * (SRCB)[0];    \
  549.       (DST)[1] += (SRCA)[1] * (SRCB)[1];    \
  550. } while (0)
  551.  
  552. /** Scalar multiplication */
  553. #define SCALE_SCALAR_2V( DST, S, SRCB )     \
  554. do {                        \
  555.       (DST)[0] = S * (SRCB)[0];         \
  556.       (DST)[1] = S * (SRCB)[1];         \
  557. } while (0)
  558.  
  559. /** In-place scalar multiplication and addition */
  560. #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
  561. do {                        \
  562.       (DST)[0] += S * (SRCB)[0];        \
  563.       (DST)[1] += S * (SRCB)[1];        \
  564. } while (0)
  565.  
  566. /** In-place scalar multiplication */
  567. #define SELF_SCALE_SCALAR_2V( DST, S )      \
  568. do {                        \
  569.       (DST)[0] *= S;                \
  570.       (DST)[1] *= S;                \
  571. } while (0)
  572.  
  573. /** In-place scalar addition */
  574. #define ACC_SCALAR_2V( DST, S )         \
  575. do {                        \
  576.       (DST)[0] += S;                \
  577.       (DST)[1] += S;                \
  578. } while (0)
  579.  
  580. /** Assign scalers to short vectors */
  581. #define ASSIGN_2V( V, V0, V1 )  \
  582. do {                            \
  583.     V[0] = V0;                  \
  584.     V[1] = V1;                  \
  585. } while(0)
  586.  
  587. /*@}*/
  588.  
  589. /** Copy \p sz elements into a homegeneous (4-element) vector, giving
  590.  * default values to the remaining components.
  591.  * The default values are chosen based on \p type.
  592.  */
  593. static inline void
  594. COPY_CLEAN_4V_TYPE_AS_FLOAT(GLfloat dst[4], int sz, const GLfloat src[4],
  595.                             GLenum type)
  596. {
  597.    switch (type) {
  598.    case GL_FLOAT:
  599.       ASSIGN_4V(dst, 0, 0, 0, 1);
  600.       break;
  601.    case GL_INT:
  602.       ASSIGN_4V(dst, INT_AS_FLT(0), INT_AS_FLT(0),
  603.                      INT_AS_FLT(0), INT_AS_FLT(1));
  604.       break;
  605.    case GL_UNSIGNED_INT:
  606.       ASSIGN_4V(dst, UINT_AS_FLT(0), UINT_AS_FLT(0),
  607.                      UINT_AS_FLT(0), UINT_AS_FLT(1));
  608.       break;
  609.    default:
  610.       ASSERT(0);
  611.    }
  612.    COPY_SZ_4V(dst, sz, src);
  613. }
  614.  
  615. /** \name Linear interpolation functions */
  616. /*@{*/
  617.  
  618. static inline GLfloat
  619. LINTERP(GLfloat t, GLfloat out, GLfloat in)
  620. {
  621.    return out + t * (in - out);
  622. }
  623.  
  624. static inline void
  625. INTERP_3F(GLfloat t, GLfloat dst[3], const GLfloat out[3], const GLfloat in[3])
  626. {
  627.    dst[0] = LINTERP( t, out[0], in[0] );
  628.    dst[1] = LINTERP( t, out[1], in[1] );
  629.    dst[2] = LINTERP( t, out[2], in[2] );
  630. }
  631.  
  632. static inline void
  633. INTERP_4F(GLfloat t, GLfloat dst[4], const GLfloat out[4], const GLfloat in[4])
  634. {
  635.    dst[0] = LINTERP( t, out[0], in[0] );
  636.    dst[1] = LINTERP( t, out[1], in[1] );
  637.    dst[2] = LINTERP( t, out[2], in[2] );
  638.    dst[3] = LINTERP( t, out[3], in[3] );
  639. }
  640.  
  641. /*@}*/
  642.  
  643.  
  644.  
  645. /** Clamp X to [MIN,MAX] */
  646. #define CLAMP( X, MIN, MAX )  ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
  647.  
  648. /** Minimum of two values: */
  649. #define MIN2( A, B )   ( (A)<(B) ? (A) : (B) )
  650.  
  651. /** Maximum of two values: */
  652. #define MAX2( A, B )   ( (A)>(B) ? (A) : (B) )
  653.  
  654. /** Minimum and maximum of three values: */
  655. #define MIN3( A, B, C ) ((A) < (B) ? MIN2(A, C) : MIN2(B, C))
  656. #define MAX3( A, B, C ) ((A) > (B) ? MAX2(A, C) : MAX2(B, C))
  657.  
  658. static inline unsigned
  659. minify(unsigned value, unsigned levels)
  660. {
  661.     return MAX2(1, value >> levels);
  662. }
  663.  
  664. /**
  665.  * Align a value up to an alignment value
  666.  *
  667.  * If \c value is not already aligned to the requested alignment value, it
  668.  * will be rounded up.
  669.  *
  670.  * \param value  Value to be rounded
  671.  * \param alignment  Alignment value to be used.  This must be a power of two.
  672.  *
  673.  * \sa ROUND_DOWN_TO()
  674.  */
  675. #define ALIGN(value, alignment)  (((value) + (alignment) - 1) & ~((alignment) - 1))
  676.  
  677.  
  678.  
  679. /** Cross product of two 3-element vectors */
  680. static inline void
  681. CROSS3(GLfloat n[3], const GLfloat u[3], const GLfloat v[3])
  682. {
  683.    n[0] = u[1] * v[2] - u[2] * v[1];
  684.    n[1] = u[2] * v[0] - u[0] * v[2];
  685.    n[2] = u[0] * v[1] - u[1] * v[0];
  686. }
  687.  
  688.  
  689. /** Dot product of two 2-element vectors */
  690. static inline GLfloat
  691. DOT2(const GLfloat a[2], const GLfloat b[2])
  692. {
  693.    return a[0] * b[0] + a[1] * b[1];
  694. }
  695.  
  696. static inline GLfloat
  697. DOT3(const GLfloat a[3], const GLfloat b[3])
  698. {
  699.    return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  700. }
  701.  
  702. static inline GLfloat
  703. DOT4(const GLfloat a[4], const GLfloat b[4])
  704. {
  705.    return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
  706. }
  707.  
  708.  
  709. static inline GLfloat
  710. LEN_SQUARED_3FV(const GLfloat v[3])
  711. {
  712.    return DOT3(v, v);
  713. }
  714.  
  715. static inline GLfloat
  716. LEN_SQUARED_2FV(const GLfloat v[2])
  717. {
  718.    return DOT2(v, v);
  719. }
  720.  
  721.  
  722. static inline GLfloat
  723. LEN_3FV(const GLfloat v[3])
  724. {
  725.    return sqrtf(LEN_SQUARED_3FV(v));
  726. }
  727.  
  728. static inline GLfloat
  729. LEN_2FV(const GLfloat v[2])
  730. {
  731.    return sqrtf(LEN_SQUARED_2FV(v));
  732. }
  733.  
  734.  
  735. /* Normalize a 3-element vector to unit length. */
  736. static inline void
  737. NORMALIZE_3FV(GLfloat v[3])
  738. {
  739.    GLfloat len = (GLfloat) LEN_SQUARED_3FV(v);
  740.    if (len) {
  741.       len = INV_SQRTF(len);
  742.       v[0] *= len;
  743.       v[1] *= len;
  744.       v[2] *= len;
  745.    }
  746. }
  747.  
  748.  
  749. /** Is float value negative? */
  750. static inline GLboolean
  751. IS_NEGATIVE(float x)
  752. {
  753.    return signbit(x) != 0;
  754. }
  755.  
  756. /** Test two floats have opposite signs */
  757. static inline GLboolean
  758. DIFFERENT_SIGNS(GLfloat x, GLfloat y)
  759. {
  760.    return signbit(x) != signbit(y);
  761. }
  762.  
  763.  
  764. /** Compute ceiling of integer quotient of A divided by B. */
  765. #define CEILING( A, B )  ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
  766.  
  767.  
  768. /** casts to silence warnings with some compilers */
  769. #define ENUM_TO_INT(E)     ((GLint)(E))
  770. #define ENUM_TO_FLOAT(E)   ((GLfloat)(GLint)(E))
  771. #define ENUM_TO_DOUBLE(E)  ((GLdouble)(GLint)(E))
  772. #define ENUM_TO_BOOLEAN(E) ((E) ? GL_TRUE : GL_FALSE)
  773.  
  774. /* Compute the size of an array */
  775. #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
  776.  
  777.  
  778. #endif
  779.