Choose your CFLAGS Wisely

Kim’s laptop died the other day.  The hard disk and all its data were fine though, and since the system runs Linux, the obvious solution is to just put the disk into another computer.

The laptop was a Pentium III, and the "new" (old) computer into which the disk is going is a Pentium II.  This would be no problem, except that in the system’s /etc/make.conf file (this is Gentoo) I was using CFLAGS="-march=pentium3".

In the new system, the disk booted fine and everything, services started, I could log in, networking worked, etc.  But X wouldn’t run; it died with a "signal 4" error -- illegal instruction/operation.  Same for GCC.  I realized that the software was trying to use PIII tricks that the PII didn’t have.

If only I had chosen my CFLAGS more carefully:

These -m options are defined for the i386 and x86-64 family of computers:

-mcpu=cpu-type

Tune to cpu-type everything applicable about the generated code, except for the ABI and the set of available instructions. The choices for cpu-type are i386, i486, i586, i686, pentium, pentium-mmx, pentiumpro, pentium2, pentium3, pentium4, k6, k6-2, k6-3, athlon, athlon-tbird, athlon-4, athlon-xp and athlon-mp.

While picking a specific cpu-type will schedule things appropriately for that particular chip, the compiler will not generate any code that does not run on the i386 without the -march=cpu-type option being used. i586 is equivalent to pentium and i686 is equivalent to pentiumpro. k6 and athlon are the AMD chips as opposed to the Intel ones.

-march=cpu-type

Generate instructions for the machine type cpu-type. The choices for cpu-type are the same as for -mcpu. Moreover, specifying -march=cpu-type implies -mcpu=cpu-type.

So -march gives you the heaviest optimization and thus the best performance, but at the cost of portability.  Programs compiled with -march=pentium3 that take advantage of the PIII’s advanced features won’t run on the PII.  But -mcpu on the other hand will "tune" the compilation to the target CPU, while still maintaining full i386 compatibility.  I could take this PIII-optimized operating system and drop it into a PII box and it would run without me needing to recompile anything.

Looks like this ol’ 333 MHz Pentium II will be spending the next few days building a lot of software...

Posted by Anthony on reply

Reply to this message here:

Your name
Email
Website (optional)
Subject
search posts:

HomeCreate PostArchivesLoginCMS by Encodable