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"
35 #include "p_pointer.h"
37 #if defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
38 __inline
double ceil(double val
)
42 if((val
- (long) val
) == 0) {
46 ceil_val
= (long) val
+ 1;
48 ceil_val
= (long) val
;
55 #ifndef PIPE_SUBSYSTEM_WINDOWS_CE
56 __inline
double floor(double val
)
60 if((val
- (long) val
) == 0) {
64 floor_val
= (long) val
;
66 floor_val
= (long) val
- 1;
75 __inline
double __cdecl
pow(double val
, double exponent
)
83 __inline
double __cdecl
log(double val
)
90 #pragma function(atan2)
91 __inline
double __cdecl
atan2(double val
)
108 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) && defined(DEBUG)
110 /* memory debugging */
114 #define MALLOC( _size ) \
115 debug_malloc( __FILE__, __LINE__, __FUNCTION__, _size )
116 #define CALLOC( _count, _size ) \
117 debug_calloc(__FILE__, __LINE__, __FUNCTION__, _count, _size )
118 #define FREE( _ptr ) \
119 debug_free( __FILE__, __LINE__, __FUNCTION__, _ptr )
120 #define REALLOC( _ptr, _old_size, _size ) \
121 debug_realloc( __FILE__, __LINE__, __FUNCTION__, _ptr, _old_size, _size )
123 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
128 unsigned long MemSize
,
135 #define MALLOC( _size ) EngAllocMem( 0, _size, 'D3AG' )
136 #define _FREE( _ptr ) EngFreeMem( _ptr )
138 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
142 unsigned long PoolType
,
143 size_t NumberOfBytes
);
148 #define MALLOC(_size) ExAllocatePool(0, _size)
149 #define _FREE(_ptr) ExFreePool(_ptr)
153 #define MALLOC( SIZE ) malloc( SIZE )
154 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
155 #define FREE( PTR ) free( PTR )
156 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
163 CALLOC( unsigned count
, unsigned size
)
165 void *ptr
= MALLOC( count
* size
);
167 memset( ptr
, 0, count
* size
);
185 REALLOC( void *old_ptr
, unsigned old_size
, unsigned new_size
)
187 void *new_ptr
= NULL
;
190 unsigned copy_size
= old_size
< new_size
? old_size
: new_size
;
191 new_ptr
= MALLOC( new_size
);
192 if (new_ptr
&& old_ptr
&& copy_size
) {
193 memcpy( new_ptr
, old_ptr
, copy_size
);
200 #endif /* !REALLOC */
203 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
205 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
209 * Return memory on given byte alignment
212 align_malloc(size_t bytes
, uint alignment
)
214 #if defined(HAVE_POSIX_MEMALIGN)
216 if(posix_memalign(& mem
, alignment
, bytes
) != 0)
222 assert( alignment
> 0 );
224 ptr
= (char *) MALLOC(bytes
+ alignment
+ sizeof(void *));
228 buf
= (char *) align_pointer( ptr
+ sizeof(void *), alignment
);
229 *(char **)(buf
- sizeof(void *)) = ptr
;
232 #endif /* defined(HAVE_POSIX_MEMALIGN) */
236 * Free memory returned by align_malloc().
239 align_free(void *ptr
)
241 #if defined(HAVE_POSIX_MEMALIGN)
244 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
245 void *realAddr
= *cubbyHole
;
247 #endif /* defined(HAVE_POSIX_MEMALIGN) */
253 * Duplicate a block of memory.
256 mem_dup(const void *src
, uint size
)
258 void *dup
= MALLOC(size
);
260 memcpy(dup
, src
, size
);
266 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
267 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
268 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
271 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
273 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
276 * Return a pointer aligned to next multiple of 16 bytes.
279 align16( void *unaligned
)
281 return align_pointer( unaligned
, 16 );
285 static INLINE
int align_int(int x
, int align
)
287 return (x
+ align
- 1) & ~(align
- 1);
292 #if defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
293 static INLINE
unsigned ffs( unsigned u
)
315 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
317 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
319 /* This function/macro is sensitive to precision. Test very carefully
322 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
328 else if (__tmp.i >= IEEE_0996) \
331 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
332 UB = (ubyte) __tmp.i; \
338 static INLINE
unsigned pack_ub4( unsigned char b0
,
343 return ((((unsigned int)b0
) << 0) |
344 (((unsigned int)b1
) << 8) |
345 (((unsigned int)b2
) << 16) |
346 (((unsigned int)b3
) << 24));
349 static INLINE
unsigned fui( float f
)
356 static INLINE
unsigned char float_to_ubyte( float f
)
359 UNCLAMPED_FLOAT_TO_UBYTE(ub
, f
);
363 static INLINE
unsigned pack_ui32_float4( float a
,
368 return pack_ub4( float_to_ubyte(a
),
374 #define COPY_4V( DST, SRC ) \
376 (DST)[0] = (SRC)[0]; \
377 (DST)[1] = (SRC)[1]; \
378 (DST)[2] = (SRC)[2]; \
379 (DST)[3] = (SRC)[3]; \
383 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
386 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
395 static INLINE
int ifloor(float f
)
401 af
= (3 << 22) + 0.5 + (double)f
;
402 bf
= (3 << 22) + 0.5 - (double)f
;
403 u
.f
= (float) af
; ai
= u
.i
;
404 u
.f
= (float) bf
; bi
= u
.i
;
405 return (ai
- bi
) >> 1;
409 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
410 static INLINE
int iround(float f
)
413 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
416 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
417 static INLINE
int iround(float f
)
427 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
431 /* Could maybe have an inline version of this?
433 #if defined(__GNUC__)
434 #define FABSF(x) fabsf(x)
436 #define FABSF(x) ((float) fabs(x))
439 /* Pretty fast, and accurate.
440 * Based on code from http://www.flipcode.com/totd/
442 static INLINE
float LOG2(float val
)
448 log_2
= ((num
.i
>> 23) & 255) - 128;
449 num
.i
&= ~(255 << 23);
451 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
452 return num
.f
+ log_2
;
455 #if defined(__GNUC__)
456 #define CEILF(x) ceilf(x)
458 #define CEILF(x) ((float) ceil(x))
461 static INLINE
int align(int value
, int alignment
)
463 return (value
+ alignment
- 1) & ~(alignment
- 1);
469 extern void pipe_copy_rect(ubyte
* dst
, const struct pipe_format_block
*block
,
470 unsigned dst_stride
, unsigned dst_x
, unsigned dst_y
,
471 unsigned width
, unsigned height
, const ubyte
* src
,
472 int src_stride
, unsigned src_x
, int src_y
);
475 pipe_fill_rect(ubyte
* dst
, const struct pipe_format_block
*block
,
476 unsigned dst_stride
, unsigned dst_x
, unsigned dst_y
,
477 unsigned width
, unsigned height
, uint32_t value
);
480 #if defined(_MSC_VER)
481 #if _MSC_VER < 1400 && !defined(__cplusplus) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
483 static INLINE
float cosf( float f
)
485 return (float) cos( (double) f
);
488 static INLINE
float sinf( float f
)
490 return (float) sin( (double) f
);
493 static INLINE
float ceilf( float f
)
495 return (float) ceil( (double) f
);
498 static INLINE
float floorf( float f
)
500 return (float) floor( (double) f
);
503 static INLINE
float powf( float f
, float g
)
505 return (float) pow( (double) f
, (double) g
);
508 static INLINE
float sqrtf( float f
)
510 return (float) sqrt( (double) f
);
513 static INLINE
float fabsf( float f
)
515 return (float) fabs( (double) f
);
518 static INLINE
float logf( float f
)
520 return (float) log( (double) f
);
524 /* Work-around an extra semi-colon in VS 2005 logf definition */
527 #define logf(x) ((float)log((double)(x)))
530 #endif /* _MSC_VER */