且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-08-21 15:40:42

转自:http://blog.csdn.net/zjujoe/article/details/2604157

目录(?)[-]

  1. 关于浮点优化选项摘自C in a nutshell
  2. 测试
    1. 测试方法
    2. marvell 编译器 -ffast-math
    3. 测试结果
    4. Meamo 编译器 -ffast-math
    5. 测试结果

关于浮点优化选项:(摘自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.

测试

测试方法

 

分别使用marvell(gcc4.1.1) 以及 maemo 编译器(codesourcery 2005q3-2,gcc3.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

【作者】张昺华
【新浪微博】 张昺华--sky
【twitter】 @sky2030_
【facebook】 张昺华 zhangbinghua
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.