Re: Folk Theorem: Assemblers are superior to Compilers

adk@sun13.SCRI.FSU.EDU (Tony Kennedy)
Fri, 29 Oct 1993 03:48:36 GMT

          From comp.compilers

Related articles
[10 earlier articles]
Re: Folk Theorem: Assemblers are superior to Compilers winikoff@munta.cs.mu.OZ.AU (1993-10-28)
Re: Folk Theorem: Assemblers are superior to Compilers prener@watson.ibm.com (1993-10-28)
Folk Theorem: Assemblers are superior to Compilers Mark_Prince@gec-epl.co.uk (1993-10-28)
Re: Folk Theorem: Assemblers are superior to Compilers mps@dent.uchicago.edu (1993-10-28)
Re: Folk Theorem: Assemblers are superior to Compilers toon@moene.indiv.nluug.nl (1993-10-28)
Re: Folk Theorem: Assemblers are superior to Compilers raymondc@microsoft.com (1993-10-28)
Re: Folk Theorem: Assemblers are superior to Compilers adk@sun13.SCRI.FSU.EDU (1993-10-29)
Re: Folk Theorem: Assemblers are superior to Compilers elliottm@csulb.edu (1993-10-29)
Re: Folk Theorem: Assemblers are superior to Compilers jvn@fermi.clas.virginia.edu (Julian V. Noble) (1993-10-29)
Re: Folk Theorem: Assemblers are superior to Compilers Freek.Wiedijk@phil.ruu.nl (1993-10-29)
Re: Folk Theorem: Assemblers are superior to Compilers synaptx!thymus!daveg@uunet.UU.NET (Dave Gillespie) (1993-10-29)
Re: Folk Theorem: Assemblers are superior to Compilers rfg@netcom.com (1993-10-30)
Re: Folk Theorem: Assemblers are superior to Compilers qualtrak@netcom.com (1993-10-30)
[11 later articles]
| List of all articles for this month |

Newsgroups: comp.compilers
From: adk@sun13.SCRI.FSU.EDU (Tony Kennedy)
Keywords: C, optimize, assembler
Organization: SCRI, Florida State University
References: 93-10-114 93-10-127
Date: Fri, 29 Oct 1993 03:48:36 GMT

> Dave Gillespie <synaptx!daveg@uunet.UU.NET> writes:


> My favorite example comes from an i860 loop we wrote here,
> which involved a bunch of multipy-adds followed by something
> of the form max(min(x, limit), -limit), i.e., constrain
> abs(x) <= limit. Branches are awful on the i860...


I am not sure why a compiler would want to use branches. Noting that


max(x,limit) = max(x-limit,0) + limit


it suffices to evaluate max(z,0) up to a couple of arithmetic
operations. Now,


max(z,0) = z & (z > 0)


where "&" means "bitwise and", and "z > 0" is a word of all ones if z
is greater than zero and a word of all zeros if it is not. This may be
computed by propagating the sign bit of z through an entire word and
logically negating it. In C terms this leads to the following function
(for 32 long ints and floats with the sign bit at the left end)


float max(float x, float y) {
                    union fl {float f; long l;} z;
z.f = x - y;
z.l &= ~z.l >> 31;
return z.f + y;}


I am not claiming that this is faster than your trick, but the C
compiler *could* generate code without any branches.


(Caveat: I checked my method on a VAX, where the sign bit is in the
middle of word. I have not directly checked the version given here).
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.