fenv.h revision 1.1 1 /* $NetBSD: fenv.h,v 1.1 2015/12/24 14:12:39 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _M68K_FENV_H_
33 #define _M68K_FENV_H_
34
35 #include <sys/stdint.h>
36 #include <m68k/float.h>
37 #include <m68k/fpreg.h>
38
39 #ifndef __fenv_static
40 #define __fenv_static static
41 #endif
42
43 /* Exception bits, from FPSR */
44 #define FE_INEXACT FPSR_AINEX
45 #define FE_DIVBYZERO FPSR_ADZ
46 #define FE_UNDERFLOW FPSR_AUNFL
47 #define FE_OVERFLOW FPSR_AOVFL
48 #define FE_INVALID FPSR_AIOP
49
50 #define FE_ALL_EXCEPT \
51 (FE_INEXACT | FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID)
52
53 /* Rounding modes, from FPSR */
54 #define FE_TONEAREST FPCR_NEAR
55 #define FE_TOWARDZERO FPCR_ZERO
56 #define FE_DOWNWARD FPCR_MINF
57 #define FE_UPWARD FPCR_PINF
58
59 #define _ROUND_MASK \
60 (FE_TONEAREST | FE_TOWARDZERO | FE_DOWNWARD | FE_UPWARD)
61
62 typedef uint32_t fexcept_t;
63
64 /* same layout as fmovem */
65 typedef struct {
66 uint32_t fpcr;
67 uint32_t fpsr;
68 uint32_t fppc;
69 } fenv_t;
70
71 #define FE_DFL_ENV ((fenv_t *) -1)
72
73 #define __get_fpcr(__fpcr) \
74 __asm__ __volatile__ ("fmove%.l %!,%0" : "=dm" (__fpcr))
75 #define __set_fpcr(__fpcr) \
76 __asm__ __volatile__ ("fmove%.l %0,%!" : : "dm" (__fpcr))
77
78 #define __get_fpsr(__fpsr) \
79 __asm__ __volatile__ ("fmove%.l %/fpsr,%0" : "=dm" (__fpsr))
80 #define __set_fpsr(__fpsr) \
81 __asm__ __volatile__ ("fmove%.l %0,%/fpsr" : : "dm" (__fpsr))
82
83 #define __fmul(__s, __t, __d) \
84 do { \
85 __t d = __d; \
86 __asm__ __volatile__ ("fmul" __s "; fnop" : "=f" (d) : "0" (d)); \
87 } while (/*CONSTCOND*/0)
88
89 #define __fdiv(__s, __t, __d) \
90 do { \
91 __t d = __d; \
92 __asm__ __volatile__ ("fdiv" __s "; fnop" : "=f" (d) : "0" (d)); \
93 } while (/*CONSTCOND*/0)
94
95 #define __fetox(__s, __t, __d) \
96 do { \
97 __t d = __d; \
98 __asm__ __volatile__ ("fetox" __s "; fnop" : "=f" (d) : "0" (d)); \
99 } while (/*CONSTCOND*/0)
100
101 #define __fgetenv(__envp) \
102 __asm__ __volatile__ ("fmovem%.l %/fpcr/%/fpsr/%/fpiar,%0" : "=m" (__envp))
103
104 #define __fsetenv(__envp) \
105 __asm__ __volatile__ ("fmovem%.l %0,%/fpcr/%/fpsr/%/fpiar" : : "m" (__envp))
106
107 __fenv_static inline int
108 feclearexcept(int __excepts)
109 {
110 fexcept_t __fpsr;
111
112 __excepts &= FE_ALL_EXCEPT;
113
114 __get_fpsr(__fpsr);
115 __fpsr &= ~__excepts;
116 __set_fpsr(__fpsr);
117
118 return 0;
119 }
120
121 __fenv_static inline int
122 fegetexceptflag(fexcept_t *__flagp, int __excepts)
123 {
124 fexcept_t __fpsr;
125
126 __get_fpsr(__fpsr);
127
128 *__flagp = __fpsr & __excepts & FE_ALL_EXCEPT;
129
130 return 0;
131 }
132
133 __fenv_static inline int
134 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
135 {
136 fexcept_t __fpsr;
137
138 __get_fpsr(__fpsr);
139
140 __fpsr &= ~(__excepts & FE_ALL_EXCEPT);
141 __fpsr |= *__flagp & __excepts & FE_ALL_EXCEPT;
142
143 __set_fpsr(__fpsr);
144
145 return 0;
146 }
147
148 __fenv_static inline int
149 feraiseexcept(int __excepts)
150 {
151 if (__excepts & FE_INVALID) /* Inf * 0 */
152 __fmul("%.s %#0r0,%0", double, __builtin_huge_val());
153
154 if (__excepts & FE_DIVBYZERO) /* 1.0 / 0 */
155 __fdiv("%.s %#0r0,%0", double, 1.0);
156
157 if (__excepts & FE_OVERFLOW) /* MAX * MAX */
158 __fmul("%.x %0,%0", long double, LDBL_MAX);
159
160 if (__excepts & FE_UNDERFLOW) /* e ^ -MAX */
161 __fetox("%.x %0", long double, -LDBL_MAX);
162
163 if (__excepts & FE_INEXACT) /* 1 / 3 */
164 __fdiv("%.s %#0r3,%0", long double, 1.0);
165
166 return 0;
167 }
168
169 __fenv_static inline int
170 fetestexcept(int __excepts)
171 {
172 fexcept_t __fpsr;
173
174 __get_fpsr(__fpsr);
175
176 return __fpsr & __excepts & FE_ALL_EXCEPT;
177 }
178
179 __fenv_static inline int
180 fegetround(void)
181 {
182 fexcept_t __fpcr;
183
184 __get_fpcr(__fpcr);
185 return __fpcr & _ROUND_MASK;
186 }
187
188 __fenv_static inline int
189 fesetround(int __round)
190 {
191 fexcept_t __fpcr;
192
193 if (__round & ~_ROUND_MASK)
194 return -1;
195
196 __get_fpcr(__fpcr);
197
198 __fpcr &= ~_ROUND_MASK;
199 __fpcr |= __round;
200
201 __set_fpcr(__fpcr);
202
203 return 0;
204 }
205
206 __fenv_static inline int
207 fegetenv(fenv_t *__envp)
208 {
209 __fgetenv(__envp);
210
211 return 0;
212 }
213
214 __fenv_static inline int
215 feholdexcept(fenv_t *__envp)
216 {
217 fexcept_t __fpcr, __fpsr;
218
219 __fgetenv(__envp);
220 __fpsr = __envp->fpsr & ~FE_ALL_EXCEPT;
221 __fset_fpsr(__fpsr); /* clear all */
222 __fpcr = __envp->fpcr & ~(FE_ALL_EXCEPT << 6);
223 __fset_fpcr(__fpcr); /* set non/stop */
224
225 return 0;
226 }
227
228 __fenv_static inline int
229 fesetenv(const fenv_t *__envp)
230 {
231 fenv_t __tenv;
232
233 __fgetenv(__tenv);
234
235 if (__envp == FE_DFL_ENV) {
236 __tenv.fpcr |=
237 __envp->fpcr & ((FE_ALL_EXCEPT << 6) | FE_UPWARD);
238 __tenv.fpsr |= __envp->fpsr & FE_ALL_EXCEPT;
239 }
240
241 __fsetenv(__tenv);
242
243 return 0;
244 }
245
246 __fenv_static inline int
247 feupdateenv(const fenv_t *__envp)
248 {
249 fexcept_t __fpsr;
250
251 __get_fpsr(__fpsr);
252 __fpsr &= FE_ALL_EXCEPT;
253 fesetenv(__envp);
254 feraiseexcept((int)__fpsr);
255 return 0;
256 }
257
258 #if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE)
259
260 /* We currently provide no external definitions of the functions below. */
261
262 static inline int
263 feenableexcept(int __mask)
264 {
265 fexcept_t __fpsr, __oldmask;
266
267 __get_fpsr(__fpsr);
268 __oldmask = __fpsr & FE_ALL_EXCEPT;
269 __fpsr |= __mask & FE_ALL_EXCEPT;
270 __set_fpsr(__fpsr);
271
272 return __oldmask;
273 }
274
275 static inline int
276 fedisableexcept(int __mask)
277 {
278 fexcept_t __fpsr, __oldmask;
279
280 __get_fpsr(__fpsr);
281 __oldmask = __fpsr & FE_ALL_EXCEPT;
282 __fpsr &= ~(__mask & FE_ALL_EXCEPT);
283 __set_fpsr(__fpsr);
284
285 return __oldmask;
286 }
287
288 static inline int
289 fegetexcept(void)
290 {
291 fexcept_t __fpsr;
292
293 __get_fpsr(__fpsr);
294
295 return __fpsr & FE_ALL_EXCEPT;
296 }
297
298 #endif /* _NETBSD_SOURCE || _GNU_SOURCE */
299
300 __END_DECLS
301
302 #endif /* _M68K_FENV_H_ */
303