我要评分
获取效率
正确性
完整性
易理解

Use GCC Compiler to Optimize the Floating-Point Calculation Precision

Symptom

When the compilation optimization option is set to the -O2 level or higher, the Kunpeng processor combines consecutive floating-point number multiplication and addition into multiply-add operation to improve performance and precision. For compilation options above the -O2 level, the x86 processor does not combine multiplication and addition into multiply-add operation. Therefore, after consecutive floating-point multiplication and addition operations are performed on the two processors, the 16 digits after the decimal point are different.

Cause

In the Kunpeng processor, the compilation optimization option is set to the -O2 level or higher, and consecutive floating-point multiplication (fmul instruction) and addition (fadd instruction) operations are performed, for example, an expression: (a+=b*c) is merged into a multiply-add operation (fmadd instruction). fmadd considers the multiplication and addition of floating-point numbers as an inseparable operation and does not round the intermediate result. Therefore, the calculation results are different.

Impact on the System

When the compilation optimization option is set to -O2 or higher, the floating-point multiply-add performance is improved, but the calculation precision is affected.

Procedure

Add the compilation option -ffp-contract=off to disable the optimization.