xref: /freebsd/sys/i386/include/ieeefp.h (revision 9768746b)
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 static __inline fp_rnd_t
48 fpgetround(void)
49 {
50 	unsigned short _cw;
51 
52 	__fnstcw(&_cw);
53 	return ((fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF));
54 }
55 
56 static __inline fp_rnd_t
57 fpsetround(fp_rnd_t _m)
58 {
59 	fp_rnd_t _p;
60 	unsigned short _cw, _newcw;
61 
62 	__fnstcw(&_cw);
63 	_p = (fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF);
64 	_newcw = _cw & ~FP_RND_FLD;
65 	_newcw |= (_m << FP_RND_OFF) & FP_RND_FLD;
66 	__fnldcw(_cw, _newcw);
67 	return (_p);
68 }
69 
70 static __inline fp_prec_t
71 fpgetprec(void)
72 {
73 	unsigned short _cw;
74 
75 	__fnstcw(&_cw);
76 	return ((fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF));
77 }
78 
79 static __inline fp_prec_t
80 fpsetprec(fp_prec_t _m)
81 {
82 	fp_prec_t _p;
83 	unsigned short _cw, _newcw;
84 
85 	__fnstcw(&_cw);
86 	_p = (fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF);
87 	_newcw = _cw & ~FP_PRC_FLD;
88 	_newcw |= (_m << FP_PRC_OFF) & FP_PRC_FLD;
89 	__fnldcw(_cw, _newcw);
90 	return (_p);
91 }
92 
93 /*
94  * Get or set the exception mask.
95  * Note that the x87 mask bits are inverted by the API -- a mask bit of 1
96  * means disable for x87 and SSE, but for fp*mask() it means enable.
97  */
98 
99 static __inline fp_except_t
100 fpgetmask(void)
101 {
102 	unsigned short _cw;
103 
104 	__fnstcw(&_cw);
105 	return ((~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF);
106 }
107 
108 static __inline fp_except_t
109 fpsetmask(fp_except_t _m)
110 {
111 	fp_except_t _p;
112 	unsigned short _cw, _newcw;
113 
114 	__fnstcw(&_cw);
115 	_p = (~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF;
116 	_newcw = _cw & ~FP_MSKS_FLD;
117 	_newcw |= (~_m << FP_MSKS_OFF) & FP_MSKS_FLD;
118 	__fnldcw(_cw, _newcw);
119 	return (_p);
120 }
121 
122 static __inline fp_except_t
123 fpgetsticky(void)
124 {
125 	unsigned _ex;
126 	unsigned short _sw;
127 
128 	__fnstsw(&_sw);
129 	_ex = (_sw & FP_STKY_FLD) >> FP_STKY_OFF;
130 	return ((fp_except_t)_ex);
131 }
132 
133 static __inline fp_except_t
134 fpresetsticky(fp_except_t _m)
135 {
136 	struct {
137 		unsigned _cw;
138 		unsigned _sw;
139 		unsigned _other[5];
140 	} _env;
141 	fp_except_t _p;
142 
143 	_m &= FP_STKY_FLD >> FP_STKY_OFF;
144 	_p = fpgetsticky();
145 	if ((_p & ~_m) == _p)
146 		return (_p);
147 	if ((_p & ~_m) == 0) {
148 		__fnclex();
149 		return (_p);
150 	}
151 	__fnstenv(&_env);
152 	_env._sw &= ~_m;
153 	__fldenv(&_env);
154 	return (_p);
155 }
156 
157 #endif /* !_MACHINE_IEEEFP_H_ */
158