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/feraiseexcept.html
40  * Referred to as 'feraiseexcept.html below.
41  *
42  * "The feraiseexcept() function shall attempt to raise the supported
43  * floating-point exceptions represented by the excepts argument. The
44  * order in which these floating-point exceptions are raised is
45  * unspecified, except that if the excepts argument represents IEC
46  * 60559 valid coincident floating-point exceptions for atomic
47  * operations (namely overflow and inexact, or underflow and inexact),
48  * then overflow or underflow shall be raised before inexact. Whether
49  * the feraiseexcept() function additionally raises the inexact
50  * floating-point exception whenever it raises the overflow or
51  * underflow floating-point exception is implementation-defined."
52  */
53 
54 #if __riscv_flen
55 
feraiseexcept(int excepts)56 int feraiseexcept(int excepts)
57 {
58 
59   /* Mask excepts to be sure only supported flag bits are set */
60 
61   excepts &= FE_ALL_EXCEPT;
62 
63   /* Set the requested exception flags */
64 
65   __asm__ volatile("csrs fflags, %0" : : "r"(excepts));
66 
67   /* Per 'feraiseexcept.html:
68    * "If the argument is zero or if all the specified exceptions were
69    * successfully raised, feraiseexcept() shall return
70    * zero. Otherwise, it shall return a non-zero value."
71    */
72 
73   /* Per "The RISC-V Instruction Set Manual: Volume I: User-Level ISA:
74    * Version 2.1":
75    *
76    * "As allowed by the standard, we do not support traps on
77    * floating-point exceptions in the base ISA, but instead require
78    * explicit checks of the flags in software."
79    *
80    */
81 
82   return 0;
83 }
84 #else
85 #include "../../fenv/feraiseexcept.c"
86 #endif
87 
88