Skip to content

Commit 428e7af

Browse files
authored
Support for riscv64 architecture (#254)
Changes needed for riscv64 support have been added. Code for openlibm_fenv_riscv.h, riscv_fpmath.h and fenv.c was taken from https://github.com/freebsd/freebsd
1 parent ed7aea3 commit 428e7af

File tree

8 files changed

+391
-1
lines changed

8 files changed

+391
-1
lines changed

Make.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ endif
8181
ifeq ($(findstring mips,$(ARCH)),mips)
8282
override ARCH := mips
8383
endif
84+
ifeq ($(findstring riscv64,$(ARCH)),riscv64)
85+
override ARCH := riscv64
86+
endif
8487

8588
# If CFLAGS does not contain a -O optimization flag, default to -O3
8689
ifeq ($(findstring -O,$(CFLAGS)),)

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ test/test-float: libopenlibm.$(OLM_MAJOR_MINOR_SHLIB_EXT)
8181
$(MAKE) -C test test-float
8282

8383
clean:
84-
rm -f aarch64/*.o amd64/*.o arm/*.o bsdsrc/*.o i387/*.o ld80/*.o ld128/*.o src/*.o powerpc/*.o mips/*.o s390/*.o
84+
rm -f aarch64/*.o amd64/*.o arm/*.o bsdsrc/*.o i387/*.o ld80/*.o ld128/*.o src/*.o powerpc/*.o mips/*.o s390/*.o riscv64/*.o
8585
rm -f libopenlibm.a libopenlibm.*$(SHLIB_EXT)*
8686
$(MAKE) -C test clean
8787

include/openlibm_fenv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <openlibm_fenv_mips.h>
1515
#elif defined(__s390__)
1616
#include <openlibm_fenv_s390.h>
17+
#elif defined(__riscv)
18+
#include <openlibm_fenv_riscv.h>
1719
#else
1820
#error "Unsupported platform"
1921
#endif

include/openlibm_fenv_riscv.h

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
/*-
2+
* Copyright (c) 2004-2005 David Schultz <[email protected]>
3+
* Copyright (c) 2015-2016 Ruslan Bukin <[email protected]>
4+
* All rights reserved.
5+
*
6+
* Portions of this software were developed by SRI International and the
7+
* University of Cambridge Computer Laboratory under DARPA/AFRL contract
8+
* FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9+
*
10+
* Portions of this software were developed by the University of Cambridge
11+
* Computer Laboratory as part of the CTSRD Project, with support from the
12+
* UK Higher Education Innovation Fund (HEIF).
13+
*
14+
* Redistribution and use in source and binary forms, with or without
15+
* modification, are permitted provided that the following conditions
16+
* are met:
17+
* 1. Redistributions of source code must retain the above copyright
18+
* notice, this list of conditions and the following disclaimer.
19+
* 2. Redistributions in binary form must reproduce the above copyright
20+
* notice, this list of conditions and the following disclaimer in the
21+
* documentation and/or other materials provided with the distribution.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33+
* SUCH DAMAGE.
34+
*
35+
* $FreeBSD: head/lib/msun/riscv/fenv.h 332792 2018-04-19 20:36:15Z brooks $
36+
*/
37+
38+
#ifndef _FENV_H_
39+
#define _FENV_H_
40+
41+
#include <stdint.h>
42+
#include "cdefs-compat.h"
43+
44+
#ifndef __fenv_static
45+
#define __fenv_static static
46+
#endif
47+
48+
typedef __uint64_t fenv_t;
49+
typedef __uint64_t fexcept_t;
50+
51+
/* Exception flags */
52+
#define FE_INVALID 0x0010
53+
#define FE_DIVBYZERO 0x0008
54+
#define FE_OVERFLOW 0x0004
55+
#define FE_UNDERFLOW 0x0002
56+
#define FE_INEXACT 0x0001
57+
#define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | \
58+
FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
59+
60+
/*
61+
* RISC-V Rounding modes
62+
*/
63+
#define _ROUND_SHIFT 5
64+
#define FE_TONEAREST (0x00 << _ROUND_SHIFT)
65+
#define FE_TOWARDZERO (0x01 << _ROUND_SHIFT)
66+
#define FE_DOWNWARD (0x02 << _ROUND_SHIFT)
67+
#define FE_UPWARD (0x03 << _ROUND_SHIFT)
68+
#define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
69+
FE_UPWARD | FE_TOWARDZERO)
70+
71+
__BEGIN_DECLS
72+
73+
/* Default floating-point environment */
74+
extern const fenv_t __fe_dfl_env;
75+
#define FE_DFL_ENV (&__fe_dfl_env)
76+
77+
#if !defined(__riscv_float_abi_soft) && !defined(__riscv_float_abi_double)
78+
#if defined(__riscv_float_abi_single)
79+
#error single precision floating point ABI not supported
80+
#else
81+
#error compiler did not set soft/hard float macros
82+
#endif
83+
#endif
84+
85+
#ifndef __riscv_float_abi_soft
86+
#define __rfs(__fcsr) __asm __volatile("csrr %0, fcsr" : "=r" (__fcsr))
87+
#define __wfs(__fcsr) __asm __volatile("csrw fcsr, %0" :: "r" (__fcsr))
88+
#endif
89+
90+
#ifdef __riscv_float_abi_soft
91+
int feclearexcept(int __excepts);
92+
int fegetexceptflag(fexcept_t *__flagp, int __excepts);
93+
int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
94+
int feraiseexcept(int __excepts);
95+
int fetestexcept(int __excepts);
96+
int fegetround(void);
97+
int fesetround(int __round);
98+
int fegetenv(fenv_t *__envp);
99+
int feholdexcept(fenv_t *__envp);
100+
int fesetenv(const fenv_t *__envp);
101+
int feupdateenv(const fenv_t *__envp);
102+
#else
103+
__fenv_static inline int
104+
feclearexcept(int __excepts)
105+
{
106+
107+
__asm __volatile("csrc fflags, %0" :: "r"(__excepts));
108+
109+
return (0);
110+
}
111+
112+
__fenv_static inline int
113+
fegetexceptflag(fexcept_t *__flagp, int __excepts)
114+
{
115+
fexcept_t __fcsr;
116+
117+
__rfs(__fcsr);
118+
*__flagp = __fcsr & __excepts;
119+
120+
return (0);
121+
}
122+
123+
__fenv_static inline int
124+
fesetexceptflag(const fexcept_t *__flagp, int __excepts)
125+
{
126+
fexcept_t __fcsr;
127+
128+
__fcsr = *__flagp;
129+
__asm __volatile("csrc fflags, %0" :: "r"(__excepts));
130+
__asm __volatile("csrs fflags, %0" :: "r"(__fcsr & __excepts));
131+
132+
return (0);
133+
}
134+
135+
__fenv_static inline int
136+
feraiseexcept(int __excepts)
137+
{
138+
139+
__asm __volatile("csrs fflags, %0" :: "r"(__excepts));
140+
141+
return (0);
142+
}
143+
144+
__fenv_static inline int
145+
fetestexcept(int __excepts)
146+
{
147+
fexcept_t __fcsr;
148+
149+
__rfs(__fcsr);
150+
151+
return (__fcsr & __excepts);
152+
}
153+
154+
__fenv_static inline int
155+
fegetround(void)
156+
{
157+
fexcept_t __fcsr;
158+
159+
__rfs(__fcsr);
160+
161+
return (__fcsr & _ROUND_MASK);
162+
}
163+
164+
__fenv_static inline int
165+
fesetround(int __round)
166+
{
167+
fexcept_t __fcsr;
168+
169+
if (__round & ~_ROUND_MASK)
170+
return (-1);
171+
172+
__rfs(__fcsr);
173+
__fcsr &= ~_ROUND_MASK;
174+
__fcsr |= __round;
175+
__wfs(__fcsr);
176+
177+
return (0);
178+
}
179+
180+
__fenv_static inline int
181+
fegetenv(fenv_t *__envp)
182+
{
183+
184+
__rfs(*__envp);
185+
186+
return (0);
187+
}
188+
189+
__fenv_static inline int
190+
feholdexcept(fenv_t *__envp)
191+
{
192+
193+
/* No exception traps. */
194+
195+
return (-1);
196+
}
197+
198+
__fenv_static inline int
199+
fesetenv(const fenv_t *__envp)
200+
{
201+
202+
__wfs(*__envp);
203+
204+
return (0);
205+
}
206+
207+
__fenv_static inline int
208+
feupdateenv(const fenv_t *__envp)
209+
{
210+
fexcept_t __fcsr;
211+
212+
__rfs(__fcsr);
213+
__wfs(*__envp);
214+
feraiseexcept(__fcsr & FE_ALL_EXCEPT);
215+
216+
return (0);
217+
}
218+
#endif /* !__riscv_float_abi_soft */
219+
220+
#if __BSD_VISIBLE
221+
222+
/* We currently provide no external definitions of the functions below. */
223+
224+
#ifdef __riscv_float_abi_soft
225+
int feenableexcept(int __mask);
226+
int fedisableexcept(int __mask);
227+
int fegetexcept(void);
228+
#else
229+
static inline int
230+
feenableexcept(int __mask)
231+
{
232+
233+
/* No exception traps. */
234+
235+
return (-1);
236+
}
237+
238+
static inline int
239+
fedisableexcept(int __mask)
240+
{
241+
242+
/* No exception traps. */
243+
244+
return (0);
245+
}
246+
247+
static inline int
248+
fegetexcept(void)
249+
{
250+
251+
/* No exception traps. */
252+
253+
return (0);
254+
}
255+
#endif /* !__riscv_float_abi_soft */
256+
257+
#endif /* __BSD_VISIBLE */
258+
259+
__END_DECLS
260+
261+
#endif /* !_FENV_H_ */

riscv64/Make.files

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$(CUR_SRCS) = fenv.c

riscv64/fenv.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*-
2+
* Copyright (c) 2004 David Schultz <[email protected]>
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+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*
26+
* $FreeBSD: head/lib/msun/riscv/fenv.c 332792 2018-04-19 20:36:15Z brooks $
27+
*/
28+
29+
#define __fenv_static
30+
#include "fenv.h"
31+
32+
#ifdef __GNUC_GNU_INLINE__
33+
#error "This file must be compiled with C99 'inline' semantics"
34+
#endif
35+
36+
/*
37+
* Hopefully the system ID byte is immutable, so it's valid to use
38+
* this as a default environment.
39+
*/
40+
const fenv_t __fe_dfl_env = 0;
41+
42+
#ifdef __riscv_float_abi_soft
43+
#define __set_env(env, flags, mask, rnd) env = ((flags) | (rnd) << 5)
44+
#define __env_flags(env) ((env) & FE_ALL_EXCEPT)
45+
#define __env_mask(env) (0) /* No exception traps. */
46+
#define __env_round(env) (((env) >> 5) & _ROUND_MASK)
47+
#include "fenv-softfloat.h"
48+
#endif
49+
50+
extern inline int feclearexcept(int __excepts);
51+
extern inline int fegetexceptflag(fexcept_t *__flagp, int __excepts);
52+
extern inline int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
53+
extern inline int feraiseexcept(int __excepts);
54+
extern inline int fetestexcept(int __excepts);
55+
extern inline int fegetround(void);
56+
extern inline int fesetround(int __round);
57+
extern inline int fegetenv(fenv_t *__envp);
58+
extern inline int feholdexcept(fenv_t *__envp);
59+
extern inline int fesetenv(const fenv_t *__envp);
60+
extern inline int feupdateenv(const fenv_t *__envp);
61+
extern inline int feenableexcept(int __mask);
62+
extern inline int fedisableexcept(int __mask);
63+
extern inline int fegetexcept(void);

src/fpmath.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#include "mips_fpmath.h"
4444
#elif defined(__s390__)
4545
#include "s390_fpmath.h"
46+
#elif defined(__riscv)
47+
#include "riscv_fpmath.h"
4648
#endif
4749

4850
/* Definitions provided directly by GCC and Clang. */

0 commit comments

Comments
 (0)