gallium: remove some debug assertions in vertex format validation
[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 "p_debug.h"
33 #include <math.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 MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
113
114 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
115
116
117 /**
118 * Return a pointer aligned to next multiple of N bytes.
119 */
120 static INLINE void *
121 align_pointer( void *unaligned, uint alignment )
122 {
123 if (sizeof(void *) == 64) {
124 union {
125 void *p;
126 uint64 u;
127 } pu;
128 pu.p = unaligned;
129 pu.u = (pu.u + alignment - 1) & ~(uint64) (alignment - 1);
130 return pu.p;
131 }
132 else {
133 /* 32-bit pointers */
134 union {
135 void *p;
136 uint u;
137 } pu;
138 pu.p = unaligned;
139 pu.u = (pu.u + alignment - 1) & ~(alignment - 1);
140 return pu.p;
141 }
142 }
143
144 /**
145 * Return memory on given byte alignment
146 */
147 static INLINE void *
148 align_malloc(size_t bytes, uint alignment)
149 {
150 #if defined(HAVE_POSIX_MEMALIGN)
151 void *mem;
152 (void) posix_memalign(& mem, alignment, bytes);
153 return mem;
154 #else
155 char *ptr, *buf;
156
157 assert( alignment > 0 );
158
159 ptr = (char *) MALLOC(bytes + alignment + sizeof(void *));
160 if (!ptr)
161 return NULL;
162
163 buf = (char *) align_pointer( ptr + sizeof(void *), alignment );
164 *(char **)(buf - sizeof(void *)) = ptr;
165
166 return buf;
167 #endif /* defined(HAVE_POSIX_MEMALIGN) */
168 }
169
170 /**
171 * Free memory returned by align_malloc().
172 */
173 static INLINE void
174 align_free(void *ptr)
175 {
176 #if defined(HAVE_POSIX_MEMALIGN)
177 FREE(ptr);
178 #else
179 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
180 void *realAddr = *cubbyHole;
181 FREE(realAddr);
182 #endif /* defined(HAVE_POSIX_MEMALIGN) */
183 }
184
185
186
187 /**
188 * Duplicate of a block of memory
189 */
190 static INLINE void *
191 mem_dup(const void *src, uint size)
192 {
193 void *dup = malloc(size);
194 if (dup)
195 memcpy(dup, src, size);
196 return dup;
197 }
198
199
200
201 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
202 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
203 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
204
205 #define Elements(x) sizeof(x)/sizeof(*(x))
206 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
207
208 /**
209 * Return a pointer aligned to next multiple of 16 bytes.
210 */
211 static INLINE void *
212 align16( void *unaligned )
213 {
214 return align_pointer( unaligned, 16 );
215 }
216
217
218 static INLINE int align_int(int x, int align)
219 {
220 return (x + align - 1) & ~(align - 1);
221 }
222
223
224
225 #if defined(__MSC__) && defined(__WIN32__)
226 static INLINE unsigned ffs( unsigned u )
227 {
228 unsigned i;
229
230 if( u == 0 ) {
231 return 0;
232 }
233
234 __asm bsf eax, [u]
235 __asm inc eax
236 __asm mov [i], eax
237
238 return i;
239 }
240 #endif
241
242 union fi {
243 float f;
244 int i;
245 unsigned ui;
246 };
247
248 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
249
250 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
251
252 /* This function/macro is sensitive to precision. Test very carefully
253 * if you change it!
254 */
255 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
256 do { \
257 union fi __tmp; \
258 __tmp.f = (F); \
259 if (__tmp.i < 0) \
260 UB = (ubyte) 0; \
261 else if (__tmp.i >= IEEE_0996) \
262 UB = (ubyte) 255; \
263 else { \
264 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
265 UB = (ubyte) __tmp.i; \
266 } \
267 } while (0)
268
269
270
271 static INLINE unsigned pack_ub4( unsigned char b0,
272 unsigned char b1,
273 unsigned char b2,
274 unsigned char b3 )
275 {
276 return ((((unsigned int)b0) << 0) |
277 (((unsigned int)b1) << 8) |
278 (((unsigned int)b2) << 16) |
279 (((unsigned int)b3) << 24));
280 }
281
282 static INLINE unsigned fui( float f )
283 {
284 union fi fi;
285 fi.f = f;
286 return fi.ui;
287 }
288
289 static INLINE unsigned char float_to_ubyte( float f )
290 {
291 unsigned char ub;
292 UNCLAMPED_FLOAT_TO_UBYTE(ub, f);
293 return ub;
294 }
295
296 static INLINE unsigned pack_ui32_float4( float a,
297 float b,
298 float c,
299 float d )
300 {
301 return pack_ub4( float_to_ubyte(a),
302 float_to_ubyte(b),
303 float_to_ubyte(c),
304 float_to_ubyte(d) );
305 }
306
307 #define COPY_4V( DST, SRC ) \
308 do { \
309 (DST)[0] = (SRC)[0]; \
310 (DST)[1] = (SRC)[1]; \
311 (DST)[2] = (SRC)[2]; \
312 (DST)[3] = (SRC)[3]; \
313 } while (0)
314
315
316 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
317
318
319 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
320 do { \
321 (DST)[0] = (V0); \
322 (DST)[1] = (V1); \
323 (DST)[2] = (V2); \
324 (DST)[3] = (V3); \
325 } while (0)
326
327
328 static INLINE int ifloor(float f)
329 {
330 int ai, bi;
331 double af, bf;
332 union fi u;
333
334 af = (3 << 22) + 0.5 + (double)f;
335 bf = (3 << 22) + 0.5 - (double)f;
336 u.f = (float) af; ai = u.i;
337 u.f = (float) bf; bi = u.i;
338 return (ai - bi) >> 1;
339 }
340
341
342 #if defined(__GNUC__) && defined(__i386__)
343 static INLINE int iround(float f)
344 {
345 int r;
346 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
347 return r;
348 }
349 #elif defined(__MSC__) && defined(__WIN32__)
350 static INLINE int iround(float f)
351 {
352 int r;
353 _asm {
354 fld f
355 fistp r
356 }
357 return r;
358 }
359 #else
360 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
361 #endif
362
363
364 /* Could maybe have an inline version of this?
365 */
366 #if defined(__GNUC__)
367 #define FABSF(x) fabsf(x)
368 #else
369 #define FABSF(x) ((float) fabs(x))
370 #endif
371
372 /* Pretty fast, and accurate.
373 * Based on code from http://www.flipcode.com/totd/
374 */
375 static INLINE float LOG2(float val)
376 {
377 union fi num;
378 int log_2;
379
380 num.f = val;
381 log_2 = ((num.i >> 23) & 255) - 128;
382 num.i &= ~(255 << 23);
383 num.i += 127 << 23;
384 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
385 return num.f + log_2;
386 }
387
388 #if defined(__GNUC__)
389 #define CEILF(x) ceilf(x)
390 #else
391 #define CEILF(x) ((float) ceil(x))
392 #endif
393
394 static INLINE int align(int value, int alignment)
395 {
396 return (value + alignment - 1) & ~(alignment - 1);
397 }
398
399
400 /* util/p_util.c
401 */
402 extern void pipe_copy_rect(ubyte * dst, unsigned cpp, unsigned dst_pitch,
403 unsigned dst_x, unsigned dst_y, unsigned width,
404 unsigned height, const ubyte * src,
405 int src_pitch, unsigned src_x, int src_y);
406
407
408 #endif