crtfastmath.c revision 1.1.1.11 1 1.1 mrg /*
2 1.1.1.11 mrg * Copyright (C) 2005-2024 Free Software Foundation, Inc.
3 1.1 mrg *
4 1.1 mrg * This file is free software; you can redistribute it and/or modify it
5 1.1 mrg * under the terms of the GNU General Public License as published by the
6 1.1 mrg * Free Software Foundation; either version 3, or (at your option) any
7 1.1 mrg * later version.
8 1.1 mrg *
9 1.1 mrg * This file is distributed in the hope that it will be useful, but
10 1.1 mrg * WITHOUT ANY WARRANTY; without even the implied warranty of
11 1.1 mrg * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 1.1 mrg * General Public License for more details.
13 1.1 mrg *
14 1.1 mrg * Under Section 7 of GPL version 3, you are granted additional
15 1.1 mrg * permissions described in the GCC Runtime Library Exception, version
16 1.1 mrg * 3.1, as published by the Free Software Foundation.
17 1.1 mrg *
18 1.1 mrg * You should have received a copy of the GNU General Public License and
19 1.1 mrg * a copy of the GCC Runtime Library Exception along with this program;
20 1.1 mrg * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21 1.1 mrg * <http://www.gnu.org/licenses/>.
22 1.1 mrg */
23 1.1 mrg
24 1.1.1.2 mrg #ifndef _SOFT_FLOAT
25 1.1 mrg #define MXCSR_DAZ (1 << 6) /* Enable denormals are zero mode */
26 1.1 mrg #define MXCSR_FTZ (1 << 15) /* Enable flush to zero mode */
27 1.1 mrg
28 1.1 mrg #ifndef __x86_64__
29 1.1 mrg /* All 64-bit targets have SSE and DAZ;
30 1.1 mrg only check them explicitly for 32-bit ones. */
31 1.1 mrg #include "cpuid.h"
32 1.1 mrg
33 1.1.1.3 mrg __attribute__ ((target("fxsr,sse")))
34 1.1.1.3 mrg static void
35 1.1 mrg /* The i386 ABI only requires 4-byte stack alignment, so this is necessary
36 1.1 mrg to make sure the fxsave struct gets correct alignment.
37 1.1 mrg See PR27537 and PR28621. */
38 1.1 mrg __attribute__ ((force_align_arg_pointer))
39 1.1.1.3 mrg set_fast_math_sse (unsigned int edx)
40 1.1.1.3 mrg {
41 1.1.1.3 mrg unsigned int mxcsr;
42 1.1.1.3 mrg
43 1.1.1.3 mrg if (edx & bit_FXSAVE)
44 1.1.1.3 mrg {
45 1.1.1.3 mrg /* Check if DAZ is available. */
46 1.1.1.3 mrg struct
47 1.1.1.3 mrg {
48 1.1.1.3 mrg unsigned short cwd;
49 1.1.1.3 mrg unsigned short swd;
50 1.1.1.3 mrg unsigned short twd;
51 1.1.1.3 mrg unsigned short fop;
52 1.1.1.3 mrg unsigned int fip;
53 1.1.1.3 mrg unsigned int fcs;
54 1.1.1.3 mrg unsigned int foo;
55 1.1.1.3 mrg unsigned int fos;
56 1.1.1.3 mrg unsigned int mxcsr;
57 1.1.1.3 mrg unsigned int mxcsr_mask;
58 1.1.1.3 mrg unsigned int st_space[32];
59 1.1.1.3 mrg unsigned int xmm_space[32];
60 1.1.1.3 mrg unsigned int padding[56];
61 1.1.1.3 mrg } __attribute__ ((aligned (16))) fxsave;
62 1.1.1.3 mrg
63 1.1.1.3 mrg /* This is necessary since some implementations of FXSAVE
64 1.1.1.3 mrg do not modify reserved areas within the image. */
65 1.1.1.3 mrg fxsave.mxcsr_mask = 0;
66 1.1.1.3 mrg
67 1.1.1.3 mrg __builtin_ia32_fxsave (&fxsave);
68 1.1.1.3 mrg
69 1.1.1.3 mrg mxcsr = fxsave.mxcsr;
70 1.1.1.3 mrg
71 1.1.1.3 mrg if (fxsave.mxcsr_mask & MXCSR_DAZ)
72 1.1.1.3 mrg mxcsr |= MXCSR_DAZ;
73 1.1.1.3 mrg }
74 1.1.1.3 mrg else
75 1.1.1.3 mrg mxcsr = __builtin_ia32_stmxcsr ();
76 1.1.1.3 mrg
77 1.1.1.3 mrg mxcsr |= MXCSR_FTZ;
78 1.1.1.3 mrg __builtin_ia32_ldmxcsr (mxcsr);
79 1.1.1.3 mrg }
80 1.1 mrg #endif
81 1.1.1.3 mrg
82 1.1.1.3 mrg static void __attribute__((constructor))
83 1.1 mrg set_fast_math (void)
84 1.1 mrg {
85 1.1 mrg #ifndef __x86_64__
86 1.1 mrg unsigned int eax, ebx, ecx, edx;
87 1.1 mrg
88 1.1 mrg if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
89 1.1 mrg return;
90 1.1 mrg
91 1.1 mrg if (edx & bit_SSE)
92 1.1.1.3 mrg set_fast_math_sse (edx);
93 1.1 mrg #else
94 1.1 mrg unsigned int mxcsr = __builtin_ia32_stmxcsr ();
95 1.1 mrg mxcsr |= MXCSR_DAZ | MXCSR_FTZ;
96 1.1 mrg __builtin_ia32_ldmxcsr (mxcsr);
97 1.1 mrg #endif
98 1.1 mrg }
99 1.1.1.2 mrg #endif
100