1. 程式人生 > >gcc的mtune和march選項分析

gcc的mtune和march選項分析

給定gcc優化選項時經常要指定march和mtune。我以前都將它們賦為一樣的值,例如pentium4.

今天仔細研究了一下它們的區別,原來還是有一些道道的。

首先是man gcc:

-mtune=name
      This option is very similar to the -mcpu= option, except that
      instead of specifying the actual target processor type, and hence
      restricting which instructions can be used, it specifies that GCC
      should tune the performance of the code as if the target were of
      the type specified in this option, but still choosing the instruc-
      tions that it will generate based on the cpu specified by a -mcpu=
      option.

上面是gcc3.2的manpage,現在mcpu已經不用了,一般都用march。望文生義,march指定的是當前cpu的架構,而mtune是真正作用於某一型號cpu的選項。

根據連結:
http://gentoo-wiki.com/Safe_Cflags

的解釋,march是“緊約束”,mtune是“鬆約束”。mtune可以提供後向相容性,也就是mtune=pentium-mmx編譯出來的程式,在pentium4處理器上也是可以執行的。

因此,像-march=i686 -mtune=pentium4這樣的優化選項編譯出來的程式,是為奔騰4處理器優化過的,但是在任何i686上都可以執行。

我猜,如果指定了-march=pentium3,那麼在奔騰4處理器上程式是不能執行的。

上面是自己根據別人的文件整理出來的一些說法,具體是不是這麼回事沒有試驗過。若有錯誤的地方,還請大蝦指正:-)