Runtime Altivec detection
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
Quoted 1 line
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 | | [email protected] \\ 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/
Quoted 2 lines · Nathan Ingersoll
I've recently been working on some Altivec optimizations for a library, and was looking for a relatively clean way to perform runtime detection.
For some time now the Linux kernel has provided support for emulating the 'mfspr rd, PVR' instruction in user space. Do this once, get the PVR and determine the processor type.
You can also just read /proc/cpuinfo and search for the 'cpu :' line. It will indicate the processor type and if altivec is supported.
There are probably other ways as well....
-- Dan
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
Quoted 9 lines · Dan Malek
For some time now the Linux kernel has provided support for emulating the 'mfspr rd, PVR' instruction in user space. Do this once, get the PVR and determine the processor type.
You can also just read
/proc/cpuinfoand search for the 'cpu :' line. It will indicate the processor type and if altivec is supported.There are probably other ways as well....
Thanks for the info Dan, I'll check into doing it this way. I am still curious about some of my other questions, such as recovering from a SIGILL caused by an actual illegal instruction. Anyone willing to chime in on that?
--
------------------------------------------------------------------------
| Nathan Ingersoll \\ Computer Systems & Network Coordinator | | [email protected] \\ 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/
Quoted 5 lines
For some time now the Linux kernel has provided support for emulating the 'mfspr rd, PVR' instruction in user space. Do this once, get the PVR and determine the processor type.
That will fail on a ppc64 box since we havent implemented that emulation.
Anton
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
Quoted 5 lines · Dan Malek
Quoted 2 lines · Nathan Ingersoll
I've recently been working on some Altivec optimizations for a library, and was looking for a relatively clean way to perform runtime detection.
Quoted 8 lines
For some time now the Linux kernel has provided support for emulating the 'mfspr rd, PVR' instruction in user space. Do this once, get the PVR and determine the processor type.
You can also just read
/proc/cpuinfoand search for the 'cpu :' line. It will indicate the processor type and if altivec is supported.There are probably other ways as well....
There's a much better way. The kernel passes down CPU features bits down to userland via ELF AUX tables.
Ben.
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
Quoted 3 lines · Benjamin Herrenschmidt
There's a much better way. The kernel passes down CPU features bits down to userland via ELF AUX tables.
Do you know of any reference code for accessing that information?
Thanks,
Nathan
--
------------------------------------------------------------------------
| Nathan Ingersoll \\ Computer Systems & Network Coordinator | | [email protected] \\ 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/
Quoted 3 lines
There's a much better way. The kernel passes down CPU features bits down to userland via ELF AUX tables.
Any pointers where I can find information about that?
/ magnus
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
Quoted 4 lines · Magnus Damm
Quoted 2 lines
There's a much better way. The kernel passes down CPU features bits down to userland via ELF AUX tables.
Any pointers where I can find information about that?
Well... I know how the kernel pass them down to glibc, but I don't know how a userland app can retreive them :)
The actual bitmask is defined in kernel's include/asm/cputable.h, there are 2 sets of feature bits, one for kernel internal usage and one passed down to userland.
Ben.
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
Quoted 4 lines · Magnus Damm
Quoted 2 lines
There's a much better way. The kernel passes down CPU features bits down to userland via ELF AUX tables.
Any pointers where I can find information about that?
I played with this once, though I don't remember the outcome (it was a long time ago). The ELF AUX table is passed as another argument to main(). I think there may have been another one in between:
int main(int argc, char *argv[], int something, struct elf_aux_table *table);
Then you can parse table as you like.
This was from information in the SysVR4 ABI PPC addendum, a link to which can be found at http://penguinppc.org/dev/library/ .
-Hollis
--
IBM Linux Technology Center
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
mplayer and ffmpeg does some kind of detection runtime. I'm playing with it right now actually.
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?
/ magnus
Quoted 30 lines · On Fri, 7 Mar 2003 10:47:53 -0600 Nathan Ingersoll <[email protected]>…
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
Quoted 1 line
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
Quoted 8 lines
--
------------------------------------------------------------------------| Nathan Ingersoll \\ Computer Systems & Network Coordinator | | [email protected] \\ 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/
Quoted 2 lines · Magnus Damm
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.
Quoted 6 lines
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.
--
------------------------------------------------------------------------
| Nathan Ingersoll \\ Computer Systems & Network Coordinator | | [email protected] \\ 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/
Quoted 5 lines · On Fri, 7 Mar 2003 12:08:02 -0600 Nathan Ingersoll <[email protected]>…
Quoted 2 lines
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.
The latest released mplayer requires you to pass --enable-altivec to configure. A runtime check is then performed in:mplayer/cpudetect.c and ffmpeg/libavcodec/ppc/dsputil_altivec.c
Quoted 7 lines
Quoted 4 lines
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.
Sorry, I think I was a little bit unclear in my question, Do you have to pass any specific flags to gcc to be able to complile altivec code, and if these flags are "-maltivec -mabi=altivec", is it possible to pass them to gcc and still compile for G3 (-mcpu=750)?
It's a nice feature to be able to build one binary that is optimized for G3 but does runtime checks and enables altivec if present.
/ magnus
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
Quoted 4 lines · Magnus Damm
The latest released mplayer requires you to pass --enable-altivec to configure. A runtime check is then performed in:
mplayer/cpudetect.candffmpeg/libavcodec/ppc/dsputil_altivec.c
Ok, that's pretty funny actually. Now that I've looked at that code, it appears they use the same method I do. Slightly different code, but they are writing it for an app, not a library. Strange that I wasn't able to find this at all through google.
Quoted 7 lines
Sorry, I think I was a little bit unclear in my question, Do you have to pass any specific flags to gcc to be able to complile altivec code, and if these flags are "-maltivec -mabi=altivec", is it possible to pass them to gcc and still compile for G3 (-mcpu=750)?
It's a nice feature to be able to build one binary that is optimized for G3 but does runtime checks and enables altivec if present.
Those are probably incompatible flags, since you are telling it to target a specific cpu, but also telling it to use features not present in that cpu model.
--
------------------------------------------------------------------------
| Nathan Ingersoll \\ Computer Systems & Network Coordinator | | [email protected] \\ 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/