xref: /freebsd/sys/amd64/include/ieeefp.h (revision 19261079)
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 /*
48  * IEEE floating point type, constant and function definitions.
49  * XXX: {FP,SSE}*FLD and {FP,SSE}*OFF are undocumented pollution.
50  */
51 
52 /*
53  * SSE mxcsr register bit-field masks.
54  */
55 #define	SSE_STKY_FLD	0x3f	/* exception flags */
56 #define	SSE_DAZ_FLD	0x40	/* Denormals are zero */
57 #define	SSE_MSKS_FLD	0x1f80	/* exception masks field */
58 #define	SSE_RND_FLD	0x6000	/* rounding control */
59 #define	SSE_FZ_FLD	0x8000	/* flush to zero on underflow */
60 
61 /*
62  * SSE mxcsr register bit-field offsets (shift counts).
63  */
64 #define	SSE_STKY_OFF	0	/* exception flags offset */
65 #define	SSE_DAZ_OFF	6	/* DAZ exception mask offset */
66 #define	SSE_MSKS_OFF	7	/* other exception masks offset */
67 #define	SSE_RND_OFF	13	/* rounding control offset */
68 #define	SSE_FZ_OFF	15	/* flush to zero offset */
69 
70 #ifdef __GNUCLIKE_ASM
71 
72 /*
73  * General notes about conflicting SSE vs FP status bits.
74  * This code assumes that software will not fiddle with the control
75  * bits of the SSE and x87 in such a way to get them out of sync and
76  * still expect this to work.  Break this at your peril.
77  * Because I based this on the i386 port, the x87 state is used for
78  * the fpget*() functions, and is shadowed into the SSE state for
79  * the fpset*() functions.  For dual source fpget*() functions, I
80  * merge the two together.  I think.
81  */
82 
83 static __inline fp_rnd_t
84 __fpgetround(void)
85 {
86 	unsigned short _cw;
87 
88 	__fnstcw(&_cw);
89 	return ((fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF));
90 }
91 
92 static __inline fp_rnd_t
93 __fpsetround(fp_rnd_t _m)
94 {
95 	fp_rnd_t _p;
96 	unsigned _mxcsr;
97 	unsigned short _cw, _newcw;
98 
99 	__fnstcw(&_cw);
100 	_p = (fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF);
101 	_newcw = _cw & ~FP_RND_FLD;
102 	_newcw |= (_m << FP_RND_OFF) & FP_RND_FLD;
103 	__fnldcw(_cw, _newcw);
104 	__stmxcsr(&_mxcsr);
105 	_mxcsr &= ~SSE_RND_FLD;
106 	_mxcsr |= (_m << SSE_RND_OFF) & SSE_RND_FLD;
107 	__ldmxcsr(&_mxcsr);
108 	return (_p);
109 }
110 
111 /*
112  * Get or set the rounding precision for x87 arithmetic operations.
113  * There is no equivalent SSE mode or control.
114  */
115 
116 static __inline fp_prec_t
117 __fpgetprec(void)
118 {
119 	unsigned short _cw;
120 
121 	__fnstcw(&_cw);
122 	return ((fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF));
123 }
124 
125 static __inline fp_prec_t
126 __fpsetprec(fp_prec_t _m)
127 {
128 	fp_prec_t _p;
129 	unsigned short _cw, _newcw;
130 
131 	__fnstcw(&_cw);
132 	_p = (fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF);
133 	_newcw = _cw & ~FP_PRC_FLD;
134 	_newcw |= (_m << FP_PRC_OFF) & FP_PRC_FLD;
135 	__fnldcw(_cw, _newcw);
136 	return (_p);
137 }
138 
139 /*
140  * Get or set the exception mask.
141  * Note that the x87 mask bits are inverted by the API -- a mask bit of 1
142  * means disable for x87 and SSE, but for fp*mask() it means enable.
143  */
144 
145 static __inline fp_except_t
146 __fpgetmask(void)
147 {
148 	unsigned short _cw;
149 
150 	__fnstcw(&_cw);
151 	return ((~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF);
152 }
153 
154 static __inline fp_except_t
155 __fpsetmask(fp_except_t _m)
156 {
157 	fp_except_t _p;
158 	unsigned _mxcsr;
159 	unsigned short _cw, _newcw;
160 
161 	__fnstcw(&_cw);
162 	_p = (~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF;
163 	_newcw = _cw & ~FP_MSKS_FLD;
164 	_newcw |= (~_m << FP_MSKS_OFF) & FP_MSKS_FLD;
165 	__fnldcw(_cw, _newcw);
166 	__stmxcsr(&_mxcsr);
167 	/* XXX should we clear non-ieee SSE_DAZ_FLD and SSE_FZ_FLD ? */
168 	_mxcsr &= ~SSE_MSKS_FLD;
169 	_mxcsr |= (~_m << SSE_MSKS_OFF) & SSE_MSKS_FLD;
170 	__ldmxcsr(&_mxcsr);
171 	return (_p);
172 }
173 
174 static __inline fp_except_t
175 __fpgetsticky(void)
176 {
177 	unsigned _ex, _mxcsr;
178 	unsigned short _sw;
179 
180 	__fnstsw(&_sw);
181 	_ex = (_sw & FP_STKY_FLD) >> FP_STKY_OFF;
182 	__stmxcsr(&_mxcsr);
183 	_ex |= (_mxcsr & SSE_STKY_FLD) >> SSE_STKY_OFF;
184 	return ((fp_except_t)_ex);
185 }
186 
187 #endif /* __GNUCLIKE_ASM */
188 
189 #if !defined(__IEEEFP_NOINLINES__) && defined(__GNUCLIKE_ASM)
190 
191 #define	fpgetmask()	__fpgetmask()
192 #define	fpgetprec()	__fpgetprec()
193 #define	fpgetround()	__fpgetround()
194 #define	fpgetsticky()	__fpgetsticky()
195 #define	fpsetmask(m)	__fpsetmask(m)
196 #define	fpsetprec(m)	__fpsetprec(m)
197 #define	fpsetround(m)	__fpsetround(m)
198 
199 #else /* !(!__IEEEFP_NOINLINES__ && __GNUCLIKE_ASM) */
200 
201 /* Augment the userland declarations. */
202 __BEGIN_DECLS
203 extern fp_rnd_t    fpgetround(void);
204 extern fp_rnd_t    fpsetround(fp_rnd_t);
205 extern fp_except_t fpgetmask(void);
206 extern fp_except_t fpsetmask(fp_except_t);
207 extern fp_except_t fpgetsticky(void);
208 extern fp_except_t fpsetsticky(fp_except_t);
209 fp_prec_t	fpgetprec(void);
210 fp_prec_t	fpsetprec(fp_prec_t);
211 __END_DECLS
212 
213 #endif /* !__IEEEFP_NOINLINES__ && __GNUCLIKE_ASM */
214 
215 #endif /* !_MACHINE_IEEEFP_H_ */
216