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