2 (c) Copyright 2017 Michael R. Neilly
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 * 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 * Neither the names of the copyright holders nor the names of their
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
35 #include "riscv_math.h"
37 /* This implementation is intended to comply with the following
40 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/feupdateenv.html
42 * "The feupdateenv() function shall attempt to save the currently
43 * raised floating-point exceptions in its automatic storage, attempt
44 * to install the floating-point environment represented by the object
45 * pointed to by envp, and then attempt to raise the saved
46 * floating-point exceptions. The argument envp shall point to an
47 * object set by a call to feholdexcept() or fegetenv(), or equal a
48 * floating-point environment macro."
51 int feupdateenv(const fenv_t
*envp
)
54 #ifdef __RISCV_HARD_FLOAT
56 /* Get current exception flags */
59 asm volatile("frflags %0" : "=r"(flags
));
61 /* Set the environment as requested */
63 fenv_t fcsr
= *envp
; /* Environment to install */
64 asm volatile("fscsr %0" : : "r"(fcsr
)); /* Install environment */
66 /* OR in the saved exception flags */
68 asm volatile("csrs fflags, %0" : : "r"(flags
));
70 /* "The feupdateenv() function shall return a zero value if and only
71 * if all the required actions were successfully carried out."
80 #if defined FE_NOMASK_ENV && FE_ALL_EXCEPT != 0
82 if (envp
== FE_NOMASK_ENV
)