source: trunk/src/opengl/mesa/macros.h@ 3721

Last change on this file since 3721 was 3597, checked in by jeroen, 25 years ago

* empty log message *

File size: 16.6 KB
Line 
1/* $Id: macros.h,v 1.2 2000-05-23 20:34:52 jeroen Exp $ */
2
3/*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999 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 * A collection of useful macros.
29 */
30
31
32#ifndef MACROS_H
33#define MACROS_H
34
35#if defined(DEBUG)
36# define ASSERT(X) assert(X)
37#define ABORT() abort()
38#define EXIT(rc) exit(rc)
39#else
40# define ASSERT(X)
41#define ABORT() abort()
42#define EXIT(rc) exit(rc)
43#endif
44
45#if defined(__WIN32OS2__)
46#ifdef DEBUG
47#undef ASSERT
48#undef ABORT
49#undef EXIT
50#define ASSERT(X) if(!(X)) { char msg[200]; sprintf(msg,"Assertion failed at line %d in %s",__LINE__,__FILE__); \
51MessageBox(0,msg,"Error",MB_OK); \
52}
53#define ABORT() {char msg[200]; sprintf(msg,"ABORT at line %d in %s",__FILE__,__LINE__); \
54abort(); }
55#define EXIT(rc) {char msg[200]; sprintf(msg,"EXIT at line %d in %s - rc %d",__FILE__,__LINE__,rc); \
56exit(rc); }
57#else
58#define ASSERT(X)
59#define ABORT() abort()
60#define EXIT(rc) exit(rc)
61#endif
62#endif
63
64
65#if defined(__GNUC__)
66#define INLINE __inline__
67#elif defined(__MSC__)
68#define INLINE __inline
69#else
70#define INLINE
71#endif
72
73
74/* Limits: */
75#define MAX_GLUSHORT 0xffff
76#define MAX_GLUINT 0xffffffff
77
78
79/* Some compilers don't like some of Mesa's const usage */
80#ifdef NO_CONST
81# define CONST
82#else
83# define CONST const
84#endif
85
86
87/* Pi */
88#ifndef M_PI
89#define M_PI (3.1415926)
90#endif
91
92
93/* Degrees to radians conversion: */
94#define DEG2RAD (M_PI/180.0)
95
96
97#ifndef NULL
98#define NULL 0
99#endif
100
101
102
103/*
104 * Bitmask helpers
105 */
106#define SET_BITS(WORD, BITS) (WORD) |= (BITS)
107#define CLEAR_BITS(WORD, BITS) (WORD) &= ~(BITS)
108#define TEST_BITS(WORD, BITS) ((WORD) & (BITS))
109
110
111/* Stepping a GLfloat pointer by a byte stride
112 */
113#define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
114#define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
115#define STRIDE_T(p, t, i) (p = (t *)((GLubyte *)p + i))
116
117
118#define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
119#define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
120#define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
121
122
123/* Copy short vectors: */
124#define COPY_2V( DST, SRC ) \
125do { \
126 (DST)[0] = (SRC)[0]; \
127 (DST)[1] = (SRC)[1]; \
128} while (0)
129
130
131#define COPY_3V( DST, SRC ) \
132do { \
133 (DST)[0] = (SRC)[0]; \
134 (DST)[1] = (SRC)[1]; \
135 (DST)[2] = (SRC)[2]; \
136} while (0)
137
138#define COPY_4V( DST, SRC ) \
139do { \
140 (DST)[0] = (SRC)[0]; \
141 (DST)[1] = (SRC)[1]; \
142 (DST)[2] = (SRC)[2]; \
143 (DST)[3] = (SRC)[3]; \
144} while (0)
145
146
147#define COPY_2FV( DST, SRC ) \
148do { \
149 const GLfloat *_tmp = (SRC); \
150 (DST)[0] = _tmp[0]; \
151 (DST)[1] = _tmp[1]; \
152} while (0)
153
154
155#define COPY_3FV( DST, SRC ) \
156do { \
157 const GLfloat *_tmp = (SRC); \
158 (DST)[0] = _tmp[0]; \
159 (DST)[1] = _tmp[1]; \
160 (DST)[2] = _tmp[2]; \
161} while (0)
162
163#define COPY_4FV( DST, SRC ) \
164do { \
165 const GLfloat *_tmp = (SRC); \
166 (DST)[0] = _tmp[0]; \
167 (DST)[1] = _tmp[1]; \
168 (DST)[2] = _tmp[2]; \
169 (DST)[3] = _tmp[3]; \
170} while (0)
171
172
173
174#define COPY_SZ_4V(DST, SZ, SRC) \
175do { \
176 switch (SZ) { \
177 case 4: (DST)[3] = (SRC)[3]; \
178 case 3: (DST)[2] = (SRC)[2]; \
179 case 2: (DST)[1] = (SRC)[1]; \
180 case 1: (DST)[0] = (SRC)[0]; \
181 } \
182} while(0)
183
184#define SUB_4V( DST, SRCA, SRCB ) \
185do { \
186 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
187 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
188 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
189 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
190} while (0)
191
192#define ADD_4V( DST, SRCA, SRCB ) \
193do { \
194 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
195 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
196 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
197 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
198} while (0)
199
200#define SCALE_4V( DST, SRCA, SRCB ) \
201do { \
202 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
203 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
204 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
205 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
206} while (0)
207
208#define ACC_4V( DST, SRC ) \
209do { \
210 (DST)[0] += (SRC)[0]; \
211 (DST)[1] += (SRC)[1]; \
212 (DST)[2] += (SRC)[2]; \
213 (DST)[3] += (SRC)[3]; \
214} while (0)
215
216#define ACC_SCALE_4V( DST, SRCA, SRCB ) \
217do { \
218 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
219 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
220 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
221 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
222} while (0)
223
224#define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
225do { \
226 (DST)[0] += S * (SRCB)[0]; \
227 (DST)[1] += S * (SRCB)[1]; \
228 (DST)[2] += S * (SRCB)[2]; \
229 (DST)[3] += S * (SRCB)[3]; \
230} while (0)
231
232#define SCALE_SCALAR_4V( DST, S, SRCB ) \
233do { \
234 (DST)[0] = S * (SRCB)[0]; \
235 (DST)[1] = S * (SRCB)[1]; \
236 (DST)[2] = S * (SRCB)[2]; \
237 (DST)[3] = S * (SRCB)[3]; \
238} while (0)
239
240
241#define SELF_SCALE_SCALAR_4V( DST, S ) \
242do { \
243 (DST)[0] *= S; \
244 (DST)[1] *= S; \
245 (DST)[2] *= S; \
246 (DST)[3] *= S; \
247} while (0)
248
249
250/*
251 * Similarly for 3-vectors.
252 */
253#define SUB_3V( DST, SRCA, SRCB ) \
254do { \
255 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
256 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
257 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
258} while (0)
259
260#define ADD_3V( DST, SRCA, SRCB ) \
261do { \
262 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
263 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
264 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
265} while (0)
266
267#define SCALE_3V( DST, SRCA, SRCB ) \
268do { \
269 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
270 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
271 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
272} while (0)
273
274#define ACC_3V( DST, SRC ) \
275do { \
276 (DST)[0] += (SRC)[0]; \
277 (DST)[1] += (SRC)[1]; \
278 (DST)[2] += (SRC)[2]; \
279} while (0)
280
281#define ACC_SCALE_3V( DST, SRCA, SRCB ) \
282do { \
283 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
284 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
285 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
286} while (0)
287
288#define SCALE_SCALAR_3V( DST, S, SRCB ) \
289do { \
290 (DST)[0] = S * (SRCB)[0]; \
291 (DST)[1] = S * (SRCB)[1]; \
292 (DST)[2] = S * (SRCB)[2]; \
293} while (0)
294
295#define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
296do { \
297 (DST)[0] += S * (SRCB)[0]; \
298 (DST)[1] += S * (SRCB)[1]; \
299 (DST)[2] += S * (SRCB)[2]; \
300} while (0)
301
302#define SELF_SCALE_SCALAR_3V( DST, S ) \
303do { \
304 (DST)[0] *= S; \
305 (DST)[1] *= S; \
306 (DST)[2] *= S; \
307} while (0)
308
309#define ACC_SCALAR_3V( DST, S ) \
310do { \
311 (DST)[0] += S; \
312 (DST)[1] += S; \
313 (DST)[2] += S; \
314} while (0)
315
316/* And also for 2-vectors
317 */
318#define SUB_2V( DST, SRCA, SRCB ) \
319do { \
320 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
321 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
322} while (0)
323
324#define ADD_2V( DST, SRCA, SRCB ) \
325do { \
326 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
327 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
328} while (0)
329
330#define SCALE_2V( DST, SRCA, SRCB ) \
331do { \
332 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
333 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
334} while (0)
335
336#define ACC_2V( DST, SRC ) \
337do { \
338 (DST)[0] += (SRC)[0]; \
339 (DST)[1] += (SRC)[1]; \
340} while (0)
341
342#define ACC_SCALE_2V( DST, SRCA, SRCB ) \
343do { \
344 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
345 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
346} while (0)
347
348#define SCALE_SCALAR_2V( DST, S, SRCB ) \
349do { \
350 (DST)[0] = S * (SRCB)[0]; \
351 (DST)[1] = S * (SRCB)[1]; \
352} while (0)
353
354#define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
355do { \
356 (DST)[0] += S * (SRCB)[0]; \
357 (DST)[1] += S * (SRCB)[1]; \
358} while (0)
359
360#define SELF_SCALE_SCALAR_2V( DST, S ) \
361do { \
362 (DST)[0] *= S; \
363 (DST)[1] *= S; \
364} while (0)
365
366#define ACC_SCALAR_2V( DST, S ) \
367do { \
368 (DST)[0] += S; \
369 (DST)[1] += S; \
370} while (0)
371
372
373
374/*
375 * Copy a vector of 4 GLubytes from SRC to DST.
376 */
377#define COPY_4UBV(DST, SRC) \
378do { \
379 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
380 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
381 } \
382 else { \
383 (DST)[0] = (SRC)[0]; \
384 (DST)[1] = (SRC)[1]; \
385 (DST)[2] = (SRC)[2]; \
386 (DST)[3] = (SRC)[3]; \
387 } \
388} while (0)
389
390
391/* Assign scalers to short vectors: */
392#define ASSIGN_2V( V, V0, V1 ) \
393do { V[0] = V0; V[1] = V1; } while(0)
394
395#define ASSIGN_3V( V, V0, V1, V2 ) \
396do { V[0] = V0; V[1] = V1; V[2] = V2; } while(0)
397
398#define ASSIGN_4V( V, V0, V1, V2, V3 ) \
399do { \
400 V[0] = V0; \
401 V[1] = V1; \
402 V[2] = V2; \
403 V[3] = V3; \
404} while(0)
405
406
407
408
409/* Absolute value (for Int, Float, Double): */
410#define ABSI(X) ((X) < 0 ? -(X) : (X))
411#define ABSF(X) ((X) < 0.0F ? -(X) : (X))
412#define ABSD(X) ((X) < 0.0 ? -(X) : (X))
413
414
415
416/* Round a floating-point value to the nearest integer: */
417#define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
418
419
420/* Compute ceiling of integer quotient of A divided by B: */
421#define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
422
423
424/* Clamp X to [MIN,MAX]: */
425#define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
426
427/* Assign X to CLAMP(X, MIN, MAX) */
428#define CLAMP_SELF(x, mn, mx) \
429 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
430
431
432
433/* Min of two values: */
434#define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
435
436
437/* MAX of two values: */
438#define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
439
440/* Dot product of two 2-element vectors */
441#define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
442
443/* Dot product of two 3-element vectors */
444#define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
445
446
447/* Dot product of two 4-element vectors */
448#define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
449 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
450
451#define DOT4V(v,a,b,c,d) (v[0]*a + v[1]*b + v[2]*c + v[3]*d)
452
453
454#define CROSS3(n, u, v) \
455do { \
456 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
457 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
458 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
459} while (0)
460
461
462/*
463 * Integer / float conversion for colors, normals, etc.
464 */
465
466#define BYTE_TO_UBYTE(b) (b < 0 ? 0 : (GLubyte) b)
467#define SHORT_TO_UBYTE(s) (s < 0 ? 0 : (GLubyte) (s >> 7))
468#define USHORT_TO_UBYTE(s) (GLubyte) (s >> 8)
469#define INT_TO_UBYTE(i) (i < 0 ? 0 : (GLubyte) (i >> 23))
470#define UINT_TO_UBYTE(i) (GLubyte) (i >> 24)
471
472/* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
473#define UBYTE_TO_FLOAT(B) ((GLfloat) (B) * (1.0F / 255.0F))
474
475/* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
476#define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F))
477
478
479/* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
480#define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
481
482/* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
483#define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
484
485
486/* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
487#define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
488
489/* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
490#define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
491
492
493/* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
494#define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
495
496/* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
497#define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
498
499
500/* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
501#define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F))
502
503/* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
504#define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
505
506
507/* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
508#define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
509
510/* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
511/* causes overflow:
512#define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
513*/
514/* a close approximation: */
515#define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
516
517
518#endif /*MACROS_H*/
Note: See TracBrowser for help on using the repository browser.