浮点优化选项 -ffast-math:极大地提高浮点运算速度

时间:2021-01-18 09:11:02

关于浮点优化选项:(摘自C in a nutshell

C99 浮点环境支持科学和数学级别的应用,这些应用必须有相当高的精度,但是某些应用却不是如此,注重速度高于精度。对于这些以速度为重的应用, -ffast-math 选项定义了预处理器宏 __FAST_MATH__, 指示编译不必遵循 IEEE ISO 的浮点运算标准。-ffast-math标记是一个群组选项,可以分别启用下面六个优化选项:

-fno-math-errno

Disables the use of the global variable errno for math functions that represent a single floating-point instruction.

-funsafe-math-optimizations

The "unsafe math optimizations" are those that might violate floating-point math standards, or that do away with verification of arguments and results. Using such optimizations may involve linking code that modifies the floating-point processor's control flags.

-fno-trapping-math

Generates "nonstop" code, on the assumption that no math exceptions will be raised that can be handled by the user program.

-ffinite-math-only

Generates executable code that disregards infinities and NaN ("not a number") values in arguments and results.

-fno-rounding-math

This option indicates that your program does not depend on a certain rounding behavior, and does not attempt to change the floating-point environment's default rounding mode. This setting is currently the default, and its opposite, -frounding-math, is still experimental.

-fno-signaling-nans

This option permits optimizations that limit the number of floating-point exceptions that may be raised by signaling NaNs. This setting is currently the default, and its opposite, -fsignaling-nans, is still experimental.

测试

测试方法

分别使用marvellgcc4.1.1 以及 maemo 编译器(codesourcery 2005q3-2gcc3.4.4)并使用不同的编译选项进行测试对比。 程序运行在 PXA310 624M CPU 下。

marvell 编译器 + -ffast-math

arm-iwmmxt-linux-gnueabi-gcc float.c -o float1

arm-iwmmxt-linux-gnueabi-strip float1

arm-iwmmxt-linux-gnueabi-gcc -ffast-math float.c -o float2

arm-iwmmxt-linux-gnueabi-strip float2

测试结果

/mnt/share # ./float1

Start time is: 0.0

DIV End time is: 4.4294564932

MUL End time is: 5.4294622841

ADD End time is: 6.4294699686

SUB End time is: 7.4294810851

/mnt/share # ./float2

Start time is: 0.0

DIV End time is: 1.66616

MUL End time is: 2.122699

ADD End time is: 3.253242

SUB End time is: 4.363897

Meamo 编译器 + -ffast-math

gcc -mfloat-abi=soft -march=iwmmxt float.c -o cs_float1

strip cs_float1

gcc -mfloat-abi=soft -march=iwmmxt -ffast-math float.c -o cs_float2

strip cs_float2

测试结果

/mnt/share # ./cs_float1

Start time is: 0.0

DIV End time is: 4.4294865258

MUL End time is: 5.258137

ADD End time is: 7.4294683333

SUB End time is: 8.169521

/mnt/share # ./cs_float2

Start time is: 0.0

DIV End time is: 1.330878

MUL End time is: 3.4294658034

ADD End time is: 4.110022

SUB End time is: 6.4294530884

 

以下运行在 meamo文件系统下:

Nokia-N810-42-19:/mnt/share# ./cs_float1

Start time is: 0.0

DIV End time is: 4.4294844089

MUL End time is: 5.229302

ADD End time is: 7.4294646888

SUB End time is: 8.127102

Nokia-N810-42-19:/mnt/share# ./cs_float2

Start time is: 0.0

DIV End time is: 1.320814

MUL End time is: 2.673556

ADD End time is: 4.84794

SUB End time is: 5.530572