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