From: torvalds@transmeta.com (Linus Torvalds)
Subject: Re: sin() on alpha.
Date: 1998/05/23 [thread overview]
Message-ID: <6k5od1$1b7$1@palladium.transmeta.com> (raw)
In-Reply-To: 6k59a0$1cq$1@blue.hex.net
In article <6k59a0$1cq$1@blue.hex.net>,
Christopher Browne <cbbrowne@hex.net> wrote:
>
>'Tis been a while since I've calculated trig values; are there any
>processes where you start with an estimate, and then iterate to refine
>it?
>
>In that case, you would start with an interpolation table (probably
>linear) involving some "small-but-useful" set of values to get the
>initial estimate, and then iterate to improve precision.
>
>A 1024 entry linear interpolation table is going to get one *fairly*
>close; I don't recall offhand what identities would be used to do
>"function point iteration," but no doubt there's something useful. A
>higher-order polynomial interpolation table would surely give somewhat
>better results, at somewhat higher cost, to be sure.
Be careful.
This is what DEC does with their integer divide routine, and it performs
pretty well for benchmarks where the data cache is completely quiescent
apart from the divide.
However, with a cold data cache (or a benchmark that wants to use the
data cache for other things) the DEC routines were actually slower than
doing the divide the "stupid" way which has very nice cache behaviour.
Remember: if you can make a small and simple routine that avoids one
cache miss, that one cache miss is often more costly than the cost of
the whole routine. This is less true of more complex functions like
sin() and cos(), but it's still something that is easily overlooked,
because when you write the routines you tend to test performance by
having something like
for (i = 0; i < 100000000; i++) {
sum += sin(x); x += 0.00001;
}
or similar where the cache will be warm all the time.
So the traditional argument goes "well, if it isn't in the cache, then
we don't care about performance, because it obviously isn't being called
often enough to matter".
Which is very shortsighted. Nobody cares about the performance of most
library routines on their own. People care about the performance of
programs that _use_ the routines, and if the routines need to be in the
cache to be fast, then that implies that something _else_ cannot be in
the cache.
So do take cache behaviour into account. One great thing about modern
CPU's is that they all tend to have cycle counters, which means that you
can actually benchmark how fast a routine is even with a cold cache,
because you can actually measure the performance of the very first
invocation rather than the performance of doing a million of the things
in a loop.
Don't be overly scared about loops. The one great thing about loops is
that they perform _very_ well icache-wise, and as a result it can be
faster to do an extra iteration or two if it cuts down on your misses.
A cache miss can easily cost you several hundred instruction slots
wasted.
Linus
next prev parent reply other threads:[~1998-05-23 4:00 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-05-22 4:00 sin() on alpha Dong-Woon Park
1998-05-22 4:00 ` Bruce Allen
1998-05-22 4:00 ` Greg Lindahl
1998-05-23 4:00 ` Christopher Browne
1998-05-23 4:00 ` Michal Jaegermann
1998-05-23 4:00 ` Linus Torvalds [this message]
1998-05-23 4:00 ` Andi Kleen
1998-05-23 4:00 ` Greg Lindahl
1998-05-24 4:00 ` sin() on alpha / Cray libm Joachim Wesner
1998-05-23 4:00 ` sin() on alpha Dong-Woon Park
1998-05-23 4:00 ` Bruce Allen
1998-05-24 4:00 ` Joachim Wesner
1998-05-24 4:00 ` Joachim Wesner
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