1#!/bin/sh
2
3if test x"$CC" = x; then
4   CC=gcc
5fi
6
7if test $# -ne 1; then
8   echo "Please give the arch (ppc or x86) as an argument!" 1>&2
9   exit 1
10fi
11
12if test `uname -s` = Darwin; then
13   IsDarwin=yes
14else
15   IsDarwin=no
16fi
17
18target=$1
19
20cc_version=`$CC -dumpversion`
21_cc_major=`echo $cc_version | cut -d'.' -f1`
22_cc_minor=`echo $cc_version | cut -d'.' -f2`
23
24if test $_cc_major -ge 4; then
25  _opt_mcpu="-mtune"
26elif test $_cc_major -ge 3 -a $_cc_minor -ge 4; then
27  _opt_mcpu="-mtune"
28else
29  _opt_mcpu="-mcpu"
30fi
31
32do_cc()
33{
34	$CC -o conftest conftest.c $@ >/dev/null 2>&1
35}
36
37extcheck()
38{
39cat > conftest.c <<EOF
40#include <signal.h>
41void catch() { exit(1); }
42int main(void){
43  signal(SIGILL, catch);
44  __asm__ __volatile__ ("$1":::"memory");
45  exit(0);
46}
47EOF
48
49do_cc
50if test -x ./conftest; then
51     ./conftest
52     if test $? -ne 0; then
53        return 1
54     fi
55     return 0
56else
57     return 1
58fi
59}
60
61do_x86()
62{
63
64CFLAGS=-O
65if test $IsDarwin = yes; then
66   CFLAGS="$CFLAGS -fno-pic -Wl,-read_only_relocs -Wl,suppress"
67fi
68
69if test -r /proc/cpuinfo; then
70	_cpuinfo="cat /proc/cpuinfo"
71else
72	$CC $CFLAGS -o cpuinfo utils/cpuinfo.c
73	_cpuinfo="./cpuinfo"
74fi
75
76# Cpu determination logic adapted from the MPlayer configure script.
77
78pname=`$_cpuinfo | grep 'model name' | cut -d ':' -f 2 | head -n 1`
79pvendor=`$_cpuinfo | grep 'vendor_id' | cut -d':' -f2 | cut -d' ' -f2 | head -n 1`
80pfamily=`$_cpuinfo | grep 'cpu family' | cut -d':' -f2 | cut -d' ' -f2 | head -n 1`
81pmodel=`$_cpuinfo | grep -v 'model name' | grep 'model' | cut -d':' -f2 | cut -d' ' -f2 | head -n 1`
82pstep=`$_cpuinfo | grep 'stepping' | cut -d':' -f2 | cut -d' ' -f2 | head -n 1`
83pparam=`$_cpuinfo | grep 'features' | cut -d':' -f2 | head -n 1`
84
85if test -z "$pparam" ; then
86 pparam=`$_cpuinfo | grep 'flags' | cut -d ':' -f 2 | head -n 1`
87fi
88
89_mmx=no
90_3dnow=no
91_3dnowex=no
92_mmx2=no
93_sse=no
94_sse2=no
95_mtrr=no
96
97for i in $pparam ; do
98 case "$i" in
99  3dnow)        _3dnow=yes               ;;
100  3dnowext)     _3dnow=yes  _3dnowex=yes ;;
101  mmx)          _mmx=yes                 ;;
102  mmxext)       _mmx2=yes                ;;
103  mtrr|k6_mtrr|cyrix_arr)   _mtrr=yes    ;;
104  xmm|sse|kni)  _sse=yes    _mmx2=yes    ;;
105  sse2)         _sse2=yes                ;;
106 esac
107done
108
109case "$pvendor" in
110	AuthenticAMD)
111		case "$pfamily" in
112			3)proc=i386
113			  ;;
114			4) proc=i486
115			  ;;
116			5) iproc=586
117			# models are: K5/SSA5 K5 K5 K5 ? ? K6 K6 K6-2 K6-3
118			# K6 model 13 are the K6-2+ and K6-III+
119			   if test "$pmodel" -eq 9 -o "$pmodel" -eq 13; then
120				proc=k6-3
121			   elif test "$pmodel" -ge 8; then
122				proc=k6-2
123			   elif test "$pmodel" -ge 6; then
124				proc=k6
125			   else
126				proc=i586
127			   fi
128			   ;;
129			6) iproc=686
130			   if test "$pmodel" -ge 7; then
131				proc=athlon-4
132			   elif test "$pmodel" -ge 6; then
133			       if test "$_sse" = yes && test "$pstep" -ge 2; then
134				   proc=athlon-xp
135			       else
136				   proc=athlon-4
137			       fi
138			   elif test "$pmodel" -ge 4; then
139				proc=athlon-tbird
140			   else
141				proc=athlon
142			   fi
143			   ;;
144			15)
145			    # Despite what the gcc into says 'athlon64' is not accepted as
146			    # synonym for 'k8'
147			   proc=k8
148			   ;;
149			*) proc=athlon-xp
150			   ;;
151		esac
152		;;
153	GenuineIntel)
154		case "$pfamily" in
155			3) proc=i386
156			   ;;
157			4) proc=i486
158			   ;;
159			5) iproc=586
160			   if test "$pmodel" -eq 4 || test "$pmodel" -eq 8; then
161				proc=pentium-mmx # 4 is desktop, 8 is mobile
162			   else
163				proc=i586
164			   fi
165			   ;;
166			6) iproc=686
167                           if test "$pmodel" -ge 15; then
168                                proc=nocona
169                           elif test "$pmodel" -ge 13; then
170                                proc=pentium-m
171			   elif test "$pmodel" -ge 7; then
172				proc=pentium3
173			   elif test "$pmodel" -ge 3; then
174				proc=pentium2
175			   else
176				proc=i686
177			   fi
178                           ;;
179			15) proc=pentium4
180			   ;;
181			*) proc=pentium4
182			   ;;
183		esac
184		;;
185	unknown)
186		case "$pfamily" in
187			3) proc=i386
188			   ;;
189			4) proc=i486
190			   ;;
191			*) proc=i586
192			   ;;
193  		esac
194		;;
195	*)
196	   proc=i586
197	   ;;
198esac
199
200# check that gcc supports our CPU, if not, fall back to earlier ones
201
202cat > conftest.c << EOF
203int main(void) { return 0; }
204EOF
205if  test "$proc" = "athlon64" ; then
206	do_cc -march=$proc $_opt_mcpu=$proc || proc=athlon-xp
207fi
208
209if test "$proc" = "athlon-xp" || test "$proc" = "athlon-4" || test "$proc" = "athlon-tbird"; then
210	do_cc -march=$proc $_opt_mcpu=$proc || proc=athlon
211fi
212
213if test "$proc" = "k6-3" || test "$proc" = "k6-2"; then
214	do_cc -march=$proc $_opt_mcpu=$proc || proc=k6
215fi
216
217if test "$proc" = "k6"; then
218    do_cc -march=$proc $_opt_mcpu=$proc
219    if test $? -ne 0; then
220        if do_cc -march=i586 $_opt_mcpu=i686; then
221          proc=i586-i686
222        else
223          proc=i586
224	fi
225    fi
226fi
227
228if test "$proc" = "pentium4" || test "$proc" = "pentium3" || test "$proc" = "pentium2" || test "$proc" = "athlon"; then
229	do_cc -march=$proc $_opt_mcpu=$proc || proc=i686
230fi
231if test "$proc" = "i686" || test "$proc" = "pentium-mmx"; then
232	do_cc -march=$proc $_opt_mcpu=$proc || proc=i586
233fi
234if test "$proc" = "i586" ; then
235	do_cc -march=$proc $_opt_mcpu=$proc || proc=i486
236fi
237if test "$proc" = "i486" ; then
238	do_cc -march=$proc $_opt_mcpu=$proc || proc=i386
239fi
240if test "$proc" = "i386" ; then
241	do_cc -march=$proc $_opt_mcpu=$proc || proc=error
242fi
243if test "$proc" = "error" ; then
244	echo "Your $_cc does not even support \"i386\" for '-march' and $_opt_mcpu."
245	_mcpu=""
246	_march=""
247elif test "$proc" = "i586-i686"; then
248	_march="-march=i586"
249	_mcpu="$_opt_mcpu=i686"
250else
251      _march="-march=$proc"
252      _mcpu="$_opt_mcpu=$proc"
253fi
254
255if test $_cc_major -ge 3; then
256   extcheck "xorps %%xmm0, %%xmm0" || _gcc3_ext="$_gcc3_ext -mno-sse"
257   extcheck "xorpd %%xmm0, %%xmm0" || _gcc3_ext="$_gcc3_ext -mno-sse2"
258
259   if test x"$_gcc3_ext" != "x"; then
260    # if we had to disable sse/sse2 because the active kernel does not
261    # support this instruction set extension, we also have to tell
262    # gcc3 to not generate sse/sse2 instructions for normal C code
263    cat > conftest.c << EOF
264int main(void) { return 0; }
265EOF
266    do_cc $_march $_gcc3_ext && _march="$_march $_gcc3_ext"
267   fi
268fi
269
270echo $_march $_mcpu
271rm -f conftest.c conftest cpuinfo
272return 0
273}
274
275do_ppc()
276{
277# Linux on a PPC has /proc/info
278# Darwin (OS/X) has the hostinfo command
279# If neither of those we have no idea what to do - so do nothing.
280if test -r /proc/cpuinfo; then
281	proc=`grep cpu /proc/cpuinfo | cut -d':' -f2 | cut -d',' -f1 | cut -b 2- | head -n 1`
282elif test $IsDarwin = yes; then
283	proc=`hostinfo | grep "Processor type" | cut -f3 -d' ' | sed 's/ppc//'`
284else
285	return 0
286fi
287
288case "$proc" in
289	601) _march="$_opt_mcpu=601" _mcpu='-mtune=601'
290	     ;;
291	603) _march="$_opt_mcpu=603" _mcpu='-mtune=603'
292	     ;;
293	603e|603ev) _march="$_opt_mcpu=603e" _mcpu='-mtune=603e'
294	     ;;
295	604|604e|604r|604ev) _march="$_opt_mcpu=604" _mcpu='-mtune=604'
296	     ;;
297	740|740/750|745/755) _march="$_opt_mcpu=740" _mcpu='-mtune=740'
298	     ;;
299	750|750CX) _march="$_opt_mcpu=750" _mcpu='-mtune=750'
300	     ;;
301	*) ;;
302esac
303
304# gcc 3.1(.1) and up supports 7400 and 7450
305if test "$_cc_major" -ge "3" && test "$_cc_minor" -ge "1" || test "$_cc_major" -ge "4"; then
306	case "$proc" in
307		7400*|7410*) _march="$_opt_mcpu=7400" _mcpu='-mtune=7400' ;;
308		7450*|7455*) _march="$_opt_mcpu=7450" _mcpu='-mtune=7450' ;;
309		*) ;;
310	esac
311fi
312
313# gcc 3.2 and up supports 970
314if test "$_cc_major" -ge "3" && test "$_cc_minor" -ge "3" || test "$_cc_major" -ge "4"; then
315	case "$proc" in
316	     970*) if test $IsDarwin = yes; then
317		      _march="$_opt_mcpu=G5 -mpowerpc64 -mpowerpc-gpopt -falign-loops=16" _mcpu='-mtune=G5'
318		   else
319		      _march="$_opt_mcpu=970" _mcpu='-mtune=970'
320		   fi
321		   ;;
322		*) ;;
323	esac
324fi
325
326echo $_march $_mcpu
327return 0
328}
329
330#
331# The script that runs the various functions above
332#
333
334if test $target = x86; then
335	do_x86
336elif test $target = ppc; then
337        do_ppc
338fi
339