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"
45 unsigned long MemSize
,
57 MALLOC( unsigned size
)
59 return EngAllocMem( 0, size
, 'D3AG' );
63 CALLOC( unsigned count
, unsigned size
)
65 void *ptr
= MALLOC( count
* size
);
67 memset( ptr
, 0, count
* size
);
81 REALLOC( void *old_ptr
, unsigned old_size
, unsigned new_size
)
84 if( new_size
<= old_size
) {
87 new_ptr
= MALLOC( new_size
);
89 memcpy( new_ptr
, old_ptr
, old_size
);
95 #define GETENV( X ) NULL
99 #define MALLOC( SIZE ) malloc( SIZE )
101 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
103 #define FREE( PTR ) free( PTR )
105 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
107 #define GETENV( X ) getenv( X )
111 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
113 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
117 * Return a pointer aligned to next multiple of N bytes.
120 align_pointer( void *unaligned
, uint alignment
)
122 if (sizeof(void *) == 64) {
128 pu
.u
= (pu
.u
+ alignment
- 1) & ~(uint64
) (alignment
- 1);
132 /* 32-bit pointers */
138 pu
.u
= (pu
.u
+ alignment
- 1) & ~(alignment
- 1);
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) */
186 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
187 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
188 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
190 #define Elements(x) sizeof(x)/sizeof(*(x))
191 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
194 * Return a pointer aligned to next multiple of 16 bytes.
197 align16( void *unaligned
)
199 return align_pointer( unaligned
, 16 );
203 static INLINE
int align_int(int x
, int align
)
205 return (x
+ align
- 1) & ~(align
- 1);
210 #if defined(__MSC__) && defined(__WIN32__)
211 static INLINE
unsigned ffs( unsigned u
)
233 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
235 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
237 /* This function/macro is sensitive to precision. Test very carefully
240 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
246 else if (__tmp.i >= IEEE_0996) \
249 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
250 UB = (ubyte) __tmp.i; \
256 static INLINE
unsigned pack_ub4( unsigned char b0
,
261 return ((((unsigned int)b0
) << 0) |
262 (((unsigned int)b1
) << 8) |
263 (((unsigned int)b2
) << 16) |
264 (((unsigned int)b3
) << 24));
267 static INLINE
unsigned fui( float f
)
274 static INLINE
unsigned char float_to_ubyte( float f
)
277 UNCLAMPED_FLOAT_TO_UBYTE(ub
, f
);
281 static INLINE
unsigned pack_ui32_float4( float a
,
286 return pack_ub4( float_to_ubyte(a
),
292 #define COPY_4V( DST, SRC ) \
294 (DST)[0] = (SRC)[0]; \
295 (DST)[1] = (SRC)[1]; \
296 (DST)[2] = (SRC)[2]; \
297 (DST)[3] = (SRC)[3]; \
301 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
304 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
313 static INLINE
int ifloor(float f
)
319 af
= (3 << 22) + 0.5 + (double)f
;
320 bf
= (3 << 22) + 0.5 - (double)f
;
321 u
.f
= (float) af
; ai
= u
.i
;
322 u
.f
= (float) bf
; bi
= u
.i
;
323 return (ai
- bi
) >> 1;
327 #if defined(__GNUC__) && defined(__i386__)
328 static INLINE
int iround(float f
)
331 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
334 #elif defined(__MSC__) && defined(__WIN32__)
335 static INLINE
int iround(float f
)
345 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
349 /* Could maybe have an inline version of this?
351 #if defined(__GNUC__)
352 #define FABSF(x) fabsf(x)
354 #define FABSF(x) ((float) fabs(x))
357 /* Pretty fast, and accurate.
358 * Based on code from http://www.flipcode.com/totd/
360 static INLINE
float LOG2(float val
)
366 log_2
= ((num
.i
>> 23) & 255) - 128;
367 num
.i
&= ~(255 << 23);
369 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
370 return num
.f
+ log_2
;
373 #if defined(__GNUC__)
374 #define CEILF(x) ceilf(x)
376 #define CEILF(x) ((float) ceil(x))
379 static INLINE
int align(int value
, int alignment
)
381 return (value
+ alignment
- 1) & ~(alignment
- 1);
386 extern void _mesa_printf(const char *str
, ...);
391 extern void pipe_copy_rect(ubyte
* dst
, unsigned cpp
, unsigned dst_pitch
,
392 unsigned dst_x
, unsigned dst_y
, unsigned width
,
393 unsigned height
, const ubyte
* src
,
394 unsigned src_pitch
, unsigned src_x
, unsigned src_y
);