u_math.h File Reference

Include dependency graph for u_math.h:

Go to the source code of this file.

Data Structures

union  fi

Defines

#define POW2_TABLE_SIZE_LOG2   9
 Math utilities and approximations for common math functions.
#define POW2_TABLE_SIZE   (1 << POW2_TABLE_SIZE_LOG2)
#define POW2_TABLE_OFFSET   (POW2_TABLE_SIZE/2)
#define POW2_TABLE_SCALE   ((float)(POW2_TABLE_SIZE/2))
#define LOG2_TABLE_SIZE_LOG2   16
#define LOG2_TABLE_SCALE   (1 << LOG2_TABLE_SIZE_LOG2)
#define LOG2_TABLE_SIZE   (LOG2_TABLE_SCALE + 1)
#define CLAMP(X, MIN, MAX)   ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
#define MIN2(A, B)   ( (A)<(B) ? (A) : (B) )
#define MAX2(A, B)   ( (A)>(B) ? (A) : (B) )
#define COPY_4V(DST, SRC)
#define COPY_4FV(DST, SRC)   COPY_4V(DST, SRC)
#define ASSIGN_4V(DST, V0, V1, V2, V3)

Functions

void util_init_math (void)
 One time init for math utilities.
static float util_fast_exp2 (float x)
 Fast version of 2^x Identity: exp2(a + b) = exp2(a) * exp2(b) Let ipart = int(x) Let fpart = x - ipart; So, exp2(x) = exp2(ipart) * exp2(fpart) Compute exp2(ipart) with i << ipart Compute exp2(fpart) with lookup table.
static float util_fast_exp (float x)
 Fast approximation to exp(x).
static float util_fast_log2 (float x)
static float util_fast_pow (float x, float y)
static int util_ifloor (float f)
 Floor(x), returned as int.
static int util_iround (float f)
 Round float to nearest int.
static unsigned fui (float f)
 Return float bits.
static float ubyte_to_float (ubyte ub)
static ubyte float_to_ubyte (float f)
 Convert float in [0,1] to ubyte in [0,255] with clamping.
static int align (int value, int alignment)

Variables

float pow2_table [(1<< 9)]
 2^x, for x in [-1.0, 1.0)
float log2_table [((1<< 16)+1)]
 log2(x), for x in [1.0, 2.0)


Define Documentation

#define ASSIGN_4V ( DST,
V0,
V1,
V2,
V3   ) 

Value:

do {                                     \
   (DST)[0] = (V0);                      \
   (DST)[1] = (V1);                      \
   (DST)[2] = (V2);                      \
   (DST)[3] = (V3);                      \
} while (0)

Definition at line 421 of file u_math.h.

#define CLAMP ( X,
MIN,
MAX   )     ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )

Definition at line 391 of file u_math.h.

#define COPY_4FV ( DST,
SRC   )     COPY_4V(DST, SRC)

Definition at line 416 of file u_math.h.

#define COPY_4V ( DST,
SRC   ) 

Value:

do {                                \
   (DST)[0] = (SRC)[0];             \
   (DST)[1] = (SRC)[1];             \
   (DST)[2] = (SRC)[2];             \
   (DST)[3] = (SRC)[3];             \
} while (0)

Definition at line 405 of file u_math.h.

#define LOG2_TABLE_SCALE   (1 << LOG2_TABLE_SIZE_LOG2)

Definition at line 251 of file u_math.h.

#define LOG2_TABLE_SIZE   (LOG2_TABLE_SCALE + 1)

Definition at line 252 of file u_math.h.

#define LOG2_TABLE_SIZE_LOG2   16

Definition at line 250 of file u_math.h.

#define MAX2 ( A,
 )     ( (A)>(B) ? (A) : (B) )

Definition at line 394 of file u_math.h.

#define MIN2 ( A,
 )     ( (A)<(B) ? (A) : (B) )

Definition at line 393 of file u_math.h.

#define POW2_TABLE_OFFSET   (POW2_TABLE_SIZE/2)

Definition at line 186 of file u_math.h.

#define POW2_TABLE_SCALE   ((float)(POW2_TABLE_SIZE/2))

Definition at line 187 of file u_math.h.

#define POW2_TABLE_SIZE   (1 << POW2_TABLE_SIZE_LOG2)

Definition at line 185 of file u_math.h.

#define POW2_TABLE_SIZE_LOG2   9

Math utilities and approximations for common math functions.

Reduced precision is usually acceptable in shaders...

"fast" is used in the names of functions which are low-precision, or at least lower-precision than the normal C lib functions.

Definition at line 184 of file u_math.h.


Function Documentation

static int align ( int  value,
int  alignment 
) [static]

Definition at line 398 of file u_math.h.

00401 {

static ubyte float_to_ubyte ( float  f  )  [static]

Convert float in [0,1] to ubyte in [0,255] with clamping.

Definition at line 371 of file u_math.h.

00374 {
00375    const int ieee_0996 = 0x3f7f0000;   /* 0.996 or so */
00376    union fi tmp;
00377 
00378    tmp.f = f;
00379    if (tmp.i < 0) {
00380       return (ubyte) 0;
00381    }
00382    else if (tmp.i >= ieee_0996) {
00383       return (ubyte) 255;
00384    }
00385    else {
00386       tmp.f = tmp.f * (255.0f/256.0f) + 32768.0f;
00387       return (ubyte) tmp.i;

static unsigned fui ( float  f  )  [static]

Return float bits.

Definition at line 351 of file u_math.h.

00354 {
00355    union fi fi;
00356    fi.f = f;

static float ubyte_to_float ( ubyte  ub  )  [static]

Definition at line 361 of file u_math.h.

00364 {

static float util_fast_exp ( float  x  )  [static]

Fast approximation to exp(x).

Definition at line 243 of file u_math.h.

References util_fast_exp2().

00245 {
00246    const float k = 1.44269f; /* = log2(e) */
00247    return util_fast_exp2(k * x);

static float util_fast_exp2 ( float  x  )  [static]

Fast version of 2^x Identity: exp2(a + b) = exp2(a) * exp2(b) Let ipart = int(x) Let fpart = x - ipart; So, exp2(x) = exp2(ipart) * exp2(fpart) Compute exp2(ipart) with i << ipart Compute exp2(fpart) with lookup table.

Definition at line 213 of file u_math.h.

References fi::f, fi::i, POW2_TABLE_OFFSET, and POW2_TABLE_SCALE.

00215 {
00216    int32_t ipart;
00217    float fpart, mpart;
00218    union fi epart;
00219    
00220    if(x > 129.00000f)
00221       return 3.402823466e+38f;
00222    
00223    if(x < -126.99999f)
00224       return 0.0f;
00225 
00226    ipart = (int32_t) x;
00227    fpart = x - (float) ipart;
00228    
00229    /* same as
00230     *   epart.f = (float) (1 << ipart)
00231     * but faster and without integer overflow for ipart > 31 */
00232    epart.i = (ipart + 127 ) << 23;
00233    
00234    mpart = pow2_table[POW2_TABLE_OFFSET + (int)(fpart * POW2_TABLE_SCALE)];
00235    
00236    return epart.f * mpart;

static float util_fast_log2 ( float  x  )  [static]

Definition at line 257 of file u_math.h.

References fi::f, fi::i, and LOG2_TABLE_SIZE_LOG2.

00259 {
00260    union fi num;
00261    float epart, mpart;
00262    num.f = x;
00263    epart = (float)(((num.i & 0x7f800000) >> 23) - 127);
00264    /* mpart = log2_table[mantissa*LOG2_TABLE_SCALE + 0.5] */
00265    mpart = log2_table[((num.i & 0x007fffff) + (1 << (22 - LOG2_TABLE_SIZE_LOG2))) >> (23 - LOG2_TABLE_SIZE_LOG2)];
00266    return epart + mpart;

static float util_fast_pow ( float  x,
float  y 
) [static]

Definition at line 270 of file u_math.h.

References util_fast_exp2(), and util_fast_log2().

00272 {
00273    return util_fast_exp2(util_fast_log2(x) * y);

static int util_ifloor ( float  f  )  [static]

Floor(x), returned as int.

Definition at line 281 of file u_math.h.

References fi::f, and fi::i.

00283 {
00284    int ai, bi;
00285    double af, bf;
00286    union fi u;
00287    af = (3 << 22) + 0.5 + (double)f;
00288    bf = (3 << 22) + 0.5 - (double)f;
00289    u.f = (float) af;  ai = u.i;
00290    u.f = (float) bf;  bi = u.i;
00291    return (ai - bi) >> 1;

void util_init_math ( void   ) 

One time init for math utilities.

Definition at line 63 of file u_math.c.

References FALSE, init_log2_table(), init_pow2_table(), and TRUE.

00064 {
00065    static boolean initialized = FALSE;
00066    if (!initialized) {
00067       init_pow2_table();
00068       init_log2_table();
00069       initialized = TRUE;
00070    }
00071 }

static int util_iround ( float  f  )  [static]

Round float to nearest int.

Definition at line 298 of file u_math.h.

00300 {
00301 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86) 
00302    int r;
00303    __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
00304    return r;
00305 #elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
00306    int r;
00307    _asm {
00308          fld f
00309          fistp r
00310         }
00311    return r;
00312 #else
00313    if (f >= 0.0f)
00314       return (int) (f + 0.5f);
00315    else
00316       return (int) (f - 0.5f);
00317 #endif


Variable Documentation

float log2_table[((1<< 16)+1)]

log2(x), for x in [1.0, 2.0)

Definition at line 47 of file u_math.c.

float pow2_table[(1<< 9)]

2^x, for x in [-1.0, 1.0)

Definition at line 34 of file u_math.c.


Generated on Tue Sep 29 06:25:28 2009 for Gallium3D by  doxygen 1.5.4