This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: __attribute__ ifunc examples?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]