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