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