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 #include <stddef.h>
36 
37 /* This implementation is intended to comply with the following
38  * specification:
39  *
40  * http://pubs.opengroup.org/onlinepubs/009695399/functions/feclearexcept.html
41  * Referred to as 'feclearexcept.html below.
42  *
43  * "The feclearexcept() function shall attempt to clear the supported
44  * floating-point exceptions represented by excepts."
45  */
46 
47 #if __riscv_flen
feclearexcept(int excepts)48 int feclearexcept(int excepts)
49 {
50 
51 
52   /* Mask excepts to be sure only supported flag bits are set */
53 
54   excepts &= FE_ALL_EXCEPT;
55 
56   /* Per "The RISC-V Instruction Set Manual: Volume I: User-Level ISA:
57    * Version 2.1":
58    *
59    * "The CSRRC (Atomic Read and Clear Bits in CSR) instruction reads
60    * the value of the CSR, zeroextends the value to XLEN bits, and
61    * writes it to integer register rd. The initial value in integer
62    * register rs1 is treated as a bit mask that specifies bit
63    * positions to be cleared in the CSR. Any bit that is high in rs1
64    * will cause the corresponding bit to be cleared in the CSR, if
65    * that CSR bit is writable. Other bits in the CSR are unaffected."
66    */
67 
68   /* Clear the requested flags */
69 
70   __asm__ volatile("csrrc zero, fflags, %0" : : "r"(excepts));
71 
72   /* Per 'feclearexcept.html
73    * "If the argument is zero or if all the specified exceptions were
74    * successfully cleared, feclearexcept() shall return zero. Otherwise,
75    * it shall return a non-zero value."
76    */
77 
78   return 0;
79 }
80 #else
81 #include "../../fenv/feclearexcept.c"
82 #endif
83