Optimizing Runtime Efficiency
ANSI C99 has introduced features to optimize the efficiency of floating-point operations.
The types float_t
and
double_t
, defined in
math.h, represent the types used internally in
floating-point arithmetic. When these types are used in a program, no
conversions are necessary before arithmetic operations are
performed. The macro
FLT_EVAL_METHOD
indicates what the equivalent basic types
are, and returns one of the values described in Table 1-33.
|
FLT_EVAL_METHOD |
Type represented by float_t |
Type represented by double_t |
0 |
float |
double |
1 |
double |
double |
2 |
long double |
long double |
CPUs may have special machine instructions to perform standard
arithmetic operations quickly. Rounding and error conditions may also
be ignored. Optimizations of these kinds can be enabled by the pragma
FP_CONTRACT
. For example:
#pragma STDC FP_CONTRACT ON
The same pragma with the switch OFF rather than
ON disables such optimizations.
Furthermore, the macro
FP_FAST_FMA
is defined if the
“floating-point multiply-add”
function fma( x, y, z ), which returns
x*y+z, is implemented as a special instruction,
and is thus faster than separate multiplication and addition
operations. The macros
FP_FAST_FMAF
and
FP_FAST_FMAL
are analogous indicators for the
functions fmaf() and fmal().
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access