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) /* memory debugging */
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 )
59 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
64 unsigned long MemSize
,
72 MALLOC( unsigned size
)
75 /* TODO: Need to abstract this */
76 return malloc( size
);
78 return EngAllocMem( 0, size
, 'D3AG' );
83 CALLOC( unsigned count
, unsigned size
)
85 void *ptr
= MALLOC( count
* size
);
87 memset( ptr
, 0, count
* size
);
97 /* TODO: Need to abstract this */
106 REALLOC( void *old_ptr
, unsigned old_size
, unsigned new_size
)
108 void *new_ptr
= NULL
;
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
);
122 #else /* !PIPE_SUBSYSTEM_WINDOWS_DISPLAY */
124 #define MALLOC( SIZE ) malloc( SIZE )
126 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
128 #define FREE( PTR ) free( PTR )
130 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
132 #endif /* !PIPE_SUBSYSTEM_WINDOWS_DISPLAY */
135 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
137 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
139 #define GETENV( X ) debug_get_option( X, NULL )
143 * Return memory on given byte alignment
146 align_malloc(size_t bytes
, uint alignment
)
148 #if defined(HAVE_POSIX_MEMALIGN)
150 (void) posix_memalign(& mem
, alignment
, bytes
);
155 assert( alignment
> 0 );
157 ptr
= (char *) MALLOC(bytes
+ alignment
+ sizeof(void *));
161 buf
= (char *) align_pointer( ptr
+ sizeof(void *), alignment
);
162 *(char **)(buf
- sizeof(void *)) = ptr
;
165 #endif /* defined(HAVE_POSIX_MEMALIGN) */
169 * Free memory returned by align_malloc().
172 align_free(void *ptr
)
174 #if defined(HAVE_POSIX_MEMALIGN)
177 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
178 void *realAddr
= *cubbyHole
;
180 #endif /* defined(HAVE_POSIX_MEMALIGN) */
186 * Duplicate a block of memory.
189 mem_dup(const void *src
, uint size
)
191 void *dup
= MALLOC(size
);
193 memcpy(dup
, src
, size
);
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) )
203 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
204 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
207 * Return a pointer aligned to next multiple of 16 bytes.
210 align16( void *unaligned
)
212 return align_pointer( unaligned
, 16 );
216 static INLINE
int align_int(int x
, int align
)
218 return (x
+ align
- 1) & ~(align
- 1);
223 #if defined(__MSC__) && defined(__WIN32__)
224 static INLINE
unsigned ffs( unsigned u
)
246 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
248 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
250 /* This function/macro is sensitive to precision. Test very carefully
253 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
259 else if (__tmp.i >= IEEE_0996) \
262 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
263 UB = (ubyte) __tmp.i; \
269 static INLINE
unsigned pack_ub4( unsigned char b0
,
274 return ((((unsigned int)b0
) << 0) |
275 (((unsigned int)b1
) << 8) |
276 (((unsigned int)b2
) << 16) |
277 (((unsigned int)b3
) << 24));
280 static INLINE
unsigned fui( float f
)
287 static INLINE
unsigned char float_to_ubyte( float f
)
290 UNCLAMPED_FLOAT_TO_UBYTE(ub
, f
);
294 static INLINE
unsigned pack_ui32_float4( float a
,
299 return pack_ub4( float_to_ubyte(a
),
305 #define COPY_4V( DST, SRC ) \
307 (DST)[0] = (SRC)[0]; \
308 (DST)[1] = (SRC)[1]; \
309 (DST)[2] = (SRC)[2]; \
310 (DST)[3] = (SRC)[3]; \
314 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
317 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
326 static INLINE
int ifloor(float f
)
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;
340 #if defined(__GNUC__) && defined(__i386__)
341 static INLINE
int iround(float f
)
344 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
347 #elif defined(__MSC__) && defined(__WIN32__)
348 static INLINE
int iround(float f
)
358 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
362 /* Could maybe have an inline version of this?
364 #if defined(__GNUC__)
365 #define FABSF(x) fabsf(x)
367 #define FABSF(x) ((float) fabs(x))
370 /* Pretty fast, and accurate.
371 * Based on code from http://www.flipcode.com/totd/
373 static INLINE
float LOG2(float val
)
379 log_2
= ((num
.i
>> 23) & 255) - 128;
380 num
.i
&= ~(255 << 23);
382 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
383 return num
.f
+ log_2
;
386 #if defined(__GNUC__)
387 #define CEILF(x) ceilf(x)
389 #define CEILF(x) ((float) ceil(x))
392 static INLINE
int align(int value
, int alignment
)
394 return (value
+ alignment
- 1) & ~(alignment
- 1);
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
);
409 #if !defined(_INC_MATH) || !defined(__cplusplus)
411 static INLINE
float cosf( float f
)
413 return (float) cos( (double) f
);
416 static INLINE
float sinf( float f
)
418 return (float) sin( (double) f
);
421 static INLINE
float ceilf( float f
)
423 return (float) ceil( (double) f
);
426 static INLINE
float floorf( float f
)
428 return (float) floor( (double) f
);
431 static INLINE
float powf( float f
, float g
)
433 return (float) pow( (double) f
, (double) g
);
436 static INLINE
float sqrtf( float f
)
438 return (float) sqrt( (double) f
);
441 static INLINE
float fabsf( float f
)
443 return (float) fabs( (double) f
);
446 static INLINE
float logf( float f
)
448 return (float) cos( (double) f
);
450 #endif /* _INC_MATH */