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