Widening of values

The previous section illustrated that certain platforms compute floating-point expressions faster using extra range and precision. The issue applies to function calls as well. Consider the simple construct:

      double f(float x) {...}
      double g(float y, float z) {
          return f(y + z);
      }
It involves the same issues as the first example. What is the type of the value passed to f? On a platform like X86 that operates internally to a format wider than float, the sequence might be:

  1. Compute the sum of y and z to long double.
  2. Convert the sum to type float
  3. Pass the float value parameter to f.
  4. Within f, convert the parameter to long double in a register for use.
The conversions to and from long double are not the best use of hardware resources; the code is slower than necessary.

For almost all numerical programs, strict adherence to the types specified by declared variables is unnecessary and possibly wasteful. Strict type adherence produced a less accurate result in the case of y = y + (x * x) and a slower computation in the case of f(y + z). However, some algorithms do depend critically on the rounding of intermediate values to a specified precision.

FPCE defines compiler pragmas for these situations:

Setting these pragmas to on can improve a program's performance. But there is no guarantee that a compiler will honor them by widening the relevant data. On CommonPoint platforms, the constructs float_t and double_t capture the spirit of fp_wide_variables in a more explicit and portable fashion.

NOTE For most portable results, use intermediate values of type float_t and double_t. They allow the compiler to do what's best for the hardware. In the (rare) circumstance that a float or double value must be rounded to exactly the precision of the type, use an explicit cast.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker