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"
46 unsigned long MemSize
,
58 MALLOC( unsigned size
)
60 return EngAllocMem( 0, size
, 'D3AG' );
64 CALLOC( unsigned count
, unsigned size
)
66 void *ptr
= MALLOC( count
* size
);
68 memset( ptr
, 0, count
* size
);
82 REALLOC( void *old_ptr
, unsigned old_size
, unsigned new_size
)
85 if( new_size
<= old_size
) {
88 new_ptr
= MALLOC( new_size
);
90 memcpy( new_ptr
, old_ptr
, old_size
);
96 #define GETENV( X ) NULL
100 #define MALLOC( SIZE ) malloc( SIZE )
102 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
104 #define FREE( PTR ) free( PTR )
106 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
108 #define GETENV( X ) getenv( X )
112 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
117 * Return memory on given byte alignment
120 align_malloc(size_t bytes
, uint alignment
)
122 #if defined(HAVE_POSIX_MEMALIGN)
124 (void) posix_memalign(& mem
, alignment
, bytes
);
129 assert( alignment
> 0 );
131 ptr
= (uintptr_t) MALLOC(bytes
+ alignment
+ sizeof(void *));
135 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
136 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
139 #endif /* defined(HAVE_POSIX_MEMALIGN) */
143 * Free memory returned by align_malloc().
146 align_free(void *ptr
)
148 #if defined(HAVE_POSIX_MEMALIGN)
151 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
152 void *realAddr
= *cubbyHole
;
154 #endif /* defined(HAVE_POSIX_MEMALIGN) */
159 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
160 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
161 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
163 #define Elements(x) sizeof(x)/sizeof(*(x))
164 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
167 * Return a pointer aligned to next multiple of 16 bytes.
170 align16( void *unaligned
)
172 if (sizeof(void *) == 64) {
178 pu
.u
= (pu
.u
+ 15) & ~15;
182 /* 32-bit pointers */
188 pu
.u
= (pu
.u
+ 15) & ~15;
194 #if defined(__MSC__) && defined(__WIN32__)
195 static INLINE
unsigned ffs( unsigned u
)
217 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
219 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
221 /* This function/macro is sensitive to precision. Test very carefully
224 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
230 else if (__tmp.i >= IEEE_0996) \
233 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
234 UB = (ubyte) __tmp.i; \
240 static INLINE
unsigned pack_ub4( unsigned char b0
,
245 return ((((unsigned int)b0
) << 0) |
246 (((unsigned int)b1
) << 8) |
247 (((unsigned int)b2
) << 16) |
248 (((unsigned int)b3
) << 24));
251 static INLINE
unsigned fui( float f
)
258 static INLINE
unsigned char float_to_ubyte( float f
)
261 UNCLAMPED_FLOAT_TO_UBYTE(ub
, f
);
265 static INLINE
unsigned pack_ui32_float4( float a
,
270 return pack_ub4( float_to_ubyte(a
),
276 #define COPY_4V( DST, SRC ) \
278 (DST)[0] = (SRC)[0]; \
279 (DST)[1] = (SRC)[1]; \
280 (DST)[2] = (SRC)[2]; \
281 (DST)[3] = (SRC)[3]; \
285 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
288 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
297 static INLINE
int ifloor(float f
)
303 af
= (3 << 22) + 0.5 + (double)f
;
304 bf
= (3 << 22) + 0.5 - (double)f
;
305 u
.f
= (float) af
; ai
= u
.i
;
306 u
.f
= (float) bf
; bi
= u
.i
;
307 return (ai
- bi
) >> 1;
311 #if defined(__GNUC__) && defined(__i386__)
312 static INLINE
int iround(float f
)
315 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
318 #elif defined(__MSC__) && defined(__WIN32__)
319 static INLINE
int iround(float f
)
329 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
333 /* Could maybe have an inline version of this?
335 #if defined(__GNUC__)
336 #define FABSF(x) fabsf(x)
338 #define FABSF(x) ((float) fabs(x))
341 /* Pretty fast, and accurate.
342 * Based on code from http://www.flipcode.com/totd/
344 static INLINE
float LOG2(float val
)
350 log_2
= ((num
.i
>> 23) & 255) - 128;
351 num
.i
&= ~(255 << 23);
353 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
354 return num
.f
+ log_2
;
357 #if defined(__GNUC__)
358 #define CEILF(x) ceilf(x)
360 #define CEILF(x) ((float) ceil(x))
363 static INLINE
int align(int value
, int alignment
)
365 return (value
+ alignment
- 1) & ~(alignment
- 1);
370 extern void _mesa_printf(const char *str
, ...);