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