1 /**************************************************************************
3 * Copyright 2008 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 **************************************************************************/
30 * Math utilities and approximations for common math functions.
31 * Reduced precision is usually acceptable in shaders...
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.
42 #include "pipe/p_compiler.h"
43 #include "pipe/p_debug.h"
51 #if defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
52 __inline
double ceil(double val
)
56 if((val
- (long) val
) == 0) {
61 ceil_val
= (long) val
+ 1;
64 ceil_val
= (long) val
;
71 #ifndef PIPE_SUBSYSTEM_WINDOWS_CE
72 __inline
double floor(double val
)
76 if((val
- (long) val
) == 0) {
81 floor_val
= (long) val
;
84 floor_val
= (long) val
- 1;
93 __inline
double __cdecl
pow(double val
, double exponent
)
100 #pragma function(log)
101 __inline
double __cdecl
log(double val
)
108 #pragma function(atan2)
109 __inline
double __cdecl
atan2(double val
)
121 #if defined(_MSC_VER)
122 #if _MSC_VER < 1400 && !defined(__cplusplus) || defined(PIPE_SUBSYSTEM_WINDOWS_CE)
124 static INLINE
float cosf( float f
)
126 return (float) cos( (double) f
);
129 static INLINE
float sinf( float f
)
131 return (float) sin( (double) f
);
134 static INLINE
float ceilf( float f
)
136 return (float) ceil( (double) f
);
139 static INLINE
float floorf( float f
)
141 return (float) floor( (double) f
);
144 static INLINE
float powf( float f
, float g
)
146 return (float) pow( (double) f
, (double) g
);
149 static INLINE
float sqrtf( float f
)
151 return (float) sqrt( (double) f
);
154 static INLINE
float fabsf( float f
)
156 return (float) fabs( (double) f
);
159 static INLINE
float logf( float f
)
161 return (float) log( (double) f
);
165 /* Work-around an extra semi-colon in VS 2005 logf definition */
168 #define logf(x) ((float)log((double)(x)))
171 #endif /* _MSC_VER */
177 #define POW2_TABLE_SIZE 256
178 #define POW2_TABLE_SCALE ((float) (POW2_TABLE_SIZE-1))
179 extern float pow2_table
[POW2_TABLE_SIZE
];
184 util_init_math(void);
195 * Fast approximation to exp(x).
196 * Compute with base 2 exponents: exp(x) = exp2(log2(e) * x)
197 * Note: log2(e) is a constant, k = 1.44269
198 * So, exp(x) = exp2(k * x);
199 * Identity: exp2(a + b) = exp2(a) * exp2(b)
200 * Let ipart = int(k*x)
201 * Let fpart = k*x - ipart;
202 * So, exp2(k*x) = exp2(ipart) * exp2(fpart)
203 * Compute exp2(ipart) with i << ipart
204 * Compute exp2(fpart) with lookup table.
207 util_fast_exp(float x
)
210 float k
= 1.44269f
; /* = log2(e) */
212 int ipart
= (int) kx
;
213 float fpart
= kx
- (float) ipart
;
214 float y
= (float) (1 << ipart
)
215 * pow2_table
[(int) (fpart
* POW2_TABLE_SCALE
)];
219 /* exp(-x) = 1.0 / exp(x) */
222 int ipart
= (int) kx
;
223 float fpart
= kx
- (float) ipart
;
224 float y
= (float) (1 << ipart
)
225 * pow2_table
[(int) (fpart
* POW2_TABLE_SCALE
)];
232 * Fast version of 2^x
233 * XXX the above function could be implemented in terms of this one.
236 util_fast_exp2(float x
)
240 float fpart
= x
- (float) ipart
;
241 float y
= (float) (1 << ipart
)
242 * pow2_table
[(int) (fpart
* POW2_TABLE_SCALE
)];
246 /* exp(-x) = 1.0 / exp(x) */
247 int ipart
= (int) -x
;
248 float fpart
= -x
- (float) ipart
;
249 float y
= (float) (1 << ipart
)
250 * pow2_table
[(int) (fpart
* POW2_TABLE_SCALE
)];
257 * Based on code from http://www.flipcode.com/totd/
260 util_fast_log2(float val
)
265 log_2
= ((num
.i
>> 23) & 255) - 128;
266 num
.i
&= ~(255 << 23);
268 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
269 return num
.f
+ log_2
;
274 util_fast_pow(float x
, float y
)
276 /* XXX these tests may need adjustment */
277 if (y
>= 3.0f
&& (-0.02f
<= x
&& x
<= 0.02f
))
279 if (y
>= 50.0f
&& (-0.9f
<= x
&& x
<= 0.9f
))
281 return util_fast_exp2(util_fast_log2(x
) * y
);
287 * Floor(x), returned as int.
295 af
= (3 << 22) + 0.5 + (double)f
;
296 bf
= (3 << 22) + 0.5 - (double)f
;
297 u
.f
= (float) af
; ai
= u
.i
;
298 u
.f
= (float) bf
; bi
= u
.i
;
299 return (ai
- bi
) >> 1;
304 * Round float to nearest int.
309 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
311 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
313 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
322 return (int) (f
+ 0.5f
);
324 return (int) (f
- 0.5f
);
330 #if defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
332 * Find first bit set in word. Least significant bit is 1.
333 * Return 0 if no bits set.
336 unsigned ffs( unsigned u
)
356 static INLINE
unsigned
367 ubyte_to_float(ubyte ub
)
369 return (float) ub
* (1.0f
/ 255.0f
);
374 * Convert float in [0,1] to ubyte in [0,255] with clamping.
377 float_to_ubyte(float f
)
379 const int ieee_0996
= 0x3f7f0000; /* 0.996 or so */
386 else if (tmp
.i
>= ieee_0996
) {
390 tmp
.f
= tmp
.f
* (255.0f
/256.0f
) + 32768.0f
;
391 return (ubyte
) tmp
.i
;
397 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
399 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
400 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
404 align(int value
, int alignment
)
406 return (value
+ alignment
- 1) & ~(alignment
- 1);
411 #define COPY_4V( DST, SRC ) \
413 (DST)[0] = (SRC)[0]; \
414 (DST)[1] = (SRC)[1]; \
415 (DST)[2] = (SRC)[2]; \
416 (DST)[3] = (SRC)[3]; \
422 #define COPY_4FV( DST, SRC ) COPY_4V(DST, SRC)
427 #define ASSIGN_4V( DST, V0, V1, V2, V3 ) \
441 #endif /* U_MATH_H */