* An sqrt() experiment
@ 1999-01-26 19:39 Munagala V. S. Ramanath
1999-01-26 20:05 ` Andrei A. Dergatchev
` (3 more replies)
0 siblings, 4 replies; 12+ messages in thread
From: Munagala V. S. Ramanath @ 1999-01-26 19:39 UTC (permalink / raw)
To: axp-list
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 ;
}
------------------------------------------------------------------------
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: An sqrt() experiment
1999-01-26 19:39 Munagala V. S. Ramanath
@ 1999-01-26 20:05 ` Andrei A. Dergatchev
1999-01-26 20:28 ` Andrei A. Dergatchev
1999-01-26 21:20 ` Joachim Wesner
` (2 subsequent siblings)
3 siblings, 1 reply; 12+ messages in thread
From: Andrei A. Dergatchev @ 1999-01-26 20:05 UTC (permalink / raw)
To: axp-list
Hi,
Thanks for pointing to the library.
> 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]
Have you used any optimization ? I've got .453125 with -O5(6) on SX.
Any expert out here wants to explain why I've got 0.450195 secs for [sqt]
running under MC?
> * WARNING: Special cases such as 0, +/-inf, +-NaN are ignored
Btw, is it fair to compare with a code which tests for special cases ?
Rgds,
Andrei
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: An sqrt() experiment
1999-01-26 20:05 ` Andrei A. Dergatchev
@ 1999-01-26 20:28 ` Andrei A. Dergatchev
0 siblings, 0 replies; 12+ messages in thread
From: Andrei A. Dergatchev @ 1999-01-26 20:28 UTC (permalink / raw)
To: axp-list
PentiumII-350, "silicon" sqrt ?
30887868596.321000 0.340000 secs [sqrt]
Ok, rand slightly different I suppose.
Andrei
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: An sqrt() experiment
1999-01-26 19:39 Munagala V. S. Ramanath
1999-01-26 20:05 ` Andrei A. Dergatchev
@ 1999-01-26 21:20 ` Joachim Wesner
1999-01-26 22:15 ` Wes Bauske
1999-01-29 6:28 ` Richard Henderson
3 siblings, 0 replies; 12+ messages in thread
From: Joachim Wesner @ 1999-01-26 21:20 UTC (permalink / raw)
To: axp-list
On Tue, 26 Jan 1999, Munagala V. S. Ramanath wrote:
>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.
>
This is a bit confusing. Already for some time, the "official" libm on
AXP/Linux **IS** based on fdlibdm !!
Especially, the very old sqrt, was that single-bit-a-time version from libfdm,
later it was replaced by an assembler version of the method you cite. libffm
slightly improves on that. It seems strange, that you get near those figures
with your C version. It seems likely that your timing is governed by other
factors, but not (really) sqrt.
From your code, I get about 303 ns/sqrt. However, the raw libffm speed of sqrt
is about 120 ns !!
Joachim Wesner
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: An sqrt() experiment
@ 1999-01-26 22:07 Munagala V. S. Ramanath
0 siblings, 0 replies; 12+ messages in thread
From: Munagala V. S. Ramanath @ 1999-01-26 22:07 UTC (permalink / raw)
To: axp-list
> Have you used any optimization ? I've got .453125 with -O5(6) on SX.
Just -O2 with the stock RH 5.2 egcs compiler.
> > * WARNING: Special cases such as 0, +/-inf, +-NaN are ignored
>
> Btw, is it fair to compare with a code which tests for special cases ?
My experiment was not intended to be a full benchmark test; just a
quick hack to get ballpark figures.
Ram
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: An sqrt() experiment
@ 1999-01-26 22:14 Munagala V. S. Ramanath
0 siblings, 0 replies; 12+ messages in thread
From: Munagala V. S. Ramanath @ 1999-01-26 22:14 UTC (permalink / raw)
To: axp-list
> PentiumII-350, "silicon" sqrt ?
>
> 30887868596.321000 0.340000 secs [sqrt]
>
> Ok, rand slightly different I suppose.
You could remove the effects of rand() by creating an array and
filling it with rand() calls in a separate loop before entering
the timing loop.
Ram
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: An sqrt() experiment
1999-01-26 19:39 Munagala V. S. Ramanath
1999-01-26 20:05 ` Andrei A. Dergatchev
1999-01-26 21:20 ` Joachim Wesner
@ 1999-01-26 22:15 ` Wes Bauske
1999-01-29 6:28 ` Richard Henderson
3 siblings, 0 replies; 12+ messages in thread
From: Wes Bauske @ 1999-01-26 22:15 UTC (permalink / raw)
To: axp-list
Munagala V. S. Ramanath wrote:
>
> 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.
>
Be careful. Your test puts as much weight on the
performance of rand() as it does on sqrt(). A better
source of test numbers would be to just init x to N
and decrement it on each iteration. That way you're
only measuring sqrt().
Wes
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: An sqrt() experiment
@ 1999-01-26 22:51 Munagala V. S. Ramanath
1999-01-27 18:30 ` Jonathan L Dubois
1999-01-28 8:17 ` Joachim Wesner
0 siblings, 2 replies; 12+ messages in thread
From: Munagala V. S. Ramanath @ 1999-01-26 22:51 UTC (permalink / raw)
To: axp-list
> This is a bit confusing. Already for some time, the "official" libm on
> AXP/Linux **IS** based on fdlibdm !!
>
> Especially, the very old sqrt, was that single-bit-a-time version from libfdm,
> later it was replaced by an assembler version of the method you cite. libffm
> slightly improves on that. It seems strange, that you get near those figures
> with your C version. It seems likely that your timing is governed by other
> factors, but not (really) sqrt.
My C routine is not the same as the code in fdlibm; it is derived
from comments in the fdlibm code that discussed Newtons method; it
uses floating point divides but since the Alpha was supposed to be
good at floating point arithmetic, I decided to give it a try. My
code also ignores all the special cases like 0, +-inf, +-NaN etc.
as noted in my comments in the code.
> >From your code, I get about 303 ns/sqrt. However, the raw libffm speed of sqrt
> is about 120 ns !!
My code includes calls to rand() in the timing loop but since I was
only interested in comparing two versions of sqrt() on the same
platform, I didn't worry about it. For absolute timing or for
cross-platform comparisons, the rand() call needs to be taken out
of the timing loop as other respondents have noted.
I took the rand() call out and now I get something like this:
y = 666667166.458800, time = 1.877930 secs. [sqrt] <-- libm
y = 666667166.458800, time = 0.292969 secs. [sqt]
y = 666667166.458800, time = 0.146484 secs. [sqrt] <-- libffm
y = 666667166.458800, time = 0.293945 secs. [sqt]
This yields a figure of 146 ns for the libffm sqrt() which is close
to your expectation and almost 13 times faster than the stock libm!
Ram
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: An sqrt() experiment
1999-01-26 22:51 Munagala V. S. Ramanath
@ 1999-01-27 18:30 ` Jonathan L Dubois
1999-01-28 8:17 ` Joachim Wesner
1 sibling, 0 replies; 12+ messages in thread
From: Jonathan L Dubois @ 1999-01-27 18:30 UTC (permalink / raw)
To: axp-list
> > This is a bit confusing. Already for some time, the "official" libm on
> > AXP/Linux **IS** based on fdlibdm !!
do you know how to measure the accuracy of your sqrt routine? I would be
curious to know how the routines compare in this regard.
from libffm README:
Ideal function routines should have a *relative* error of less than 0.5
ulp (ulp = units of the last place), this means that the result (mantissa)
is always a perfectly rounded, 52 bit version of the infinite precision,
theoretical result.
The new fast sqrt is a stripped down, rescheduled version of the libm one,
that does no final rounding of the last bit. Error is guaranteed to be
within -1 ulp < err < +1.0625 ulp (according to libdm documentation). This
routine is now as fast as Crays sqrt, but much more accurate.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: An sqrt() experiment
1999-01-26 22:51 Munagala V. S. Ramanath
1999-01-27 18:30 ` Jonathan L Dubois
@ 1999-01-28 8:17 ` Joachim Wesner
1 sibling, 0 replies; 12+ messages in thread
From: Joachim Wesner @ 1999-01-28 8:17 UTC (permalink / raw)
To: axp-list
On Tue, 26 Jan 1999, Munagala V. S. Ramanath wrote:
>> This is a bit confusing. Already for some time, the "official" libm on
>> AXP/Linux **IS** based on fdlibdm !!
>>
>> Especially, the very old sqrt, was that single-bit-a-time version from libfdm,
>> later it was replaced by an assembler version of the method you cite. libffm
>> slightly improves on that. It seems strange, that you get near those figures
>> with your C version. It seems likely that your timing is governed by other
>> factors, but not (really) sqrt.
>
> My C routine is not the same as the code in fdlibm; it is derived
> from comments in the fdlibm code that discussed Newtons method; it
> uses floating point divides but since the Alpha was supposed to be
> good at floating point arithmetic, I decided to give it a try. My
> code also ignores all the special cases like 0, +-inf, +-NaN etc.
> as noted in my comments in the code.
>
YEP, what I wanted to say, that the latest "official" Alpha libm sqrt
is ALREADY based on those comments you used too. This change was done
by Davids Mousberger already some time ago, so the differences should not be so
large !!?? Only in the very *early* times of AXP/Linux, the single-bit version
was used and was known to be "dog slow".
Joachim
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: An sqrt() experiment
@ 1999-01-28 15:18 Munagala V. S. Ramanath
0 siblings, 0 replies; 12+ messages in thread
From: Munagala V. S. Ramanath @ 1999-01-28 15:18 UTC (permalink / raw)
To: axp-list
> YEP, what I wanted to say, that the latest "official" Alpha libm sqrt
> is ALREADY based on those comments you used too. This change was done
> by Davids Mousberger already some time ago, so the differences should not be so
> large !!?? Only in the very *early* times of AXP/Linux, the single-bit version
> was used and was known to be "dog slow".
Here is what rpm says about my glibc on RH 5.2:
------------------------------------------------------------------------
Name : glibc Distribution: Manhattan
Version : 2.0.7 Vendor: Red Hat Software
Release : 29 Build Date: Mon Oct 12 19:24:27 1998
Install date: Thu May 4 20:55:48 2000 Build Host: spacely.redhat.com
Group : Development/Libraries/Libc Source RPM: glibc-2.0.7-29.src.rpm
Size : 18253243 License: LGPL
Packager : Red Hat Software <bugs@redhat.com>
Summary : GNU libc
Description :
Contains the standard libraries that are used by multiple programs on
the system. In order to save disk space and memory, as well as to
ease upgrades, common system code is kept in one place and shared between
programs. This package contains the most important sets of shared libraries,
the standard C library and the standard math library. Without these, a
Linux system will not function. It also contains national language (locale)
support and timezone databases.
------------------------------------------------------------------------
And it is still, to put it gently, "dog slow". Can somebody who has the
source RPMs handy check the source for sqrt() ? Thanks.
Ram
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: An sqrt() experiment
1999-01-26 19:39 Munagala V. S. Ramanath
` (2 preceding siblings ...)
1999-01-26 22:15 ` Wes Bauske
@ 1999-01-29 6:28 ` Richard Henderson
3 siblings, 0 replies; 12+ messages in thread
From: Richard Henderson @ 1999-01-29 6:28 UTC (permalink / raw)
To: axp-list
On Tue, Jan 26, 1999 at 11:39:45AM -0800, Munagala V. S. Ramanath wrote:
> 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
That is what is already included in both glibc and netbsd's libm.
r~
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~1999-01-29 6:24 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-01-26 22:14 An sqrt() experiment Munagala V. S. Ramanath
-- strict thread matches above, loose matches on Subject: below --
1999-01-28 15:18 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-26 22:07 Munagala V. S. Ramanath
1999-01-26 19:39 Munagala V. S. Ramanath
1999-01-26 20:05 ` 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox