Merge branch 'gallium-tex-surfaces' into 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 #define GETENV( X ) debug_get_option( X, NULL )
144
145
146 /**
147 * Return memory on given byte alignment
148 */
149 static INLINE void *
150 align_malloc(size_t bytes, uint alignment)
151 {
152 #if defined(HAVE_POSIX_MEMALIGN)
153 void *mem;
154 (void) posix_memalign(& mem, alignment, bytes);
155 return mem;
156 #else
157 char *ptr, *buf;
158
159 assert( alignment > 0 );
160
161 ptr = (char *) MALLOC(bytes + alignment + sizeof(void *));
162 if (!ptr)
163 return NULL;
164
165 buf = (char *) align_pointer( ptr + sizeof(void *), alignment );
166 *(char **)(buf - sizeof(void *)) = ptr;
167
168 return buf;
169 #endif /* defined(HAVE_POSIX_MEMALIGN) */
170 }
171
172 /**
173 * Free memory returned by align_malloc().
174 */
175 static INLINE void
176 align_free(void *ptr)
177 {
178 #if defined(HAVE_POSIX_MEMALIGN)
179 FREE(ptr);
180 #else
181 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
182 void *realAddr = *cubbyHole;
183 FREE(realAddr);
184 #endif /* defined(HAVE_POSIX_MEMALIGN) */
185 }
186
187
188
189 /**
190 * Duplicate a block of memory.
191 */
192 static INLINE void *
193 mem_dup(const void *src, uint size)
194 {
195 void *dup = MALLOC(size);
196 if (dup)
197 memcpy(dup, src, size);
198 return dup;
199 }
200
201
202
203 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
204 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
205 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
206
207 #ifndef Elements
208 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
209 #endif
210 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
211
212 /**
213 * Return a pointer aligned to next multiple of 16 bytes.
214 */
215 static INLINE void *
216 align16( void *unaligned )
217 {
218 return align_pointer( unaligned, 16 );
219 }
220
221
222 static INLINE int align_int(int x, int align)
223 {
224 return (x + align - 1) & ~(align - 1);
225 }
226
227
228
229 #if defined(__MSC__) && defined(__WIN32__)
230 static INLINE unsigned ffs( unsigned u )
231 {
232 unsigned i;
233
234 if( u == 0 ) {
235 return 0;
236 }
237
238 __asm bsf eax, [u]
239 __asm inc eax
240 __asm mov [i], eax
241
242 return i;
243 }
244 #endif
245
246 union fi {
247 float f;
248 int i;
249 unsigned ui;
250 };
251
252 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
253
254 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
255
256 /* This function/macro is sensitive to precision. Test very carefully
257 * if you change it!
258 */
259 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
260 do { \
261 union fi __tmp; \
262 __tmp.f = (F); \
263 if (__tmp.i < 0) \
264 UB = (ubyte) 0; \
265 else if (__tmp.i >= IEEE_0996) \
266 UB = (ubyte) 255; \
267 else { \
268 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
269 UB = (ubyte) __tmp.i; \
270 } \
271 } while (0)
272
273
274
275 static INLINE unsigned pack_ub4( unsigned char b0,
276 unsigned char b1,
277 unsigned char b2,
278 unsigned char b3 )
279 {
280 return ((((unsigned int)b0) << 0) |
281 (((unsigned int)b1) << 8) |
282 (((unsigned int)b2) << 16) |
283 (((unsigned int)b3) << 24));
284 }
285
286 static INLINE unsigned fui( float f )
287 {
288 union fi fi;
289 fi.f = f;
290 return fi.ui;
291 }
292
293 static INLINE unsigned char float_to_ubyte( float f )
294 {
295 unsigned char ub;
296 UNCLAMPED_FLOAT_TO_UBYTE(ub, f);
297 return ub;
298 }
299
300 static INLINE unsigned pack_ui32_float4( float a,
301 float b,
302 float c,
303 float d )
304 {
305 return pack_ub4( float_to_ubyte(a),
306 float_to_ubyte(b),
307 float_to_ubyte(c),
308 float_to_ubyte(d) );
309 }
310
311 #define COPY_4V( DST, SRC ) \
312 do { \
313 (DST)[0] = (SRC)[0]; \
314 (DST)[1] = (SRC)[1]; \
315 (DST)[2] = (SRC)[2]; \
316 (DST)[3] = (SRC)[3]; \
317 } while (0)
318
319
320 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
321
322
323 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
324 do { \
325 (DST)[0] = (V0); \
326 (DST)[1] = (V1); \
327 (DST)[2] = (V2); \
328 (DST)[3] = (V3); \
329 } while (0)
330
331
332 static INLINE int ifloor(float f)
333 {
334 int ai, bi;
335 double af, bf;
336 union fi u;
337
338 af = (3 << 22) + 0.5 + (double)f;
339 bf = (3 << 22) + 0.5 - (double)f;
340 u.f = (float) af; ai = u.i;
341 u.f = (float) bf; bi = u.i;
342 return (ai - bi) >> 1;
343 }
344
345
346 #if defined(__GNUC__) && defined(__i386__)
347 static INLINE int iround(float f)
348 {
349 int r;
350 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
351 return r;
352 }
353 #elif defined(__MSC__) && defined(__WIN32__)
354 static INLINE int iround(float f)
355 {
356 int r;
357 _asm {
358 fld f
359 fistp r
360 }
361 return r;
362 }
363 #else
364 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
365 #endif
366
367
368 /* Could maybe have an inline version of this?
369 */
370 #if defined(__GNUC__)
371 #define FABSF(x) fabsf(x)
372 #else
373 #define FABSF(x) ((float) fabs(x))
374 #endif
375
376 /* Pretty fast, and accurate.
377 * Based on code from http://www.flipcode.com/totd/
378 */
379 static INLINE float LOG2(float val)
380 {
381 union fi num;
382 int log_2;
383
384 num.f = val;
385 log_2 = ((num.i >> 23) & 255) - 128;
386 num.i &= ~(255 << 23);
387 num.i += 127 << 23;
388 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
389 return num.f + log_2;
390 }
391
392 #if defined(__GNUC__)
393 #define CEILF(x) ceilf(x)
394 #else
395 #define CEILF(x) ((float) ceil(x))
396 #endif
397
398 static INLINE int align(int value, int alignment)
399 {
400 return (value + alignment - 1) & ~(alignment - 1);
401 }
402
403
404 /* util/p_util.c
405 */
406 extern void pipe_copy_rect(ubyte * dst, unsigned cpp, unsigned dst_pitch,
407 unsigned dst_x, unsigned dst_y, unsigned width,
408 unsigned height, const ubyte * src,
409 int src_pitch, unsigned src_x, int src_y);
410
411
412
413 #if defined(_MSC_VER)
414 #if _MSC_VER < 1400 && !defined(__cplusplus)
415
416 static INLINE float cosf( float f )
417 {
418 return (float) cos( (double) f );
419 }
420
421 static INLINE float sinf( float f )
422 {
423 return (float) sin( (double) f );
424 }
425
426 static INLINE float ceilf( float f )
427 {
428 return (float) ceil( (double) f );
429 }
430
431 static INLINE float floorf( float f )
432 {
433 return (float) floor( (double) f );
434 }
435
436 static INLINE float powf( float f, float g )
437 {
438 return (float) pow( (double) f, (double) g );
439 }
440
441 static INLINE float sqrtf( float f )
442 {
443 return (float) sqrt( (double) f );
444 }
445
446 static INLINE float fabsf( float f )
447 {
448 return (float) fabs( (double) f );
449 }
450
451 static INLINE float logf( float f )
452 {
453 return (float) log( (double) f );
454 }
455
456 #else
457 /* Work-around an extra semi-colon in VS 2005 logf definition */
458 #ifdef logf
459 #undef logf
460 #define logf(x) ((float)log((double)(x)))
461 #endif /* logf */
462 #endif
463 #endif /* _MSC_VER */
464
465
466 #ifdef __cplusplus
467 }
468 #endif
469
470 #endif