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