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