Single precision math in C

Guy Harris guy at auspex.UUCP
Tue Dec 13 03:33:37 AEST 1988


>I am looking for more or less portable ways to get C compilers to perform
>floating point math for intrinsic functions like *, /, etc.

If the operands of the operand in question are both floating point (or
one is floating point, which causes the other to be converted to
floating point), any valid C compiler will, obviously, generate code to
perform floating point math.  However...

>Because some of the machines that I run on have no floating point processor,
>it is useful to be able to force single-precision math for speed.  

...if you want to get the compiler to perform *single-precision*
floating point math, there's no way within the C language as specified
by K&R to get this, since it specifies that floating point math is done
in double precision.

However, in the draft ANSI C standard, specifying "float" rather than
"double" operands permits the compiler to do single-precision math.  (I
don't have the standard handy, so I don't know whether it *requires* the
compiler to do single-precision math.)

Furthermore...

>Is there a reasonably portable way to do what I want?  I would like to 
>make sure that I can recompile my programs on (at least) Sun 4s running
>SunOS 4.0.

...on some C implementations, there is a compiler option to request that
arithmetic involving "float"s but no "double"s be performed in single
precision.  On Sun machines (Sun-2, Sun-3, Sun-4; the manual page lists
them but not the Sun386i) under 4.0 and, I think, earlier releases, you
can give the compiler the "-fsingle" flag.

Similar flags may exist under other implementations.

There is no portable way of doing this; i.e., there's no way to write
your code so that *every* C implementation in existence will do
single-precision math, because not all of the ones written to the older
standard have a "single-precision floating point" flag.



More information about the Comp.lang.c mailing list