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