[comp.lang.c] Single precision math in C

bradb@ai.toronto.edu (12/10/88)

I am looking for more or less portable ways to get C compilers to perform
floating point math for intrinsic functions like *, /, etc.  In my case,
I write code that runs on IBM ATs with Microsoft C 5.1 and Turbo C 2.0.  
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.  

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.

Thanks in advance...

					(-:  Brads  :-)
					bradb@ai.toronto.edu

gwyn@smoke.BRL.MIL (Doug Gwyn ) (12/13/88)

In article <88Dec10.135843est.10521@ephemeral.ai.toronto.edu> bradb@ai.toronto.edu writes:
>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.  

So, use "float" for such data types (don't forget to cast the inherently
double floating-point constants, e.g. (float)1.0).

Watch out for varargs parameters and arguments to functions when you do
this; normally those will have to be double.

guy@auspex.UUCP (Guy Harris) (12/13/88)

>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.