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