Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / gallium / include / 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_config.h"
32 #include "p_compiler.h"
33 #include "p_debug.h"
34 #include "p_pointer.h"
35 #include <math.h>
36 #include <stdarg.h>
37
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43
44 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) && defined(DEBUG)
45
46 /* memory debugging */
47
48 #include "p_debug.h"
49
50 #define MALLOC( _size ) \
51 debug_malloc( __FILE__, __LINE__, __FUNCTION__, _size )
52 #define CALLOC( _count, _size ) \
53 debug_calloc(__FILE__, __LINE__, __FUNCTION__, _count, _size )
54 #define FREE( _ptr ) \
55 debug_free( __FILE__, __LINE__, __FUNCTION__, _ptr )
56 #define REALLOC( _ptr, _old_size, _size ) \
57 debug_realloc( __FILE__, __LINE__, __FUNCTION__, _ptr, _old_size, _size )
58
59 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
60
61 void * __stdcall
62 EngAllocMem(
63 unsigned long Flags,
64 unsigned long MemSize,
65 unsigned long Tag );
66
67 void __stdcall
68 EngFreeMem(
69 void *Mem );
70
71 #define MALLOC( _size ) EngAllocMem( 0, _size, 'D3AG' )
72 #define _FREE( _ptr ) EngFreeMem( _ptr )
73
74 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
75
76 void *
77 ExAllocatePool(
78 unsigned long PoolType,
79 size_t NumberOfBytes);
80
81 void
82 ExFreePool(void *P);
83
84 #define MALLOC(_size) ExAllocatePool(0, _size)
85 #define _FREE(_ptr) ExFreePool(_ptr)
86
87 #else
88
89 #define MALLOC( SIZE ) malloc( SIZE )
90 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
91 #define FREE( PTR ) free( PTR )
92 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
93
94 #endif
95
96
97 #ifndef CALLOC
98 static INLINE void *
99 CALLOC( unsigned count, unsigned size )
100 {
101 void *ptr = MALLOC( count * size );
102 if( ptr ) {
103 memset( ptr, 0, count * size );
104 }
105 return ptr;
106 }
107 #endif /* !CALLOC */
108
109 #ifndef FREE
110 static INLINE void
111 FREE( void *ptr )
112 {
113 if( ptr ) {
114 _FREE( ptr );
115 }
116 }
117 #endif /* !FREE */
118
119 #ifndef REALLOC
120 static INLINE void *
121 REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
122 {
123 void *new_ptr = NULL;
124
125 if (new_size != 0) {
126 unsigned copy_size = old_size < new_size ? old_size : new_size;
127 new_ptr = MALLOC( new_size );
128 if (new_ptr && old_ptr && copy_size) {
129 memcpy( new_ptr, old_ptr, copy_size );
130 }
131 }
132
133 FREE( old_ptr );
134 return new_ptr;
135 }
136 #endif /* !REALLOC */
137
138
139 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
140
141 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
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 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 #ifndef Elements
206 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
207 #endif
208 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
209
210 /**
211 * Return a pointer aligned to next multiple of 16 bytes.
212 */
213 static INLINE void *
214 align16( void *unaligned )
215 {
216 return align_pointer( unaligned, 16 );
217 }
218
219
220 static INLINE int align_int(int x, int align)
221 {
222 return (x + align - 1) & ~(align - 1);
223 }
224
225
226
227 #if defined(__MSC__) && defined(__WIN32__)
228 static INLINE unsigned ffs( unsigned u )
229 {
230 unsigned i;
231
232 if( u == 0 ) {
233 return 0;
234 }
235
236 __asm bsf eax, [u]
237 __asm inc eax
238 __asm mov [i], eax
239
240 return i;
241 }
242 #endif
243
244 union fi {
245 float f;
246 int i;
247 unsigned ui;
248 };
249
250 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
251
252 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
253
254 /* This function/macro is sensitive to precision. Test very carefully
255 * if you change it!
256 */
257 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
258 do { \
259 union fi __tmp; \
260 __tmp.f = (F); \
261 if (__tmp.i < 0) \
262 UB = (ubyte) 0; \
263 else if (__tmp.i >= IEEE_0996) \
264 UB = (ubyte) 255; \
265 else { \
266 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
267 UB = (ubyte) __tmp.i; \
268 } \
269 } while (0)
270
271
272
273 static INLINE unsigned pack_ub4( unsigned char b0,
274 unsigned char b1,
275 unsigned char b2,
276 unsigned char b3 )
277 {
278 return ((((unsigned int)b0) << 0) |
279 (((unsigned int)b1) << 8) |
280 (((unsigned int)b2) << 16) |
281 (((unsigned int)b3) << 24));
282 }
283
284 static INLINE unsigned fui( float f )
285 {
286 union fi fi;
287 fi.f = f;
288 return fi.ui;
289 }
290
291 static INLINE unsigned char float_to_ubyte( float f )
292 {
293 unsigned char ub;
294 UNCLAMPED_FLOAT_TO_UBYTE(ub, f);
295 return ub;
296 }
297
298 static INLINE unsigned pack_ui32_float4( float a,
299 float b,
300 float c,
301 float d )
302 {
303 return pack_ub4( float_to_ubyte(a),
304 float_to_ubyte(b),
305 float_to_ubyte(c),
306 float_to_ubyte(d) );
307 }
308
309 #define COPY_4V( DST, SRC ) \
310 do { \
311 (DST)[0] = (SRC)[0]; \
312 (DST)[1] = (SRC)[1]; \
313 (DST)[2] = (SRC)[2]; \
314 (DST)[3] = (SRC)[3]; \
315 } while (0)
316
317
318 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
319
320
321 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
322 do { \
323 (DST)[0] = (V0); \
324 (DST)[1] = (V1); \
325 (DST)[2] = (V2); \
326 (DST)[3] = (V3); \
327 } while (0)
328
329
330 static INLINE int ifloor(float f)
331 {
332 int ai, bi;
333 double af, bf;
334 union fi u;
335
336 af = (3 << 22) + 0.5 + (double)f;
337 bf = (3 << 22) + 0.5 - (double)f;
338 u.f = (float) af; ai = u.i;
339 u.f = (float) bf; bi = u.i;
340 return (ai - bi) >> 1;
341 }
342
343
344 #if defined(__GNUC__) && defined(__i386__)
345 static INLINE int iround(float f)
346 {
347 int r;
348 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
349 return r;
350 }
351 #elif defined(__MSC__) && defined(__WIN32__)
352 static INLINE int iround(float f)
353 {
354 int r;
355 _asm {
356 fld f
357 fistp r
358 }
359 return r;
360 }
361 #else
362 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
363 #endif
364
365
366 /* Could maybe have an inline version of this?
367 */
368 #if defined(__GNUC__)
369 #define FABSF(x) fabsf(x)
370 #else
371 #define FABSF(x) ((float) fabs(x))
372 #endif
373
374 /* Pretty fast, and accurate.
375 * Based on code from http://www.flipcode.com/totd/
376 */
377 static INLINE float LOG2(float val)
378 {
379 union fi num;
380 int log_2;
381
382 num.f = val;
383 log_2 = ((num.i >> 23) & 255) - 128;
384 num.i &= ~(255 << 23);
385 num.i += 127 << 23;
386 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
387 return num.f + log_2;
388 }
389
390 #if defined(__GNUC__)
391 #define CEILF(x) ceilf(x)
392 #else
393 #define CEILF(x) ((float) ceil(x))
394 #endif
395
396 static INLINE int align(int value, int alignment)
397 {
398 return (value + alignment - 1) & ~(alignment - 1);
399 }
400
401
402 /* util/p_util.c
403 */
404 extern void pipe_copy_rect(ubyte * dst, unsigned cpp, unsigned dst_pitch,
405 unsigned dst_x, unsigned dst_y, unsigned width,
406 unsigned height, const ubyte * src,
407 int src_pitch, unsigned src_x, int src_y);
408
409
410
411 #if defined(_MSC_VER)
412 #if _MSC_VER < 1400 && !defined(__cplusplus)
413
414 static INLINE float cosf( float f )
415 {
416 return (float) cos( (double) f );
417 }
418
419 static INLINE float sinf( float f )
420 {
421 return (float) sin( (double) f );
422 }
423
424 static INLINE float ceilf( float f )
425 {
426 return (float) ceil( (double) f );
427 }
428
429 static INLINE float floorf( float f )
430 {
431 return (float) floor( (double) f );
432 }
433
434 static INLINE float powf( float f, float g )
435 {
436 return (float) pow( (double) f, (double) g );
437 }
438
439 static INLINE float sqrtf( float f )
440 {
441 return (float) sqrt( (double) f );
442 }
443
444 static INLINE float fabsf( float f )
445 {
446 return (float) fabs( (double) f );
447 }
448
449 static INLINE float logf( float f )
450 {
451 return (float) log( (double) f );
452 }
453
454 #else
455 /* Work-around an extra semi-colon in VS 2005 logf definition */
456 #ifdef logf
457 #undef logf
458 #define logf(x) ((float)log((double)(x)))
459 #endif /* logf */
460 #endif
461 #endif /* _MSC_VER */
462
463
464 #ifdef __cplusplus
465 }
466 #endif
467
468 #endif