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