gallium: WinCE portability fixes.
[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_compiler.h"
32 #include "p_debug.h"
33 #include "p_pointer.h"
34 #include <math.h>
35
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41
42 #ifdef WIN32
43
44 void * __stdcall
45 EngAllocMem(
46 unsigned long Flags,
47 unsigned long MemSize,
48 unsigned long Tag );
49
50 void __stdcall
51 EngFreeMem(
52 void *Mem );
53
54 static INLINE void *
55 MALLOC( unsigned size )
56 {
57 #ifdef WINCE
58 /* TODO: Need to abstract this */
59 return malloc( size );
60 #else
61 return EngAllocMem( 0, size, 'D3AG' );
62 #endif
63 }
64
65 static INLINE void *
66 CALLOC( unsigned count, unsigned size )
67 {
68 void *ptr = MALLOC( count * size );
69 if( ptr ) {
70 memset( ptr, 0, count * size );
71 }
72 return ptr;
73 }
74
75 static INLINE void
76 FREE( void *ptr )
77 {
78 if( ptr ) {
79 #ifdef WINCE
80 /* TODO: Need to abstract this */
81 free( ptr );
82 #else
83 EngFreeMem( ptr );
84 #endif
85 }
86 }
87
88 static INLINE void *
89 REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
90 {
91 void *new_ptr;
92 if( new_size <= old_size ) {
93 return old_ptr;
94 }
95 new_ptr = MALLOC( new_size );
96 if( new_ptr ) {
97 memcpy( new_ptr, old_ptr, old_size );
98 }
99 FREE( old_ptr );
100 return new_ptr;
101 }
102
103 #define GETENV( X ) NULL
104
105 #else /* WIN32 */
106
107 #define MALLOC( SIZE ) malloc( SIZE )
108
109 #define CALLOC( COUNT, SIZE ) calloc( COUNT, SIZE )
110
111 #define FREE( PTR ) free( PTR )
112
113 #define REALLOC( OLDPTR, OLDSIZE, NEWSIZE ) realloc( OLDPTR, NEWSIZE )
114
115 #define GETENV( X ) getenv( X )
116
117 #endif /* WIN32 */
118
119 #define MALLOC_STRUCT(T) (struct T *) MALLOC(sizeof(struct T))
120
121 #define CALLOC_STRUCT(T) (struct T *) CALLOC(1, sizeof(struct T))
122
123
124 /**
125 * Return memory on given byte alignment
126 */
127 static INLINE void *
128 align_malloc(size_t bytes, uint alignment)
129 {
130 #if defined(HAVE_POSIX_MEMALIGN)
131 void *mem;
132 (void) posix_memalign(& mem, alignment, bytes);
133 return mem;
134 #else
135 char *ptr, *buf;
136
137 assert( alignment > 0 );
138
139 ptr = (char *) MALLOC(bytes + alignment + sizeof(void *));
140 if (!ptr)
141 return NULL;
142
143 buf = (char *) align_pointer( ptr + sizeof(void *), alignment );
144 *(char **)(buf - sizeof(void *)) = ptr;
145
146 return buf;
147 #endif /* defined(HAVE_POSIX_MEMALIGN) */
148 }
149
150 /**
151 * Free memory returned by align_malloc().
152 */
153 static INLINE void
154 align_free(void *ptr)
155 {
156 #if defined(HAVE_POSIX_MEMALIGN)
157 FREE(ptr);
158 #else
159 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
160 void *realAddr = *cubbyHole;
161 FREE(realAddr);
162 #endif /* defined(HAVE_POSIX_MEMALIGN) */
163 }
164
165
166
167 /**
168 * Duplicate a block of memory.
169 */
170 static INLINE void *
171 mem_dup(const void *src, uint size)
172 {
173 void *dup = MALLOC(size);
174 if (dup)
175 memcpy(dup, src, size);
176 return dup;
177 }
178
179
180
181 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
182 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
183 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
184
185 #define Elements(x) sizeof(x)/sizeof(*(x))
186 #define Offset(TYPE, MEMBER) ((unsigned)&(((TYPE *)NULL)->MEMBER))
187
188 /**
189 * Return a pointer aligned to next multiple of 16 bytes.
190 */
191 static INLINE void *
192 align16( void *unaligned )
193 {
194 return align_pointer( unaligned, 16 );
195 }
196
197
198 static INLINE int align_int(int x, int align)
199 {
200 return (x + align - 1) & ~(align - 1);
201 }
202
203
204
205 #if defined(__MSC__) && defined(__WIN32__)
206 static INLINE unsigned ffs( unsigned u )
207 {
208 unsigned i;
209
210 if( u == 0 ) {
211 return 0;
212 }
213
214 __asm bsf eax, [u]
215 __asm inc eax
216 __asm mov [i], eax
217
218 return i;
219 }
220 #endif
221
222 union fi {
223 float f;
224 int i;
225 unsigned ui;
226 };
227
228 #define UBYTE_TO_FLOAT( ub ) ((float)(ub) / 255.0F)
229
230 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
231
232 /* This function/macro is sensitive to precision. Test very carefully
233 * if you change it!
234 */
235 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
236 do { \
237 union fi __tmp; \
238 __tmp.f = (F); \
239 if (__tmp.i < 0) \
240 UB = (ubyte) 0; \
241 else if (__tmp.i >= IEEE_0996) \
242 UB = (ubyte) 255; \
243 else { \
244 __tmp.f = __tmp.f * (255.0f/256.0f) + 32768.0f; \
245 UB = (ubyte) __tmp.i; \
246 } \
247 } while (0)
248
249
250
251 static INLINE unsigned pack_ub4( unsigned char b0,
252 unsigned char b1,
253 unsigned char b2,
254 unsigned char b3 )
255 {
256 return ((((unsigned int)b0) << 0) |
257 (((unsigned int)b1) << 8) |
258 (((unsigned int)b2) << 16) |
259 (((unsigned int)b3) << 24));
260 }
261
262 static INLINE unsigned fui( float f )
263 {
264 union fi fi;
265 fi.f = f;
266 return fi.ui;
267 }
268
269 static INLINE unsigned char float_to_ubyte( float f )
270 {
271 unsigned char ub;
272 UNCLAMPED_FLOAT_TO_UBYTE(ub, f);
273 return ub;
274 }
275
276 static INLINE unsigned pack_ui32_float4( float a,
277 float b,
278 float c,
279 float d )
280 {
281 return pack_ub4( float_to_ubyte(a),
282 float_to_ubyte(b),
283 float_to_ubyte(c),
284 float_to_ubyte(d) );
285 }
286
287 #define COPY_4V( DST, SRC ) \
288 do { \
289 (DST)[0] = (SRC)[0]; \
290 (DST)[1] = (SRC)[1]; \
291 (DST)[2] = (SRC)[2]; \
292 (DST)[3] = (SRC)[3]; \
293 } while (0)
294
295
296 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
297
298
299 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
300 do { \
301 (DST)[0] = (V0); \
302 (DST)[1] = (V1); \
303 (DST)[2] = (V2); \
304 (DST)[3] = (V3); \
305 } while (0)
306
307
308 static INLINE int ifloor(float f)
309 {
310 int ai, bi;
311 double af, bf;
312 union fi u;
313
314 af = (3 << 22) + 0.5 + (double)f;
315 bf = (3 << 22) + 0.5 - (double)f;
316 u.f = (float) af; ai = u.i;
317 u.f = (float) bf; bi = u.i;
318 return (ai - bi) >> 1;
319 }
320
321
322 #if defined(__GNUC__) && defined(__i386__)
323 static INLINE int iround(float f)
324 {
325 int r;
326 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
327 return r;
328 }
329 #elif defined(__MSC__) && defined(__WIN32__)
330 static INLINE int iround(float f)
331 {
332 int r;
333 _asm {
334 fld f
335 fistp r
336 }
337 return r;
338 }
339 #else
340 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
341 #endif
342
343
344 /* Could maybe have an inline version of this?
345 */
346 #if defined(__GNUC__)
347 #define FABSF(x) fabsf(x)
348 #else
349 #define FABSF(x) ((float) fabs(x))
350 #endif
351
352 /* Pretty fast, and accurate.
353 * Based on code from http://www.flipcode.com/totd/
354 */
355 static INLINE float LOG2(float val)
356 {
357 union fi num;
358 int log_2;
359
360 num.f = val;
361 log_2 = ((num.i >> 23) & 255) - 128;
362 num.i &= ~(255 << 23);
363 num.i += 127 << 23;
364 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
365 return num.f + log_2;
366 }
367
368 #if defined(__GNUC__)
369 #define CEILF(x) ceilf(x)
370 #else
371 #define CEILF(x) ((float) ceil(x))
372 #endif
373
374 static INLINE int align(int value, int alignment)
375 {
376 return (value + alignment - 1) & ~(alignment - 1);
377 }
378
379
380 /* util/p_util.c
381 */
382 extern void pipe_copy_rect(ubyte * dst, unsigned cpp, unsigned dst_pitch,
383 unsigned dst_x, unsigned dst_y, unsigned width,
384 unsigned height, const ubyte * src,
385 int src_pitch, unsigned src_x, int src_y);
386
387
388 #ifdef __cplusplus
389 }
390 #endif
391
392 #endif