From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111c93,2cb1c7dcdf4349e1 X-Google-Attributes: gid111c93,public From: torvalds@transmeta.com (Linus Torvalds) Subject: Re: sin() on alpha. Date: 1998/05/23 Message-ID: <6k5od1$1b7$1@palladium.transmeta.com>#1/1 X-Deja-AN: 355768380 References: <3565295F.7367C07E@dirac.phys.uwm.edu> <6k59a0$1cq$1@blue.hex.net> Organization: Transmeta Corporation, Santa Clara, CA Newsgroups: comp.os.linux.alpha In article <6k59a0$1cq$1@blue.hex.net>, Christopher Browne 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