xref: /freebsd/sys/i386/include/ieeefp.h (revision d7d962ead0b6e5e8a39202d0590022082bf5bfb6)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2003 Peter Wemm.
5  * Copyright (c) 1990 Andrew Moore, Talke Studio
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * 	from: @(#) ieeefp.h 	1.0 (Berkeley) 9/23/93
37  * $FreeBSD$
38  */
39 
40 #ifndef _MACHINE_IEEEFP_H_
41 #define _MACHINE_IEEEFP_H_
42 
43 /* Deprecated historical FPU control interface */
44 
45 #include <x86/x86_ieeefp.h>
46 
47 #ifdef __GNUCLIKE_ASM
48 
49 static __inline fp_rnd_t
50 fpgetround(void)
51 {
52 	unsigned short _cw;
53 
54 	__fnstcw(&_cw);
55 	return ((fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF));
56 }
57 
58 static __inline fp_rnd_t
59 fpsetround(fp_rnd_t _m)
60 {
61 	fp_rnd_t _p;
62 	unsigned short _cw, _newcw;
63 
64 	__fnstcw(&_cw);
65 	_p = (fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF);
66 	_newcw = _cw & ~FP_RND_FLD;
67 	_newcw |= (_m << FP_RND_OFF) & FP_RND_FLD;
68 	__fnldcw(_cw, _newcw);
69 	return (_p);
70 }
71 
72 static __inline fp_prec_t
73 fpgetprec(void)
74 {
75 	unsigned short _cw;
76 
77 	__fnstcw(&_cw);
78 	return ((fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF));
79 }
80 
81 static __inline fp_prec_t
82 fpsetprec(fp_prec_t _m)
83 {
84 	fp_prec_t _p;
85 	unsigned short _cw, _newcw;
86 
87 	__fnstcw(&_cw);
88 	_p = (fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF);
89 	_newcw = _cw & ~FP_PRC_FLD;
90 	_newcw |= (_m << FP_PRC_OFF) & FP_PRC_FLD;
91 	__fnldcw(_cw, _newcw);
92 	return (_p);
93 }
94 
95 /*
96  * Get or set the exception mask.
97  * Note that the x87 mask bits are inverted by the API -- a mask bit of 1
98  * means disable for x87 and SSE, but for fp*mask() it means enable.
99  */
100 
101 static __inline fp_except_t
102 fpgetmask(void)
103 {
104 	unsigned short _cw;
105 
106 	__fnstcw(&_cw);
107 	return ((~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF);
108 }
109 
110 static __inline fp_except_t
111 fpsetmask(fp_except_t _m)
112 {
113 	fp_except_t _p;
114 	unsigned short _cw, _newcw;
115 
116 	__fnstcw(&_cw);
117 	_p = (~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF;
118 	_newcw = _cw & ~FP_MSKS_FLD;
119 	_newcw |= (~_m << FP_MSKS_OFF) & FP_MSKS_FLD;
120 	__fnldcw(_cw, _newcw);
121 	return (_p);
122 }
123 
124 static __inline fp_except_t
125 fpgetsticky(void)
126 {
127 	unsigned _ex;
128 	unsigned short _sw;
129 
130 	__fnstsw(&_sw);
131 	_ex = (_sw & FP_STKY_FLD) >> FP_STKY_OFF;
132 	return ((fp_except_t)_ex);
133 }
134 
135 static __inline fp_except_t
136 fpresetsticky(fp_except_t _m)
137 {
138 	struct {
139 		unsigned _cw;
140 		unsigned _sw;
141 		unsigned _other[5];
142 	} _env;
143 	fp_except_t _p;
144 
145 	_m &= FP_STKY_FLD >> FP_STKY_OFF;
146 	_p = fpgetsticky();
147 	if ((_p & ~_m) == _p)
148 		return (_p);
149 	if ((_p & ~_m) == 0) {
150 		__fnclex();
151 		return (_p);
152 	}
153 	__fnstenv(&_env);
154 	_env._sw &= ~_m;
155 	__fldenv(&_env);
156 	return (_p);
157 }
158 
159 #endif /* __GNUCLIKE_ASM */
160 
161 #endif /* !_MACHINE_IEEEFP_H_ */
162