Mon, 3 Apr 1995 21:17:40 GMT

Related articles |
---|

Q: division vs multiplication t.hulek@imperial.ac.uk (1995-03-24) |

Re: Q: division vs multiplication kptben@aol.com (1995-04-02) |

Re: Q: division vs multiplication Terje.Mathisen@hda.hydro.com (1995-04-02) |

Re: Q: division vs multiplication mikeq@primenet.com (1995-04-02) |

Re: Q: division vs multiplication hbaker@netcom.com (1995-04-03) |

Re: Q: division vs multiplication davidm@Rational.COM (1995-04-03) |

Re: Q: division vs multiplication brandis@inf.ethz.ch (1995-04-04) |

Re: Q: division vs multiplication Terje.Mathisen@hda.hydro.com (1995-04-06) |

Re: Q: division vs multiplication meissner@cygnus.com (Mike Meissner) (1995-04-16) |

Re: Q: division vs multiplication martens@cis.ohio-state.edu (1995-04-16) |

Re: Q: division vs multiplication jmccarty@spdmail.spd.dsccc.com (1995-04-18) |

Re: Q: division vs multiplication leichter@zodiac.rutgers.edu (1995-04-11) |

[7 later articles] |

Newsgroups: | comp.compilers |

From: | davidm@Rational.COM (David Moore) |

Keywords: | arithmetic, optimize |

Organization: | Rational Software Corporation |

References: | 95-04-003 |

Date: | Mon, 3 Apr 1995 21:17:40 GMT |

t.hulek@imperial.ac.uk (Mr Tomas Hulek) writes:

*>My question is: does this also apply to division by constants that are powers*

*>of 2? For real-world compilers (C,C++ let's say) and machines, is there any*

*>benefit in using*

*>r = 0.5*(a+b);*

*>rather than*

*>r = (a+b)/2.0;*

*>???*

Just subtract 1 from the exponent, right?

Actually, I have done just that at times (working in assembler) but

there are several gotchas.

i) Does not work with denormalised numbers, or numbers that would

become denormalised if you did it the usual way. As a result, this is

only really useful after range reduction has guaranteed that you are

far enough away from zero.

Of course, you could test for this, but then your multiply would

probably have finished faster - and even if it would not, you have

burned a lot more issue slots than required by the one instruction

for the multiply, and at times issue slots are your limiting resource.

ii) Requires a 1 bit up near the left end of a word, which can be

hard to do. It might take 2 cycles to get that bit in place, in which

time you could start two floating point multiplies (of course, it

would take two cycles to load 0.5 too - but if you have 0.5 to hand,

it is not a win)

iii) (The killer on most modern Risc machines). There is no simple

path from the floating point registers to the integer registers. You

probably have to store the value to memory (possibly generating a

cache miss!) and reload it. Then you have to store and reload on the

way back to the floater. By now, your multiply would be done.

So, in general it is better to use the 0.5*(a+b) form. One should be

aware that 0.5*(a+b) and (a+b)/2.0 may not produce the same value. On

some compilers, 0.5 will get represented as 0.49999999999 (add 9's to

taste), although this should undoubtably be considered a bug.

Optimizers have to be cautious about replacing floating divides by

multiplies, so I expect many C compilers will not convert the divide

to a multiply for you.

[ASIDE]. A year or two ago, there were a couple of papers that

appeared together somewhere, and which I know I have, but which I

have been unable to locate. One was entitled something like "How to

write floating point numbers" and the other "How to read floating

point numbers".

Can anyone give me the reference?

--

Post a followup to this message

Return to the
comp.compilers page.

Search the
comp.compilers archives again.