1 /* FPU-related code for x86 and x86_64 processors.
2 Copyright 2005, 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
3 Contributed by Francois-Xavier Coudert <coudert@clipper.ens.fr>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
30 #if defined(__sun__) && defined(__svr4__)
34 static volatile sig_atomic_t sigill_caught
;
37 sigill_hdlr (int sig
__attribute((unused
)),
38 siginfo_t
*sip
__attribute__((unused
)),
42 /* Set PC to the instruction after the faulting one to skip over it,
43 otherwise we enter an infinite loop. 3 is the size of the movaps
45 ucp
->uc_mcontext
.gregs
[EIP
] += 3;
54 unsigned int eax
, ebx
, ecx
, edx
;
56 if (!__get_cpuid (1, &eax
, &ebx
, &ecx
, &edx
))
59 #if defined(__sun__) && defined(__svr4__)
60 /* Solaris 2 before Solaris 9 4/04 cannot execute SSE instructions even
61 if the CPU supports them. Programs receive SIGILL instead, so check
62 for that at runtime. */
66 struct sigaction act
, oact
;
68 act
.sa_handler
= sigill_hdlr
;
69 sigemptyset (&act
.sa_mask
);
70 /* Need to set SA_SIGINFO so a ucontext_t * is passed to the handler. */
71 act
.sa_flags
= SA_SIGINFO
;
72 sigaction (SIGILL
, &act
, &oact
);
74 /* We need a single SSE instruction here so the handler can safely skip
76 __asm__
volatile ("movaps %xmm0,%xmm0");
78 sigaction (SIGILL
, &oact
, NULL
);
83 #endif /* __sun__ && __svr4__ */
91 /* i387 -- see linux <fpu_control.h> header file for details. */
92 #define _FPU_MASK_IM 0x01
93 #define _FPU_MASK_DM 0x02
94 #define _FPU_MASK_ZM 0x04
95 #define _FPU_MASK_OM 0x08
96 #define _FPU_MASK_UM 0x10
97 #define _FPU_MASK_PM 0x20
103 asm volatile ("fnstcw %0" : "=m" (cw
));
105 cw
|= (_FPU_MASK_IM
| _FPU_MASK_DM
| _FPU_MASK_ZM
| _FPU_MASK_OM
106 | _FPU_MASK_UM
| _FPU_MASK_PM
);
108 if (options
.fpe
& GFC_FPE_INVALID
) cw
&= ~_FPU_MASK_IM
;
109 if (options
.fpe
& GFC_FPE_DENORMAL
) cw
&= ~_FPU_MASK_DM
;
110 if (options
.fpe
& GFC_FPE_ZERO
) cw
&= ~_FPU_MASK_ZM
;
111 if (options
.fpe
& GFC_FPE_OVERFLOW
) cw
&= ~_FPU_MASK_OM
;
112 if (options
.fpe
& GFC_FPE_UNDERFLOW
) cw
&= ~_FPU_MASK_UM
;
113 if (options
.fpe
& GFC_FPE_INEXACT
) cw
&= ~_FPU_MASK_PM
;
115 asm volatile ("fldcw %0" : : "m" (cw
));
121 asm volatile ("%vstmxcsr %0" : "=m" (cw_sse
));
123 cw_sse
&= 0xffff0000;
124 cw_sse
|= (_FPU_MASK_IM
| _FPU_MASK_DM
| _FPU_MASK_ZM
| _FPU_MASK_OM
125 | _FPU_MASK_UM
| _FPU_MASK_PM
) << 7;
127 if (options
.fpe
& GFC_FPE_INVALID
) cw_sse
&= ~(_FPU_MASK_IM
<< 7);
128 if (options
.fpe
& GFC_FPE_DENORMAL
) cw_sse
&= ~(_FPU_MASK_DM
<< 7);
129 if (options
.fpe
& GFC_FPE_ZERO
) cw_sse
&= ~(_FPU_MASK_ZM
<< 7);
130 if (options
.fpe
& GFC_FPE_OVERFLOW
) cw_sse
&= ~(_FPU_MASK_OM
<< 7);
131 if (options
.fpe
& GFC_FPE_UNDERFLOW
) cw_sse
&= ~(_FPU_MASK_UM
<< 7);
132 if (options
.fpe
& GFC_FPE_INEXACT
) cw_sse
&= ~(_FPU_MASK_PM
<< 7);
134 asm volatile ("%vldmxcsr %0" : : "m" (cw_sse
));