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