view filter/imb.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
line wrap: on
line source
/*
    imb.c
    =====

    integer version of logarithms using units of milliBells.

*/

#include <math.h>

#include "stitch.h"

#define LOG_ZERO (-10)

int imB( number )
long number ;
{
    register int i, out ;
    register long in ;
    static int precision = 12 ;
    static int *table = { ( int * ) 0 } ;
    static unsigned table_size ;
    static int inc ;
    double k ;

    if( table == ( int * ) 0 ) {

	table_size = 1 << precision ;

	k   = 2000. / log( 10. ) ;
	inc = k     * log(  2. ) + 0.5 ;

	table = (int *) stitch_malloc( table_size * sizeof( *table ), "imb.c for log table" ) ;

	for( i=0 ; i < table_size ; i++ )
	    table[ i ] = log( (double) ( table_size + i ) / table_size ) * k + 0.5 ;
    }

    if( number > 0 ) {

	in = number ;

	out = precision * inc ;

	if( number < table_size )
	    do {
		in <<= 1 ;
		out -= inc ;
	    }
	    while( in < table_size ) ;
	else
	    while( in - table_size >= table_size ) {
		in >>= 1 ;
		out += inc ;
	    }

	return( out + table[ in - table_size ] ) ;
    }
    else
	return ( LOG_ZERO ) ;
}