eec413842fbca8db0fe8ad03fdd1eb13fbe4941d
[mesa.git] / src / gallium / include / pipe / p_util.h
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #ifndef P_UTIL_H
29 #define P_UTIL_H
30
31 #include "p_config.h"
32 #include "p_compiler.h"
33 #include "p_debug.h"
34 #include "p_format.h"
35 #include "p_pointer.h"
36
37 #if defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
38 __inline double ceil(double val)
39 {
40 double ceil_val;
41
42 if((val - (long) val) == 0) {
43 ceil_val = val;
44 } else {
45 if(val > 0) {
46 ceil_val = (long) val + 1;
47 } else {
48 ceil_val = (long) val;
49 }
50 }
51
52 return ceil_val;
53 }
54
55 #ifndef PIPE_SUBSYSTEM_WINDOWS_CE
56 __inline double floor(double val)
57 {
58 double floor_val;
59
60 if((val - (long) val) == 0) {
61 floor_val = val;
62 } else {
63 if(val > 0) {
64 floor_val = (long) val;
65 } else {
66 floor_val = (long) val - 1;
67 }
68 }
69
70 return floor_val;
71 }
72 #endif
73
74 #pragma function(pow)
75 __inline double __cdecl pow(double val, double exponent)
76 {
77 /* XXX */
78 assert(0);
79 return 0;
80 }
81
82 #pragma function(log)
83 __inline double __cdecl log(double val)
84 {
85 /* XXX */
86 assert(0);
87 return 0;
88 }
89
90 #pragma function(atan2)
91 __inline double __cdecl atan2(double val)
92 {
93 /* XXX */
94 assert(0);
95 return 0;
96 }
97 #else
98 #include <math.h>
99 #include <stdarg.h>
100 #endif
101
102
103 #ifdef __cplusplus
104 extern "C" {
105 #endif
106
107
108 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) && defined(DEBUG)
109
110 /* memory debugging */
111
112 #include "p_debug.h"
113
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 )
122
123 #elif defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
124
125 void * __stdcall
126 EngAllocMem(
127 unsigned long Flags,
128 unsigned long MemSize,
129 unsigned long Tag );
130
131 void __stdcall
132 EngFreeMem(
133 void *Mem );
134
135 #define MALLOC( _size ) EngAllocMem( 0, _size, 'D3AG' )
136 #define _FREE( _ptr ) EngFreeMem( _ptr )
137
138 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
139
140 void *
141 ExAllocatePool(
142 unsigned long PoolType,
143 size_t NumberOfBytes);
144
145 void
146 ExFreePool(void *P);
147
148 #define MALLOC(_size) ExAllocatePool(0, _size)
149 #define _FREE(_ptr) ExFreePool(_ptr)
150
151 #else
152
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 )
157
158 #endif
159
160
161 #ifndef CALLOC
162 static INLINE void *
163 CALLOC( unsigned count, unsigned size )
164 {
165 void *ptr = MALLOC( count * size );
166 if( ptr ) {
167 memset( ptr, 0, count * size );
168 }
169 return ptr;
170 }
171 #endif /* !CALLOC */
172
173 #ifndef FREE
174 static INLINE void
175 FREE( void *ptr )
176 {
177 if( ptr ) {
178 _FREE( ptr );
179 }
180 }
181 #endif /* !FREE */
182
183 #ifndef REALLOC
184 static INLINE void *
185 REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
186 {
187 void *new_ptr = NULL;
188
189 if (new_size != 0) {
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 );
194 }
195 }
196
197 FREE( old_ptr );
198 return new_ptr;
199 }
200 #endif /* !REALLOC */
201
202
203 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
204
205 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
206
207
208 /**
209 * Return memory on given byte alignment
210 */
211 static INLINE void *
212 align_malloc(size_t bytes, uint alignment)
213 {
214 #if defined(HAVE_POSIX_MEMALIGN)
215 void *mem;
216 (void) posix_memalign(& mem, alignment, bytes);
217 return mem;
218 #else
219 char *ptr, *buf;
220
221 assert( alignment > 0 );
222
223 ptr = (char *) MALLOC(bytes + alignment + sizeof(void *));
224 if (!ptr)
225 return NULL;
226
227 buf = (char *) align_pointer( ptr + sizeof(void *), alignment );
228 *(char **)(buf - sizeof(void *)) = ptr;
229
230 return buf;
231 #endif /* defined(HAVE_POSIX_MEMALIGN) */
232 }
233
234 /**
235 * Free memory returned by align_malloc().
236 */
237 static INLINE void
238 align_free(void *ptr)
239 {
240 #if defined(HAVE_POSIX_MEMALIGN)
241 FREE(ptr);
242 #else
243 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
244 void *realAddr = *cubbyHole;
245 FREE(realAddr);
246 #endif /* defined(HAVE_POSIX_MEMALIGN) */
247 }
248
249
250
251 /**
252 * Duplicate a block of memory.
253 */
254 static INLINE void *
255 mem_dup(const void *src, uint size)
256 {
257 void *dup = MALLOC(size);
258 if (dup)
259 memcpy(dup, src, size);
260 return dup;
261 }
262
263
264
265 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
266 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
267 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
268
269 #ifndef Elements
270 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
271 #endif
272 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
273
274 /**
275 * Return a pointer aligned to next multiple of 16 bytes.
276 */
277 static INLINE void *
278 align16( void *unaligned )
279 {
280 return align_pointer( unaligned, 16 );
281 }
282
283
284 static INLINE int align_int(int x, int align)
285 {
286 return (x + align - 1) & ~(align - 1);
287 }
288
289
290
291 #if defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
292 static INLINE unsigned ffs( unsigned u )
293 {
294 unsigned i;
295
296 if( u == 0 ) {
297 return 0;
298 }
299
300 __asm bsf eax, [u]
301 __asm inc eax
302 __asm mov [i], eax
303
304 return i;
305 }
306 #endif
307
308 union fi {
309 float f;
310 int i;
311 unsigned ui;
312 };
313
314 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
315
316 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
317
318 /* This function/macro is sensitive to precision. Test very carefully
319 * if you change it!
320 */
321 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
322 do { \
323 union fi __tmp; \
324 __tmp.f = (F); \
325 if (__tmp.i < 0) \
326 UB = (ubyte) 0; \
327 else if (__tmp.i >= IEEE_0996) \
328 UB = (ubyte) 255; \
329 else { \
330 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
331 UB = (ubyte) __tmp.i; \
332 } \
333 } while (0)
334
335
336
337 static INLINE unsigned pack_ub4( unsigned char b0,
338 unsigned char b1,
339 unsigned char b2,
340 unsigned char b3 )
341 {
342 return ((((unsigned int)b0) << 0) |
343 (((unsigned int)b1) << 8) |
344 (((unsigned int)b2) << 16) |
345 (((unsigned int)b3) << 24));
346 }
347
348 static INLINE unsigned fui( float f )
349 {
350 union fi fi;
351 fi.f = f;
352 return fi.ui;
353 }
354
355 static INLINE unsigned char float_to_ubyte( float f )
356 {
357 unsigned char ub;
358 UNCLAMPED_FLOAT_TO_UBYTE(ub, f);
359 return ub;
360 }
361
362 static INLINE unsigned pack_ui32_float4( float a,
363 float b,
364 float c,
365 float d )
366 {
367 return pack_ub4( float_to_ubyte(a),
368 float_to_ubyte(b),
369 float_to_ubyte(c),
370 float_to_ubyte(d) );
371 }
372
373 #define COPY_4V( DST, SRC ) \
374 do { \
375 (DST)[0] = (SRC)[0]; \
376 (DST)[1] = (SRC)[1]; \
377 (DST)[2] = (SRC)[2]; \
378 (DST)[3] = (SRC)[3]; \
379 } while (0)
380
381
382 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
383
384
385 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
386 do { \
387 (DST)[0] = (V0); \
388 (DST)[1] = (V1); \
389 (DST)[2] = (V2); \
390 (DST)[3] = (V3); \
391 } while (0)
392
393
394 static INLINE int ifloor(float f)
395 {
396 int ai, bi;
397 double af, bf;
398 union fi u;
399
400 af = (3 << 22) + 0.5 + (double)f;
401 bf = (3 << 22) + 0.5 - (double)f;
402 u.f = (float) af; ai = u.i;
403 u.f = (float) bf; bi = u.i;
404 return (ai - bi) >> 1;
405 }
406
407
408 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
409 static INLINE int iround(float f)
410 {
411 int r;
412 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
413 return r;
414 }
415 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
416 static INLINE int iround(float f)
417 {
418 int r;
419 _asm {
420 fld f
421 fistp r
422 }
423 return r;
424 }
425 #else
426 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
427 #endif
428
429
430 /* Could maybe have an inline version of this?
431 */
432 #if defined(__GNUC__)
433 #define FABSF(x) fabsf(x)
434 #else
435 #define FABSF(x) ((float) fabs(x))
436 #endif
437
438 /* Pretty fast, and accurate.
439 * Based on code from http://www.flipcode.com/totd/
440 */
441 static INLINE float LOG2(float val)
442 {
443 union fi num;
444 int log_2;
445
446 num.f = val;
447 log_2 = ((num.i >> 23) & 255) - 128;
448 num.i &= ~(255 << 23);
449 num.i += 127 << 23;
450 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
451 return num.f + log_2;
452 }
453
454 #if defined(__GNUC__)
455 #define CEILF(x) ceilf(x)
456 #else
457 #define CEILF(x) ((float) ceil(x))
458 #endif
459
460 static INLINE int align(int value, int alignment)
461 {
462 return (value + alignment - 1) & ~(alignment - 1);
463 }
464
465
466 /* util/p_util.c
467 */
468 extern void pipe_copy_rect(ubyte * dst, const struct pipe_format_block *block,
469 unsigned dst_stride, unsigned dst_x, unsigned dst_y,
470 unsigned width, unsigned height, const ubyte * src,
471 int src_stride, unsigned src_x, int src_y);
472
473 extern void
474 pipe_fill_rect(ubyte * dst, const struct pipe_format_block *block,
475 unsigned dst_stride, unsigned dst_x, unsigned dst_y,
476 unsigned width, unsigned height, uint32_t value);
477
478
479 #if defined(_MSC_VER)
480 #if _MSC_VER < 1400 && !defined(__cplusplus) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
481
482 static INLINE float cosf( float f )
483 {
484 return (float) cos( (double) f );
485 }
486
487 static INLINE float sinf( float f )
488 {
489 return (float) sin( (double) f );
490 }
491
492 static INLINE float ceilf( float f )
493 {
494 return (float) ceil( (double) f );
495 }
496
497 static INLINE float floorf( float f )
498 {
499 return (float) floor( (double) f );
500 }
501
502 static INLINE float powf( float f, float g )
503 {
504 return (float) pow( (double) f, (double) g );
505 }
506
507 static INLINE float sqrtf( float f )
508 {
509 return (float) sqrt( (double) f );
510 }
511
512 static INLINE float fabsf( float f )
513 {
514 return (float) fabs( (double) f );
515 }
516
517 static INLINE float logf( float f )
518 {
519 return (float) log( (double) f );
520 }
521
522 #else
523 /* Work-around an extra semi-colon in VS 2005 logf definition */
524 #ifdef logf
525 #undef logf
526 #define logf(x) ((float)log((double)(x)))
527 #endif /* logf */
528 #endif
529 #endif /* _MSC_VER */
530
531
532 #ifdef __cplusplus
533 }
534 #endif
535
536 #endif