From: "Munagala V. S. Ramanath" <ram@netcom.com>
To: axp-list@redhat.com
Subject: An sqrt() experiment
Date: Tue, 26 Jan 1999 11:39:45 -0800 (PST) [thread overview]
Message-ID: <199901261939.LAA05186@netcom6.netcom.com> (raw)
Hi,
This may be of interest to the math lib aficionados: Sun released a
freely redistributable floating point math library (full sources)
around 1996 called fdlibm. I've found it at:
ftp://ftp.imar.ro/pub/trieste/math/netlib/fdlibm
The code is not 64-bit aware but makes very interesting reading
including the comments. In particular, the comments in e_sqrt.c
suggested what seemed like an interesting approach, so I did a
quick-and-dirty C coding job for timing purposes. It runs about 4
times faster than the version in libm but libffm routine runs an
additional 36% faster, presumably because of the hand-tuned scheduling.
Here are the timing results on my 533MHz 21164a (UX2 Ruffian):
y = 30865358776.919712, time = 2.037109 secs. [sqrt] <-- libm
y = 30865358776.919712, time = 0.470703 secs. [sqt]
y = 30865358776.919712, time = 0.303711 secs. [sqrt] <-- libffm
y = 30865358776.919712, time = 0.475586 secs. [sqt]
Just looking at the code, it seems that a hand-tuned version of sqt()
below may do better than the libffm version, but I don't understand
the architecture of the 21164a well enough to try it.
Here is the C code I used:
------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <sys/resource.h>
static struct rusage init ;
double
CpuTime( )
{
/* seconds to microseconds conversion factor */
#define F 1.0e6
struct rusage fin ;
double usec ;
if ( getrusage( RUSAGE_SELF, &fin ) ) {
fprintf( stderr, "ERROR: getrusage() failed\n" );
fflush( stderr ); exit( 1 );
}
usec = (fin.ru_utime.tv_sec - init.ru_utime.tv_sec) * F ;
usec += (double)(fin.ru_utime.tv_usec - init.ru_utime.tv_usec) ;
usec += (fin.ru_stime.tv_sec - init.ru_stime.tv_sec) * F ;
usec += (double)(fin.ru_stime.tv_usec - init.ru_stime.tv_usec) ;
init = fin ;
return usec / F ;
#undef F
}
/*
* Quick and dirty sqrt() by Newton's method as described in file
* e_sqrt.c of the free fdlibm released by Sun.
* WARNING: Special cases such as 0, +/-inf, +-NaN are ignored
*/
double
sqt( double const x )
{
static int T1[ 32 ] = {
0, 1024, 3062, 5746, 9193, 13348, 18162, 23592,
29598, 36145, 43202, 50740, 58733, 67158, 75992, 85215,
83599, 71378, 60428, 50647, 41945, 34246, 27478, 21581,
16499, 12183, 8588, 5674, 3403, 1742, 661, 130,
};
union {
double x ;
long i ;
} val ;
double y ;
long k, j ;
val.x = x ;
k = (val.i >> 33) + 0x1ff80000 ;
j = k - T1[ 31 & (k >> 15) ] ;
val.i = j << 32 ;
y = val.x ;
y = (y + x/y) / 2.0 ;
y = (y + x/y) / 2.0 ;
y = y - (y - x/y) / 2.0 ;
return y ;
}
int
main( int argc, char **argv )
{
/* no. of loop iterations */
#define N 1000000
int seed = argc > 1 ? atoi( argv[ 1 ] ) : 47 ;
long i = N ;
double y = 0.0 ;
/* initialize timer */
if ( getrusage( RUSAGE_SELF, &init ) ) {
fprintf( stderr, "ERROR: getrusage() failed\n" ) ;
fflush( stderr ) ; exit( 1 ) ;
}
CpuTime() ;
for ( srand( seed ) ; --i >= 0 ; ) {
double x = rand() ;
y += sqrt( x ) ;
}
printf( "y = %f, time = %f secs. [sqrt]\n", y, CpuTime() ) ;
fflush( stdout ) ;
i = N ;
y = 0.0 ;
for ( srand( seed ) ; --i >= 0 ; ) {
double x = rand() ;
y += sqt( x ) ;
}
printf( "y = %f, time = %f secs. [sqt]\n", y, CpuTime() ) ;
fflush( stdout ) ;
return 0 ;
}
------------------------------------------------------------------------
next reply other threads:[~1999-01-26 20:08 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-01-26 19:39 Munagala V. S. Ramanath [this message]
1999-01-26 20:05 ` An sqrt() experiment Andrei A. Dergatchev
1999-01-26 20:28 ` Andrei A. Dergatchev
1999-01-26 21:20 ` Joachim Wesner
1999-01-26 22:15 ` Wes Bauske
1999-01-29 6:28 ` Richard Henderson
-- strict thread matches above, loose matches on Subject: below --
1999-01-26 22:07 Munagala V. S. Ramanath
1999-01-26 22:14 Munagala V. S. Ramanath
1999-01-26 22:51 Munagala V. S. Ramanath
1999-01-27 18:30 ` Jonathan L Dubois
1999-01-28 8:17 ` Joachim Wesner
1999-01-28 15:18 Munagala V. S. Ramanath
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox