简体   繁体   中英

How to compile parts of code with different floating-point options?

I'm optimizing some C++ code using the Intel compiler and I need to use different compile options in some parts of code, in a single source file.

I know the #pragma pack directive can change structure members' alignment inside code but I'd like to know if there are other directives for other options.

In my case, I'm compiling my code with the /fp:precise option but I'd like to use /fp:fast in some parts of the code.

You can select some floating-point options for specific functions using #pragma float_control (supported by both MSVC and the Intel® C++ compiler).

However, note the important caveat given in the linked document:

The float_control pragma doesn't have the same behavior as the /fp compiler option. The float_control pragma only governs part of the floating-point behavior. It must be combined with fp_contract and fenv_access pragmas to recreate the /fp compiler options...

Using this and related #pragma directives will allow you to compile code sections in a single translation unit with different options.

According to the table shown in the linked document (just below the quoted text), the only difference between /fp:precise and /fp:fast is controlled by the float_control pragma (but you will need others to switch from /fp:strict ), so code like the following would allow one function in a given source file to use the fast option:

#pragma float_control( precise, off, push ) // Save current setting and turn off /fp:precise
double FastFloatFunc(double x)
{
    double y = x * (x - 1);
    // Do something: Generated code will use /fp:fast
    return y;
}
#pragma float_control(pop) // Restore file's default settings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM