From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (qmail 7235 invoked from network); 26 Jan 1999 20:08:20 -0000 Received: from mail.redhat.com (199.183.24.239) by lists.redhat.com with SMTP; 26 Jan 1999 20:08:20 -0000 Received: from netcom6.netcom.com (netcom6.netcom.com [192.100.81.114]) by mail.redhat.com (8.8.7/8.8.7) with ESMTP id OAA26994 for ; Tue, 26 Jan 1999 14:39:56 -0500 Received: (from ram@localhost) by netcom6.netcom.com (8.8.5-r-beta/8.8.5/(NETCOM v1.02)) id LAA05186 for axp-list@redhat.com; Tue, 26 Jan 1999 11:39:45 -0800 (PST) Date: Tue, 26 Jan 1999 11:39:45 -0800 (PST) From: "Munagala V. S. Ramanath" Message-Id: <199901261939.LAA05186@netcom6.netcom.com> To: axp-list@redhat.com Subject: An sqrt() experiment 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 #include #include #include #include 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 ; } ------------------------------------------------------------------------