fix some semantic info mix-ups in calculate_vertex_layout()
[mesa.git] / src / mesa / pipe / p_util.h
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #ifndef P_UTIL_H
29 #define P_UTIL_H
30
31 #include "p_compiler.h"
32 #include <math.h>
33 #include <stdint.h>
34
35
36 #ifdef WIN32
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #endif
42
43 void * __stdcall
44 EngAllocMem(
45 unsigned long Flags,
46 unsigned long MemSize,
47 unsigned long Tag );
48
49 void __stdcall
50 EngFreeMem(
51 void *Mem );
52
53 #ifdef __cplusplus
54 }
55 #endif
56
57 static INLINE void *
58 MALLOC( unsigned size )
59 {
60 return EngAllocMem( 0, size, 'D3AG' );
61 }
62
63 static INLINE void *
64 CALLOC( unsigned count, unsigned size )
65 {
66 void *ptr = MALLOC( count * size );
67 if( ptr ) {
68 memset( ptr, 0, count * size );
69 }
70 return ptr;
71 }
72
73 static INLINE void
74 FREE( void *ptr )
75 {
76 if( ptr ) {
77 EngFreeMem( ptr );
78 }
79 }
80
81 static INLINE void *
82 REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
83 {
84 void *new_ptr;
85 if( new_size <= old_size ) {
86 return old_ptr;
87 }
88 new_ptr = MALLOC( new_size );
89 if( new_ptr ) {
90 memcpy( new_ptr, old_ptr, old_size );
91 }
92 FREE( old_ptr );
93 return new_ptr;
94 }
95
96 #define GETENV( X ) NULL
97
98 #else /* WIN32 */
99
100 #define MALLOC( SIZE ) malloc( SIZE )
101
102 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
103
104 #define FREE( PTR ) free( PTR )
105
106 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
107
108 #define GETENV( X ) getenv( X )
109
110 #endif /* WIN32 */
111
112 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
113
114
115
116 /**
117 * Return memory on given byte alignment
118 */
119 static INLINE void *
120 align_malloc(size_t bytes, uint alignment)
121 {
122 #if defined(HAVE_POSIX_MEMALIGN)
123 void *mem;
124 (void) posix_memalign(& mem, alignment, bytes);
125 return mem;
126 #else
127 uintptr_t ptr, buf;
128
129 assert( alignment > 0 );
130
131 ptr = (uintptr_t) MALLOC(bytes + alignment + sizeof(void *));
132 if (!ptr)
133 return NULL;
134
135 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
136 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
137
138 return (void *) buf;
139 #endif /* defined(HAVE_POSIX_MEMALIGN) */
140 }
141
142 /**
143 * Free memory returned by align_malloc().
144 */
145 static INLINE void
146 align_free(void *ptr)
147 {
148 #if defined(HAVE_POSIX_MEMALIGN)
149 FREE(ptr);
150 #else
151 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
152 void *realAddr = *cubbyHole;
153 FREE(realAddr);
154 #endif /* defined(HAVE_POSIX_MEMALIGN) */
155 }
156
157
158
159 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
160 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
161 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
162
163 #define Elements(x) sizeof(x)/sizeof(*(x))
164 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
165
166 /**
167 * Return a pointer aligned to next multiple of 16 bytes.
168 */
169 static INLINE void *
170 align16( void *unaligned )
171 {
172 if (sizeof(void *) == 64) {
173 union {
174 void *p;
175 uint64 u;
176 } pu;
177 pu.p = unaligned;
178 pu.u = (pu.u + 15) & ~15;
179 return pu.p;
180 }
181 else {
182 /* 32-bit pointers */
183 union {
184 void *p;
185 uint u;
186 } pu;
187 pu.p = unaligned;
188 pu.u = (pu.u + 15) & ~15;
189 return pu.p;
190 }
191 }
192
193
194 #if defined(__MSC__) && defined(__WIN32__)
195 static INLINE unsigned ffs( unsigned u )
196 {
197 unsigned i;
198
199 if( u == 0 ) {
200 return 0;
201 }
202
203 __asm bsf eax, [u]
204 __asm inc eax
205 __asm mov [i], eax
206
207 return i;
208 }
209 #endif
210
211 union fi {
212 float f;
213 int i;
214 unsigned ui;
215 };
216
217 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
218
219 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
220
221 /* This function/macro is sensitive to precision. Test very carefully
222 * if you change it!
223 */
224 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
225 do { \
226 union fi __tmp; \
227 __tmp.f = (F); \
228 if (__tmp.i < 0) \
229 UB = (ubyte) 0; \
230 else if (__tmp.i >= IEEE_0996) \
231 UB = (ubyte) 255; \
232 else { \
233 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
234 UB = (ubyte) __tmp.i; \
235 } \
236 } while (0)
237
238
239
240 static INLINE unsigned pack_ub4( unsigned char b0,
241 unsigned char b1,
242 unsigned char b2,
243 unsigned char b3 )
244 {
245 return ((((unsigned int)b0) << 0) |
246 (((unsigned int)b1) << 8) |
247 (((unsigned int)b2) << 16) |
248 (((unsigned int)b3) << 24));
249 }
250
251 static INLINE unsigned fui( float f )
252 {
253 union fi fi;
254 fi.f = f;
255 return fi.ui;
256 }
257
258 static INLINE unsigned char float_to_ubyte( float f )
259 {
260 unsigned char ub;
261 UNCLAMPED_FLOAT_TO_UBYTE(ub, f);
262 return ub;
263 }
264
265 static INLINE unsigned pack_ui32_float4( float a,
266 float b,
267 float c,
268 float d )
269 {
270 return pack_ub4( float_to_ubyte(a),
271 float_to_ubyte(b),
272 float_to_ubyte(c),
273 float_to_ubyte(d) );
274 }
275
276 #define COPY_4V( DST, SRC ) \
277 do { \
278 (DST)[0] = (SRC)[0]; \
279 (DST)[1] = (SRC)[1]; \
280 (DST)[2] = (SRC)[2]; \
281 (DST)[3] = (SRC)[3]; \
282 } while (0)
283
284
285 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
286
287
288 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
289 do { \
290 (DST)[0] = (V0); \
291 (DST)[1] = (V1); \
292 (DST)[2] = (V2); \
293 (DST)[3] = (V3); \
294 } while (0)
295
296
297 static INLINE int ifloor(float f)
298 {
299 int ai, bi;
300 double af, bf;
301 union fi u;
302
303 af = (3 << 22) + 0.5 + (double)f;
304 bf = (3 << 22) + 0.5 - (double)f;
305 u.f = (float) af; ai = u.i;
306 u.f = (float) bf; bi = u.i;
307 return (ai - bi) >> 1;
308 }
309
310
311 #if defined(__GNUC__) && defined(__i386__)
312 static INLINE int iround(float f)
313 {
314 int r;
315 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
316 return r;
317 }
318 #elif defined(__MSC__) && defined(__WIN32__)
319 static INLINE int iround(float f)
320 {
321 int r;
322 _asm {
323 fld f
324 fistp r
325 }
326 return r;
327 }
328 #else
329 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
330 #endif
331
332
333 /* Could maybe have an inline version of this?
334 */
335 #if defined(__GNUC__)
336 #define FABSF(x) fabsf(x)
337 #else
338 #define FABSF(x) ((float) fabs(x))
339 #endif
340
341 /* Pretty fast, and accurate.
342 * Based on code from http://www.flipcode.com/totd/
343 */
344 static INLINE float LOG2(float val)
345 {
346 union fi num;
347 int log_2;
348
349 num.f = val;
350 log_2 = ((num.i >> 23) & 255) - 128;
351 num.i &= ~(255 << 23);
352 num.i += 127 << 23;
353 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
354 return num.f + log_2;
355 }
356
357 #if defined(__GNUC__)
358 #define CEILF(x) ceilf(x)
359 #else
360 #define CEILF(x) ((float) ceil(x))
361 #endif
362
363 static INLINE int align(int value, int alignment)
364 {
365 return (value + alignment - 1) & ~(alignment - 1);
366 }
367
368 /* Convenient...
369 */
370 extern void _mesa_printf(const char *str, ...);
371
372 #endif