From 43873ef0ff403a6bc0242f8cc8510060127abde3 Mon Sep 17 00:00:00 2001 From: Matthew Cline Date: Sat, 17 Oct 2009 20:22:24 -0700 Subject: makefile: Automatically optimize for your machine Setting the makefile variable AUTO_OPT_GCC will give GCC a list of optimization flags suited for the machine make is run on. It does this using util/auto-opt.sh. "-march=native -mtune=native" are always outputted, which causes GCC to detect the CPU type you use and generate code for that. On Linux systems it also looks at the flags line of /proc/cpuinfo to pick x86 optimization flags. --- crawl-ref/source/makefile | 25 +++++++++++++++++--- crawl-ref/source/util/auto-opt.sh | 50 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) create mode 100755 crawl-ref/source/util/auto-opt.sh (limited to 'crawl-ref') diff --git a/crawl-ref/source/makefile b/crawl-ref/source/makefile index f3bcd0b51b..03557f4a4f 100644 --- a/crawl-ref/source/makefile +++ b/crawl-ref/source/makefile @@ -24,16 +24,35 @@ GAME = crawl # CFWARN(_L) - Warning flags # CFOTHERS(_L) - Anything else # -# These are good optimizations for a Pentium M or better: -#CFOPTIMIZE := -O2 -march=i686 -mtune=i686 -mmmx -msse -msse2 -mfpmath=sse -funroll-all-loops + # These are really good options for the Intel C++ compiler: #CFOPTIMIZE := -O2 -parallel -CFOPTIMIZE := -O2 + +# Define this to automatically generate code optimized for your machine +# (GCC only as of now). +# +# NOTE: Don't use this with a release build, since the generated code +# won't work for all machines. +ifdef AUTO_OPT_GCC + +ifdef CROSSHOST +error Can not do AUTO_OPT_GCC with CROSSHOST +endif + +ifdef HURRY +error Can not do AUTO_OPT_GCC with HURRY +endif + +AUTO_OPT_FLAGS := $(shell util/auto-opt.sh) +endif + +CFOPTIMIZE := -O2 $(AUTO_OPT_FLAGS) CFOTHERS := -fno-strict-aliasing -pipe CFOTHERS_L := -fsigned-char CFWARN := -Wall MAKEFLAGS := --no-print-directory + # # The GCC and GXX variables are set later. # diff --git a/crawl-ref/source/util/auto-opt.sh b/crawl-ref/source/util/auto-opt.sh new file mode 100755 index 0000000000..8f4aa185cb --- /dev/null +++ b/crawl-ref/source/util/auto-opt.sh @@ -0,0 +1,50 @@ +#!/bin/bash --noprofile + +######################################################################### +# Called by the makefile if AUTO_OPT_GCC is true, outputs the list of CPU +# specific optimization flags that should be used. +######################################################################### + +#TODO: Detect extra compiler options for: +# +# * Non-gcc compilers. +# * CYGWIN. +# * Mac OS X. +# * Non-x86 Linux systems. + +######################################################################### + +# This line makes GCC automatically detect the CPU architecture to use. Also +# might cause the use of some of the "-m" options we use down below, +# but GCC doesn't complain about an "-m" option being used repeatedly. +OUT="-mtune=native -march=native" + +if [ -f /proc/cpuinfo ]; then + # On Linux system, the "flags: " line of /proc/cpuinfo indicates which + # extended instruction sets are available (among other things), so we + # can use that to detect what to use. + + # Only get info for the first CPU; assumes that all CPUs in the system + # are the same + INFO=`cat /proc/cpuinfo | grep '^flags.*:' | head -n 1` + + # These are currently all x86 specific. + for flag in cx16 mmx sse sse2 sse3 sse4.1 sse4.2 sse4 sse4a 3dnow abm; do + # Put a space on either side of the flag name, so that it won't + # match in the middle of a flag. + if [[ $INFO == *\ $flag\ * ]] ; then + OUT="$OUT -m$flag" + fi + done + + if [[ $INFO == *\ lahf_lm\ * ]]; then + OUT="$OUT -msahf" + fi + + # Any available SSE lets us use -mfpmath=sse + if [[ $INFO == *\ sse* ]]; then + OUT="$OUT -mfpmath=sse" + fi +fi + +echo "$OUT" -- cgit v1.2.3-54-g00ecf