Skip to content

Commit 4f5112e

Browse files
Radovan Birdiccdluminate
authored andcommitted
Support for mips architectures
Changes needed for mips suport have been added. Code for openlibm_fenv_mips.h, mips_fpmath.h and fenv.c was taken from https://github.com/freebsd/freebsd
1 parent a844d58 commit 4f5112e

7 files changed

Lines changed: 409 additions & 0 deletions

File tree

Make.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ endif
6767
ifeq ($(ARCH),x86_64)
6868
override ARCH := amd64
6969
endif
70+
ifeq ($(findstring mips,$(ARCH)),mips)
71+
override ARCH := mips
72+
endif
7073

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

include/openlibm_fenv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <openlibm_fenv_i387.h>
1111
#elif defined(__powerpc__)
1212
#include <openlibm_fenv_powerpc.h>
13+
#elif defined(__mips__)
14+
#include <openlibm_fenv_mips.h>
1315
#else
1416
#error "Unsupported platform"
1517
#endif

include/openlibm_fenv_mips.h

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
/*-
2+
* Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
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$
27+
*/
28+
29+
#ifndef _FENV_H_
30+
#define _FENV_H_
31+
32+
#include <sys/types.h>
33+
34+
#ifndef __fenv_static
35+
#define __fenv_static static
36+
#endif
37+
38+
typedef __uint32_t fenv_t;
39+
typedef __uint32_t fexcept_t;
40+
41+
/* Exception flags */
42+
#ifdef SOFTFLOAT
43+
#define _FPUSW_SHIFT 16
44+
#define FE_INVALID 0x0001
45+
#define FE_DIVBYZERO 0x0002
46+
#define FE_OVERFLOW 0x0004
47+
#define FE_UNDERFLOW 0x0008
48+
#define FE_INEXACT 0x0010
49+
#else
50+
#define _FCSR_CAUSE_SHIFT 10
51+
#define FE_INVALID 0x0040
52+
#define FE_DIVBYZERO 0x0020
53+
#define FE_OVERFLOW 0x0010
54+
#define FE_UNDERFLOW 0x0008
55+
#define FE_INEXACT 0x0004
56+
#endif
57+
#define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | \
58+
FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
59+
60+
/* Rounding modes */
61+
#define FE_TONEAREST 0x0000
62+
#define FE_TOWARDZERO 0x0001
63+
#define FE_UPWARD 0x0002
64+
#define FE_DOWNWARD 0x0003
65+
#define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
66+
FE_UPWARD | FE_TOWARDZERO)
67+
__BEGIN_DECLS
68+
69+
/* Default floating-point environment */
70+
extern const fenv_t __fe_dfl_env;
71+
#define FE_DFL_ENV (&__fe_dfl_env)
72+
73+
/* We need to be able to map status flag positions to mask flag positions */
74+
#define _ENABLE_SHIFT 5
75+
#define _ENABLE_MASK (FE_ALL_EXCEPT << _ENABLE_SHIFT)
76+
77+
#ifndef SOFTFLOAT
78+
#define __cfc1(__fcsr) __asm __volatile("cfc1 %0, $31" : "=r" (__fcsr))
79+
#define __ctc1(__fcsr) __asm __volatile("ctc1 %0, $31" :: "r" (__fcsr))
80+
#endif
81+
82+
#ifdef SOFTFLOAT
83+
int feclearexcept(int __excepts);
84+
int fegetexceptflag(fexcept_t *__flagp, int __excepts);
85+
int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
86+
int feraiseexcept(int __excepts);
87+
int fetestexcept(int __excepts);
88+
int fegetround(void);
89+
int fesetround(int __round);
90+
int fegetenv(fenv_t *__envp);
91+
int feholdexcept(fenv_t *__envp);
92+
int fesetenv(const fenv_t *__envp);
93+
int feupdateenv(const fenv_t *__envp);
94+
#else
95+
__fenv_static inline int
96+
feclearexcept(int __excepts)
97+
{
98+
fexcept_t fcsr;
99+
100+
__excepts &= FE_ALL_EXCEPT;
101+
__cfc1(fcsr);
102+
fcsr &= ~(__excepts | (__excepts << _FCSR_CAUSE_SHIFT));
103+
__ctc1(fcsr);
104+
105+
return (0);
106+
}
107+
108+
__fenv_static inline int
109+
fegetexceptflag(fexcept_t *__flagp, int __excepts)
110+
{
111+
fexcept_t fcsr;
112+
113+
__excepts &= FE_ALL_EXCEPT;
114+
__cfc1(fcsr);
115+
*__flagp = fcsr & __excepts;
116+
117+
return (0);
118+
}
119+
120+
__fenv_static inline int
121+
fesetexceptflag(const fexcept_t *__flagp, int __excepts)
122+
{
123+
fexcept_t fcsr;
124+
125+
__excepts &= FE_ALL_EXCEPT;
126+
__cfc1(fcsr);
127+
fcsr &= ~__excepts;
128+
fcsr |= *__flagp & __excepts;
129+
__ctc1(fcsr);
130+
131+
return (0);
132+
}
133+
134+
__fenv_static inline int
135+
feraiseexcept(int __excepts)
136+
{
137+
fexcept_t fcsr;
138+
139+
__excepts &= FE_ALL_EXCEPT;
140+
__cfc1(fcsr);
141+
fcsr |= __excepts | (__excepts << _FCSR_CAUSE_SHIFT);
142+
__ctc1(fcsr);
143+
144+
return (0);
145+
}
146+
147+
__fenv_static inline int
148+
fetestexcept(int __excepts)
149+
{
150+
fexcept_t fcsr;
151+
152+
__excepts &= FE_ALL_EXCEPT;
153+
__cfc1(fcsr);
154+
155+
return (fcsr & __excepts);
156+
}
157+
158+
__fenv_static inline int
159+
fegetround(void)
160+
{
161+
fexcept_t fcsr;
162+
163+
__cfc1(fcsr);
164+
165+
return (fcsr & _ROUND_MASK);
166+
}
167+
168+
__fenv_static inline int
169+
fesetround(int __round)
170+
{
171+
fexcept_t fcsr;
172+
173+
if (__round & ~_ROUND_MASK)
174+
return (-1);
175+
176+
__cfc1(fcsr);
177+
fcsr &= ~_ROUND_MASK;
178+
fcsr |= __round;
179+
__ctc1(fcsr);
180+
181+
return (0);
182+
}
183+
184+
__fenv_static inline int
185+
fegetenv(fenv_t *__envp)
186+
{
187+
188+
__cfc1(*__envp);
189+
190+
return (0);
191+
}
192+
193+
__fenv_static inline int
194+
feholdexcept(fenv_t *__envp)
195+
{
196+
fexcept_t fcsr;
197+
198+
__cfc1(fcsr);
199+
*__envp = fcsr;
200+
fcsr &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
201+
__ctc1(fcsr);
202+
203+
return (0);
204+
}
205+
206+
__fenv_static inline int
207+
fesetenv(const fenv_t *__envp)
208+
{
209+
210+
__ctc1(*__envp);
211+
212+
return (0);
213+
}
214+
215+
__fenv_static inline int
216+
feupdateenv(const fenv_t *__envp)
217+
{
218+
fexcept_t fcsr;
219+
220+
__cfc1(fcsr);
221+
fesetenv(__envp);
222+
feraiseexcept(fcsr);
223+
224+
return (0);
225+
}
226+
#endif /* !SOFTFLOAT */
227+
228+
#if __BSD_VISIBLE
229+
230+
/* We currently provide no external definitions of the functions below. */
231+
232+
#ifdef SOFTFLOAT
233+
int feenableexcept(int __mask);
234+
int fedisableexcept(int __mask);
235+
int fegetexcept(void);
236+
#else
237+
static inline int
238+
feenableexcept(int __mask)
239+
{
240+
fenv_t __old_fcsr, __new_fcsr;
241+
242+
__cfc1(__old_fcsr);
243+
__new_fcsr = __old_fcsr | (__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT;
244+
__ctc1(__new_fcsr);
245+
246+
return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
247+
}
248+
249+
static inline int
250+
fedisableexcept(int __mask)
251+
{
252+
fenv_t __old_fcsr, __new_fcsr;
253+
254+
__cfc1(__old_fcsr);
255+
__new_fcsr = __old_fcsr & ~((__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT);
256+
__ctc1(__new_fcsr);
257+
258+
return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
259+
}
260+
261+
static inline int
262+
fegetexcept(void)
263+
{
264+
fexcept_t fcsr;
265+
266+
__cfc1(fcsr);
267+
268+
return ((fcsr & _ENABLE_MASK) >> _ENABLE_SHIFT);
269+
}
270+
271+
#endif /* !SOFTFLOAT */
272+
273+
#endif /* __BSD_VISIBLE */
274+
275+
__END_DECLS
276+
277+
#endif /* !_FENV_H_ */

mips/Make.files

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

mips/fenv.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*-
2+
* Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
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$
27+
*/
28+
29+
#define __fenv_static
30+
#include "openlibm_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 SOFTFLOAT
43+
#define __set_env(env, flags, mask, rnd) env = ((flags) \
44+
| (mask)<<_FPUSW_SHIFT \
45+
| (rnd) << 24)
46+
#define __env_flags(env) ((env) & FE_ALL_EXCEPT)
47+
#define __env_mask(env) (((env) >> _FPUSW_SHIFT) \
48+
& FE_ALL_EXCEPT)
49+
#define __env_round(env) (((env) >> 24) & _ROUND_MASK)
50+
#include "fenv-softfloat.h"
51+
#endif
52+
53+
extern inline int feclearexcept(int __excepts);
54+
extern inline int fegetexceptflag(fexcept_t *__flagp, int __excepts);
55+
extern inline int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
56+
extern inline int feraiseexcept(int __excepts);
57+
extern inline int fetestexcept(int __excepts);
58+
extern inline int fegetround(void);
59+
extern inline int fesetround(int __round);
60+
extern inline int fegetenv(fenv_t *__envp);
61+
extern inline int feholdexcept(fenv_t *__envp);
62+
extern inline int fesetenv(const fenv_t *__envp);
63+
extern inline int feupdateenv(const fenv_t *__envp);
64+
extern inline int feenableexcept(int __mask);
65+
extern inline int fedisableexcept(int __mask);
66+
extern inline int fegetexcept(void);
67+

src/fpmath.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#endif
4040
#elif defined(__powerpc__)
4141
#include "powerpc_fpmath.h"
42+
#elif defined(__mips__)
43+
#include "mips_fpmath.h"
4244
#endif
4345

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

0 commit comments

Comments
 (0)