1 /* $NetBSD: fenv.c,v 1.2 2017/03/22 23:11:09 chs Exp $ */
4 * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __RCSID("$NetBSD: fenv.c,v 1.2 2017/03/22 23:11:09 chs Exp $");
34 #define _DIAGASSERT(x) assert(x)
37 __weak_alias(feclearexcept
,_feclearexcept
)
38 __weak_alias(fedisableexcept
,_fedisableexcept
)
39 __weak_alias(feenableexcept
,_feenableexcept
)
40 __weak_alias(fegetenv
,_fegetenv
)
41 __weak_alias(fegetexcept
,_fegetexcept
)
42 __weak_alias(fegetexceptflag
,_fegetexceptflag
)
43 __weak_alias(fegetround
,_fegetround
)
44 __weak_alias(feholdexcept
,_feholdexcept
)
45 __weak_alias(feraiseexcept
,_feraiseexcept
)
46 __weak_alias(fesetenv
,_fesetenv
)
47 __weak_alias(fesetexceptflag
,_fesetexceptflag
)
48 __weak_alias(fesetround
,_fesetround
)
49 __weak_alias(fetestexcept
,_fetestexcept
)
50 __weak_alias(feupdateenv
,_feupdateenv
)
53 /* Load floating-point state register (32bits) */
54 #define __ldfsr(__r) __asm__ __volatile__ \
55 ("ld %0, %%fsr" : : "m" (__r))
57 /* Save floating-point state register (32bits) */
58 #define __stfsr(__r) __asm__ __volatile__ \
59 ("st %%fsr, %0" : "=m" (*(__r)))
62 * The feclearexcept() function clears the supported floating-point exceptions
63 * represented by `excepts'.
66 feclearexcept(int excepts
)
71 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
73 ex
= excepts
& FE_ALL_EXCEPT
;
84 * The fegetexceptflag() function stores an implementation-defined
85 * representation of the states of the floating-point status flags indicated
86 * by the argument excepts in the object pointed to by the argument flagp.
89 fegetexceptflag(fexcept_t
*flagp
, int excepts
)
94 _DIAGASSERT(flagp
!= NULL
);
95 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
97 ex
= excepts
& FE_ALL_EXCEPT
;
108 * This function sets the floating-point status flags indicated by the argument
109 * `excepts' to the states stored in the object pointed to by `flagp'. It does
110 * NOT raise any floating-point exceptions, but only sets the state of the flags.
113 fesetexceptflag(const fexcept_t
*flagp
, int excepts
)
118 _DIAGASSERT(flagp
!= NULL
);
119 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
121 ex
= excepts
& FE_ALL_EXCEPT
;
133 * The feraiseexcept() function raises the supported floating-point exceptions
134 * represented by the argument `excepts'.
136 * The order in which these floating-point exceptions are raised is unspecified
140 feraiseexcept(int excepts
)
145 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
147 ex
= excepts
& FE_ALL_EXCEPT
;
150 * With a compiler that supports the FENV_ACCESS pragma properly, simple
151 * expressions like '0.0 / 0.0' should be sufficient to generate traps.
152 * Unfortunately, we need to bring a volatile variable into the equation
153 * to prevent incorrect optimizations.
155 if (ex
& FE_INVALID
) {
159 if (ex
& FE_DIVBYZERO
) {
163 if (ex
& FE_OVERFLOW
) {
167 if (ex
& FE_UNDERFLOW
) {
171 if (ex
& FE_INEXACT
) {
181 * The fetestexcept() function determines which of a specified subset of the
182 * floating-point exception flags are currently set. The `excepts' argument
183 * specifies the floating-point status flags to be queried.
186 fetestexcept(int excepts
)
190 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
194 return r
& (excepts
& FE_ALL_EXCEPT
);
198 * The fegetround() function gets the current rounding direction.
207 return (r
>> _ROUND_SHIFT
) & _ROUND_MASK
;
211 * The fesetround() function establishes the rounding direction represented by
212 * its argument `round'. If the argument is not equal to the value of a rounding
213 * direction macro, the rounding direction is not changed.
216 fesetround(int round
)
220 _DIAGASSERT((round
& ~_ROUND_MASK
) == 0);
221 if (round
& ~_ROUND_MASK
)
225 r
&= ~(_ROUND_MASK
<< _ROUND_SHIFT
);
226 r
|= round
<< _ROUND_SHIFT
;
234 * The fegetenv() function attempts to store the current floating-point
235 * environment in the object pointed to by envp.
238 fegetenv(fenv_t
*envp
)
240 _DIAGASSERT(envp
!= NULL
);
250 * The feholdexcept() function saves the current floating-point environment
251 * in the object pointed to by envp, clears the floating-point status flags, and
252 * then installs a non-stop (continue on floating-point exceptions) mode, if
253 * available, for all floating-point exceptions.
256 feholdexcept(fenv_t
*envp
)
260 _DIAGASSERT(envp
!= NULL
);
264 r
&= ~(FE_ALL_EXCEPT
| _ENABLE_MASK
);
272 * The fesetenv() function attempts to establish the floating-point environment
273 * represented by the object pointed to by envp. The argument `envp' points
274 * to an object set by a call to fegetenv() or feholdexcept(), or equal a
275 * floating-point environment macro. The fesetenv() function does not raise
276 * floating-point exceptions, but only installs the state of the floating-point
277 * status flags represented through its argument.
280 fesetenv(const fenv_t
*envp
)
282 _DIAGASSERT(envp
!= NULL
);
292 * The feupdateenv() function saves the currently raised floating-point
293 * exceptions in its automatic storage, installs the floating-point environment
294 * represented by the object pointed to by `envp', and then raises the saved
295 * floating-point exceptions. The argument `envp' shall point to an object set
296 * by a call to feholdexcept() or fegetenv(), or equal a floating-point
300 feupdateenv(const fenv_t
*envp
)
304 _DIAGASSERT(envp
!= NULL
);
309 _DIAGASSERT((r
& ~FE_ALL_EXCEPT
) == 0);
310 feraiseexcept(r
& FE_ALL_EXCEPT
);
317 * The following functions are extentions to the standard
320 feenableexcept(int mask
)
325 new_r
= old_r
| ((mask
& FE_ALL_EXCEPT
) << _FPUSW_SHIFT
);
328 return (old_r
>> _FPUSW_SHIFT
) & FE_ALL_EXCEPT
;
332 fedisableexcept(int mask
)
337 new_r
= old_r
& ~((mask
& FE_ALL_EXCEPT
) << _FPUSW_SHIFT
);
340 return (old_r
>> _FPUSW_SHIFT
) & FE_ALL_EXCEPT
;
349 return (r
& _ENABLE_MASK
) >> _FPUSW_SHIFT
;