From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 102b75,7b37ae94a49a9eec X-Google-Attributes: gid102b75,public X-Google-Thread: 111c93,b79a29863eb727e7 X-Google-Attributes: gid111c93,public From: Terje Mathisen Subject: [++] x86 FCOS (Was: Re: gcc vs Dec Unix c) Date: 1998/01/13 Message-ID: <34BBB772.16@hda.hydro.com> X-Deja-AN: 315615208 Content-Transfer-Encoding: 7bit References: <884291935.1575288842@dejanews.com> <694nm1$74c$1@news2.xs4all.nl> <884364975.2133262340@dejanews.com> <695t9e$kft$1@agate.berkeley.edu> <884411893.151322629@dejanews.com> <884563899.2019688456@dejanews.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Norsk Hydro ASA Newsgroups: comp.os.linux.alpha,comp.arch (This was a very long post, I'll just snip & comment on a few items:) Craig Burley wrote: > cekim@ix.netcom.com writes: [snip] > Of course, if you can use clever hardware to reduce a lengthy > operation to one that is sufficiently short in all cases, you > get to skip lots of stuff, including task switching, interrupt > worries, and so on. > > FCOS would appear to be a candidate for this sort of thing, > but I don't believe Intel has quite accomplished that in any > of its CPUs. > > In other words, I believe Intel's FCOS implementations all are > basically just like internally microcoded operations. But that's > just speculation on my part. >From what I learned during the FDIV/FPATAN workaround task 3 years ago, I'd have to say you're right: All the trancendental (or at least the trigonometric) function on Pentium+ fpus use a combination of table lookups and polynomial or rational approximations, implemented with microcode. The main advantages of microcode vs. asm seems to be that the microcode has direct access to the 2-3 guard bits, as well as the capability to run some algorithms at non-standard (reduced) precision. It might also be the case that microcode can achieve slightly better latency on some operations, maybe by bypassing the normalization/rounding step after some internal operations. > > If intel had any sense at all(gaping hole jump in), they would have > > implimented the fcos function such that would avoid all(most) of the > > slowdowns that typically follow such repetative calculations. Thus, > > software would have to work very hard to match this and may not ever be > > able to given some inaccessable internal register space and so on. This is right, in that it would be _very_ hard to achieve full 80 (1+15+64) bit accuracy using asm code. > Let's assume FCOS, implemented using the "vanilla" instruction > set that *is* hard-wired, in the sense that this set is basically > "single-cycle" for some definition of "cycle" that is the same > length for two processors. (Usually that includes add, sub, and, > or, maybe shifts, tests, branches, and so on.) [snipped discussion of why software implementations can be faster than microcode, due to much better latency hiding, when doing many FCOS operations in parallel.] > The result is that one thousand FCOS operations takes ~10000 > cycles on the first processor. > > Meanwhile, the second processor's dedicated FCOS hardware is > not parallelized, not pipelined, etc., so for those same thousand > operations, it takes ~20000 cycles -- in other words, the > *hardware* implementation is twice as slow! This is of course valid. However, If I ever had to do 10K FCOS operations in parallel, I would definitely look into a much more agressive table-driven algorithm: By definition, if FCOS is so critical for my executiuon time, I would gladly pay the penalty of filling both data and code caches with FCOS-specific stuff. Assuming a lookup table with 1K entries (10-11 bits), it should be possible to get double-accurate results with just a couple of polynomial operations. > > I was not __accusing__ the alpha of being slow, I happen to like it and > > paid a great deal for its speed!! Just a recognition that it would be > > very easy and not surprising if the lack of hardware, high level math > > functions slowed individual operations down. Just hopefully not this > > much!!! > > I don't disagree with any of that. > > The thing I disagreed with was the flat-out statement that, because > Pentium implemented FCOS in hardware and Alpha did not, therefore > Pentium would *always* beat Alpha in FCOS-heavy benchmarks. > > As should be clear by now, thanks to Robert Harley's post, not > only was I right, but the reality is that the reason you see > faster Pentium performance is that you don't mind getting wrong > answers. > > Modify your Alpha's implementation of FCOS in a way that allows > the returning of wrong answers, and I bet it'll go *much* faster > than Pentium, for sufficiently interesting definitions of "wrong". The interesting (to me) definition here is: What happens if the input value is outside N*PI, for N larger than 1 or 2. Intel have documented the value of PI they use in the FPU to do range reduction, it has about 66-67 significant bits. Wouldn't it still be possible to write code which checks the input value first? float pi_array[]; double mycos(double x) { if (( double px = fabs(x)) > COS_ACCURACY_LIMIT)) { double n = floor(px * (1.0 / 3.141592653589793238462643383279)); /* Subtract N times a multi-precision value for PI */ for (i = 0; i < PI_ARRAY_SIZE; i++) { px -= pi_array[i] * n; // Exact if enough guard bits! } if (x < 0) px = -px; x = px; } return FCOS(x); } I can see that is is easy to extend the range of accuracy by quite a bit, i.e. to exponents up to about 20-30, but to get all the way to 2^1024, the maximum possible input value, would require quite a lot more work! Terje > So, the Pentium might well be performing FCOS in parallel with > some of the independent instructions in your code stream, > including possibly call, return, load, and other miscellaneous > instructions, which could *appear* to make it take much less > time than it does. On a PPro, using call/ret in a simple loop like the one aboe, wouldn't really matter, since (allmost?) all fp operations allow the integer ops to run simultaneously, so for the code above, the less than 10 cycles used for a subroutine call would still be completely hidden by the FCOS execution time. > This is all in the statistical sense, of course. Once you > truly *know* how the Pentium and Alpha do their respective > jobs, it might be 100% clear that lack of hardware assist > cripples Alpha (not the case I believe, but I'm just saying...). > > So, *speculating* that the Pentium does FCOS faster simply > because it has an instruction to perform it is wrong. > > However, *knowing* that's the case is okay, if you truly know it. I do believe that an Alpha cannot beat FCOS, assuming both that the input values are inrange, and you need long double (extended) precision. However, code like this is very rare indeed. :-) > Perhaps. They won't have more, and perhaps have less, incentive > to make FCOS fast, pipelined, and parallel for their audience than > Digital does for its Alpha audience, but that's just speculation on > my part, based on my observation that Alpha is intended for > high-performance scientific markets that Intel doesn't really > target (at least not with it's x86 line). That might be so, but I am still impressed by the job Peter Tang did on the Pentium trancendentals: Getting less than 1 ulp errors for all in-range inputs is _hard_. > The way I'm most familiar with this impacting code generation > is the ability to interleave independent threads of code. > Ultra-1s and Alphas permit that in many ways I assume Pentia > do not (by "permit" I really mean "make worthwhile for > generating faster code ;-). Actually, the Pentium might be the cpu where hand-interleaving (i.e. optimized asm) can gain you the most performance. PentiumPro/PII is a different matter, since the OO core handles at least a good chunk of the bad compiler-generated code out there. Terje -- - Using self-discipline, see http://www.eiffel.com/discipline "almost all programming can be viewed as an exercise in caching"