This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: __attribute__ ifunc examples?
- From: Ian Lance Taylor <iant at google dot com>
- To: James Cloos <cloos at jhcloos dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 14 Mar 2012 19:44:55 -0700
- Subject: Re: __attribute__ ifunc examples?
- References: <m3ehsvmfeh.fsf@carbon.jhcloos.org>
James Cloos <cloos@jhcloos.com> writes:
> Does anyone have a worked example in C of using attribute ifunc to
> dispatch over the various x86 SSE/AVX flags, as well as arm neon?
Not using SSE/AVX flags, but the basic idea is not too hard. Here is an
example that sets the implementation of foo based on a global variable.
#include <stdio.h>
int foo (void) __attribute__ ((ifunc ("foo_ifunc")));
static int global = 1;
static int
f1 (void)
{
return 0;
}
static int
f2 (void)
{
return 1;
}
void *
foo_ifunc (void)
{
return global == 1 ? f1 : f2;
}
int
main ()
{
printf ("%d\n", foo());
}
For your case you need to write foo_ifunc using appropriate asm
instructions to check cpuid or whatever. Note that foo_ifunc runs as
the program is just starting, so it can't call library routines or check
command line arguments or anything like that. Checking cpuid is fine.
> Must one call cpuid manually, or are flags already available to check?
At present you have to call it manually. There is an outstanding patch
to add __builtin_cpuid on x86 systems, but I don't think it has been
approved.
Ian