This is the mail archive of the
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: <email@example.com>
James Cloos <firstname.lastname@example.org> 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.
int foo (void) __attribute__ ((ifunc ("foo_ifunc")));
static int global = 1;
return global == 1 ? f1 : f2;
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