Important: The information in this document is obsolete and should not be used for new development.
Environmental Access Switch
To allow compilers to better optimize applications without ignoring the floating-point environment altogether, the FPCE technical report defines the following pragma to be used as an environmental access switch:
#pragma fenv_access on | off | defaultThe environmental access switch specifies whether an application may access the floating-point environment. Access to the floating-point environment must occur as if at run time, whereas optimizations occur at compile time. At compile time, the default (to nearest) rounding mode is in effect and all exception flags are clear (this is the default environment). Without an environmental access switch, the compiler must always assume that every floating-point expression might produce an exception, and therefore the compiler cannot perform some types of optimizations (such as forward and backward code motion) on floating-point expressions.If the environmental access switch is supported, whenever programmers use any of the environmental control functions (described in Chapter 8, "Environmental Control Functions"), they should first turn on the switch. Where the switch is on, the compiler does not fully optimize floating-point expressions, because it assumes that that part of the application can access the floating-point environment. (Accessing the floating-point environment means setting the rounding direction or reading the status of the exception flags.) Where the switch is off, the compiler can fully optimize any floating-point expression because it assumes that that part of the application does not access the floating-point environment. If the application accesses the floating-point environment when the switch is off, the result is undefined.
If an application uses the default rounding mode and does not access floating-point exception flags, the programmer may turn off the environmental access switch, allowing the application to be fully optimized. If the application contains modules that must access the floating-point environment, the programmer must turn on the environmental access switch in those modules and turn it off in all other modules. In this way, the modules that do not require access can be fully optimized.
The FPCE technical report recommends these programming conventions:
These conventions allow the programmer to ignore the floating-point environment altogether if default modes are sufficient for the application or function.
- A function call must not alter its caller's modes, clear its caller's flags, or depend on the state of its caller's flags unless the function is so documented.
- A function call is assumed to require default modes unless its documentation specifically promises otherwise or unless it does not contain floating-point expressions.
- A function call is assumed to have the potential of raising floating-point exceptions unless its documentation specifically promises otherwise or unless it does not contain floating-point expressions.
- At compile time, the default environment is in effect.
Where supported, the
fenv_access
pragma can occur only outside external declarations. It enables or disables compiler optimizations until anotherfenv_access
pragma is encountered or until the end of the module. The default state forfenv_access
is implementation dependent.