xref: /freebsd/lib/msun/arm/fenv.h (revision 328110da2661a8841f12000b99fea27ceacdd5b2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. 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  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef	_FENV_H_
30 #define	_FENV_H_
31 
32 #include <sys/cdefs.h>
33 #include <sys/_types.h>
34 
35 #ifndef	__fenv_static
36 #define	__fenv_static	static
37 #endif
38 
39 typedef	__uint32_t	fenv_t;
40 typedef	__uint32_t	fexcept_t;
41 
42 /* Exception flags */
43 #define	FE_INVALID	0x0001
44 #define	FE_DIVBYZERO	0x0002
45 #define	FE_OVERFLOW	0x0004
46 #define	FE_UNDERFLOW	0x0008
47 #define	FE_INEXACT	0x0010
48 #ifdef __ARM_PCS_VFP
49 #define	FE_DENORMAL	0x0080
50 #define	FE_ALL_EXCEPT	(FE_DIVBYZERO | FE_INEXACT | \
51 			 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW | FE_DENORMAL)
52 #else
53 #define	FE_ALL_EXCEPT	(FE_DIVBYZERO | FE_INEXACT | \
54 			 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
55 #endif
56 
57 /* Rounding modes */
58 #define	VFP_FE_TONEAREST	0x00000000
59 #define	VFP_FE_UPWARD		0x00400000
60 #define	VFP_FE_DOWNWARD		0x00800000
61 #define	VFP_FE_TOWARDZERO	0x00c00000
62 
63 #ifdef __ARM_PCS_VFP
64 #define	FE_TONEAREST	VFP_FE_TONEAREST
65 #define	FE_UPWARD	VFP_FE_UPWARD
66 #define	FE_DOWNWARD	VFP_FE_DOWNWARD
67 #define	FE_TOWARDZERO	VFP_FE_TOWARDZERO
68 #else
69 #define	FE_TONEAREST	0x0000
70 #define	FE_TOWARDZERO	0x0001
71 #define	FE_UPWARD	0x0002
72 #define	FE_DOWNWARD	0x0003
73 #endif
74 #define	_ROUND_MASK	(FE_TONEAREST | FE_DOWNWARD | \
75 			 FE_UPWARD | FE_TOWARDZERO)
76 __BEGIN_DECLS
77 
78 /* Default floating-point environment */
79 extern const fenv_t	__fe_dfl_env;
80 #define	FE_DFL_ENV	(&__fe_dfl_env)
81 
82 /* We need to be able to map status flag positions to mask flag positions */
83 #ifndef __ARM_PCS_VFP
84 #define	_FPUSW_SHIFT	16
85 #define	_ENABLE_MASK	(FE_ALL_EXCEPT << _FPUSW_SHIFT)
86 #endif
87 
88 #ifndef __ARM_PCS_VFP
89 
90 int feclearexcept(int __excepts);
91 int fegetexceptflag(fexcept_t *__flagp, int __excepts);
92 int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
93 int feraiseexcept(int __excepts);
94 int fetestexcept(int __excepts);
95 int fegetround(void);
96 int fesetround(int __round);
97 int fegetenv(fenv_t *__envp);
98 int feholdexcept(fenv_t *__envp);
99 int fesetenv(const fenv_t *__envp);
100 int feupdateenv(const fenv_t *__envp);
101 #if __BSD_VISIBLE
102 int feenableexcept(int __mask);
103 int fedisableexcept(int __mask);
104 int fegetexcept(void);
105 #endif
106 
107 #else	/* __ARM_PCS_VFP */
108 
109 #define	vmrs_fpscr(__r)	__asm __volatile("vmrs %0, fpscr" : "=&r"(__r))
110 #define	vmsr_fpscr(__r)	__asm __volatile("vmsr fpscr, %0" : : "r"(__r))
111 
112 #define _FPU_MASK_SHIFT	8
113 
114 __fenv_static inline int
115 feclearexcept(int __excepts)
116 {
117 	fexcept_t __fpsr;
118 
119 	vmrs_fpscr(__fpsr);
120 	__fpsr &= ~__excepts;
121 	vmsr_fpscr(__fpsr);
122 	return (0);
123 }
124 
125 __fenv_static inline int
126 fegetexceptflag(fexcept_t *__flagp, int __excepts)
127 {
128 	fexcept_t __fpsr;
129 
130 	vmrs_fpscr(__fpsr);
131 	*__flagp = __fpsr & __excepts;
132 	return (0);
133 }
134 
135 __fenv_static inline int
136 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
137 {
138 	fexcept_t __fpsr;
139 
140 	vmrs_fpscr(__fpsr);
141 	__fpsr &= ~__excepts;
142 	__fpsr |= *__flagp & __excepts;
143 	vmsr_fpscr(__fpsr);
144 	return (0);
145 }
146 
147 __fenv_static inline int
148 feraiseexcept(int __excepts)
149 {
150 	fexcept_t __ex = __excepts;
151 
152 	fesetexceptflag(&__ex, __excepts);	/* XXX */
153 	return (0);
154 }
155 
156 __fenv_static inline int
157 fetestexcept(int __excepts)
158 {
159 	fexcept_t __fpsr;
160 
161 	vmrs_fpscr(__fpsr);
162 	return (__fpsr & __excepts);
163 }
164 
165 __fenv_static inline int
166 fegetround(void)
167 {
168 	fenv_t __fpsr;
169 
170 	vmrs_fpscr(__fpsr);
171 	return (__fpsr & _ROUND_MASK);
172 }
173 
174 __fenv_static inline int
175 fesetround(int __round)
176 {
177 	fenv_t __fpsr;
178 
179 	vmrs_fpscr(__fpsr);
180 	__fpsr &= ~(_ROUND_MASK);
181 	__fpsr |= __round;
182 	vmsr_fpscr(__fpsr);
183 	return (0);
184 }
185 
186 __fenv_static inline int
187 fegetenv(fenv_t *__envp)
188 {
189 
190 	vmrs_fpscr(*__envp);
191 	return (0);
192 }
193 
194 __fenv_static inline int
195 feholdexcept(fenv_t *__envp)
196 {
197 	fenv_t __env;
198 
199 	vmrs_fpscr(__env);
200 	*__envp = __env;
201 	__env &= ~(FE_ALL_EXCEPT);
202 	vmsr_fpscr(__env);
203 	return (0);
204 }
205 
206 __fenv_static inline int
207 fesetenv(const fenv_t *__envp)
208 {
209 
210 	vmsr_fpscr(*__envp);
211 	return (0);
212 }
213 
214 __fenv_static inline int
215 feupdateenv(const fenv_t *__envp)
216 {
217 	fexcept_t __fpsr;
218 
219 	vmrs_fpscr(__fpsr);
220 	vmsr_fpscr(*__envp);
221 	feraiseexcept(__fpsr & FE_ALL_EXCEPT);
222 	return (0);
223 }
224 
225 #if __BSD_VISIBLE
226 
227 /* We currently provide no external definitions of the functions below. */
228 
229 __fenv_static inline int
230 feenableexcept(int __mask)
231 {
232 	fenv_t __old_fpsr, __new_fpsr;
233 
234 	vmrs_fpscr(__old_fpsr);
235 	__new_fpsr = __old_fpsr |
236 	    ((__mask & FE_ALL_EXCEPT) << _FPU_MASK_SHIFT);
237 	vmsr_fpscr(__new_fpsr);
238 	return ((__old_fpsr >> _FPU_MASK_SHIFT) & FE_ALL_EXCEPT);
239 }
240 
241 __fenv_static inline int
242 fedisableexcept(int __mask)
243 {
244 	fenv_t __old_fpsr, __new_fpsr;
245 
246 	vmrs_fpscr(__old_fpsr);
247 	__new_fpsr = __old_fpsr &
248 	    ~((__mask & FE_ALL_EXCEPT) << _FPU_MASK_SHIFT);
249 	vmsr_fpscr(__new_fpsr);
250 	return ((__old_fpsr >> _FPU_MASK_SHIFT) & FE_ALL_EXCEPT);
251 }
252 
253 __fenv_static inline int
254 fegetexcept(void)
255 {
256 	fenv_t __fpsr;
257 
258 	vmrs_fpscr(__fpsr);
259 	return (__fpsr & FE_ALL_EXCEPT);
260 }
261 
262 #endif /* __BSD_VISIBLE */
263 
264 #endif	/* __ARM_PCS_VFP */
265 
266 __END_DECLS
267 
268 #endif	/* !_FENV_H_ */
269