Re: Floating point constant question

hbaker@netcom.com (Henry G. Baker)
Thu, 31 Mar 1994 18:55:42 GMT

          From comp.compilers

Related articles
[2 earlier articles]
Re: Floating point constant question chase@Think.COM (1994-03-29)
Re: Floating point constant question bill@amber.csd.harris.com (1994-03-29)
Re: Floating point constant question maydan@mti.mti.sgi.com (1994-03-29)
Re: Floating point constant question hbaker@netcom.com (1994-03-30)
Re: Floating point constant question conte@ece.scarolina.edu (1994-03-30)
Re: Floating point constant question chase@Think.COM (1994-03-30)
Re: Floating point constant question hbaker@netcom.com (1994-03-31)
Re: Floating point constant question przemek@rrdjazz.nist.gov (1994-03-31)
| List of all articles for this month |
Newsgroups: comp.compilers
From: hbaker@netcom.com (Henry G. Baker)
Keywords: arithmetic, optimize
Organization: nil
References: 94-03-157 94-03-183
Date: Thu, 31 Mar 1994 18:55:42 GMT

Scott Mahlke <mahlke@crhc.uiuc.edu> writes:
|> I have a question regarding the following compiler optimization:
|> x = y / 500.0; ===> x = y * 0.002;


Here's a real, live example taken from my paper "Computing A*B (mod N)
Efficiently in ANSI C", Sigplan Not. 27,1 (Jan 1992), 95-98.


(I'll try to type it by hand w/o making transcription errors)


void emul(d,q,r,hp,lp) unsigned long d,q,r,*hp,*lp;
/* 31x31+31 bit multiplication and addition. */
{unsigned long l; double fd,fr,fl;
  fd=d; fr=r; fl=l=(d*q+r)&0x7fffffff; *lp=l;
  *hp=((q&0x7fff0000)*fd+(((q&0xffff)*fd+fr)-fl))/2147483648.0;}
                                                                                                ^^^^^^^^^^^^^


This scheme works pretty well on the 80860, where integer multiplication
is done by the floating point unit, anyway, so there isn't any efficiency
loss (well, hardly any efficiency loss) in converting to float. In any
case, I think the Metaware C compiler _does_ convert this division into a
multiplication, which is ok, since the divisor is a power of 2 and exactly
representable.


While we are on the subject, why don't ANSI C compilers open code the
math.h 'ldexp' function? When you need it, you really need it to be fast;
otherwise, it's pretty much useless. Most of the floating point units
have this functionality as a single instruction, so this shouldn't be a
big deal. The usual optimizations should apply -- a constant second
argument should be faster, etc.
--


Post a followup to this message

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