fenv.h revision 1.3 1 /* $NetBSD: fenv.h,v 1.3 2019/10/27 21:27:10 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2004-2005 David Schultz <das (at) FreeBSD.ORG>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: src/lib/msun/alpha/fenv.h,v 1.3 2005/03/16 19:03:44 das Exp $
29 */
30
31 #ifndef _ALPHA_FENV_H_
32 #define _ALPHA_FENV_H_
33
34 #include <sys/stdint.h>
35
36 typedef __uint64_t fenv_t;
37 typedef __uint16_t fexcept_t;
38
39 /* Exception flags */
40 #define FE_INVALID 0x01
41 #define FE_DIVBYZERO 0x02
42 #define FE_OVERFLOW 0x04
43 #define FE_UNDERFLOW 0x08
44 #define FE_INEXACT 0x10
45 #define FE_INTOVF 0x20 /* not maskable */
46 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INTOVF | \
47 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
48
49 /* Rounding modes */
50 #define FE_TOWARDZERO 0x00
51 #define FE_DOWNWARD 0x01
52 #define FE_TONEAREST 0x02
53 #define FE_UPWARD 0x03
54 #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
55 FE_UPWARD | FE_TOWARDZERO)
56 #define _ROUND_SHIFT 58
57
58 #define _FPUSW_SHIFT 52
59
60 #define __excb() __asm __volatile("excb")
61 #define __mf_fpcr(__cw) __asm __volatile("mf_fpcr %0" : "=f" (*(__cw)))
62 #define __mt_fpcr(__cw) __asm __volatile("mt_fpcr %0" : : "f" (__cw))
63
64 union __fpcr {
65 double __d;
66 fenv_t __bits;
67 };
68
69 __BEGIN_DECLS
70
71 /* Default floating-point environment */
72 extern const fenv_t __fe_dfl_env;
73 #define FE_DFL_ENV (&__fe_dfl_env)
74
75 #if __GNUC_PREREQ__(8, 0)
76 #pragma GCC diagnostic push
77 #pragma GCC diagnostic ignored "-Wshadow"
78 #endif
79
80 static __inline int
81 feclearexcept(int __excepts)
82 {
83 union __fpcr __r;
84
85 __excb();
86 __mf_fpcr(&__r.__d);
87 __r.__bits &= ~((fenv_t)__excepts << _FPUSW_SHIFT);
88 __mt_fpcr(__r.__d);
89 __excb();
90 return 0;
91 }
92
93 static __inline int
94 fegetexceptflag(fexcept_t *__flagp, int __excepts)
95 {
96 union __fpcr __r;
97
98 __excb();
99 __mf_fpcr(&__r.__d);
100 __excb();
101 *__flagp = (__r.__bits >> _FPUSW_SHIFT) & __excepts;
102 return 0;
103 }
104
105 static __inline int
106 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
107 {
108 union __fpcr __r;
109 fenv_t __xflag, __xexcepts;
110
111 __xflag = (fenv_t)*__flagp << _FPUSW_SHIFT;
112 __xexcepts = (fenv_t)__excepts << _FPUSW_SHIFT;
113 __excb();
114 __mf_fpcr(&__r.__d);
115 __r.__bits &= ~__xexcepts;
116 __r.__bits |= __xflag & __xexcepts;
117 __mt_fpcr(__r.__d);
118 __excb();
119 return 0;
120 }
121
122 static __inline int
123 feraiseexcept(int __excepts)
124 {
125
126 /*
127 * XXX Generating exceptions this way does not actually invoke
128 * a userland trap handler when enabled, but neither do
129 * arithmetic operations as far as I can tell. Perhaps there
130 * are more bugs in the kernel trap handler.
131 */
132 fexcept_t __ex = __excepts;
133 fesetexceptflag(&__ex, __excepts);
134 return 0;
135 }
136
137 static __inline int
138 fetestexcept(int __excepts)
139 {
140 union __fpcr __r;
141
142 __excb();
143 __mf_fpcr(&__r.__d);
144 __excb();
145 return (__r.__bits >> _FPUSW_SHIFT) & __excepts;
146 }
147
148 static __inline int
149 fegetround(void)
150 {
151 union __fpcr __r;
152
153 /*
154 * No exception barriers should be required here if we assume
155 * that only fesetround() can change the rounding mode.
156 */
157 __mf_fpcr(&__r.__d);
158 return (int)(__r.__bits >> _ROUND_SHIFT) & _ROUND_MASK;
159 }
160
161 static __inline int
162 fesetround(int __round)
163 {
164 union __fpcr __r;
165
166 if (__round & ~_ROUND_MASK)
167 return (-1);
168 __excb();
169 __mf_fpcr(&__r.__d);
170 __r.__bits &= ~((fenv_t)_ROUND_MASK << _ROUND_SHIFT);
171 __r.__bits |= (fenv_t)__round << _ROUND_SHIFT;
172 __mt_fpcr(__r.__d);
173 __excb();
174 return 0;
175 }
176
177 #if __GNUC_PREREQ__(8, 0)
178 #pragma GCC diagnostic pop
179 #endif
180
181 int fegetenv(fenv_t *);
182 int feholdexcept(fenv_t *);
183 int fesetenv(const fenv_t *);
184 int feupdateenv(const fenv_t *);
185
186 #if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE)
187 int feenableexcept(int);
188 int fedisableexcept(int);
189 int fegetexcept(void);
190 #endif /* _NETBSD_SOURCE || _GNU_SOURCE */
191
192
193 __END_DECLS
194
195 #endif /* !_ALPHA_FENV_H_ */
196