softpipe: shortcircuit repeated lookups of the same tile
[mesa.git] / src / gallium / auxiliary / util / u_math.h
1 /**************************************************************************
2 *
3 * Copyright 2008 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
29 /**
30 * Math utilities and approximations for common math functions.
31 * Reduced precision is usually acceptable in shaders...
32 *
33 * "fast" is used in the names of functions which are low-precision,
34 * or at least lower-precision than the normal C lib functions.
35 */
36
37
38 #ifndef U_MATH_H
39 #define U_MATH_H
40
41
42 #include "pipe/p_compiler.h"
43 #include "util/u_debug.h"
44
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50
51 #if defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
52 __inline double ceil(double val)
53 {
54 double ceil_val;
55
56 if((val - (long) val) == 0) {
57 ceil_val = val;
58 }
59 else {
60 if(val > 0) {
61 ceil_val = (long) val + 1;
62 }
63 else {
64 ceil_val = (long) val;
65 }
66 }
67
68 return ceil_val;
69 }
70
71 #ifndef PIPE_SUBSYSTEM_WINDOWS_CE_OGL
72 __inline double floor(double val)
73 {
74 double floor_val;
75
76 if((val - (long) val) == 0) {
77 floor_val = val;
78 }
79 else {
80 if(val > 0) {
81 floor_val = (long) val;
82 }
83 else {
84 floor_val = (long) val - 1;
85 }
86 }
87
88 return floor_val;
89 }
90 #endif
91
92 #pragma function(pow)
93 __inline double __cdecl pow(double val, double exponent)
94 {
95 /* XXX */
96 assert(0);
97 return 0;
98 }
99
100 #pragma function(log)
101 __inline double __cdecl log(double val)
102 {
103 /* XXX */
104 assert(0);
105 return 0;
106 }
107
108 #pragma function(atan2)
109 __inline double __cdecl atan2(double val)
110 {
111 /* XXX */
112 assert(0);
113 return 0;
114 }
115 #else
116 #include <math.h>
117 #include <stdarg.h>
118 #endif
119
120
121 #if defined(_MSC_VER)
122
123 #if _MSC_VER < 1400 && !defined(__cplusplus) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
124
125 static INLINE float cosf( float f )
126 {
127 return (float) cos( (double) f );
128 }
129
130 static INLINE float sinf( float f )
131 {
132 return (float) sin( (double) f );
133 }
134
135 static INLINE float ceilf( float f )
136 {
137 return (float) ceil( (double) f );
138 }
139
140 static INLINE float floorf( float f )
141 {
142 return (float) floor( (double) f );
143 }
144
145 static INLINE float powf( float f, float g )
146 {
147 return (float) pow( (double) f, (double) g );
148 }
149
150 static INLINE float sqrtf( float f )
151 {
152 return (float) sqrt( (double) f );
153 }
154
155 static INLINE float fabsf( float f )
156 {
157 return (float) fabs( (double) f );
158 }
159
160 static INLINE float logf( float f )
161 {
162 return (float) log( (double) f );
163 }
164
165 #else
166 /* Work-around an extra semi-colon in VS 2005 logf definition */
167 #ifdef logf
168 #undef logf
169 #define logf(x) ((float)log((double)(x)))
170 #endif /* logf */
171 #endif
172
173 static INLINE double log2( double x )
174 {
175 const double invln2 = 1.442695041;
176 return log( x ) * invln2;
177 }
178
179 #endif /* _MSC_VER */
180
181
182
183
184
185 #define POW2_TABLE_SIZE_LOG2 9
186 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
187 #define POW2_TABLE_OFFSET (POW2_TABLE_SIZE/2)
188 #define POW2_TABLE_SCALE ((float)(POW2_TABLE_SIZE/2))
189 extern float pow2_table[POW2_TABLE_SIZE];
190
191
192
193 extern void
194 util_init_math(void);
195
196
197 union fi {
198 float f;
199 int32_t i;
200 uint32_t ui;
201 };
202
203
204 /**
205 * Fast version of 2^x
206 * Identity: exp2(a + b) = exp2(a) * exp2(b)
207 * Let ipart = int(x)
208 * Let fpart = x - ipart;
209 * So, exp2(x) = exp2(ipart) * exp2(fpart)
210 * Compute exp2(ipart) with i << ipart
211 * Compute exp2(fpart) with lookup table.
212 */
213 static INLINE float
214 util_fast_exp2(float x)
215 {
216 int32_t ipart;
217 float fpart, mpart;
218 union fi epart;
219
220 if(x > 129.00000f)
221 return 3.402823466e+38f;
222
223 if(x < -126.99999f)
224 return 0.0f;
225
226 ipart = (int32_t) x;
227 fpart = x - (float) ipart;
228
229 /* same as
230 * epart.f = (float) (1 << ipart)
231 * but faster and without integer overflow for ipart > 31 */
232 epart.i = (ipart + 127 ) << 23;
233
234 mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
235
236 return epart.f * mpart;
237 }
238
239
240 /**
241 * Fast approximation to exp(x).
242 */
243 static INLINE float
244 util_fast_exp(float x)
245 {
246 const float k = 1.44269f; /* = log2(e) */
247 return util_fast_exp2(k * x);
248 }
249
250
251 #define LOG2_TABLE_SIZE_LOG2 16
252 #define LOG2_TABLE_SCALE (1 << LOG2_TABLE_SIZE_LOG2)
253 #define LOG2_TABLE_SIZE (LOG2_TABLE_SCALE + 1)
254 extern float log2_table[LOG2_TABLE_SIZE];
255
256
257 static INLINE float
258 util_fast_log2(float x)
259 {
260 union fi num;
261 float epart, mpart;
262 num.f = x;
263 epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
264 /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
265 mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
266 return epart + mpart;
267 }
268
269
270 static INLINE float
271 util_fast_pow(float x, float y)
272 {
273 return util_fast_exp2(util_fast_log2(x) * y);
274 }
275
276
277
278 /**
279 * Floor(x), returned as int.
280 */
281 static INLINE int
282 util_ifloor(float f)
283 {
284 int ai, bi;
285 double af, bf;
286 union fi u;
287 af = (3 << 22) + 0.5 + (double)f;
288 bf = (3 << 22) + 0.5 - (double)f;
289 u.f = (float) af; ai = u.i;
290 u.f = (float) bf; bi = u.i;
291 return (ai - bi) >> 1;
292 }
293
294
295 /**
296 * Round float to nearest int.
297 */
298 static INLINE int
299 util_iround(float f)
300 {
301 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
302 int r;
303 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
304 return r;
305 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
306 int r;
307 _asm {
308 fld f
309 fistp r
310 }
311 return r;
312 #else
313 if (f >= 0.0f)
314 return (int) (f + 0.5f);
315 else
316 return (int) (f - 0.5f);
317 #endif
318 }
319
320
321
322 /**
323 * Test if x is NaN or +/- infinity.
324 */
325 static INLINE boolean
326 util_is_inf_or_nan(float x)
327 {
328 union fi tmp;
329 tmp.f = x;
330 return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
331 }
332
333
334 /**
335 * Find first bit set in word. Least significant bit is 1.
336 * Return 0 if no bits set.
337 */
338 #if defined(_MSC_VER) && _MSC_VER >= 1300
339 static INLINE
340 unsigned long ffs( unsigned long u )
341 {
342 unsigned long i;
343 if(_BitScanForward(&i, u))
344 return i + 1;
345 else
346 return 0;
347 }
348 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
349 static INLINE
350 unsigned ffs( unsigned u )
351 {
352 unsigned i;
353
354 if( u == 0 ) {
355 return 0;
356 }
357
358 __asm bsf eax, [u]
359 __asm inc eax
360 __asm mov [i], eax
361
362 return i;
363 }
364 #elif defined(__MINGW32__)
365 #define ffs __builtin_ffs
366 #endif
367
368
369 /* Could also binary search for the highest bit.
370 */
371 static INLINE unsigned
372 util_unsigned_logbase2(unsigned n)
373 {
374 unsigned log2 = 0;
375 while (n >>= 1)
376 ++log2;
377 return log2;
378 }
379
380
381 /**
382 * Return float bits.
383 */
384 static INLINE unsigned
385 fui( float f )
386 {
387 union fi fi;
388 fi.f = f;
389 return fi.ui;
390 }
391
392
393
394 static INLINE float
395 ubyte_to_float(ubyte ub)
396 {
397 return (float) ub * (1.0f / 255.0f);
398 }
399
400
401 /**
402 * Convert float in [0,1] to ubyte in [0,255] with clamping.
403 */
404 static INLINE ubyte
405 float_to_ubyte(float f)
406 {
407 const int ieee_0996 = 0x3f7f0000; /* 0.996 or so */
408 union fi tmp;
409
410 tmp.f = f;
411 if (tmp.i < 0) {
412 return (ubyte) 0;
413 }
414 else if (tmp.i >= ieee_0996) {
415 return (ubyte) 255;
416 }
417 else {
418 tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
419 return (ubyte) tmp.i;
420 }
421 }
422
423
424
425 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
426
427 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
428 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
429
430
431 static INLINE int
432 align(int value, int alignment)
433 {
434 return (value + alignment - 1) & ~(alignment - 1);
435 }
436
437
438 #ifndef COPY_4V
439 #define COPY_4V( DST, SRC ) \
440 do { \
441 (DST)[0] = (SRC)[0]; \
442 (DST)[1] = (SRC)[1]; \
443 (DST)[2] = (SRC)[2]; \
444 (DST)[3] = (SRC)[3]; \
445 } while (0)
446 #endif
447
448
449 #ifndef COPY_4FV
450 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
451 #endif
452
453
454 #ifndef ASSIGN_4V
455 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
456 do { \
457 (DST)[0] = (V0); \
458 (DST)[1] = (V1); \
459 (DST)[2] = (V2); \
460 (DST)[3] = (V3); \
461 } while (0)
462 #endif
463
464
465 #ifdef __cplusplus
466 }
467 #endif
468
469 #endif /* U_MATH_H */