1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
32 #include "p_compiler.h"
34 #include "p_pointer.h"
44 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) && defined(DEBUG)
46 /* memory debugging */
50 #define MALLOC( _size ) \
51 debug_malloc( __FILE__, __LINE__, __FUNCTION__, _size )
52 #define CALLOC( _count, _size ) \
53 debug_calloc(__FILE__, __LINE__, __FUNCTION__, _count, _size )
54 #define FREE( _ptr ) \
55 debug_free( __FILE__, __LINE__, __FUNCTION__, _ptr )
56 #define REALLOC( _ptr, _old_size, _size ) \
57 debug_realloc( __FILE__, __LINE__, __FUNCTION__, _ptr, _old_size, _size )
59 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
64 unsigned long MemSize
,
71 #define MALLOC( _size ) EngAllocMem( 0, _size, 'D3AG' )
72 #define _FREE( _ptr ) EngFreeMem( _ptr )
74 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
78 unsigned long PoolType
,
79 size_t NumberOfBytes
);
84 #define MALLOC(_size) ExAllocatePool(0, _size)
85 #define _FREE(_ptr) ExFreePool(_ptr)
89 #define MALLOC( SIZE ) malloc( SIZE )
90 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
91 #define FREE( PTR ) free( PTR )
92 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
99 CALLOC( unsigned count
, unsigned size
)
101 void *ptr
= MALLOC( count
* size
);
103 memset( ptr
, 0, count
* size
);
121 REALLOC( void *old_ptr
, unsigned old_size
, unsigned new_size
)
123 void *new_ptr
= NULL
;
126 unsigned copy_size
= old_size
< new_size
? old_size
: new_size
;
127 new_ptr
= MALLOC( new_size
);
128 if (new_ptr
&& old_ptr
&& copy_size
) {
129 memcpy( new_ptr
, old_ptr
, copy_size
);
136 #endif /* !REALLOC */
139 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
141 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
145 * Return memory on given byte alignment
148 align_malloc(size_t bytes
, uint alignment
)
150 #if defined(HAVE_POSIX_MEMALIGN)
152 (void) posix_memalign(& mem
, alignment
, bytes
);
157 assert( alignment
> 0 );
159 ptr
= (char *) MALLOC(bytes
+ alignment
+ sizeof(void *));
163 buf
= (char *) align_pointer( ptr
+ sizeof(void *), alignment
);
164 *(char **)(buf
- sizeof(void *)) = ptr
;
167 #endif /* defined(HAVE_POSIX_MEMALIGN) */
171 * Free memory returned by align_malloc().
174 align_free(void *ptr
)
176 #if defined(HAVE_POSIX_MEMALIGN)
179 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
180 void *realAddr
= *cubbyHole
;
182 #endif /* defined(HAVE_POSIX_MEMALIGN) */
188 * Duplicate a block of memory.
191 mem_dup(const void *src
, uint size
)
193 void *dup
= MALLOC(size
);
195 memcpy(dup
, src
, size
);
201 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
202 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
203 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
206 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
208 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
211 * Return a pointer aligned to next multiple of 16 bytes.
214 align16( void *unaligned
)
216 return align_pointer( unaligned
, 16 );
220 static INLINE
int align_int(int x
, int align
)
222 return (x
+ align
- 1) & ~(align
- 1);
227 #if defined(__MSC__) && defined(__WIN32__)
228 static INLINE
unsigned ffs( unsigned u
)
250 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
252 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
254 /* This function/macro is sensitive to precision. Test very carefully
257 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
263 else if (__tmp.i >= IEEE_0996) \
266 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
267 UB = (ubyte) __tmp.i; \
273 static INLINE
unsigned pack_ub4( unsigned char b0
,
278 return ((((unsigned int)b0
) << 0) |
279 (((unsigned int)b1
) << 8) |
280 (((unsigned int)b2
) << 16) |
281 (((unsigned int)b3
) << 24));
284 static INLINE
unsigned fui( float f
)
291 static INLINE
unsigned char float_to_ubyte( float f
)
294 UNCLAMPED_FLOAT_TO_UBYTE(ub
, f
);
298 static INLINE
unsigned pack_ui32_float4( float a
,
303 return pack_ub4( float_to_ubyte(a
),
309 #define COPY_4V( DST, SRC ) \
311 (DST)[0] = (SRC)[0]; \
312 (DST)[1] = (SRC)[1]; \
313 (DST)[2] = (SRC)[2]; \
314 (DST)[3] = (SRC)[3]; \
318 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
321 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
330 static INLINE
int ifloor(float f
)
336 af
= (3 << 22) + 0.5 + (double)f
;
337 bf
= (3 << 22) + 0.5 - (double)f
;
338 u
.f
= (float) af
; ai
= u
.i
;
339 u
.f
= (float) bf
; bi
= u
.i
;
340 return (ai
- bi
) >> 1;
344 #if defined(__GNUC__) && defined(__i386__)
345 static INLINE
int iround(float f
)
348 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
351 #elif defined(__MSC__) && defined(__WIN32__)
352 static INLINE
int iround(float f
)
362 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
366 /* Could maybe have an inline version of this?
368 #if defined(__GNUC__)
369 #define FABSF(x) fabsf(x)
371 #define FABSF(x) ((float) fabs(x))
374 /* Pretty fast, and accurate.
375 * Based on code from http://www.flipcode.com/totd/
377 static INLINE
float LOG2(float val
)
383 log_2
= ((num
.i
>> 23) & 255) - 128;
384 num
.i
&= ~(255 << 23);
386 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
387 return num
.f
+ log_2
;
390 #if defined(__GNUC__)
391 #define CEILF(x) ceilf(x)
393 #define CEILF(x) ((float) ceil(x))
396 static INLINE
int align(int value
, int alignment
)
398 return (value
+ alignment
- 1) & ~(alignment
- 1);
404 extern void pipe_copy_rect(ubyte
* dst
, unsigned cpp
, unsigned dst_pitch
,
405 unsigned dst_x
, unsigned dst_y
, unsigned width
,
406 unsigned height
, const ubyte
* src
,
407 int src_pitch
, unsigned src_x
, int src_y
);
411 #if defined(_MSC_VER)
412 #if _MSC_VER < 1400 && !defined(__cplusplus)
414 static INLINE
float cosf( float f
)
416 return (float) cos( (double) f
);
419 static INLINE
float sinf( float f
)
421 return (float) sin( (double) f
);
424 static INLINE
float ceilf( float f
)
426 return (float) ceil( (double) f
);
429 static INLINE
float floorf( float f
)
431 return (float) floor( (double) f
);
434 static INLINE
float powf( float f
, float g
)
436 return (float) pow( (double) f
, (double) g
);
439 static INLINE
float sqrtf( float f
)
441 return (float) sqrt( (double) f
);
444 static INLINE
float fabsf( float f
)
446 return (float) fabs( (double) f
);
449 static INLINE
float logf( float f
)
451 return (float) log( (double) f
);
455 /* Work-around an extra semi-colon in VS 2005 logf definition */
458 #define logf(x) ((float)log((double)(x)))
461 #endif /* _MSC_VER */