Gentoo Archives: gentoo-dev

From: "Diego 'Flameeyes' Pettenò" <flameeyes@g.o>
To: gentoo-dev@l.g.o
Subject: [gentoo-dev] Replacing cpu-feature USE flags
Date: Thu, 06 Jul 2006 10:59:13
So, I've been drafting this up in my blog[1], and it is a simple way to 
replace the CPU feature useflags. Let's try to summarise:

Right now we have mmx, 3dnow, 3dnowex, sse, sse2 and so on useflags present in 
the tree, almost never used to get new dependencies, but usually used to 
supply econf switches.

This works as long as the user enable the flags, but for AMD64 the story was, 
until now, that we simply enabled them when they worked, because we had some 
minimum support available. Unfortunately this became a problem with the 
introduction of nocona, because that is an amd64-like system but with no 
3dnow. And there is the problem that sse3 is supported only in later versions 
of Athlon64 and so on.

To try to clean up this mess, and to make it simpler to work in 
cross-compilation, I thought of using the definitions created by the C 
Preprocessor (CPP) by default for the given CFLAGS. Basically when 
using -march=athlon64, the preprocessor will enable a few definitions that 
tells the availability of MMX, 3dNOW, SSE and so on... if we wrap that 
around, we can use it to know if it's the case of enabling something or not.
This is customisable by the user by setting the CFLAGS themselves. If one does 
not want MMX instructions to be generated, but still want the rest of 
Athlon64 optimisations, it's simply the matter of 
using "-march=athlon64 -mno-mmx".

So, rather than the functions proposed in [1], I've sampled today the 
following function:

has_cpuset() {
    local def hasfeat

    while [[ -n $1 ]]; do
        case $1 in
                def="__MMX__" ;;
                def="__3dNOW__" ;;
                def="__3dNOW_A__" ;;
                def="__SSE__" ;;
                def="__SSE2__" ;;
                def="__SSE3__" ;;
                def="__ALTIVEC__" ;;
                ewarn "Instruction set $1 not supported."
                die "Instruction set not supported."

        echo | $(tc-getCC) ${CFLAGS} -dM -E - 2>/dev/null | grep -q ${def} || 

    if [[ ${hasfeat} == "no" ]]; then
        return 1
        return 0

that can be tested easily with the following code:

yesno() {
    if "$@"; then
        echo "yes"
        echo "no"

echo "Does it have 3dnow mmx sse2?"
yesno has_cpuset 3dnow mmx sse2
echo "Does it have mmx sse sse3?"
yesno has_cpuset mmx sse sse3
echo "Does it have altivec?"
yesno has_cpuset altivec

Note that you  need to set your CFLAGS corretly or it will, by default, tell 
you that everything is disabled.

Thoughts? Comments?

SPARC team: I'd like to know if VIS does a similar thing, would make simpler 
for instance its handling in xine-lib ebuild.

Diego "Flameeyes" Pettenò -
Gentoo/Alt lead, Gentoo/FreeBSD, Video, AMD64, Sound, PAM, KDE


Subject Author
Re: [gentoo-dev] Replacing cpu-feature USE flags Stuart Herbert <stuart.herbert@×××××.com>
Re: [gentoo-dev] Replacing cpu-feature USE flags Ioannis Aslanidis <aslanidis@×××××.com>
Re: [gentoo-dev] Replacing cpu-feature USE flags Donnie Berkholz <spyderous@g.o>
Re: [gentoo-dev] Replacing cpu-feature USE flags Ciaran McCreesh <ciaran.mccreesh@×××××××××××××.uk>
Re: [gentoo-dev] Replacing cpu-feature USE flags "Kevin F. Quinn" <kevquinn@g.o>
Re: [gentoo-dev] Replacing cpu-feature USE flags Luca Barbato <lu_zero@g.o>
Re: [gentoo-dev] Replacing cpu-feature USE flags "Kevin F. Quinn" <kevquinn@g.o>
Re: [gentoo-dev] Replacing cpu-feature USE flags Luca Barbato <lu_zero@g.o>
Re: [gentoo-dev] Replacing cpu-feature USE flags Richard Fish <bigfish@××××××××××.org>