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