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