Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

gcccpuopt 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #!/bin/sh
  2. # Print the gcc cpu specific options tailored for the current CPU
  3. # License: LGPLv2
  4. # Author:
  5. # http://www.pixelbeat.org/
  6. # Notes:
  7. # This script currently supports Linux,FreeBSD,Cygwin
  8. # This script is x86 (32 bit) specific
  9. # It should work on any gcc >= 2.95 at least
  10. # It only returns CPU specific options. You probably also want -03 etc.
  11. # Changes:
  12. # V0.1, 12 Mar 2003, Initial release
  13. # V0.2, 01 Jun 2005, Added support for 3.2>=gcc<=4.0
  14. # V0.3, 03 Jun 2005, Added support for pentium-m
  15. # V0.4, 03 Jun 2005, Fix silly bugs
  16. # V0.5, 07 Jun 2005, Clarify/Simplify confusing floating point expr usage
  17. # Print warning when CPU only supported on a newer gcc
  18. # V0.6, 15 Dec 2006, Added support for Intel Core and Core 2 processors
  19. # Added support for 4.1>=gcc<=4.3
  20. # Added support for gcc -msse3 option
  21. # Added support for new gcc -march=native option
  22. # V0.7, 18 Dec 2006, Changes from Conor McDermottroe
  23. # Added support for FreeBSD
  24. # Remove bash specific constructs
  25. # Better error handling
  26. # V0.8, 19 Dec 2006, Give warnings and 32 bit -march on 64 bit platforms.
  27. # Previously it just gave an invalid blank -march.
  28. # Reported and tested by Ewan Oughton.
  29. # V0.9, 30 Apr 2007, Give error if compiler not present.
  30. # Warn about rather than default to -march=native option.
  31. # V0.92, 08 Nov 2007, Change from Krzysztof Jankowski to support Cygwin.
  32. # Added support for AMD family 10 processors.
  33. # Add support for gcc -msse4 & -msse5 options.
  34. # Use "prescott" rather than "pentium4" for all
  35. # models >= 3 in intel family 15, not just model 3.
  36. # V0.93, 13 Nov 2007, Oops, actually not all intel family 15, model >= 3
  37. # are prescotts. Use the sse3 flag to distinguish.
  38. # V0.94, 31 Dec 2007, Oops, actually all intel family 15, model >= 3
  39. # are prescotts. This was indicated by Adam Drzewiecki.
  40. # I was confused by a bug in older linux kernels where pni
  41. # was not reported for my intel "F41" processor at least.
  42. # V0.95, 18 Jan 2008, Changes from Conor McDermottroe
  43. # Support for Mac OS X
  44. # Support for FreeBSD base system
  45. # V0.96, 17 Dec 2008, Following a report from Alfredo Pons, add support
  46. # for newer intel core2 processors (models 23,26,29,...)
  47. # by assuming models >= 15 are core2.
  48. # Report details of unrecognised CPUs.
  49. # V0.97, 19 Dec 2008, Use prescott for Intel Atom (model 28).
  50. # Ensure all errors are output to stderr.
  51. # V0.98, 24 Feb 2009, Actually Intel Atom (model 28) is core2 ISA compliant.
  52. # It's an in-order core (like i586) so mtune as such.
  53. # V0.99, 30 Apr 2009, Intel Atom (model 28) is getting a corresponding "atom"
  54. # option in GCC 4.5
  55. # V0.99.3, 11 Aug 2009, Support for AMD Geode LX processor.
  56. # V0.99.4, 24 Oct 2009, Following a report from Maxime de Roucy, add support
  57. # for AMD family 17 (griffin).
  58. # V0.99.12, 22 Oct 2013
  59. # http://github.com/pixelb/scripts/commits/master/scripts/gcccpuopt
  60. if [ "$1" = "--version" ]; then
  61. echo "0.99.12" && exit
  62. fi
  63. # This table shows when -march options were introduced into _official_ gcc releases.
  64. # Note there are vendor deviations that complicate this.
  65. # For e.g. Red Hat introduced the prescott option in 3.3-13.
  66. # gcc-2.95 = i386, i486, i586,pentium, i686,pentiumpro, k6
  67. # gcc-3.0 += athlon
  68. # gcc-3.1 += pentium-mmx, pentium2, pentium3, pentium4, k6-2, k6-3, athlon-{tbird, 4,xp,mp}
  69. # gcc-3.3 += winchip-c6, winchip2, c3
  70. # gcc-3.4.0 += k8,opteron,athlon64,athlon-fx, c3-2
  71. # gcc-3.4.1 += pentium-m, pentium3m, pentium4m, prescott, nocona
  72. # gcc-4.3 += core2, amdfam10, geode
  73. # gcc-4.5 += atom
  74. # gcc-4.6 += corei7, corei7-avx, bdver1, btver1
  75. # gcc-4.7 += core-avx-i, core-avx2, bdver2
  76. # gcc-4.8 += btver2
  77. [ -z "$CC" ] && CC=gcc
  78. try_gcc_options() {
  79. $CC $* -S -o /dev/null -xc /dev/null >/dev/null 2>&1
  80. }
  81. if ! try_gcc_options; then
  82. echo "Error: Couldn't execute your compiler ($CC)" >&2
  83. exit 1
  84. fi
  85. if try_gcc_options -march=native; then
  86. echo "Warning: Your compiler supports the -march=native option which you may prefer" >&2
  87. #gcc -march=native --verbose -xc /dev/null 2>&1 | sed -n 's|.*/dev/null \(-march.*-mtune[^ ]*\).*|\1|p' >&2
  88. fi
  89. if ! try_gcc_options -march=i386; then
  90. if ! try_gcc_options -m32 -march=i386; then
  91. echo "Error: This script only supports 32 bit x86 architectures" >&2
  92. exit 1
  93. else
  94. echo "Warning: The optimum *32 bit* architecture is reported" >&2
  95. m32="-m32 "
  96. fi
  97. fi
  98. try_line() {
  99. skip=0
  100. for arch in $1; do
  101. if try_gcc_options $m32 -march=$arch; then
  102. echo $arch
  103. return
  104. elif [ "$skip" = "0" ] && [ "$arch" != "native" ]; then
  105. skip=1
  106. echo "Warning: Newer versions of GCC better support your CPU with -march=$arch" >&2
  107. fi
  108. done
  109. return 1
  110. }
  111. read_cpu_data_linux() {
  112. IFS=":"
  113. while read name value; do
  114. unset IFS
  115. name=`echo $name` #strip spaces
  116. value=`echo $value` #strip spaces
  117. if [ "$name" = "vendor_id" ]; then
  118. value=`echo $value | sed 's/\([^ ]*\).*/\1/'` #take first word
  119. fi
  120. IFS=":"
  121. if [ "$name" = "vendor_id" ]; then
  122. vendor_id="$value"
  123. elif [ "$name" = "cpu family" ]; then
  124. cpu_family="$value"
  125. elif [ "$name" = "model" ]; then
  126. cpu_model="$value"
  127. elif [ "$name" = "flags" ]; then
  128. flags="$value"
  129. break #flags last so break early
  130. fi
  131. done < /proc/cpuinfo
  132. unset IFS
  133. }
  134. read_cpu_data_freebsd() {
  135. local _line _cpu_id
  136. if [ ! -r /var/run/dmesg.boot ]; then
  137. echo "Error: /var/run/dmesg.boot does not exist!" >&2
  138. exit 1;
  139. fi
  140. IFS="
  141. "
  142. for _line in `grep -A2 '^CPU: ' /var/run/dmesg.boot`; do
  143. if [ -n "`echo $_line | grep '^ Origin = '`" ]; then
  144. vendor_id="`echo $_line | sed -e 's/^ Origin = .//' -e 's/[^A-Za-z0-9].*$//'`"
  145. _cpu_id="`echo $_line | sed -e 's/^.*Id = //' -e 's/ .*$//' -e 'y/abcdef/ABCDEF/'`"
  146. cpu_family=$(( ($_cpu_id & 0xF0F) >> 8 )) #FreeBSD 5.0 sh doesn't support 0x
  147. cpu_model=$(( (($_cpu_id & 0xF0) >> 4) + (($_cpu_id & 0xF0000) >> 12) ))
  148. fi
  149. if [ -n "`echo $_line | grep '^ Features='`" ]; then
  150. flags="`echo $_line | sed -e 's/^.*<//' -e 's/>.*//' -e 's/,/ /g' | tr 'A-Z' 'a-z'`"
  151. fi
  152. done
  153. unset IFS
  154. }
  155. read_cpu_data_darwin() {
  156. vendor_id="`/usr/sbin/sysctl -n machdep.cpu.vendor`"
  157. cpu_family="`/usr/sbin/sysctl -n machdep.cpu.family`"
  158. cpu_model="`/usr/sbin/sysctl -n machdep.cpu.model`"
  159. flags="`/usr/sbin/sysctl -n machdep.cpu.features | tr 'A-Z' 'a-z'`"
  160. }
  161. read_cpu_data() {
  162. # Default values
  163. vendor_id="NotFound"
  164. cpu_family="-1"
  165. cpu_model="-1"
  166. flags=""
  167. case "$(uname)" in
  168. Linux|CYGWIN*)
  169. read_cpu_data_linux ;;
  170. FreeBSD)
  171. read_cpu_data_freebsd ;;
  172. Darwin)
  173. read_cpu_data_darwin ;;
  174. *)
  175. echo "Error: $(uname) is not a supported operating system" >&2
  176. exit 1 ;;
  177. esac
  178. }
  179. read_cpu_data
  180. if [ "$vendor_id" = "AuthenticAMD" ]; then
  181. if [ $cpu_family -eq 4 ]; then
  182. _CFLAGS="-march=i486"
  183. elif [ $cpu_family -eq 5 ]; then
  184. if [ $cpu_model -lt 4 ]; then
  185. _CFLAGS="-march=pentium"
  186. elif [ \( $cpu_model -eq 6 \) -o \( $cpu_model -eq 7 \) ]; then
  187. _CFLAGS="-march=k6"
  188. elif [ \( $cpu_model -eq 8 \) -o \( $cpu_model -eq 12 \) ]; then
  189. line="k6-2 k6"
  190. elif [ \( $cpu_model -eq 9 \) -o \( $cpu_model -eq 13 \) ]; then
  191. line="k6-3 k6-2 k6"
  192. elif [ $cpu_model -eq 10 ]; then #geode LX
  193. line="geode k6-2 k6"
  194. #The LX supports 3dnowext in addition to the k6-2 instructions,
  195. #however gcc doesn't support explicitly selecting that.
  196. fi
  197. elif [ $cpu_family -eq 6 ]; then
  198. if [ $cpu_model -le 3 ]; then
  199. line="athlon k6-3 k6-2 k6"
  200. elif [ $cpu_model -eq 4 ]; then
  201. line="athlon-tbird athlon k6-3 k6-2 k6"
  202. elif [ $cpu_model -ge 6 ]; then #athlon-{4,xp,mp} (also geode NX)
  203. line="athlon-4 athlon k6-3 k6-2 k6"
  204. fi
  205. elif [ $cpu_family -eq 15 ]; then #k8,opteron,athlon64,athlon-fx
  206. line="k8 athlon-4 athlon k6-3 k6-2 k6"
  207. elif [ $cpu_family -eq 16 ] || #barcelona,amdfam10
  208. [ $cpu_family -eq 17 ] || #griffin
  209. [ $cpu_family -eq 18 ]; then #llano
  210. line="amdfam10 k8 athlon-4 athlon k6-3 k6-2 k6"
  211. elif [ $cpu_family -eq 20 ]; then #bobcat
  212. line="btver1 amdfam10 k8 athlon-4 athlon k6-3 k6-2 k6"
  213. elif [ $cpu_family -eq 21 ]; then #bulldozer
  214. if echo "$flags" | grep -q bmi; then #piledriver
  215. line="bdver2 bdver1 btver1 amdfam10 k8 athlon-4 athlon k6-3 k6-2 k6"
  216. else
  217. line="bdver1 btver1 amdfam10 k8 athlon-4 athlon k6-3 k6-2 k6"
  218. fi
  219. elif [ $cpu_family -eq 22 ]; then #jaguar
  220. line="btver2 btver1 amdfam10 k8 athlon-4 athlon k6-3 k6-2 k6"
  221. fi
  222. elif [ "$vendor_id" = "CentaurHauls" ]; then
  223. if [ $cpu_family -eq 5 ]; then
  224. if [ $cpu_model -eq 4 ]; then
  225. line="winchip-c6 pentium"
  226. elif [ $cpu_model -eq 8 ]; then
  227. line="winchip2 winchip-c6 pentium"
  228. elif [ $cpu_model -ge 9 ]; then
  229. line="winchip2 winchip-c6 pentium" #actually winchip3 but gcc doesn't support this currently
  230. fi
  231. elif [ $cpu_family -eq 6 ]; then
  232. if echo "$flags" | grep -q cmov; then
  233. fallback=pentiumpro
  234. else
  235. fallback=pentium #gcc incorrectly assumes i686 always has cmov
  236. fi
  237. if [ $cpu_model -eq 6 ]; then
  238. _CFLAGS="-march=pentium" # ? Cyrix 3 (samuel)
  239. elif [ $cpu_model -eq 7 ] || [ $cpu_model -eq 8 ]; then
  240. line="c3 winchip2 winchip-c6 $fallback"
  241. elif [ $cpu_model -ge 9 ]; then
  242. line="c3-2 c3 winchip2 winchip-c6 $fallback"
  243. fi
  244. fi
  245. elif [ "$vendor_id" = "GenuineIntel" ]; then
  246. if [ $cpu_family -eq 3 ]; then
  247. _CFLAGS="-march=i386"
  248. elif [ $cpu_family -eq 4 ]; then
  249. _CFLAGS="-march=i486"
  250. elif [ $cpu_family -eq 5 ]; then
  251. if [ $cpu_model -ne 4 ]; then
  252. _CFLAGS="-march=pentium"
  253. else
  254. line="pentium-mmx pentium" #No overlap with other vendors
  255. fi
  256. elif [ $cpu_family -eq 6 ]; then
  257. if [ \( $cpu_model -eq 0 \) -o \( $cpu_model -eq 1 \) ]; then
  258. _CFLAGS="-march=pentiumpro"
  259. elif [ \( $cpu_model -ge 2 \) -a \( $cpu_model -le 6 \) ]; then #4=TM5600 at least
  260. line="pentium2 pentiumpro pentium-mmx pentium i486 i386"
  261. if [ \( $cpu_model -eq 2 \) ]; then #qemu generic
  262. echo "\
  263. Warning: Unspecified QEMU CPU model.
  264. Please consider upgrading QEMU or configuring it to use a specific model." >&2
  265. fi
  266. elif [ \( $cpu_model -eq 9 \) -o \( $cpu_model -eq 13 \) ]; then #centrino
  267. line="pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
  268. elif [ \( $cpu_model -eq 14 \) ]; then #Core
  269. line="prescott pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
  270. elif [ $cpu_model -eq 28 ] || # pine
  271. [ $cpu_model -eq 38 ] || # oak
  272. [ $cpu_model -eq 54 ]; then # cedar
  273. line="atom core2 pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
  274. elif [ $cpu_model -eq 26 ] ||
  275. [ $cpu_model -eq 30 ] ||
  276. [ $cpu_model -eq 31 ] ||
  277. [ $cpu_model -eq 46 ] ||
  278. # ^ Nehalem ^
  279. [ $cpu_model -eq 37 ] ||
  280. [ $cpu_model -eq 44 ] ||
  281. [ $cpu_model -eq 47 ]; then
  282. # ^ Westmere ^
  283. line="corei7 core2 pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
  284. elif [ $cpu_model -eq 42 ] || #Sandy Bridge
  285. [ $cpu_model -eq 45 ]; then #Sandy Bridge E
  286. line="corei7-avx corei7 core2 pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
  287. elif [ $cpu_model -eq 58 ] || #Ivy Bridge
  288. [ $cpu_model -eq 62 ]; then #Ivy Bridge E
  289. line="core-avx-i corei7-avx corei7 core2 pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
  290. elif [ $cpu_model -eq 60 ] || #Haswell
  291. [ $cpu_model -eq 63 ]; then #Haswell E
  292. line="core-avx2 core-avx-i corei7-avx corei7 core2 pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
  293. elif [ $cpu_model -eq 15 ] ||
  294. # ^ Merom ^
  295. [ $cpu_model -eq 22 ] ||
  296. # ^ Conroe-L ^
  297. [ $cpu_model -eq 23 ] ||
  298. [ $cpu_model -eq 29 ]; then
  299. # ^ Penryn ^
  300. line="core2 pentium-m pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
  301. elif [ \( $cpu_model -ge 7 \) -a \( $cpu_model -le 11 \) ]; then
  302. line="pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
  303. fi
  304. elif [ $cpu_family -eq 15 ]; then
  305. line="pentium4 pentium3 pentium2 pentiumpro pentium-mmx pentium i486 i386"
  306. if [ $cpu_model -ge 3 ]; then
  307. line="prescott $line"
  308. fi
  309. fi
  310. elif [ "$vendor_id" = "Geode" ]; then #by NSC
  311. if [ $cpu_family -eq 5 ]; then
  312. if [ \( $cpu_model -eq 4 \) -o \( $cpu_model -eq 9 \) ]; then
  313. # Note both models 4 and 9 have cmov.
  314. # In addition, model 9 has cxmmx.
  315. # Note also, the "geode" gcc arch is for newer AMD geode cores
  316. # and is not appropriate for this older core.
  317. line="pentium-mmx pentium"
  318. fi
  319. fi
  320. fi
  321. if [ \( -z "$_CFLAGS" \) -a \( -z "$line" \) ]; then
  322. echo "\
  323. Unrecognised CPU. Please email the following to: P@draigBrady.com
  324. Vendor:$vendor_id family:$cpu_family model:$cpu_model
  325. flags:$flags" >&2
  326. exit 1
  327. fi
  328. [ -z "$_CFLAGS" ] && _CFLAGS="-march=`try_line "$line"`"
  329. #The Atom CPU supports the full core2 instruction set,
  330. #but it's an in-order core, the last one of those being the i586.
  331. #Therefore if gcc hasn't explicit support for the Atom,
  332. #tune it for the i586 architecture.
  333. if ! echo "$_CFLAGS" | grep -q "atom"; then #gcc hasn't specific Atom support
  334. if echo "$line" | grep -q "core2"; then #atom ISA line
  335. if [ $cpu_model -eq 28 ]; then #atom
  336. if echo "$_CFLAGS" | grep -E -q "(core2|pentium[^ ])"; then #gcc chose out of order arch
  337. _CFLAGS="$_CFLAGS -mtune=pentium" #tune for last in-order core
  338. fi
  339. fi
  340. fi
  341. fi
  342. #SSE is not used for floating point by default in gcc 32 bit
  343. #so turn that on here.
  344. if echo "$flags" | grep -q "sse"; then
  345. if try_gcc_options "-mfpmath=sse"; then #gcc >= 3.1
  346. _CFLAGS="$_CFLAGS -mfpmath=sse"
  347. fi
  348. fi
  349. #The SSE options are mostly selected automatically
  350. #when a particular march option is selected.
  351. #There are a few exceptions unfortunately, which we handle here.
  352. #Note the sse instruction lines are:
  353. # intel: [sse4.2] [sse4.1] ssse3 sse3 sse2 sse ...
  354. # amd: [sse5] sse4a [sse3] sse2 sse ...
  355. # The bracketed ones are only available on some cpus
  356. # in a particular family and so need to be added explicitly.
  357. if echo "$_CFLAGS" | grep -q "amdfam10"; then
  358. if echo "$flags" | grep -q "sse5"; then
  359. if try_gcc_options "-msse5"; then #gcc >= 4.3
  360. _CFLAGS="$_CFLAGS -msse5"
  361. fi
  362. fi
  363. elif echo "$_CFLAGS" | grep -E -q "(k8|c3-2)"; then
  364. if echo "$flags" | grep -E -q "(sse3|pni)"; then
  365. if try_gcc_options "-msse3"; then #gcc >= 3.3.3
  366. _CFLAGS="$_CFLAGS -msse3"
  367. fi
  368. fi
  369. elif echo "$_CFLAGS" | grep -q "core2"; then
  370. if echo "$flags" | grep -q "sse4_2"; then
  371. if try_gcc_options "-msse4"; then #gcc >= 4.3
  372. _CFLAGS="$_CFLAGS -msse4"
  373. fi
  374. elif echo "$flags" | grep -q "sse4_1"; then
  375. if try_gcc_options "-msse4.1"; then #gcc >= 4.3
  376. _CFLAGS="$_CFLAGS -msse4.1"
  377. fi
  378. fi
  379. fi
  380. echo "$m32$_CFLAGS"