# gcc tuning Lowest Common Denomintator flags script

## Goverp

Has anyone written a script to generate a set of "lowest common denominator" for two (or maybe more) different but similar CPUs?  (If not, I'll try, but I'd rather not reinvent the wheel.)

<oops>Sorry for the duplicate postings - I got (clearly false) messages saying "Submit" had failed, so I tried again</oops>

To elaborate, my use case is a new AMD Ryzen 9 3900X in my desktop machine, with enough power to compile qtwebengine and anything else, and an AMD A9 9420 in my laptop (approx 13.5 times slower on big stuff like that).  In terms of code, the two processors are fairly similar, even though their performance is wildly different.  I want to emerge everything just once on the new desktop, and create binary package host that the laptop can use to save compiling stuff thereupon.

Running 

```
gcc -march=native -Q --help=target
```

 tells me that the Ryzen is march=znver2, and the A9 is march=bdver4, along with a list of target-specific flag settings.  Now a naive approach would be to specify:

```
CFLAGS="-march=bdver4 -mtune=znver2"
```

 but there are a few flags that need to be disabled in from the bdver4 settings, which if I've compared the help=target listings correctly, gives me: 

```
CFLAGS="-march=bdver4 -mtune=znver2 -mno-avx256-split-unaligned-store -mno-fma4 -mno-lwp -mno-tbm -mno-xop -mno-xsaves"
```

As this is a pretty tricky process, it should be relatively easy to automate. In fact, so easy, surely someone's done it already...

Another tweak is possible using differing CPU_FLAGS_X86 settings on the two boxes.  Some, but not all, of the differing -m... tuning flags can appear in CPU_FLAGS_X86, and "emerge --usepkg" will spot the different settings and choose to compile a processor-specific version on the A9, but only for packages which specify the relevant USE flags.  So this trick will sometimes, but not always, exploit extra optimizations on the A9.

Thinking about this some more, if I set up the LCM binary package host, and then use that to install the running system on the Ryzen, that would allow similar upgrades there.  I'm pretty sure there must be a way to create a binary package host as if I were cross-compiling.  I presume it's using the chroot and crossdev stuff in the Cross build environment wiki entry, though without many of the problems resulting from 100% dissimilar architectures.  (Did I miss a simpler wiki item on cross-building - that one's complicated by totally different hardware architectures requiring qemu?)

----------

