Update concepts branch to revision 131834
[official-gcc.git] / libgfortran / config / fpu-387.h
blobf96f7156619c4ffdb123bf47b364709ce47793de
1 /* FPU-related code for x86 and x86_64 processors.
2 Copyright 2005, 2007 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 2 of the License, or (at your option) any later version.
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.)
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.
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. */
31 #ifndef __x86_64__
32 #include "cpuid.h"
33 #endif
35 static int
36 has_sse (void)
38 #ifndef __x86_64__
39 unsigned int eax, ebx, ecx, edx;
41 if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
42 return 0;
44 return edx & bit_SSE;
45 #else
46 return 1;
47 #endif
50 /* i387 -- see linux <fpu_control.h> header file for details. */
51 #define _FPU_MASK_IM 0x01
52 #define _FPU_MASK_DM 0x02
53 #define _FPU_MASK_ZM 0x04
54 #define _FPU_MASK_OM 0x08
55 #define _FPU_MASK_UM 0x10
56 #define _FPU_MASK_PM 0x20
58 void set_fpu (void)
60 unsigned short cw;
62 asm volatile ("fnstcw %0" : "=m" (cw));
64 cw |= (_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM | _FPU_MASK_OM
65 | _FPU_MASK_UM | _FPU_MASK_PM);
67 if (options.fpe & GFC_FPE_INVALID) cw &= ~_FPU_MASK_IM;
68 if (options.fpe & GFC_FPE_DENORMAL) cw &= ~_FPU_MASK_DM;
69 if (options.fpe & GFC_FPE_ZERO) cw &= ~_FPU_MASK_ZM;
70 if (options.fpe & GFC_FPE_OVERFLOW) cw &= ~_FPU_MASK_OM;
71 if (options.fpe & GFC_FPE_UNDERFLOW) cw &= ~_FPU_MASK_UM;
72 if (options.fpe & GFC_FPE_PRECISION) cw &= ~_FPU_MASK_PM;
74 asm volatile ("fldcw %0" : : "m" (cw));
76 if (has_sse())
78 unsigned int cw_sse;
80 asm volatile ("stmxcsr %0" : "=m" (cw_sse));
82 cw_sse &= 0xffff0000;
83 cw_sse |= (_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM | _FPU_MASK_OM
84 | _FPU_MASK_UM | _FPU_MASK_PM ) << 7;
86 if (options.fpe & GFC_FPE_INVALID) cw_sse &= ~(_FPU_MASK_IM << 7);
87 if (options.fpe & GFC_FPE_DENORMAL) cw_sse &= ~(_FPU_MASK_DM << 7);
88 if (options.fpe & GFC_FPE_ZERO) cw_sse &= ~(_FPU_MASK_ZM << 7);
89 if (options.fpe & GFC_FPE_OVERFLOW) cw_sse &= ~(_FPU_MASK_OM << 7);
90 if (options.fpe & GFC_FPE_UNDERFLOW) cw_sse &= ~(_FPU_MASK_UM << 7);
91 if (options.fpe & GFC_FPE_PRECISION) cw_sse &= ~(_FPU_MASK_PM << 7);
93 asm volatile ("ldmxcsr %0" : : "m" (cw_sse));