1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 * $FreeBSD$
29 */
30
31#ifndef	_FENV_H_
32#define	_FENV_H_
33
34#include <sys/cdefs.h>
35#include <sys/_types.h>
36#include <ieeefp.h>
37
38#ifndef	__fenv_static
39#define	__fenv_static	static
40#endif
41
42typedef	__uint16_t	fexcept_t;
43
44/* Exception flags */
45#define	FE_INVALID	0x01
46#define	FE_DENORMAL	0x02
47#define	FE_DIVBYZERO	0x04
48#define	FE_OVERFLOW	0x08
49#define	FE_UNDERFLOW	0x10
50#define	FE_INEXACT	0x20
51#define	FE_ALL_EXCEPT	(FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
52			 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
53
54/* Rounding modes */
55#define	FE_TONEAREST	0x0000
56#define	FE_DOWNWARD	0x0400
57#define	FE_UPWARD	0x0800
58#define	FE_TOWARDZERO	0x0c00
59#define	_ROUND_MASK	(FE_TONEAREST | FE_DOWNWARD | \
60			 FE_UPWARD | FE_TOWARDZERO)
61
62/*
63 * As compared to the x87 control word, the SSE unit's control word
64 * has the rounding control bits offset by 3 and the exception mask
65 * bits offset by 7.
66 */
67#define	_SSE_ROUND_SHIFT	3
68#define	_SSE_EMASK_SHIFT	7
69
70#ifdef __i386__
71/*
72 * To preserve binary compatibility with FreeBSD 5.3, we pack the
73 * mxcsr into some reserved fields, rather than changing sizeof(fenv_t).
74 */
75typedef struct {
76	__uint16_t	__control;
77	__uint16_t      __mxcsr_hi;
78	__uint16_t	__status;
79	__uint16_t      __mxcsr_lo;
80	__uint32_t	__tag;
81	char		__other[16];
82} fenv_t;
83#else /* __amd64__ */
84typedef struct {
85	struct {
86		__uint32_t	__control;
87		__uint32_t	__status;
88		__uint32_t	__tag;
89		char		__other[16];
90	} __x87;
91	__uint32_t		__mxcsr;
92} fenv_t;
93#endif /* __i386__ */
94
95__BEGIN_DECLS
96
97/* Default floating-point environment */
98extern const fenv_t	__fe_dfl_env;
99#define	FE_DFL_ENV	(&__fe_dfl_env)
100
101#define	__fldenvx(__env)	__asm __volatile("fldenv %0" : : "m" (__env)  \
102				: "st", "st(1)", "st(2)", "st(3)", "st(4)",   \
103				"st(5)", "st(6)", "st(7)")
104#define	__fwait()		__asm __volatile("fwait")
105
106int fegetenv(fenv_t *__envp);
107int feholdexcept(fenv_t *__envp);
108int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
109int feraiseexcept(int __excepts);
110int feupdateenv(const fenv_t *__envp);
111
112__fenv_static inline int
113fegetround(void)
114{
115	__uint16_t __control;
116
117	/*
118	 * We assume that the x87 and the SSE unit agree on the
119	 * rounding mode.  Reading the control word on the x87 turns
120	 * out to be about 5 times faster than reading it on the SSE
121	 * unit on an Opteron 244.
122	 */
123	__fnstcw(&__control);
124	return (__control & _ROUND_MASK);
125}
126
127#if __BSD_VISIBLE
128
129int feenableexcept(int __mask);
130int fedisableexcept(int __mask);
131
132/* We currently provide no external definition of fegetexcept(). */
133static inline int
134fegetexcept(void)
135{
136	__uint16_t __control;
137
138	/*
139	 * We assume that the masks for the x87 and the SSE unit are
140	 * the same.
141	 */
142	__fnstcw(&__control);
143	return (~__control & FE_ALL_EXCEPT);
144}
145
146#endif /* __BSD_VISIBLE */
147
148#ifdef __i386__
149
150/* After testing for SSE support once, we cache the result in __has_sse. */
151enum __sse_support { __SSE_YES, __SSE_NO, __SSE_UNK };
152extern enum __sse_support __has_sse;
153int __test_sse(void);
154#ifdef __SSE__
155#define	__HAS_SSE()	1
156#else
157#define	__HAS_SSE()	(__has_sse == __SSE_YES ||			\
158			 (__has_sse == __SSE_UNK && __test_sse()))
159#endif
160
161#define	__get_mxcsr(env)	(((env).__mxcsr_hi << 16) |	\
162				 ((env).__mxcsr_lo))
163#define	__set_mxcsr(env, x)	do {				\
164	(env).__mxcsr_hi = (__uint32_t)(x) >> 16;		\
165	(env).__mxcsr_lo = (__uint16_t)(x);			\
166} while (0)
167
168__fenv_static inline int
169feclearexcept(int __excepts)
170{
171	fenv_t __env;
172	__uint32_t __mxcsr;
173
174	if (__excepts == FE_ALL_EXCEPT) {
175		__fnclex();
176	} else {
177		__fnstenv(&__env);
178		__env.__status &= ~__excepts;
179		__fldenv(&__env);
180	}
181	if (__HAS_SSE()) {
182		__stmxcsr(&__mxcsr);
183		__mxcsr &= ~__excepts;
184		__ldmxcsr(&__mxcsr);
185	}
186	return (0);
187}
188
189__fenv_static inline int
190fegetexceptflag(fexcept_t *__flagp, int __excepts)
191{
192	__uint32_t __mxcsr;
193	__uint16_t __status;
194
195	__fnstsw(&__status);
196	if (__HAS_SSE())
197		__stmxcsr(&__mxcsr);
198	else
199		__mxcsr = 0;
200	*__flagp = (__mxcsr | __status) & __excepts;
201	return (0);
202}
203
204__fenv_static inline int
205fetestexcept(int __excepts)
206{
207	__uint32_t __mxcsr;
208	__uint16_t __status;
209
210	__fnstsw(&__status);
211	if (__HAS_SSE())
212		__stmxcsr(&__mxcsr);
213	else
214		__mxcsr = 0;
215	return ((__status | __mxcsr) & __excepts);
216}
217
218__fenv_static inline int
219fesetround(int __round)
220{
221	__uint32_t __mxcsr;
222	__uint16_t __control;
223
224	if (__round & ~_ROUND_MASK)
225		return (-1);
226
227	__fnstcw(&__control);
228	__control &= ~_ROUND_MASK;
229	__control |= __round;
230	__fldcw(&__control);
231
232	if (__HAS_SSE()) {
233		__stmxcsr(&__mxcsr);
234		__mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
235		__mxcsr |= __round << _SSE_ROUND_SHIFT;
236		__ldmxcsr(&__mxcsr);
237	}
238
239	return (0);
240}
241
242__fenv_static inline int
243fesetenv(const fenv_t *__envp)
244{
245	fenv_t __env = *__envp;
246	__uint32_t __mxcsr;
247
248	__mxcsr = __get_mxcsr(__env);
249	__set_mxcsr(__env, 0xffffffff);
250	/*
251	 * XXX Using fldenvx() instead of fldenv() tells the compiler that this
252	 * instruction clobbers the i387 register stack.  This happens because
253	 * we restore the tag word from the saved environment.  Normally, this
254	 * would happen anyway and we wouldn't care, because the ABI allows
255	 * function calls to clobber the i387 regs.  However, fesetenv() is
256	 * inlined, so we need to be more careful.
257	 */
258	__fldenvx(__env);
259	if (__HAS_SSE())
260		__ldmxcsr(&__mxcsr);
261	return (0);
262}
263
264#else /* __amd64__ */
265
266__fenv_static inline int
267feclearexcept(int __excepts)
268{
269	fenv_t __env;
270
271	if (__excepts == FE_ALL_EXCEPT) {
272		__fnclex();
273	} else {
274		__fnstenv(&__env.__x87);
275		__env.__x87.__status &= ~__excepts;
276		__fldenv(&__env.__x87);
277	}
278	__stmxcsr(&__env.__mxcsr);
279	__env.__mxcsr &= ~__excepts;
280	__ldmxcsr(&__env.__mxcsr);
281	return (0);
282}
283
284__fenv_static inline int
285fegetexceptflag(fexcept_t *__flagp, int __excepts)
286{
287	__uint32_t __mxcsr;
288	__uint16_t __status;
289
290	__stmxcsr(&__mxcsr);
291	__fnstsw(&__status);
292	*__flagp = (__mxcsr | __status) & __excepts;
293	return (0);
294}
295
296__fenv_static inline int
297fetestexcept(int __excepts)
298{
299	__uint32_t __mxcsr;
300	__uint16_t __status;
301
302	__stmxcsr(&__mxcsr);
303	__fnstsw(&__status);
304	return ((__status | __mxcsr) & __excepts);
305}
306
307__fenv_static inline int
308fesetround(int __round)
309{
310	__uint32_t __mxcsr;
311	__uint16_t __control;
312
313	if (__round & ~_ROUND_MASK)
314		return (-1);
315
316	__fnstcw(&__control);
317	__control &= ~_ROUND_MASK;
318	__control |= __round;
319	__fldcw(&__control);
320
321	__stmxcsr(&__mxcsr);
322	__mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
323	__mxcsr |= __round << _SSE_ROUND_SHIFT;
324	__ldmxcsr(&__mxcsr);
325
326	return (0);
327}
328
329__fenv_static inline int
330fesetenv(const fenv_t *__envp)
331{
332
333	/*
334	 * XXX Using fldenvx() instead of fldenv() tells the compiler that this
335	 * instruction clobbers the i387 register stack.  This happens because
336	 * we restore the tag word from the saved environment.  Normally, this
337	 * would happen anyway and we wouldn't care, because the ABI allows
338	 * function calls to clobber the i387 regs.  However, fesetenv() is
339	 * inlined, so we need to be more careful.
340	 */
341	__fldenvx(__envp->__x87);
342	__ldmxcsr(&__envp->__mxcsr);
343	return (0);
344}
345
346#endif /* __i386__ */
347
348__END_DECLS
349
350#endif	/* !_FENV_H_ */
351