OSDN Git Service

* config/fpu-387.h (set_fpu): Add "=m" for stmxcsr.
[pf3gnuchains/gcc-fork.git] / libgfortran / config / fpu-387.h
1 /* FPU-related code for x86 and x86_64 processors.
2    Copyright 2005 Free Software Foundation, Inc.
3    Contributed by Francois-Xavier Coudert <coudert@clipper.ens.fr>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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 2 of the License, or (at your option) any later version.
11
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file.  (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
20
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING.  If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA.  */
30
31
32 static int
33 has_sse (void)
34 {
35 #ifdef __x86_64__
36   return 1;
37 #else
38   unsigned int eax, ebx, ecx, edx;
39
40   /* See if we can use cpuid.  */
41   asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;"
42                 "pushl %0; popfl; pushfl; popl %0; popfl"
43                 : "=&r" (eax), "=&r" (ebx)
44                 : "i" (0x00200000));
45
46   if (((eax ^ ebx) & 0x00200000) == 0)
47     return 0;
48
49   /* Check the highest input value for eax.  */
50   asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1"
51                 : "=a" (eax), "=r" (ebx), "=c" (ecx), "=d" (edx)
52                 : "0" (0));
53
54   if (eax == 0)
55     return 0;
56
57   asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1"
58                 : "=a" (eax), "=r" (ebx), "=c" (ecx), "=d" (edx)
59                 : "0" (1));
60
61   if (edx & (1 << 25))
62     return 1;
63
64   return 0;
65 #endif
66 }
67
68 void set_fpu (void)
69 {
70   short cw;
71   int cw_sse;
72
73   /* i387 -- see linux <fpu_control.h> header file for details.  */
74 #define _FPU_MASK_IM  0x01
75 #define _FPU_MASK_DM  0x02
76 #define _FPU_MASK_ZM  0x04
77 #define _FPU_MASK_OM  0x08
78 #define _FPU_MASK_UM  0x10
79 #define _FPU_MASK_PM  0x20
80   asm volatile ("fnstcw %0" : "=m" (cw));
81   cw |= _FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM | _FPU_MASK_OM | _FPU_MASK_UM | _FPU_MASK_PM;
82   if (options.fpe & GFC_FPE_INVALID) cw &= ~_FPU_MASK_IM;
83   if (options.fpe & GFC_FPE_DENORMAL) cw &= ~_FPU_MASK_DM;
84   if (options.fpe & GFC_FPE_ZERO) cw &= ~_FPU_MASK_ZM;
85   if (options.fpe & GFC_FPE_OVERFLOW) cw &= ~_FPU_MASK_OM;
86   if (options.fpe & GFC_FPE_UNDERFLOW) cw &= ~_FPU_MASK_UM;
87   if (options.fpe & GFC_FPE_PRECISION) cw &= ~_FPU_MASK_PM;
88   asm volatile ("fldcw %0" : : "m" (cw));
89
90   if (has_sse())
91     {
92       /* SSE */
93       asm volatile ("stmxcsr %0" : : "=m" (cw_sse));
94       cw_sse &= 0xFFFF0000;
95       if (options.fpe & GFC_FPE_INVALID) cw_sse |= 1 << 7;
96       if (options.fpe & GFC_FPE_DENORMAL) cw_sse |= 1 << 8;
97       if (options.fpe & GFC_FPE_ZERO) cw_sse |= 1 << 9;
98       if (options.fpe & GFC_FPE_OVERFLOW) cw_sse |= 1 << 10;
99       if (options.fpe & GFC_FPE_UNDERFLOW) cw_sse |= 1 << 11;
100       if (options.fpe & GFC_FPE_PRECISION) cw_sse |= 1 << 12;
101       asm volatile ("ldmxcsr %0" : : "m" (cw_sse));
102     }
103 }