Merge branch 'gallium-i915-current' 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 /* JB stop warnings */
208 #ifndef Elements
209 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
210 #endif
211 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
212
213 /**
214 * Return a pointer aligned to next multiple of 16 bytes.
215 */
216 static INLINE void *
217 align16( void *unaligned )
218 {
219 return align_pointer( unaligned, 16 );
220 }
221
222
223 static INLINE int align_int(int x, int align)
224 {
225 return (x + align - 1) & ~(align - 1);
226 }
227
228
229
230 #if defined(__MSC__) && defined(__WIN32__)
231 static INLINE unsigned ffs( unsigned u )
232 {
233 unsigned i;
234
235 if( u == 0 ) {
236 return 0;
237 }
238
239 __asm bsf eax, [u]
240 __asm inc eax
241 __asm mov [i], eax
242
243 return i;
244 }
245 #endif
246
247 union fi {
248 float f;
249 int i;
250 unsigned ui;
251 };
252
253 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
254
255 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
256
257 /* This function/macro is sensitive to precision. Test very carefully
258 * if you change it!
259 */
260 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
261 do { \
262 union fi __tmp; \
263 __tmp.f = (F); \
264 if (__tmp.i < 0) \
265 UB = (ubyte) 0; \
266 else if (__tmp.i >= IEEE_0996) \
267 UB = (ubyte) 255; \
268 else { \
269 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
270 UB = (ubyte) __tmp.i; \
271 } \
272 } while (0)
273
274
275
276 static INLINE unsigned pack_ub4( unsigned char b0,
277 unsigned char b1,
278 unsigned char b2,
279 unsigned char b3 )
280 {
281 return ((((unsigned int)b0) << 0) |
282 (((unsigned int)b1) << 8) |
283 (((unsigned int)b2) << 16) |
284 (((unsigned int)b3) << 24));
285 }
286
287 static INLINE unsigned fui( float f )
288 {
289 union fi fi;
290 fi.f = f;
291 return fi.ui;
292 }
293
294 static INLINE unsigned char float_to_ubyte( float f )
295 {
296 unsigned char ub;
297 UNCLAMPED_FLOAT_TO_UBYTE(ub, f);
298 return ub;
299 }
300
301 static INLINE unsigned pack_ui32_float4( float a,
302 float b,
303 float c,
304 float d )
305 {
306 return pack_ub4( float_to_ubyte(a),
307 float_to_ubyte(b),
308 float_to_ubyte(c),
309 float_to_ubyte(d) );
310 }
311
312 #define COPY_4V( DST, SRC ) \
313 do { \
314 (DST)[0] = (SRC)[0]; \
315 (DST)[1] = (SRC)[1]; \
316 (DST)[2] = (SRC)[2]; \
317 (DST)[3] = (SRC)[3]; \
318 } while (0)
319
320
321 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
322
323
324 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
325 do { \
326 (DST)[0] = (V0); \
327 (DST)[1] = (V1); \
328 (DST)[2] = (V2); \
329 (DST)[3] = (V3); \
330 } while (0)
331
332
333 static INLINE int ifloor(float f)
334 {
335 int ai, bi;
336 double af, bf;
337 union fi u;
338
339 af = (3 << 22) + 0.5 + (double)f;
340 bf = (3 << 22) + 0.5 - (double)f;
341 u.f = (float) af; ai = u.i;
342 u.f = (float) bf; bi = u.i;
343 return (ai - bi) >> 1;
344 }
345
346
347 #if defined(__GNUC__) && defined(__i386__)
348 static INLINE int iround(float f)
349 {
350 int r;
351 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
352 return r;
353 }
354 #elif defined(__MSC__) && defined(__WIN32__)
355 static INLINE int iround(float f)
356 {
357 int r;
358 _asm {
359 fld f
360 fistp r
361 }
362 return r;
363 }
364 #else
365 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
366 #endif
367
368
369 /* Could maybe have an inline version of this?
370 */
371 #if defined(__GNUC__)
372 #define FABSF(x) fabsf(x)
373 #else
374 #define FABSF(x) ((float) fabs(x))
375 #endif
376
377 /* Pretty fast, and accurate.
378 * Based on code from http://www.flipcode.com/totd/
379 */
380 static INLINE float LOG2(float val)
381 {
382 union fi num;
383 int log_2;
384
385 num.f = val;
386 log_2 = ((num.i >> 23) & 255) - 128;
387 num.i &= ~(255 << 23);
388 num.i += 127 << 23;
389 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
390 return num.f + log_2;
391 }
392
393 #if defined(__GNUC__)
394 #define CEILF(x) ceilf(x)
395 #else
396 #define CEILF(x) ((float) ceil(x))
397 #endif
398
399 static INLINE int align(int value, int alignment)
400 {
401 return (value + alignment - 1) & ~(alignment - 1);
402 }
403
404
405 /* util/p_util.c
406 */
407 extern void pipe_copy_rect(ubyte * dst, unsigned cpp, unsigned dst_pitch,
408 unsigned dst_x, unsigned dst_y, unsigned width,
409 unsigned height, const ubyte * src,
410 int src_pitch, unsigned src_x, int src_y);
411
412
413
414 #ifdef WIN32
415
416 #if !defined(_INC_MATH) || !defined(__cplusplus)
417
418 static INLINE float cosf( float f )
419 {
420 return (float) cos( (double) f );
421 }
422
423 static INLINE float sinf( float f )
424 {
425 return (float) sin( (double) f );
426 }
427
428 static INLINE float ceilf( float f )
429 {
430 return (float) ceil( (double) f );
431 }
432
433 static INLINE float floorf( float f )
434 {
435 return (float) floor( (double) f );
436 }
437
438 static INLINE float powf( float f, float g )
439 {
440 return (float) pow( (double) f, (double) g );
441 }
442
443 static INLINE float sqrtf( float f )
444 {
445 return (float) sqrt( (double) f );
446 }
447
448 static INLINE float fabsf( float f )
449 {
450 return (float) fabs( (double) f );
451 }
452
453 static INLINE float logf( float f )
454 {
455 return (float) cos( (double) f );
456 }
457 #endif /* _INC_MATH */
458 #endif
459
460
461 #ifdef __cplusplus
462 }
463 #endif
464
465 #endif