Usenet comp.os.linux.alpha (1997-2013)
 help / color / mirror / Atom feed
From: Craig Burley <burley@tweedledumb.cygnus.com>
Subject: Re: gcc vs Dec Unix c
Date: 1998/01/13	[thread overview]
Message-ID: <y6oh1ge5w2.fsf@tweedledumb.cygnus.com> (raw)
In-Reply-To: 884563899.2019688456@dejanews.com


cekim@ix.netcom.com writes:

> Craig Burley <burley@tweedledumb.cygnus.com> wrote:
> > cekim@ix.netcom.com writes:
> >
> > >   peterm@landau.EECS.Berkeley.EDU (Peter Mardahl) wrote:
> > > > I believe that x86 implements cos in HARDWARE, while Alpha does NOT.
> > > > Therefore, if you're doing cos() quite a lot, the x86 will stomp
> > > > alpha, and most other RISC.
> > > I should have realized
> > > such a gap could have only come from hard vs soft code.  Never expected
> > > such a thing in a RISC, of course.
> >
> > Whether an operation is done "in hardware" or "in software" has
> > *little* relevance to its performance, in practice, and that's
> > most certainly true in this particular case.
> 
> Well, *little* might be a *little* strong.  I write VHDL to perform such
> things and can without much effort achieve orders of magnitude more GOPS,
> FLOPS and MIPS than software on any architecture, becasue I am not
> encumbered by the load/store/register quirks of any architecture, much
> less task switching and shared memory.

I stand by my statement -- the context was *general-purpose
processor* designs, not including special-purpose hardware.

In general-purpose processor designs, you often have to design
your hardware while still encumbered by the "quirks" to which you
refer.

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.

> 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.

That's not the entire issue.

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.)

Now, let's assume one of these processors has no FCOS, so it
*must* be implemented in software.  Let's say it takes 50 cycles
in the average case.

The other processor has an FCOS implemented in hardware, and
let's say it takes 20 cycles in the average case.

Is the FCOS-laden processor faster at doing lots of FCOS
operations?

Not necessarily.  "Assuming all else is the same" is basically
never the case in real processor design.

So, the *first* processor has used the design time and die space
to implement parallel functional units, additional registers,
pipelining, register renaming, whatever, while the second processor
devotes those resources solely to its FCOS implementation.

The effect of this is that the first processor still takes 50
cycles to do an FCOS, but to do two FCOS operations in a row
it takes 60 cycles, to do three it takes 70 cycles, and so on.

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!

Agreed, the first processor can probably achieve its speeds only
when the software is coded properly, but that's why God created
compiler writers.  ("And he saw that everything was very good.
And God brought forth compiler users.  And there were many bugs
found upon the lands."  ;-)

This scenario reflects the realities of the CISC/supercomputer/VLIW/RISC
world somewhat, but ignores, for the purpose of illustration, other
factors, such as the fact that the first processor *probably*
can tolerate more increase of clock rate (faster cycle time) due
to having a less-complex instruction decode (no FCOS probably means
no other "engage weird hardware and wait for it to finish"
operations).

> Yes, hardware can be made to appear slow by poor software and creative
> programming can always squeeze a little more out of slow hardware.
> 
> 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".

;-)

> read on...
> > > One thing that threw me off of that trail is that the ".s" assembly dump
> > > has "call cos" instead of the TASM or AT&T equivalent "FCOS".  I assume
> > > Gcc just replaces this with the appropriate code or macro according to
> > > the host architecture.
> >
> > Nope.  At least, I challenge you to prove your theory -- to
> > my knowledge, gcc *can't*, and gas *doesn't*, replace "call cos"
> > in assembly code with "fcos".  However, gcc might (and g77 should,
> 
> Oops I meant to say "gas" there not GCC..

Ok, that does make more sense.

> Ok here is where I get a little confused for lack of experience with the
> underpinnings of GCC:
> 
> "call cos" is meaningless to AT&T-style asm unless there is a
> corresponding "cos" subroutine in some header. That I know :)

I don't think gcc usually uses native assembler format unless
it has to (e.g. was configured that way).  One of the central
ideas of the GNU project is to minimize (instead of, as
hardware vendors like to do to lock-in their customers, maximize)
differences between machines and operating systems.  So, gas
offers a fairly "regular" syntax, presumably without getting
too pedantic about it, across all the processors it supports,
so that most vanilla instructions look about the same on all
CPUs.  (E.g. I suspect "add x,y,z" in gas always means
`z := x + y', instead of meaning that on some CPUs and meaning
`x := y + z' on others.)  Note that I've hardly looked at gas
at all, myself, except by reading gcc output, and I pretty much
pick up syntax by context, regardless of the machine I'm
debugging on at the time.  (That's what learning assembly coding
at age 12 can do for you.  ;-)

> I was assuming that one of two things must be done here: 1. "cos()"
> exists in GCC lib as lookup/series/something.  That macro calls this code
> and is inserted by the pre-processing of gas. 2. On an architecture with
> FCOS or similar support, one could invoke the use of the hardware code
> through flags, as you mentioned.

I don't believe gas does #2.  "call cos" is, AFAIK, a pretty universal
way to tell the assembler to assemble a call to the procedure named
"cos", *not* an opportunity to insert some in-line assembler code via
some kind of macro facility.  I think you're saying the same thing
via #1, i.e. I believe you're right, but want to be specific.

You can verify this for yourself by disassembling the resulting
executable.  Have you tried this?

> So, you seem to indicate that, having not known about or used those
> flags. I would have most likely compiled something using scenario #1
> above?

Yup, if I'm reading your statement right.  I don't think this is
a "pre-processing" phase of gas any more than assembling a
vanilla "ld" instruction uses a pre-processing phase.

> I realize that GCC makes slow code on the alpha, but that(x86 was using
> software) is a little rediculus.  Given the the code did little else
> other than impliment the cosine function.  That would mean that the GCC
> code was 6X slower on fp hardware that is 3X faster.
> 
> You would literaly have to insert wait states!!!

No, not really.  All you need is poorly implemented code, which gcc
*has*, in its history, as many compilers have, been quite good at
producing for the Alpha.  Teaching compilers to produce code
that is properly optimized for the target processor is fun, but
takes sufficient expertise that those of us who know how to do
it typically get paid so much for so little work that we have time
to engage in debates like this.  ;-)

> Is that going on?  Tell me I will start hacking that out right away:)

Please, if you speed up any of the GNU/Linux software (libm, libc, etc.)
to run faster on Alpha, tell us all about it, donate the source
back, etc.!  This sort of thing has been a *long-known* issue
(over a year that I've known about problems with things like
Linux libm and, IIRC, BLAS on Alpha).  I haven't really kept
track of to the extent of improvements since about a year ago
or so, but I'm sure there's always more progress to be made.

> > So, you can probably tweak-up the performance of your x86 program
> > by ensuring theat "fcos" is used in-line, one way or the other.
> 
> x86 was already doing a bang up job, I want faster alpha!! :)

Yup, I just felt it was fair to point out that the x86 could perhaps
be made to run even faster on your benchmark, though probably
only a little bit.  (I suspect the FCOS overlaps the "return"
instruction, and maybe even the subsequent "call" instruction,
depending on how the benchmark is coded.)

And, I *suspect* the Pentium Pro (or PII?), the one that returns
wrong results for your benchmark, could be modified to return
correct results without much of a hit in benchmark time, but
I'm not sure if that's true.  I've read about the inaccurate
PI approximation in the Pentia on comp.arch, and don't
recall whether t's simply a matter of a bogus approximation,
or hardware assuming it can use a lower-precision approximation
than is necessary to compute correct results.  The latter might
mean using a higher-precision one would require a more time-consuming
modulo operation (in hardware), thus slowing down FCOS for
at least operands outside the minimum domain, perhaps for
all operands.

> > Probably *the* biggest reason for the observations of bad
> > performance of cos() on an Alpha that, otherwise, would be
> > considered somewhat faster than a particular Pentium
> > configuration, is simply that the *software* is slow.  And

> Given that I tested the same code with and without the cos function:
> ie:
> C=A*B;
> AND then
> C=A*cos(B);   //for the purposes of demonstration
> 
> And the ALpha ran 65 minutes with cos and 4 sec without.  I can safely
> say that it is the cos function(*software*) which is the problem. So some
> hypothetical math:
> 
> PPRO 200 w/cos =10 minutes -120 billion cycles
> ALpha 533 w/cos =65 minutes -2.08 trillion cycles
> 
> Assuming niether using hardware FCOS:
> Alpha is executing 17.33 cycles for each 1 PPRO cycle to perform the same
> function (with more available register space!)
> 
> Some more hypothetical math:
> 
> I asked the loop to run 1 billion times so I have rougly 120 cycles per
> loop given the number above for the PPRO.
> 
> Old TASM book reports that the 486 uses ~241 clocks to do FCOS.  I hope
> the P6 is a little more efficient, so it seems that there is precious
> little time for software to jump in here and still get these execution
> times!?!

Microcode can jump in where software does not.  And, either way,
when parallel instruction units are available and usable, it
can make software *appear* to "disappear".

For example, what do you think is the difference in execution
time between the following chunks of code on a Sun UltraSPARC-1:

	addcc	%i0,%i1,%o0	/* add and produce condition codes */
	addccc	%i2,%o0,%o0	/* add with condition codes, etc. */
	addccc	%i3,%o0,%o0
	addc	%i4,%o0,%o0

	sll	%g1,1,%g1	/* shift left 1 bit */
	addcc	%i0,%i1,%o0	/* add and produce condition codes */
	or	%g1,12,%g1
	addccc	%i2,%o0,%o0	/* add with condition codes, etc. */
	and	%g1,255,%g1
	addccc	%i3,%o0,%o0
	xor	%g1,1023,%g1
	addc	%i4,%o0,%o0

Assuming %i0, %i1, and %g1 are all either written, or not, in the
previous cycle, the answer is "there is no difference", even though
more work is being done by the second code sequence...but this *would*
be about twice as slow:

	sll	%g1,1,%g1	/* shift left 1 bit */
	or	%g1,12,%g1
	and	%g1,255,%g1
	xor	%g1,1023,%g1
	addcc	%i0,%i1,%o0	/* add and produce condition codes */
	addccc	%i2,%o0,%o0	/* add with condition codes, etc. */
	addccc	%i3,%o0,%o0
	addc	%i4,%o0,%o0

Even though the above, third, sequence accomplishes exactly
the same thing as the previous (second), it takes about
twice as long to execute!

(Apologies if I'm too out-of-practice on Ultras to have gotten this
exactly right.)

The reason is that the second, interleaved example makes use of
the *two* independent ALUs in the UltraSPARC-1.  Both can perform
most "vanilla" ALU ops (add, sub, or, and), one can also perform
shifts, the other can also perform condition-code generation and
incorporation.  (Dunno whether the instruction pairs have to be
aligned properly to make this really work...haven't investigated
that yet, as my Ultra-1 remains off-line, awaiting my investing
time to install Solaris, get it up on my LAN, install GNU
software, etc.)

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.

And *still* the Alpha could, with appropriate software, perform
the benchmark faster, assuming the same results were permissible!

Maybe not, but the Alpha is *not* out of the running *simply*
because Pentia implement FCOS in hardware while Alpha does not.

As I said, that distinction -- hardware vs. software, which is
*really* instruction-set vs. requires-software-implementation
(since any x86 chip could just trap on FCOS and leave it to
a software implementation as well) -- has little relevance
in the overall picture of determining performance, even for
*one* benchmark.

(In fact, this is a good question to ask yourself.  The SPARCv9
instruction set defines POPC, a population-count instruction,
which returns the number of bits set in a 64-bit value.  Let's
say the Alpha does not.  Now, which do you think runs faster,
code making heavy use of POPC on an Ultra-1, or *longer*
equivalent code on the Alpha that does the same operation
using low-level instructions?  Answer: the Alpha will likely
to be faster.  Even though POPC is implemented "in hardware" on
the Ultra-1.  Why?  Because, although POPC *could* be
implemented in hardware probably as less than a 3-cycle
operation, the Ultra-1 implements it as "trap to OS for
instruction emulation".  ;-)

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.

> > In my experience, somewhat encouraged by people who do actual
> > *hardware* design (of CPUs), complicated hardware instructions
> > often should be *avoided*, for maximum performance, in favor
> > of a software solution employing the *exact same CPU*'s lower-level
> > instructions (which they've had more time to thoroughly optimize)
> 
> Not surprising with a newer function, but this one has been around since
> 386/387.
> Am I giving Intel too much credit here?

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).

> I am not surprised that the alpha is choking a little here, it has a long
> way to go to catch up (in linux) to the amount of time that the x86 had
> had for tweaking.

That, and, I think there's more room for tweaking (over and above
"vanila" code generation and traditional optimizations) on the
Alpha, especially 21164 and, who knows, maybe 21264, chips, than
there is for x86.

Put another way: it's not so much that Alphas have more registers
than Pentiums.  Maybe they don't.  What they *do* have is more
*programmer-visible* registers (especially of the general-purpose
variety).  This tends to expose more of the internal workings
and timings of the processor to software, thus putting more
burden on the software author (compiler or human coder) to
properly schedule instructions and allocate variables to
registers.  One still needs to properly schedule instructions
different ways on different x86 machines, but there's less
register "magic" to play on them, because there are fewer
registers that are *visible*.

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 ;-).  The old 128-bit VLIW machine
I worked in *really* permitted that, and optimizing scalar
code both by hand and within the Fortran compiler the
company built for it was something of a specialty of mine.
(The real brains behind *that* operation had already pretty
much squeezed out all of the loop performance, though I still
helped out some with that.)

> I am very surprised if the difference between the x86 software
> implimentation is really that much faster than the alpha implimentation. 
> The alpha's speed(literaly clock speed) is leaves a 2X-3X window for bad
> coding and RISCness yet the alpha is 6X slower in the end?  Sounds fishy.

Smells that way to me too.  Dunno if Robert Harley's post
is the last word on this topic; I'd like to believe that,
even for this out-of-minimal-domain values benchmark, it
*could* be more efficiently handled by the GNU/Linux/Alpha
cos() routines, but haven't made time to look at it myself yet.
(And, I don't have the same degree of confidence I have with
less-mathematical code.  When it comes to low-level optimizations,
I'm more of a bit-banger than an algorithm-fiddler.)

> Oh well, I guess I either nede to use fotran or handcode my math
> functions.

GNU fortran (g77) might not help you much, but Digital Fortran
should...perhaps so if it offers a version of COS() that returns
Pentium's (wrong) answers.  ;-)

> I am very curious now, I would like to hunt through the code and see if
> there is an FCOS in there.  I would hope there is.

Yup, use "stepi" in gdb (on the Pentium) and see if you find it
shortly after "call cos".  Let us know.
-- 

"Practice random senselessness and act kind of beautiful."
James Craig Burley, Software Craftsperson    burley@gnu.org



  reply	other threads:[~1998-01-13  5:00 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-01-08  5:00 gcc vs Dec Unix c fonte
1998-01-09  5:00 ` Toon Moene
1998-01-09  5:00 ` Toon Moene
1998-01-09  5:00 ` Toon Moene
1998-01-09  5:00 ` Jaap Schellekens
1998-01-09  5:00   ` cekim
1998-01-09  5:00     ` Peter Mardahl
1998-01-09  5:00       ` Craig Burley
1998-01-10  5:00       ` cekim
1998-01-10  5:00         ` Craig Burley
1998-01-13  5:00           ` cekim
1998-01-13  5:00             ` Craig Burley [this message]
1998-01-13  5:00               ` [++] x86 FCOS (Was: Re: gcc vs Dec Unix c) Terje Mathisen
1998-01-13  5:00                 ` Andy Glew
1998-01-14  5:00                   ` Terje Mathisen
1998-01-13  5:00               ` gcc vs Dec Unix c James Van Buskirk
1998-01-14  5:00                 ` brianh
1998-01-14  5:00                   ` James Van Buskirk
1998-01-13  5:00             ` Rob Komar
1998-01-13  5:00             ` Yan-Song Chen
1998-01-13  5:00               ` Greg Lindahl
1998-01-15  5:00               ` Lupo LeBoucher
1998-01-15  5:00                 ` Greg Lindahl
1998-01-14  5:00             ` bmeyer
1998-01-15  5:00               ` cekim
1998-01-14  5:00     ` Donald K.Sinclair
1998-01-14  5:00       ` Rob Komar
1998-01-16  5:00         ` Torbjorn Lindgren
1998-01-17  5:00           ` Rob Komar
1998-01-14  5:00       ` cekim
  -- strict thread matches above, loose matches on Subject: below --
1998-01-13  5:00 Robert Harley
1998-01-14  5:00 ` cekim
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