[comp.lang.c] FP math used for int operations

pmk@hall.cray.com (Peter Klausler) (01/23/88)

In article <2335@haddock.ISC.COM>, karl@haddock.ISC.COM (Karl Heuer) writes:
> ... Just as the "/" symbol invokes the integer-divide operator when both
> operands are integers (it most certainly does *not* invoke floating-point
> divide and then truncate the result), ...

Our architectures do not possess integer division instructions, so we're
forced to do generic integer division (i=j/k) via something like:

	i = (int) ((float) j / (float) k)

In fact, it's even more complicated, since there's no actual floating
division instruction either, but rather a "reciprocal approximation"
instruction, which generates a half-precision reciprocal that must then
be refined with the Newton-Raphson method iteration instruction.

It gets worse on Cray-1's and X-MP's, which don't have instructions
for coercing full ints into floats or vice versa. All of these
complications conspire to require 15 instructions for i=j/k.

(There's no integer multiplication instruction for full-word integers,
either, so similar tricks are also necessary for things like i=j*k.)

Moral:	Try to use floats when programming a scientific machine.
	Your generated code will use them anyway.

- Peter Klausler @ Cray Research compiler development (ihnp4!cray!hall!pmk)

firth@sei.cmu.edu (Robert Firth) (01/24/88)

In article <3337@hall.cray.com> pmk@hall.cray.com (Peter Klausler) writes:

>Our architectures do not possess integer division instructions, so we're
>forced to do generic integer division (i=j/k) via something like:
>
>	i = (int) ((float) j / (float) k)

No you're not: you can write a subroutine to perform integer division,
the way we did on the old PDP-11/20 and MC68000.  It might be slower,
but at least it would give the right answer for (almost) all of the domain.

>In fact, it's even more complicated, since there's no actual floating
>division instruction either, but rather a "reciprocal approximation"
>instruction, which generates a half-precision reciprocal that must then
>be refined with the Newton-Raphson method iteration instruction.

Which means that the division of two exactly represented quantities does
not always yield the mathematically correct exactly representable
quotient.  Maybe you need to write a subroutine for FLOATING division
also, since to any serious numerical programmer that behaviour is
USELESS.

>Moral:	Try to use floats when programming a scientific machine.
>	Your generated code will use them anyway.

Moral: The world is full of junk hardware.  And the more you buy it, the
more they'll build it.