linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* Runtime Altivec detection
@ 2003-03-07 16:47 Nathan Ingersoll
  2003-03-07 17:24 ` Dan Malek
  2003-03-07 18:54 ` Magnus Damm
  0 siblings, 2 replies; 18+ messages in thread
From: Nathan Ingersoll @ 2003-03-07 16:47 UTC (permalink / raw)
  To: linuxppc-dev


I've recently been working on some Altivec optimizations for a library,
and was looking for a relatively clean way to perform runtime detection.
I did quite a bit of searching and didn't find any good examples of
someone doing this, other than using the sysctl call on Mac OS X.

I decided to whip up a simple test program based on the assumption that
running Altivec code on a non-Altivec enabled CPU would cause a SIGILL.
Basically, I setjmp a position before an altivec instruction is
performed, and catch the SIGILL, then longjmp back into the main program
>from the signal handler.

This seems to work pretty well, at least in my simplistic test case. Is
anyone aware of some long term problems doing this? some possible side
effects? I've seen other projects catch the SIGILL and exit on it, are
they not attempting to fix up after because they know something I don't?

If this is a reasonable approach, I'm hoping to extend it for generic
feature testing on a variety of processors. I've heard that the process
state is undefined by POSIX after a SIGILL has occurred, does anyone know
of a platform where this could be an issue?

I've posted the code I'm testing at http://ningerso.atmos.org/code/alt.c
If no one points out a serious flaw to the approach, and the idea is useful
feel free to use the code under the BSD+advertising clause license.

Any comments or insights?
Thanks,
Nathan

--
------------------------------------------------------------------------
| Nathan Ingersoll           \\ Computer Systems & Network Coordinator |
| ningerso@ruralcenter.org    \\ http://www.ruralcenter.org            |
| http://ningerso.atmos.org/   \\ Minnesota Center for Rural Health    |
------------------------------------------------------------------------

** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread
* Re: Runtime Altivec detection
@ 2003-03-08  2:02 Bill Fink
  2003-03-08  2:11 ` Hollis Blanchard
  0 siblings, 1 reply; 18+ messages in thread
From: Bill Fink @ 2003-03-08  2:02 UTC (permalink / raw)
  To: LinuxPPC Developers; +Cc: Bill Fink


Hi Nathan,

> On Fri, 7 Mar 2003, Nathan Ingersoll wrote:
>
> On Fri, Mar 07, 2003 at 06:54:58PM +0000, Magnus Damm wrote:
> > mplayer and ffmpeg does some kind of detection runtime.
> > I'm playing with it right now actually.
>
> Last I heard, it was a compile time detection or flag. So you had to build
> separate binaries for your G4 and non-G4 machines.
>
> > While at it:
> >
> > I've seen that -maltivec and -mabi=altivec is passed sometimes.
> > If I want to build a binary that should be able to run on a
> > G3 without altivec and utilize altivec when present on a G4,
> > what flags should I use?
>
> There is no specific flag for doing so, you need some code to detect the
> capability, and then decide whether to use the Altivec routine.

Here is the code xine uses to do run time Altivec detection
(from xine-utils/cpu_accel.c):

#if defined (ARCH_PPC) && defined (ENABLE_ALTIVEC)
static sigjmp_buf jmpbuf;
static volatile sig_atomic_t canjump = 0;

static void sigill_handler (int sig)
{
    if (!canjump) {
        signal (sig, SIG_DFL);
        raise (sig);
    }

    canjump = 0;
    siglongjmp (jmpbuf, 1);
}

static uint32_t arch_accel (void)
{
    signal (SIGILL, sigill_handler);
    if (sigsetjmp (jmpbuf, 1)) {
        signal (SIGILL, SIG_DFL);
        return 0;
    }

    canjump = 1;

    __asm__ volatile ("mtspr 256, %0\n\t"
                  "vand %%v0, %%v0, %%v0"
                  :
                  : "r" (-1));

    signal (SIGILL, SIG_DFL);
    return MM_ACCEL_PPC_ALTIVEC;
}
#endif /* ARCH_PPC */

And here's the gcc command used to compile cpu_accel.c:

gcc -DHAVE_CONFIG_H -I. -I. -I../.. -I../.. -I../../include -I../../include -I../../src -I../../src/xine-engine -I../../src/xine-engine -I../../src/xine-utils -I/usr/X11R6/include -std=gnu89 -Wall -D_REENTRANT -D_FILE_OFFSET_BITS=64 -DXINE_COMPILE -O3 -pipe -fomit-frame-pointer -fexpensive-optimizations -fschedule-insns2 -fno-strict-aliasing -ffast-math -funroll-loops -funroll-all-loops -finline-functions -Wa,-m7400 -I/usr/include/kde/artsc -c cpu_accel.c -Wp,-MD,.deps/cpu_accel.TPlo  -fPIC -DPIC -o cpu_accel.lo

						-Bill

** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread
* Re: Runtime Altivec detection
@ 2003-03-09  4:01 Albert Cahalan
  0 siblings, 0 replies; 18+ messages in thread
From: Albert Cahalan @ 2003-03-09  4:01 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: damm, hollis, benh, ningerso


For those of you needing to get at ELF note info
to determine CPU features and other things, here
is the code procps-3.1.x has been using. You'll
need the appropriate ID code; here I use AT_CLKTCK
to determine USER_HZ. Pass the ID code into this
function, and you get back the ELF note value.

//////////////////////////////////////////////////////////////////////////
#ifndef AT_CLKTCK
#define AT_CLKTCK       17    // frequency of times()
#endif

#define ERROR_CODE 42  // choose something better!

extern char** environ;

// for ELF executables, notes are pushed before environment and args
static unsigned long find_elf_note(unsigned long findme){
  unsigned long *ep = (unsigned long *)environ;
  while(*ep++);
  while(*ep){
    if(ep[0]==findme) return ep[1];
    ep+=2;
  }
  return ERROR_CODE;
}

// Usage:
// Hertz = find_elf_note(AT_CLKTCK);

////////////////////////////////////////////////////////////////////////


** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2003-03-09  4:01 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-03-07 16:47 Runtime Altivec detection Nathan Ingersoll
2003-03-07 17:24 ` Dan Malek
2003-03-07 17:35   ` Nathan Ingersoll
2003-03-07 17:37   ` Anton Blanchard
2003-03-07 17:41   ` Benjamin Herrenschmidt
2003-03-07 17:52     ` Nathan Ingersoll
2003-03-07 18:55     ` Magnus Damm
2003-03-07 18:06       ` Benjamin Herrenschmidt
2003-03-07 18:18       ` Hollis Blanchard
2003-03-07 18:54 ` Magnus Damm
2003-03-07 18:08   ` Nathan Ingersoll
2003-03-07 19:44     ` Magnus Damm
2003-03-07 19:23       ` Nathan Ingersoll
  -- strict thread matches above, loose matches on Subject: below --
2003-03-08  2:02 Bill Fink
2003-03-08  2:11 ` Hollis Blanchard
2003-03-08  8:04   ` Bill Fink
2003-03-08 18:21     ` Nathan Ingersoll
2003-03-09  4:01 Albert Cahalan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).