d84d2edd7c8ccf93c6a68205a336a4f97f4a8347
[mesa.git] / src / mesa / main / macros.h
1 /* $Id: macros.h,v 1.13 2000/11/05 18:40:58 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /*
29 * A collection of useful macros.
30 */
31
32
33 #ifndef MACROS_H
34 #define MACROS_H
35
36
37 #include "glheader.h"
38
39
40 /* Limits: */
41 #define MAX_GLUSHORT 0xffff
42 #define MAX_GLUINT 0xffffffff
43
44
45 /* Pi */
46 #ifndef M_PI
47 #define M_PI (3.1415926)
48 #endif
49
50
51 /* Degrees to radians conversion: */
52 #define DEG2RAD (M_PI/180.0)
53
54
55 #ifndef NULL
56 #define NULL 0
57 #endif
58
59
60
61 /*
62 * Bitmask helpers
63 */
64 #define SET_BITS(WORD, BITS) (WORD) |= (BITS)
65 #define CLEAR_BITS(WORD, BITS) (WORD) &= ~(BITS)
66 #define TEST_BITS(WORD, BITS) ((WORD) & (BITS))
67
68
69 /* Stepping a GLfloat pointer by a byte stride
70 */
71 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
72 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
73 #define STRIDE_T(p, t, i) (p = (t *)((GLubyte *)p + i))
74
75
76 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
77 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
78 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
79
80
81 /* Copy short vectors: */
82 #define COPY_2V( DST, SRC ) \
83 do { \
84 (DST)[0] = (SRC)[0]; \
85 (DST)[1] = (SRC)[1]; \
86 } while (0)
87
88 #define COPY_3V( DST, SRC ) \
89 do { \
90 (DST)[0] = (SRC)[0]; \
91 (DST)[1] = (SRC)[1]; \
92 (DST)[2] = (SRC)[2]; \
93 } while (0)
94
95 #define COPY_4V( DST, SRC ) \
96 do { \
97 (DST)[0] = (SRC)[0]; \
98 (DST)[1] = (SRC)[1]; \
99 (DST)[2] = (SRC)[2]; \
100 (DST)[3] = (SRC)[3]; \
101 } while (0)
102
103 #define COPY_4UBV(DST, SRC) \
104 do { \
105 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
106 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
107 } \
108 else { \
109 (DST)[0] = (SRC)[0]; \
110 (DST)[1] = (SRC)[1]; \
111 (DST)[2] = (SRC)[2]; \
112 (DST)[3] = (SRC)[3]; \
113 } \
114 } while (0)
115
116
117 #define COPY_2FV( DST, SRC ) \
118 do { \
119 const GLfloat *_tmp = (SRC); \
120 (DST)[0] = _tmp[0]; \
121 (DST)[1] = _tmp[1]; \
122 } while (0)
123
124 #define COPY_3FV( DST, SRC ) \
125 do { \
126 const GLfloat *_tmp = (SRC); \
127 (DST)[0] = _tmp[0]; \
128 (DST)[1] = _tmp[1]; \
129 (DST)[2] = _tmp[2]; \
130 } while (0)
131
132 #define COPY_4FV( DST, SRC ) \
133 do { \
134 const GLfloat *_tmp = (SRC); \
135 (DST)[0] = _tmp[0]; \
136 (DST)[1] = _tmp[1]; \
137 (DST)[2] = _tmp[2]; \
138 (DST)[3] = _tmp[3]; \
139 } while (0)
140
141
142
143 #define COPY_SZ_4V(DST, SZ, SRC) \
144 do { \
145 switch (SZ) { \
146 case 4: (DST)[3] = (SRC)[3]; \
147 case 3: (DST)[2] = (SRC)[2]; \
148 case 2: (DST)[1] = (SRC)[1]; \
149 case 1: (DST)[0] = (SRC)[0]; \
150 } \
151 } while(0)
152
153 #define COPY_CLEAN_4V(DST, SZ, SRC) \
154 do { \
155 ASSIGN_4V( DST, 0, 0, 0, 1 ); \
156 COPY_SZ_4V( DST, SZ, SRC ); \
157 } while (0)
158
159 #define SUB_4V( DST, SRCA, SRCB ) \
160 do { \
161 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
162 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
163 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
164 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
165 } while (0)
166
167 #define ADD_4V( DST, SRCA, SRCB ) \
168 do { \
169 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
170 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
171 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
172 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
173 } while (0)
174
175 #define SCALE_4V( DST, SRCA, SRCB ) \
176 do { \
177 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
178 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
179 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
180 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
181 } while (0)
182
183 #define ACC_4V( DST, SRC ) \
184 do { \
185 (DST)[0] += (SRC)[0]; \
186 (DST)[1] += (SRC)[1]; \
187 (DST)[2] += (SRC)[2]; \
188 (DST)[3] += (SRC)[3]; \
189 } while (0)
190
191 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
192 do { \
193 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
194 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
195 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
196 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
197 } while (0)
198
199 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
200 do { \
201 (DST)[0] += S * (SRCB)[0]; \
202 (DST)[1] += S * (SRCB)[1]; \
203 (DST)[2] += S * (SRCB)[2]; \
204 (DST)[3] += S * (SRCB)[3]; \
205 } while (0)
206
207 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
208 do { \
209 (DST)[0] = S * (SRCB)[0]; \
210 (DST)[1] = S * (SRCB)[1]; \
211 (DST)[2] = S * (SRCB)[2]; \
212 (DST)[3] = S * (SRCB)[3]; \
213 } while (0)
214
215
216 #define SELF_SCALE_SCALAR_4V( DST, S ) \
217 do { \
218 (DST)[0] *= S; \
219 (DST)[1] *= S; \
220 (DST)[2] *= S; \
221 (DST)[3] *= S; \
222 } while (0)
223
224
225 /*
226 * Similarly for 3-vectors.
227 */
228 #define SUB_3V( DST, SRCA, SRCB ) \
229 do { \
230 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
231 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
232 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
233 } while (0)
234
235 #define ADD_3V( DST, SRCA, SRCB ) \
236 do { \
237 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
238 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
239 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
240 } while (0)
241
242 #define SCALE_3V( DST, SRCA, SRCB ) \
243 do { \
244 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
245 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
246 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
247 } while (0)
248
249 #define ACC_3V( DST, SRC ) \
250 do { \
251 (DST)[0] += (SRC)[0]; \
252 (DST)[1] += (SRC)[1]; \
253 (DST)[2] += (SRC)[2]; \
254 } while (0)
255
256 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
257 do { \
258 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
259 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
260 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
261 } while (0)
262
263 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
264 do { \
265 (DST)[0] = S * (SRCB)[0]; \
266 (DST)[1] = S * (SRCB)[1]; \
267 (DST)[2] = S * (SRCB)[2]; \
268 } while (0)
269
270 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
271 do { \
272 (DST)[0] += S * (SRCB)[0]; \
273 (DST)[1] += S * (SRCB)[1]; \
274 (DST)[2] += S * (SRCB)[2]; \
275 } while (0)
276
277 #define SELF_SCALE_SCALAR_3V( DST, S ) \
278 do { \
279 (DST)[0] *= S; \
280 (DST)[1] *= S; \
281 (DST)[2] *= S; \
282 } while (0)
283
284 #define ACC_SCALAR_3V( DST, S ) \
285 do { \
286 (DST)[0] += S; \
287 (DST)[1] += S; \
288 (DST)[2] += S; \
289 } while (0)
290
291 /* And also for 2-vectors
292 */
293 #define SUB_2V( DST, SRCA, SRCB ) \
294 do { \
295 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
296 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
297 } while (0)
298
299 #define ADD_2V( DST, SRCA, SRCB ) \
300 do { \
301 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
302 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
303 } while (0)
304
305 #define SCALE_2V( DST, SRCA, SRCB ) \
306 do { \
307 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
308 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
309 } while (0)
310
311 #define ACC_2V( DST, SRC ) \
312 do { \
313 (DST)[0] += (SRC)[0]; \
314 (DST)[1] += (SRC)[1]; \
315 } while (0)
316
317 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
318 do { \
319 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
320 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
321 } while (0)
322
323 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
324 do { \
325 (DST)[0] = S * (SRCB)[0]; \
326 (DST)[1] = S * (SRCB)[1]; \
327 } while (0)
328
329 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
330 do { \
331 (DST)[0] += S * (SRCB)[0]; \
332 (DST)[1] += S * (SRCB)[1]; \
333 } while (0)
334
335 #define SELF_SCALE_SCALAR_2V( DST, S ) \
336 do { \
337 (DST)[0] *= S; \
338 (DST)[1] *= S; \
339 } while (0)
340
341 #define ACC_SCALAR_2V( DST, S ) \
342 do { \
343 (DST)[0] += S; \
344 (DST)[1] += S; \
345 } while (0)
346
347
348
349 /* Assign scalers to short vectors: */
350 #define ASSIGN_2V( V, V0, V1 ) \
351 do { \
352 V[0] = V0; \
353 V[1] = V1; \
354 } while(0)
355
356 #define ASSIGN_3V( V, V0, V1, V2 ) \
357 do { \
358 V[0] = V0; \
359 V[1] = V1; \
360 V[2] = V2; \
361 } while(0)
362
363 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
364 do { \
365 V[0] = V0; \
366 V[1] = V1; \
367 V[2] = V2; \
368 V[3] = V3; \
369 } while(0)
370
371
372
373
374 /* Absolute value (for Int, Float, Double): */
375 #define ABSI(X) ((X) < 0 ? -(X) : (X))
376 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
377 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
378
379
380
381 /* Round a floating-point value to the nearest integer: */
382 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
383
384
385 /* Compute ceiling of integer quotient of A divided by B: */
386 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
387
388
389 /* Clamp X to [MIN,MAX]: */
390 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
391
392 /* Assign X to CLAMP(X, MIN, MAX) */
393 #define CLAMP_SELF(x, mn, mx) \
394 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
395
396
397
398 /* Min of two values: */
399 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
400
401 /* MAX of two values: */
402 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
403
404 /* Dot product of two 2-element vectors */
405 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
406
407 /* Dot product of two 3-element vectors */
408 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
409
410 /* Dot product of two 4-element vectors */
411 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
412 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
413
414 #define DOT4V(v,a,b,c,d) (v[0]*(a) + v[1]*(b) + v[2]*(c) + v[3]*(d))
415
416
417 #define CROSS3(n, u, v) \
418 do { \
419 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
420 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
421 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
422 } while (0)
423
424
425 #endif