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"
42 #if defined(WIN32) && defined(DEBUG) /* memory debugging */
46 #define MALLOC( _size ) \
47 debug_malloc( __FILE__, __LINE__, __FUNCTION__, _size )
48 #define CALLOC( _count, _size ) \
49 debug_calloc(__FILE__, __LINE__, __FUNCTION__, _count, _size )
50 #define FREE( _ptr ) \
51 debug_free( __FILE__, __LINE__, __FUNCTION__, _ptr )
52 #define REALLOC( _ptr, _old_size, _size ) \
53 debug_realloc( __FILE__, __LINE__, __FUNCTION__, _ptr, _old_size, _size )
54 #define GETENV( X ) NULL
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
);
121 #define GETENV( X ) NULL
125 #define MALLOC( SIZE ) malloc( SIZE )
127 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
129 #define FREE( PTR ) free( PTR )
131 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
133 #define GETENV( X ) getenv( X )
138 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
140 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
144 * Return memory on given byte alignment
147 align_malloc(size_t bytes
, uint alignment
)
149 #if defined(HAVE_POSIX_MEMALIGN)
151 (void) posix_memalign(& mem
, alignment
, bytes
);
156 assert( alignment
> 0 );
158 ptr
= (char *) MALLOC(bytes
+ alignment
+ sizeof(void *));
162 buf
= (char *) align_pointer( ptr
+ sizeof(void *), alignment
);
163 *(char **)(buf
- sizeof(void *)) = ptr
;
166 #endif /* defined(HAVE_POSIX_MEMALIGN) */
170 * Free memory returned by align_malloc().
173 align_free(void *ptr
)
175 #if defined(HAVE_POSIX_MEMALIGN)
178 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
179 void *realAddr
= *cubbyHole
;
181 #endif /* defined(HAVE_POSIX_MEMALIGN) */
187 * Duplicate a block of memory.
190 mem_dup(const void *src
, uint size
)
192 void *dup
= MALLOC(size
);
194 memcpy(dup
, src
, size
);
200 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
201 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
202 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
204 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
205 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
208 * Return a pointer aligned to next multiple of 16 bytes.
211 align16( void *unaligned
)
213 return align_pointer( unaligned
, 16 );
217 static INLINE
int align_int(int x
, int align
)
219 return (x
+ align
- 1) & ~(align
- 1);
224 #if defined(__MSC__) && defined(__WIN32__)
225 static INLINE
unsigned ffs( unsigned u
)
247 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
249 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
251 /* This function/macro is sensitive to precision. Test very carefully
254 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
260 else if (__tmp.i >= IEEE_0996) \
263 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
264 UB = (ubyte) __tmp.i; \
270 static INLINE
unsigned pack_ub4( unsigned char b0
,
275 return ((((unsigned int)b0
) << 0) |
276 (((unsigned int)b1
) << 8) |
277 (((unsigned int)b2
) << 16) |
278 (((unsigned int)b3
) << 24));
281 static INLINE
unsigned fui( float f
)
288 static INLINE
unsigned char float_to_ubyte( float f
)
291 UNCLAMPED_FLOAT_TO_UBYTE(ub
, f
);
295 static INLINE
unsigned pack_ui32_float4( float a
,
300 return pack_ub4( float_to_ubyte(a
),
306 #define COPY_4V( DST, SRC ) \
308 (DST)[0] = (SRC)[0]; \
309 (DST)[1] = (SRC)[1]; \
310 (DST)[2] = (SRC)[2]; \
311 (DST)[3] = (SRC)[3]; \
315 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
318 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
327 static INLINE
int ifloor(float f
)
333 af
= (3 << 22) + 0.5 + (double)f
;
334 bf
= (3 << 22) + 0.5 - (double)f
;
335 u
.f
= (float) af
; ai
= u
.i
;
336 u
.f
= (float) bf
; bi
= u
.i
;
337 return (ai
- bi
) >> 1;
341 #if defined(__GNUC__) && defined(__i386__)
342 static INLINE
int iround(float f
)
345 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
348 #elif defined(__MSC__) && defined(__WIN32__)
349 static INLINE
int iround(float f
)
359 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
363 /* Could maybe have an inline version of this?
365 #if defined(__GNUC__)
366 #define FABSF(x) fabsf(x)
368 #define FABSF(x) ((float) fabs(x))
371 /* Pretty fast, and accurate.
372 * Based on code from http://www.flipcode.com/totd/
374 static INLINE
float LOG2(float val
)
380 log_2
= ((num
.i
>> 23) & 255) - 128;
381 num
.i
&= ~(255 << 23);
383 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
384 return num
.f
+ log_2
;
387 #if defined(__GNUC__)
388 #define CEILF(x) ceilf(x)
390 #define CEILF(x) ((float) ceil(x))
393 static INLINE
int align(int value
, int alignment
)
395 return (value
+ alignment
- 1) & ~(alignment
- 1);
401 extern void pipe_copy_rect(ubyte
* dst
, unsigned cpp
, unsigned dst_pitch
,
402 unsigned dst_x
, unsigned dst_y
, unsigned width
,
403 unsigned height
, const ubyte
* src
,
404 int src_pitch
, unsigned src_x
, int src_y
);