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