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 **************************************************************************/
31 #include "p_compiler.h"
33 #include "p_pointer.h"
43 #if defined(WIN32) && defined(DEBUG) /* memory debugging */
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 )
63 unsigned long MemSize
,
71 MALLOC( unsigned size
)
74 /* TODO: Need to abstract this */
75 return malloc( size
);
77 return EngAllocMem( 0, size
, 'D3AG' );
82 CALLOC( unsigned count
, unsigned size
)
84 void *ptr
= MALLOC( count
* size
);
86 memset( ptr
, 0, count
* size
);
96 /* TODO: Need to abstract this */
105 REALLOC( void *old_ptr
, unsigned old_size
, unsigned new_size
)
107 void *new_ptr
= NULL
;
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
);
123 #define MALLOC( SIZE ) malloc( SIZE )
125 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
127 #define FREE( PTR ) free( PTR )
129 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
134 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
136 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
138 #define GETENV( X ) debug_get_option( X, NULL )
142 * Return memory on given byte alignment
145 align_malloc(size_t bytes
, uint alignment
)
147 #if defined(HAVE_POSIX_MEMALIGN)
149 (void) posix_memalign(& mem
, alignment
, bytes
);
154 assert( alignment
> 0 );
156 ptr
= (char *) MALLOC(bytes
+ alignment
+ sizeof(void *));
160 buf
= (char *) align_pointer( ptr
+ sizeof(void *), alignment
);
161 *(char **)(buf
- sizeof(void *)) = ptr
;
164 #endif /* defined(HAVE_POSIX_MEMALIGN) */
168 * Free memory returned by align_malloc().
171 align_free(void *ptr
)
173 #if defined(HAVE_POSIX_MEMALIGN)
176 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
177 void *realAddr
= *cubbyHole
;
179 #endif /* defined(HAVE_POSIX_MEMALIGN) */
185 * Duplicate a block of memory.
188 mem_dup(const void *src
, uint size
)
190 void *dup
= MALLOC(size
);
192 memcpy(dup
, src
, size
);
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) )
202 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
203 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
206 * Return a pointer aligned to next multiple of 16 bytes.
209 align16( void *unaligned
)
211 return align_pointer( unaligned
, 16 );
215 static INLINE
int align_int(int x
, int align
)
217 return (x
+ align
- 1) & ~(align
- 1);
222 #if defined(__MSC__) && defined(__WIN32__)
223 static INLINE
unsigned ffs( unsigned u
)
245 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
247 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
249 /* This function/macro is sensitive to precision. Test very carefully
252 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
258 else if (__tmp.i >= IEEE_0996) \
261 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
262 UB = (ubyte) __tmp.i; \
268 static INLINE
unsigned pack_ub4( unsigned char b0
,
273 return ((((unsigned int)b0
) << 0) |
274 (((unsigned int)b1
) << 8) |
275 (((unsigned int)b2
) << 16) |
276 (((unsigned int)b3
) << 24));
279 static INLINE
unsigned fui( float f
)
286 static INLINE
unsigned char float_to_ubyte( float f
)
289 UNCLAMPED_FLOAT_TO_UBYTE(ub
, f
);
293 static INLINE
unsigned pack_ui32_float4( float a
,
298 return pack_ub4( float_to_ubyte(a
),
304 #define COPY_4V( DST, SRC ) \
306 (DST)[0] = (SRC)[0]; \
307 (DST)[1] = (SRC)[1]; \
308 (DST)[2] = (SRC)[2]; \
309 (DST)[3] = (SRC)[3]; \
313 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
316 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
325 static INLINE
int ifloor(float f
)
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;
339 #if defined(__GNUC__) && defined(__i386__)
340 static INLINE
int iround(float f
)
343 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
346 #elif defined(__MSC__) && defined(__WIN32__)
347 static INLINE
int iround(float f
)
357 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
361 /* Could maybe have an inline version of this?
363 #if defined(__GNUC__)
364 #define FABSF(x) fabsf(x)
366 #define FABSF(x) ((float) fabs(x))
369 /* Pretty fast, and accurate.
370 * Based on code from http://www.flipcode.com/totd/
372 static INLINE
float LOG2(float val
)
378 log_2
= ((num
.i
>> 23) & 255) - 128;
379 num
.i
&= ~(255 << 23);
381 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
382 return num
.f
+ log_2
;
385 #if defined(__GNUC__)
386 #define CEILF(x) ceilf(x)
388 #define CEILF(x) ((float) ceil(x))
391 static INLINE
int align(int value
, int alignment
)
393 return (value
+ alignment
- 1) & ~(alignment
- 1);
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
);
408 #if !defined(_INC_MATH) || !defined(__cplusplus)
410 static INLINE
float cosf( float f
)
412 return (float) cos( (double) f
);
415 static INLINE
float sinf( float f
)
417 return (float) sin( (double) f
);
420 static INLINE
float ceilf( float f
)
422 return (float) ceil( (double) f
);
425 static INLINE
float floorf( float f
)
427 return (float) floor( (double) f
);
430 static INLINE
float powf( float f
, float g
)
432 return (float) pow( (double) f
, (double) g
);
435 static INLINE
float sqrtf( float f
)
437 return (float) sqrt( (double) f
);
440 static INLINE
float fabsf( float f
)
442 return (float) fabs( (double) f
);
445 static INLINE
float logf( float f
)
447 return (float) cos( (double) f
);
449 #endif /* _INC_MATH */