1 /* $OpenBSD: fenv.c,v 1.6 2022/12/27 17:10:07 jmc Exp $ */
2
3 /*
4 * Copyright (c) 2011 Martynas Venckus <martynas@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <fenv.h>
20 #include <machine/ieeefp.h>
21
22 extern fp_except _softfloat_float_exception_flags;
23 extern fp_except _softfloat_float_exception_mask;
24 extern fp_rnd _softfloat_float_rounding_mode;
25
26 /*
27 * The following constant represents the default floating-point environment
28 * (that is, the one installed at program startup) and has type pointer to
29 * const-qualified fenv_t.
30 *
31 * It can be used as an argument to the functions within the <fenv.h> header
32 * that manage the floating-point environment, namely fesetenv() and
33 * feupdateenv().
34 */
35 fenv_t __fe_dfl_env = {
36 0,
37 0,
38 0
39 };
40
41 /*
42 * The feclearexcept() function clears the supported floating-point exceptions
43 * represented by `excepts'.
44 */
45 int
feclearexcept(int excepts)46 feclearexcept(int excepts)
47 {
48 unsigned int fpscr;
49
50 excepts &= FE_ALL_EXCEPT;
51
52 /* Clear the requested floating-point exceptions */
53 _softfloat_float_exception_flags &= ~excepts;
54
55 __asm volatile("vmrs %0, fpscr" : "=r" (fpscr));
56 fpscr &= ~excepts;
57 __asm volatile("vmsr fpscr, %0" :: "r" (fpscr));
58
59 return 0;
60 }
61 DEF_STD(feclearexcept);
62
63 /*
64 * The fegetexceptflag() function stores an implementation-defined
65 * representation of the states of the floating-point status flags indicated by
66 * the argument excepts in the object pointed to by the argument flagp.
67 */
68 int
fegetexceptflag(fexcept_t * flagp,int excepts)69 fegetexceptflag(fexcept_t *flagp, int excepts)
70 {
71 unsigned int fpscr;
72
73 excepts &= FE_ALL_EXCEPT;
74
75 /* Store the results in flagp */
76 __asm volatile("vmrs %0, fpscr" : "=r" (fpscr));
77 fpscr |= _softfloat_float_exception_flags;
78 *flagp = fpscr & excepts;
79
80 return 0;
81 }
82
83 /*
84 * The feraiseexcept() function raises the supported floating-point exceptions
85 * represented by the argument `excepts'.
86 */
87 int
feraiseexcept(int excepts)88 feraiseexcept(int excepts)
89 {
90 excepts &= FE_ALL_EXCEPT;
91
92 fesetexceptflag((fexcept_t *)&excepts, excepts);
93
94 return 0;
95 }
96 DEF_STD(feraiseexcept);
97
98 /*
99 * This function sets the floating-point status flags indicated by the argument
100 * `excepts' to the states stored in the object pointed to by `flagp'. It does
101 * NOT raise any floating-point exceptions, but only sets the state of the flags.
102 */
103 int
fesetexceptflag(const fexcept_t * flagp,int excepts)104 fesetexceptflag(const fexcept_t *flagp, int excepts)
105 {
106 unsigned int fpscr;
107
108 excepts &= FE_ALL_EXCEPT;
109
110 /* Set the requested status flags */
111 _softfloat_float_exception_flags &= ~excepts;
112 _softfloat_float_exception_flags |= *flagp & excepts;
113
114 __asm volatile("vmrs %0, fpscr" : "=r" (fpscr));
115 fpscr &= ~excepts;
116 fpscr |= *flagp & excepts;
117 __asm volatile("vmsr fpscr, %0" :: "r" (fpscr));
118
119 return 0;
120 }
121 DEF_STD(fesetexceptflag);
122
123 /*
124 * The fetestexcept() function determines which of a specified subset of the
125 * floating-point exception flags are currently set. The `excepts' argument
126 * specifies the floating-point status flags to be queried.
127 */
128 int
fetestexcept(int excepts)129 fetestexcept(int excepts)
130 {
131 unsigned int fpscr;
132
133 excepts &= FE_ALL_EXCEPT;
134
135 __asm volatile("vmrs %0, fpscr" : "=r" (fpscr));
136 fpscr |= _softfloat_float_exception_flags;
137
138 return fpscr & excepts;
139 }
140 DEF_STD(fetestexcept);
141
142 /*
143 * The fegetround() function gets the current rounding direction.
144 */
145 int
fegetround(void)146 fegetround(void)
147 {
148 unsigned int fpscr;
149
150 __asm volatile("vmrs %0, fpscr" : "=r" (fpscr));
151
152 return (fpscr >> 22) & _ROUND_MASK;
153 }
154 DEF_STD(fegetround);
155
156 /*
157 * The fesetround() function establishes the rounding direction represented by
158 * its argument `round'. If the argument is not equal to the value of a rounding
159 * direction macro, the rounding direction is not changed.
160 */
161 int
fesetround(int round)162 fesetround(int round)
163 {
164 unsigned int fpscr;
165
166 /* Check whether requested rounding direction is supported */
167 if (round & ~_ROUND_MASK)
168 return -1;
169
170 /* Set the rounding direction */
171 _softfloat_float_rounding_mode &= ~_ROUND_MASK;
172 _softfloat_float_rounding_mode |= round;
173
174 __asm volatile("vmrs %0, fpscr" : "=r" (fpscr));
175 fpscr &= ~(_ROUND_MASK << 22);
176 fpscr |= round << 22;
177 __asm volatile("vmsr fpscr, %0" :: "r" (fpscr));
178
179 return 0;
180 }
181 DEF_STD(fesetround);
182
183 /*
184 * The fegetenv() function attempts to store the current floating-point
185 * environment in the object pointed to by envp.
186 */
187 int
fegetenv(fenv_t * envp)188 fegetenv(fenv_t *envp)
189 {
190 unsigned int fpscr;
191
192 __asm volatile("vmrs %0, fpscr" : "=r" (fpscr));
193 fpscr |= _softfloat_float_exception_flags;
194
195 /* Store the current floating-point sticky flags */
196 envp->__sticky = fpscr & FE_ALL_EXCEPT;
197
198 /* Store the current floating-point masks */
199 envp->__mask = 0;
200
201 /* Store the current floating-point control register */
202 envp->__round = (fpscr >> 22) & _ROUND_MASK;
203
204 return 0;
205 }
206 DEF_STD(fegetenv);
207
208 /*
209 * The feholdexcept() function saves the current floating-point environment
210 * in the object pointed to by envp, clears the floating-point status flags, and
211 * then installs a non-stop (continue on floating-point exceptions) mode, if
212 * available, for all floating-point exceptions.
213 */
214 int
feholdexcept(fenv_t * envp)215 feholdexcept(fenv_t *envp)
216 {
217 unsigned int fpscr;
218
219 /* Store the current floating-point environment */
220 fegetenv(envp);
221
222 /* Clear exception flags */
223 _softfloat_float_exception_flags &= ~FE_ALL_EXCEPT;
224
225 /* Mask all exceptions */
226 _softfloat_float_exception_mask &= ~FE_ALL_EXCEPT;
227
228 __asm volatile("vmrs %0, fpscr" : "=r" (fpscr));
229 fpscr &= ~(FE_ALL_EXCEPT << 8 | FE_ALL_EXCEPT);
230 __asm volatile("vmsr fpscr, %0" :: "r" (fpscr));
231
232 return 0;
233 }
234 DEF_STD(feholdexcept);
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 int
fesetenv(const fenv_t * envp)245 fesetenv(const fenv_t *envp)
246 {
247 unsigned int fpscr;
248
249 __asm volatile("vmrs %0, fpscr" : "=r" (fpscr));
250 fpscr &= ~(_ROUND_MASK << 22 | FE_ALL_EXCEPT << 8 | FE_ALL_EXCEPT);
251
252 /* Load the floating-point sticky flags */
253 fpscr |= envp->__sticky & FE_ALL_EXCEPT;
254 _softfloat_float_exception_flags = envp->__sticky & FE_ALL_EXCEPT;
255
256 /* Load the floating-point masks */
257 fpscr |= (envp->__mask & FE_ALL_EXCEPT) << 8;
258 _softfloat_float_exception_mask = envp->__mask & FE_ALL_EXCEPT;
259
260 /* Load the floating-point rounding mode */
261 fpscr |= (envp->__round & _ROUND_MASK) << 22;
262 _softfloat_float_rounding_mode = envp->__round & _ROUND_MASK;
263
264 __asm volatile("vmsr fpscr, %0" :: "r" (fpscr));
265
266 return 0;
267 }
268 DEF_STD(fesetenv);
269
270 /*
271 * The feupdateenv() function saves the currently raised floating-point
272 * exceptions in its automatic storage, installs the floating-point environment
273 * represented by the object pointed to by `envp', and then raises the saved
274 * floating-point exceptions. The argument `envp' shall point to an object set
275 * by a call to feholdexcept() or fegetenv(), or equal a floating-point
276 * environment macro.
277 */
278 int
feupdateenv(const fenv_t * envp)279 feupdateenv(const fenv_t *envp)
280 {
281 unsigned int fpscr;
282
283 __asm volatile("vmrs %0, fpscr" : "=r" (fpscr));
284 fpscr |= _softfloat_float_exception_flags;
285
286 /* Install new floating-point environment */
287 fesetenv(envp);
288
289 /* Raise any previously accumulated exceptions */
290 feraiseexcept(fpscr & FE_ALL_EXCEPT);
291
292 return 0;
293 }
294 DEF_STD(feupdateenv);
295
296 /*
297 * The following functions are extensions to the standard
298 */
299 int
feenableexcept(int mask)300 feenableexcept(int mask)
301 {
302 return -1;
303 }
304
305 int
fedisableexcept(int mask)306 fedisableexcept(int mask)
307 {
308 return 0;
309 }
310
311 int
fegetexcept(void)312 fegetexcept(void)
313 {
314 return 0;
315 }
316