2006-02-28 Roland McGrath <roland@redhat.com>
[glibc-ports.git] / sysdeps / m68k / fpu / switch / switch.c
blobe0558176dc265d0e77bb1643450fe31f35a43c4c
1 /* Copyright (C) 1991, 1992, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <signal.h>
20 #include <68881-sw.h>
23 /* The signal that is sent when a 68881 instruction
24 is executed and there is no 68881. */
25 #ifndef TRAPSIG
26 #define TRAPSIG SIGILL
27 #endif
29 /* Zero if no 68881, one if we have a 68881, or -1 if we don't know yet. */
30 static int have_fpu = -1;
33 /* Signal handler for the trap that happens if we don't have a 68881. */
34 static void
35 trap (sig)
36 int sig;
38 have_fpu = 0;
41 /* This function is called by functions that want to switch.
42 The calling function must be a `struct switch_caller' in data space.
43 It determines whether a 68881 is present, and modifies its caller
44 to be a static jump to either the 68881 version or the soft version.
45 It then returns into the function it has chosen to do the work. */
46 void
47 __68881_switch (dummy)
48 int dummy;
50 void **return_address_location = &((void **) &dummy)[-1];
51 struct switch_caller *const caller
52 = (struct switch_caller *) (((short int *) *return_address_location) - 1);
54 if (have_fpu < 0)
56 /* Figure out whether or not we have a 68881. */
57 __sighandler_t handler = signal (TRAPSIG, trap);
58 if (handler == SIG_ERR)
59 /* We can't figure it out, so assume we don't have a 68881.
60 This assumption will never cause us any problems other than
61 lost performance, while the reverse assumption could cause
62 the program to crash. */
63 have_fpu = 0;
64 else
66 /* We set `have_fpu' to nonzero, and then execute a 68881
67 no-op instruction. If we have a 68881, this will do nothing.
68 If we don't have one, this will trap and the signal handler
69 will clear `have_fpu'. */
70 have_fpu = 1;
71 asm ("fnop");
73 /* Restore the old signal handler. */
74 (void) signal (TRAPSIG, handler);
78 /* Modify the caller to be a jump to the appropriate address. */
79 caller->insn = JMP;
80 caller->target = have_fpu ? caller->fpu : caller->soft;
82 /* Make the address we will return to be the target we have chosen.
83 Our return will match the `jsr' done by the caller we have
84 just modified, and it will be just as if that had instead
85 been a `jmp' to the new target. */
86 *return_address_location = caller->target;