浮點優化選項 -ffast-math:極大地提升浮點運算速度【轉】

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

關於浮點優化選項:(摘自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..net

-funsafe-math-optimizationscode

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.blog

-fno-trapping-mathip

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

-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

相關文章
相關標籤/搜索