fenv.h revision 1.2 1 1.2 christos /* $NetBSD: fenv.h,v 1.2 2016/08/25 12:29:14 christos 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.1 christos typedef uint32_t fexcept_t;
64 1.1 christos
65 1.1 christos typedef struct {
66 1.1 christos uint32_t __fpscr;
67 1.1 christos } fenv_t;
68 1.1 christos
69 1.1 christos #define FE_DFL_ENV ((fenv_t *) -1)
70 1.1 christos
71 1.1 christos #define __get_fpscr(__fpscr) \
72 1.1 christos __asm__ __volatile__ ("sts fpscr,%0" : "=r" (__fpscr))
73 1.1 christos #define __set_fpscr(__fpscr) \
74 1.1 christos __asm__ __volatile__ ("lds %0,fpscr" : : "r" (__fpscr))
75 1.1 christos
76 1.1 christos __BEGIN_DECLS
77 1.1 christos
78 1.1 christos __fenv_static inline int
79 1.1 christos feclearexcept(int __excepts)
80 1.1 christos {
81 1.1 christos fexcept_t __fpscr;
82 1.1 christos
83 1.1 christos __excepts &= FE_ALL_EXCEPT;
84 1.1 christos
85 1.1 christos __get_fpscr(__fpscr);
86 1.1 christos __fpscr &= ~__excepts;
87 1.1 christos __set_fpscr(__fpscr);
88 1.1 christos
89 1.1 christos return 0;
90 1.1 christos }
91 1.1 christos
92 1.1 christos __fenv_static inline int
93 1.1 christos fegetexceptflag(fexcept_t *__flagp, int __excepts)
94 1.1 christos {
95 1.1 christos fexcept_t __fpscr;
96 1.1 christos
97 1.1 christos __get_fpscr(__fpscr);
98 1.1 christos
99 1.1 christos *__flagp = __fpscr & __excepts & FE_ALL_EXCEPT;
100 1.1 christos
101 1.1 christos return 0;
102 1.1 christos }
103 1.1 christos
104 1.1 christos __fenv_static inline int
105 1.1 christos fesetexceptflag(const fexcept_t *__flagp, int __excepts)
106 1.1 christos {
107 1.1 christos fexcept_t __fpscr;
108 1.1 christos
109 1.1 christos __get_fpscr(__fpscr);
110 1.1 christos
111 1.1 christos __fpscr &= ~(__excepts & FE_ALL_EXCEPT);
112 1.1 christos __fpscr |= *__flagp & __excepts & FE_ALL_EXCEPT;
113 1.1 christos
114 1.1 christos __set_fpscr(__fpscr);
115 1.1 christos
116 1.1 christos return 0;
117 1.1 christos }
118 1.1 christos
119 1.1 christos static inline void
120 1.1 christos __fmul(double a, double b)
121 1.1 christos {
122 1.1 christos #ifdef __sh4__
123 1.1 christos __asm__ __volatile__ ("fmul %1, %0" : "+d" (a) : "d" (b));
124 1.1 christos #endif
125 1.1 christos }
126 1.1 christos
127 1.1 christos static inline void
128 1.1 christos __fdiv(double a, double b) {
129 1.1 christos #ifdef __sh4__
130 1.1 christos __asm__ __volatile__ ("fdiv %1, %0" : "+d" (a) : "d" (b));
131 1.1 christos #endif
132 1.1 christos }
133 1.1 christos
134 1.1 christos __fenv_static inline int
135 1.1 christos feraiseexcept(int __excepts)
136 1.1 christos {
137 1.1 christos fexcept_t __fpscr;
138 1.1 christos
139 1.1 christos if (__excepts & FE_INVALID) /* Inf * 0 */
140 1.1 christos __fmul(__builtin_huge_val(), 0.0);
141 1.1 christos
142 1.1 christos if (__excepts & FE_DIVBYZERO) /* 1.0 / 0 */
143 1.1 christos __fdiv(1.0, 0.0);
144 1.1 christos
145 1.1 christos if (__excepts & FE_OVERFLOW) /* MAX * MAX */
146 1.1 christos __fmul(LDBL_MAX, LDBL_MAX);
147 1.1 christos
148 1.1 christos if (__excepts & FE_UNDERFLOW) /* MIN / 10.0 */
149 1.1 christos __fdiv(LDBL_MIN, 10.0);
150 1.1 christos
151 1.1 christos if (__excepts & FE_INEXACT) /* 1 / 3 */
152 1.1 christos __fdiv(1.0, 3.0);
153 1.1 christos
154 1.1 christos __get_fpscr(__fpscr);
155 1.1 christos
156 1.1 christos __fpscr |= __excepts & FE_ALL_EXCEPT;
157 1.1 christos
158 1.1 christos __set_fpscr(__fpscr);
159 1.1 christos
160 1.1 christos return 0;
161 1.1 christos }
162 1.1 christos
163 1.1 christos __fenv_static inline int
164 1.1 christos fetestexcept(int __excepts)
165 1.1 christos {
166 1.1 christos fexcept_t __fpscr;
167 1.1 christos
168 1.1 christos __get_fpscr(__fpscr);
169 1.1 christos
170 1.1 christos return __fpscr & __excepts & FE_ALL_EXCEPT;
171 1.1 christos }
172 1.1 christos
173 1.1 christos __fenv_static inline int
174 1.1 christos fegetround(void)
175 1.1 christos {
176 1.1 christos fexcept_t __fpscr;
177 1.1 christos
178 1.1 christos __get_fpscr(__fpscr);
179 1.1 christos return __fpscr & _ROUND_MASK;
180 1.1 christos }
181 1.1 christos
182 1.1 christos __fenv_static inline int
183 1.1 christos fesetround(int __round)
184 1.1 christos {
185 1.1 christos fexcept_t __fpscr;
186 1.1 christos
187 1.1 christos if (__round & ~_ROUND_MASK)
188 1.1 christos return -1;
189 1.1 christos
190 1.1 christos __get_fpscr(__fpscr);
191 1.1 christos
192 1.1 christos __fpscr &= ~_ROUND_MASK;
193 1.1 christos __fpscr |= __round;
194 1.1 christos
195 1.1 christos __set_fpscr(__fpscr);
196 1.1 christos
197 1.1 christos return 0;
198 1.1 christos }
199 1.1 christos
200 1.1 christos __fenv_static inline int
201 1.1 christos fegetenv(fenv_t *__envp)
202 1.1 christos {
203 1.1 christos fexcept_t __fpscr;
204 1.1 christos
205 1.1 christos __get_fpscr(__fpscr);
206 1.1 christos __envp->__fpscr = __fpscr;
207 1.1 christos
208 1.1 christos return 0;
209 1.1 christos }
210 1.1 christos
211 1.1 christos __fenv_static inline int
212 1.1 christos feholdexcept(fenv_t *__envp)
213 1.1 christos {
214 1.1 christos fexcept_t __fpscr;
215 1.1 christos
216 1.1 christos __get_fpscr(__fpscr);
217 1.1 christos __envp->__fpscr = __fpscr;
218 1.1 christos
219 1.1 christos __fpscr &= ~FE_ALL_EXCEPT;
220 1.1 christos __fpscr &= ~(FE_ALL_EXCEPT << 5);
221 1.1 christos __set_fpscr(__fpscr); /* clear all */
222 1.1 christos
223 1.1 christos return 0;
224 1.1 christos }
225 1.1 christos
226 1.1 christos __fenv_static inline int
227 1.1 christos fesetenv(const fenv_t *__envp)
228 1.1 christos {
229 1.1 christos if (__envp == FE_DFL_ENV)
230 1.1 christos __set_fpscr(FPSCR_DEFAULT);
231 1.1 christos else
232 1.1 christos __set_fpscr(__envp->__fpscr);
233 1.1 christos
234 1.1 christos return 0;
235 1.1 christos }
236 1.1 christos
237 1.1 christos __fenv_static inline int
238 1.1 christos feupdateenv(const fenv_t *__envp)
239 1.1 christos {
240 1.1 christos fexcept_t __fpscr;
241 1.1 christos
242 1.1 christos __get_fpscr(__fpscr);
243 1.1 christos __fpscr &= FE_ALL_EXCEPT;
244 1.1 christos fesetenv(__envp);
245 1.1 christos feraiseexcept((int)__fpscr);
246 1.1 christos return 0;
247 1.1 christos }
248 1.1 christos
249 1.1 christos #if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE)
250 1.1 christos
251 1.1 christos /* We currently provide no external definitions of the functions below. */
252 1.1 christos
253 1.1 christos static inline int
254 1.1 christos feenableexcept(int __mask)
255 1.1 christos {
256 1.1 christos fexcept_t __fpscr, __oldmask;
257 1.1 christos
258 1.1 christos __get_fpscr(__fpscr);
259 1.1 christos __oldmask = (__fpscr >> 5) & FE_ALL_EXCEPT;
260 1.1 christos __fpscr |= (__mask & FE_ALL_EXCEPT) << 5;
261 1.1 christos __set_fpscr(__fpscr);
262 1.1 christos
263 1.1 christos return __oldmask;
264 1.1 christos }
265 1.1 christos
266 1.1 christos static inline int
267 1.1 christos fedisableexcept(int __mask)
268 1.1 christos {
269 1.1 christos fexcept_t __fpscr, __oldmask;
270 1.1 christos
271 1.1 christos __get_fpscr(__fpscr);
272 1.1 christos __oldmask = (__fpscr >> 5) & FE_ALL_EXCEPT;
273 1.1 christos __fpscr &= ~(__mask & FE_ALL_EXCEPT) << 5;
274 1.1 christos __set_fpscr(__fpscr);
275 1.1 christos
276 1.1 christos return __oldmask;
277 1.1 christos }
278 1.1 christos
279 1.1 christos static inline int
280 1.1 christos fegetexcept(void)
281 1.1 christos {
282 1.1 christos fexcept_t __fpscr;
283 1.1 christos
284 1.1 christos __get_fpscr(__fpscr);
285 1.1 christos
286 1.1 christos return (__fpscr >> 5) & FE_ALL_EXCEPT;
287 1.1 christos }
288 1.1 christos
289 1.1 christos #endif /* _NETBSD_SOURCE || _GNU_SOURCE */
290 1.1 christos
291 1.1 christos __END_DECLS
292 1.1 christos
293 1.1 christos #endif /* _SH3_FENV_H_ */
294