1 /* $NetBSD: fenv.c,v 1.2 2017/03/22 23:11:09 chs Exp $ */
2
3 /*-
4 * Copyright (c) 2004-2005 David Schultz <das@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 */
26
27 /* Load floating-point state register (32bits) */
28 #define __ldfsr(__r) __asm__ __volatile__ \
29 ("ld %0, %%fsr" : : "m" (__r))
30
31 /* Save floating-point state register (32bits) */
32 #define __stfsr(__r) __asm__ __volatile__ \
33 ("st %%fsr, %0" : "=m" (*(__r)))
34
35 /*
36 * The feclearexcept() function clears the supported floating-point exceptions
37 * represented by `excepts'.
38 */
39 __declare_fenv_inline(int)
feclearexcept(int excepts)40 feclearexcept(int excepts)
41 {
42 fexcept_t r;
43 int ex;
44
45 ex = excepts & FE_ALL_EXCEPT;
46
47 __stfsr(&r);
48 r &= ~ex;
49 __ldfsr(r);
50
51 /* Success */
52 return 0;
53 }
54
55 /*
56 * The fegetexceptflag() function stores an implementation-defined
57 * representation of the states of the floating-point status flags indicated
58 * by the argument excepts in the object pointed to by the argument flagp.
59 */
60 __declare_fenv_inline(int)
fegetexceptflag(fexcept_t * flagp,int excepts)61 fegetexceptflag(fexcept_t *flagp, int excepts)
62 {
63 fexcept_t r;
64 int ex;
65
66 ex = excepts & FE_ALL_EXCEPT;
67
68 __stfsr(&r);
69 *flagp = r & ex;
70
71 /* Success */
72 return 0;
73 }
74
75
76 /*
77 * This function sets the floating-point status flags indicated by the argument
78 * `excepts' to the states stored in the object pointed to by `flagp'. It does
79 * NOT raise any floating-point exceptions, but only sets the state of the flags.
80 */
81 __declare_fenv_inline(int)
fesetexceptflag(const fexcept_t * flagp,int excepts)82 fesetexceptflag(const fexcept_t *flagp, int excepts)
83 {
84 fexcept_t r;
85 int ex;
86
87 ex = excepts & FE_ALL_EXCEPT;
88
89 __stfsr(&r);
90 r &= ~ex;
91 r |= *flagp & ex;
92 __ldfsr(r);
93
94 /* Success */
95 return 0;
96 }
97
98 __declare_fenv_inline(int)
fesetexcept(int excepts)99 fesetexcept(int excepts)
100 {
101 fexcept_t __ex = excepts;
102
103 return fesetexceptflag(&__ex, excepts);
104 }
105
106 /*
107 * The feraiseexcept() function raises the supported floating-point exceptions
108 * represented by the argument `excepts'.
109 *
110 * The order in which these floating-point exceptions are raised is unspecified
111 * (by the standard).
112 */
113 __declare_fenv_inline(int)
feraiseexcept(int excepts)114 feraiseexcept(int excepts)
115 {
116 volatile double d;
117 int ex;
118
119 ex = excepts & FE_ALL_EXCEPT;
120
121 /*
122 * With a compiler that supports the FENV_ACCESS pragma properly, simple
123 * expressions like '0.0 / 0.0' should be sufficient to generate traps.
124 * Unfortunately, we need to bring a volatile variable into the equation
125 * to prevent incorrect optimizations.
126 */
127 if (ex & FE_INVALID) {
128 d = 0.0;
129 d = 0.0 / d;
130 }
131 if (ex & FE_DIVBYZERO) {
132 d = 0.0;
133 d = 1.0 / d;
134 }
135 if (ex & FE_OVERFLOW) {
136 d = 0x1.ffp1023;
137 d *= 2.0;
138 }
139 if (ex & FE_UNDERFLOW) {
140 d = 0x1p-1022;
141 d /= 0x1p1023;
142 }
143 if (ex & FE_INEXACT) {
144 d = 0x1p-1022;
145 d += 1.0;
146 }
147
148 /* Success */
149 return 0;
150 }
151
152 /*
153 * The fetestexcept() function determines which of a specified subset of the
154 * floating-point exception flags are currently set. The `excepts' argument
155 * specifies the floating-point status flags to be queried.
156 */
157 __declare_fenv_inline(int)
fetestexcept(int excepts)158 fetestexcept(int excepts)
159 {
160 fexcept_t r;
161
162 __stfsr(&r);
163
164 return r & (excepts & FE_ALL_EXCEPT);
165 }
166
167 /*
168 * The fegetround() function gets the current rounding direction.
169 */
170 __declare_fenv_inline(int)
fegetround(void)171 fegetround(void)
172 {
173 fenv_t r;
174
175 __stfsr(&r);
176
177 return (r >> _ROUND_SHIFT) & _ROUND_MASK;
178 }
179
180 /*
181 * The fesetround() function establishes the rounding direction represented by
182 * its argument `round'. If the argument is not equal to the value of a rounding
183 * direction macro, the rounding direction is not changed.
184 */
185 __declare_fenv_inline(int)
fesetround(int round)186 fesetround(int round)
187 {
188 fenv_t r;
189
190 if (round & ~_ROUND_MASK)
191 return -1;
192
193 __stfsr(&r);
194 r &= ~(_ROUND_MASK << _ROUND_SHIFT);
195 r |= round << _ROUND_SHIFT;
196 __ldfsr(r);
197
198 /* Success */
199 return 0;
200 }
201
202 /*
203 * The fegetenv() function attempts to store the current floating-point
204 * environment in the object pointed to by envp.
205 */
206 __declare_fenv_inline(int)
fegetenv(fenv_t * envp)207 fegetenv(fenv_t *envp)
208 {
209 __stfsr(envp);
210
211 /* Success */
212 return 0;
213 }
214
215
216 /*
217 * The feholdexcept() function saves the current floating-point environment
218 * in the object pointed to by envp, clears the floating-point status flags, and
219 * then installs a non-stop (continue on floating-point exceptions) mode, if
220 * available, for all floating-point exceptions.
221 */
222 __declare_fenv_inline(int)
feholdexcept(fenv_t * envp)223 feholdexcept(fenv_t *envp)
224 {
225 fenv_t r;
226
227 __stfsr(&r);
228 *envp = r;
229 r &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
230 __ldfsr(r);
231
232 /* Success */
233 return 0;
234 }
235
236 /*
237 * The fesetenv() function attempts to establish the floating-point environment
238 * represented by the object pointed to by envp. The argument `envp' points
239 * to an object set by a call to fegetenv() or feholdexcept(), or equal a
240 * floating-point environment macro. The fesetenv() function does not raise
241 * floating-point exceptions, but only installs the state of the floating-point
242 * status flags represented through its argument.
243 */
244 __declare_fenv_inline(int)
fesetenv(const fenv_t * envp)245 fesetenv(const fenv_t *envp)
246 {
247 __ldfsr(*envp);
248
249 /* Success */
250 return 0;
251 }
252
253
254 /*
255 * The feupdateenv() function saves the currently raised floating-point
256 * exceptions in its automatic storage, installs the floating-point environment
257 * represented by the object pointed to by `envp', and then raises the saved
258 * floating-point exceptions. The argument `envp' shall point to an object set
259 * by a call to feholdexcept() or fegetenv(), or equal a floating-point
260 * environment macro.
261 */
262 __declare_fenv_inline(int)
feupdateenv(const fenv_t * envp)263 feupdateenv(const fenv_t *envp)
264 {
265 fexcept_t r;
266
267 __stfsr(&r);
268 __ldfsr(*envp);
269
270 feraiseexcept(r & FE_ALL_EXCEPT);
271
272 /* Success */
273 return 0;
274 }
275
276 /*
277 * The following functions are extentions to the standard
278 */
279 __declare_fenv_inline(int)
feenableexcept(int mask)280 feenableexcept(int mask)
281 {
282 fenv_t old_r, new_r;
283
284 __stfsr(&old_r);
285 new_r = old_r | ((mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
286 __ldfsr(new_r);
287
288 return (old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT;
289 }
290
291 __declare_fenv_inline(int)
fedisableexcept(int mask)292 fedisableexcept(int mask)
293 {
294 fenv_t old_r, new_r;
295
296 __stfsr(&old_r);
297 new_r = old_r & ~((mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
298 __ldfsr(new_r);
299
300 return (old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT;
301 }
302
303 __declare_fenv_inline(int)
fegetexcept(void)304 fegetexcept(void)
305 {
306 fenv_t r;
307
308 __stfsr(&r);
309 return (r & _ENABLE_MASK) >> _FPUSW_SHIFT;
310 }
311