]>
Commit | Line | Data |
---|---|---|
8d9254fc | 1 | /* Copyright (C) 2013-2020 Free Software Foundation, Inc. |
6fbf9877 UB |
2 | |
3 | This file is part of the GNU Atomic Library (libatomic). | |
4 | ||
5 | Libatomic is free software; you can redistribute it and/or modify it | |
6 | under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | Libatomic is distributed in the hope that it will be useful, but WITHOUT ANY | |
11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
12 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | more details. | |
14 | ||
15 | Under Section 7 of GPL version 3, you are granted additional | |
16 | permissions described in the GCC Runtime Library Exception, version | |
17 | 3.1, as published by the Free Software Foundation. | |
18 | ||
19 | You should have received a copy of the GNU General Public License and | |
20 | a copy of the GCC Runtime Library Exception along with this program; | |
21 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
22 | <http://www.gnu.org/licenses/>. */ | |
23 | ||
24 | #include "libatomic_i.h" | |
25 | ||
26 | #define FE_INVALID 0x01 | |
27 | #define FE_DENORM 0x02 | |
28 | #define FE_DIVBYZERO 0x04 | |
29 | #define FE_OVERFLOW 0x08 | |
30 | #define FE_UNDERFLOW 0x10 | |
31 | #define FE_INEXACT 0x20 | |
32 | ||
33 | struct fenv | |
34 | { | |
35 | unsigned short int __control_word; | |
36 | unsigned short int __unused1; | |
37 | unsigned short int __status_word; | |
38 | unsigned short int __unused2; | |
39 | unsigned short int __tags; | |
40 | unsigned short int __unused3; | |
41 | unsigned int __eip; | |
42 | unsigned short int __cs_selector; | |
43 | unsigned int __opcode:11; | |
44 | unsigned int __unused4:5; | |
45 | unsigned int __data_offset; | |
46 | unsigned short int __data_selector; | |
47 | unsigned short int __unused5; | |
48 | }; | |
49 | ||
50 | /* Raise the supported floating-point exceptions from EXCEPTS. Other | |
51 | bits in EXCEPTS are ignored. */ | |
52 | ||
53 | void | |
54 | __atomic_feraiseexcept (int excepts) | |
55 | { | |
56 | if (excepts & FE_INVALID) | |
57 | { | |
58 | float f = 0.0f; | |
8b02e720 | 59 | #ifdef __SSE_MATH__ |
6fbf9877 UB |
60 | volatile float r __attribute__ ((unused)); |
61 | asm volatile ("%vdivss\t{%0, %d0|%d0, %0}" : "+x" (f)); | |
62 | r = f; /* Needed to trigger exception. */ | |
63 | #else | |
64 | asm volatile ("fdiv\t{%y0, %0|%0, %y0}" : "+t" (f)); | |
65 | /* No need for fwait, exception is triggered by emitted fstp. */ | |
66 | #endif | |
67 | } | |
68 | if (excepts & FE_DENORM) | |
69 | { | |
70 | struct fenv temp; | |
71 | asm volatile ("fnstenv\t%0" : "=m" (temp)); | |
72 | temp.__status_word |= FE_DENORM; | |
73 | asm volatile ("fldenv\t%0" : : "m" (temp)); | |
74 | asm volatile ("fwait"); | |
75 | } | |
76 | if (excepts & FE_DIVBYZERO) | |
77 | { | |
78 | float f = 1.0f, g = 0.0f; | |
8b02e720 | 79 | #ifdef __SSE_MATH__ |
6fbf9877 UB |
80 | volatile float r __attribute__ ((unused)); |
81 | asm volatile ("%vdivss\t{%1, %d0|%d0, %1}" : "+x" (f) : "xm" (g)); | |
82 | r = f; /* Needed to trigger exception. */ | |
83 | #else | |
84 | asm volatile ("fdivs\t%1" : "+t" (f) : "m" (g)); | |
85 | /* No need for fwait, exception is triggered by emitted fstp. */ | |
86 | #endif | |
87 | } | |
88 | if (excepts & FE_OVERFLOW) | |
89 | { | |
90 | struct fenv temp; | |
91 | asm volatile ("fnstenv\t%0" : "=m" (temp)); | |
92 | temp.__status_word |= FE_OVERFLOW; | |
93 | asm volatile ("fldenv\t%0" : : "m" (temp)); | |
94 | asm volatile ("fwait"); | |
95 | } | |
96 | if (excepts & FE_UNDERFLOW) | |
97 | { | |
98 | struct fenv temp; | |
99 | asm volatile ("fnstenv\t%0" : "=m" (temp)); | |
100 | temp.__status_word |= FE_UNDERFLOW; | |
101 | asm volatile ("fldenv\t%0" : : "m" (temp)); | |
102 | asm volatile ("fwait"); | |
103 | } | |
104 | if (excepts & FE_INEXACT) | |
105 | { | |
106 | float f = 1.0f, g = 3.0f; | |
8b02e720 | 107 | #ifdef __SSE_MATH__ |
6fbf9877 UB |
108 | volatile float r __attribute__ ((unused)); |
109 | asm volatile ("%vdivss\t{%1, %d0|%d0, %1}" : "+x" (f) : "xm" (g)); | |
110 | r = f; /* Needed to trigger exception. */ | |
111 | #else | |
112 | asm volatile ("fdivs\t%1" : "+t" (f) : "m" (g)); | |
113 | /* No need for fwait, exception is triggered by emitted fstp. */ | |
114 | #endif | |
115 | } | |
116 | } |