]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/msun/mips/fenv.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r303197, and update
[FreeBSD/FreeBSD.git] / lib / msun / mips / fenv.h
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 __mips_soft_float
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 #if !defined(__mips_soft_float) && !defined(__mips_hard_float)
78 #error compiler didnt set soft/hard float macros
79 #endif
80
81 #ifndef __mips_soft_float
82 #define __cfc1(__fcsr)  __asm __volatile("cfc1 %0, $31" : "=r" (__fcsr))
83 #define __ctc1(__fcsr)  __asm __volatile("ctc1 %0, $31" :: "r" (__fcsr))
84 #endif
85
86 #ifdef __mips_soft_float
87 int feclearexcept(int __excepts);
88 int fegetexceptflag(fexcept_t *__flagp, int __excepts);
89 int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
90 int feraiseexcept(int __excepts);
91 int fetestexcept(int __excepts);
92 int fegetround(void);
93 int fesetround(int __round);
94 int fegetenv(fenv_t *__envp);
95 int feholdexcept(fenv_t *__envp);
96 int fesetenv(const fenv_t *__envp);
97 int feupdateenv(const fenv_t *__envp);
98 #else
99 __fenv_static inline int
100 feclearexcept(int __excepts)
101 {
102         fexcept_t fcsr;
103
104         __excepts &= FE_ALL_EXCEPT;
105         __cfc1(fcsr);
106         fcsr &= ~(__excepts | (__excepts << _FCSR_CAUSE_SHIFT));
107         __ctc1(fcsr);
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         __excepts &= FE_ALL_EXCEPT;
118         __cfc1(fcsr);
119         *__flagp = fcsr & __excepts;
120
121         return (0);
122 }
123
124 __fenv_static inline int
125 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
126 {
127         fexcept_t fcsr;
128
129         __excepts &= FE_ALL_EXCEPT;
130         __cfc1(fcsr);
131         fcsr &= ~__excepts;
132         fcsr |= *__flagp & __excepts;
133         __ctc1(fcsr);
134
135         return (0);
136 }
137
138 __fenv_static inline int
139 feraiseexcept(int __excepts)
140 {
141         fexcept_t fcsr;
142
143         __excepts &= FE_ALL_EXCEPT;
144         __cfc1(fcsr);
145         fcsr |= __excepts | (__excepts << _FCSR_CAUSE_SHIFT);
146         __ctc1(fcsr);
147
148         return (0);
149 }
150
151 __fenv_static inline int
152 fetestexcept(int __excepts)
153 {
154         fexcept_t fcsr;
155
156         __excepts &= FE_ALL_EXCEPT;
157         __cfc1(fcsr);
158
159         return (fcsr & __excepts);
160 }
161
162 __fenv_static inline int
163 fegetround(void)
164 {
165         fexcept_t fcsr;
166
167         __cfc1(fcsr);
168
169         return (fcsr & _ROUND_MASK);
170 }
171
172 __fenv_static inline int
173 fesetround(int __round)
174 {
175         fexcept_t fcsr;
176
177         if (__round & ~_ROUND_MASK)
178                 return (-1);
179
180         __cfc1(fcsr);
181         fcsr &= ~_ROUND_MASK;
182         fcsr |= __round;
183         __ctc1(fcsr);
184
185         return (0);
186 }
187
188 __fenv_static inline int
189 fegetenv(fenv_t *__envp)
190 {
191
192         __cfc1(*__envp);
193
194         return (0);
195 }
196
197 __fenv_static inline int
198 feholdexcept(fenv_t *__envp)
199 {
200         fexcept_t fcsr;
201
202         __cfc1(fcsr);
203         *__envp = fcsr;
204         fcsr &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
205         __ctc1(fcsr);
206
207         return (0);
208 }
209
210 __fenv_static inline int
211 fesetenv(const fenv_t *__envp)
212 {
213
214         __ctc1(*__envp);
215
216         return (0);
217 }
218
219 __fenv_static inline int
220 feupdateenv(const fenv_t *__envp)
221 {
222         fexcept_t fcsr;
223
224         __cfc1(fcsr);
225         fesetenv(__envp);
226         feraiseexcept(fcsr);
227
228         return (0);
229 }
230 #endif /* !__mips_soft_float */
231
232 #if __BSD_VISIBLE
233
234 /* We currently provide no external definitions of the functions below. */
235
236 #ifdef __mips_soft_float
237 int feenableexcept(int __mask);
238 int fedisableexcept(int __mask);
239 int fegetexcept(void);
240 #else
241 static inline int
242 feenableexcept(int __mask)
243 {
244         fenv_t __old_fcsr, __new_fcsr;
245
246         __cfc1(__old_fcsr);
247         __new_fcsr = __old_fcsr | (__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT;
248         __ctc1(__new_fcsr);
249
250         return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
251 }
252
253 static inline int
254 fedisableexcept(int __mask)
255 {
256         fenv_t __old_fcsr, __new_fcsr;
257
258         __cfc1(__old_fcsr);
259         __new_fcsr = __old_fcsr & ~((__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT);
260         __ctc1(__new_fcsr);
261
262         return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
263 }
264
265 static inline int
266 fegetexcept(void)
267 {
268         fexcept_t fcsr;
269
270         __cfc1(fcsr);
271
272         return ((fcsr & _ENABLE_MASK) >> _ENABLE_SHIFT);
273 }
274
275 #endif /* !__mips_soft_float */
276
277 #endif /* __BSD_VISIBLE */
278
279 __END_DECLS
280
281 #endif  /* !_FENV_H_ */