1 /*
2   (c) Copyright 2017 Michael R. Neilly
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8 
9   * Redistributions of source code must retain the above copyright
10   notice, this list of conditions and the following disclaimer.
11 
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.
15 
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.
19 
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.
32 */
33 
34 #include <fenv.h>
35 
36 /* This implementation is intended to comply with the following
37  * specification:
38  *
39  * http://pubs.opengroup.org/onlinepubs/009695399/functions/fesetexceptflag.html
40  * Referred to as 'fesetexceptflag.html below.
41  *
42  * "The fesetexceptflag() function shall attempt to set the
43  * floating-point status flags indicated by the argument excepts to
44  * the states stored in the object pointed to by flagp. The value
45  * pointed to by flagp shall have been set by a previous call to
46  * fegetexceptflag() whose second argument represented at least those
47  * floating-point exceptions represented by the argument excepts. This
48  * function does not raise floating-point exceptions, but only sets
49  * the state of the flags."
50  *
51  */
52 
53 #if __riscv_flen
54 
fesetexceptflag(const fexcept_t * flagp,int excepts)55 int fesetexceptflag(const fexcept_t *flagp, int excepts)
56 {
57 
58   /* Mask excepts to be sure only supported flag bits are set */
59 
60   excepts &= FE_ALL_EXCEPT;
61 
62   /* Set the requested flags */
63 
64   fexcept_t flags = *flagp & FE_ALL_EXCEPT;
65 
66   /* Set new the flags */
67 
68   __asm__ volatile("csrc fflags, %0" : : "r"(excepts));
69   __asm__ volatile("csrs fflags, %0" : : "r"(flags & excepts));
70 
71   /* Per 'fesetexceptflag.html:
72    *
73    * "If the excepts argument is zero or if all the specified
74    * exceptions were successfully set, fesetexceptflag() shall return
75    * zero. Otherwise, it shall return a non-zero value."
76    */
77 
78   return 0;
79 }
80 #else
81 #include "../../fenv/fesetexceptflag.c"
82 #endif
83