xref: /freebsd/sys/x86/include/x86_ieeefp.h (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
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 _X86_X86_IEEEFP_H_
41 #define _X86_X86_IEEEFP_H_
42 
43 /* Deprecated historical FPU control interface */
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  * Rounding modes.
52  */
53 typedef enum {
54 	FP_RN=0,	/* round to nearest */
55 	FP_RM,		/* round down towards minus infinity */
56 	FP_RP,		/* round up towards plus infinity */
57 	FP_RZ		/* truncate */
58 } fp_rnd_t;
59 
60 /*
61  * Precision (i.e., rounding precision) modes.
62  */
63 typedef enum {
64 	FP_PS=0,	/* 24 bit (single-precision) */
65 	FP_PRS,		/* reserved */
66 	FP_PD,		/* 53 bit (double-precision) */
67 	FP_PE		/* 64 bit (extended-precision) */
68 } fp_prec_t;
69 
70 #define fp_except_t	int
71 
72 /*
73  * Exception bit masks.
74  */
75 #define FP_X_INV	0x01	/* invalid operation */
76 #define FP_X_DNML	0x02	/* denormal */
77 #define FP_X_DZ		0x04	/* zero divide */
78 #define FP_X_OFL	0x08	/* overflow */
79 #define FP_X_UFL	0x10	/* underflow */
80 #define FP_X_IMP	0x20	/* (im)precision */
81 #define FP_X_STK	0x40	/* stack fault */
82 
83 /*
84  * FPU control word bit-field masks.
85  */
86 #define FP_MSKS_FLD	0x3f	/* exception masks field */
87 #define FP_PRC_FLD	0x300	/* precision control field */
88 #define	FP_RND_FLD	0xc00	/* rounding control field */
89 
90 /*
91  * FPU status word bit-field masks.
92  */
93 #define FP_STKY_FLD	0x3f	/* sticky flags field */
94 
95 /*
96  * FPU control word bit-field offsets (shift counts).
97  */
98 #define FP_MSKS_OFF	0	/* exception masks offset */
99 #define FP_PRC_OFF	8	/* precision control offset */
100 #define	FP_RND_OFF	10	/* rounding control offset */
101 
102 /*
103  * FPU status word bit-field offsets (shift counts).
104  */
105 #define FP_STKY_OFF	0	/* sticky flags offset */
106 
107 #define	__fldcw(addr)	__asm __volatile("fldcw %0" : : "m" (*(addr)))
108 #define	__fldenv(addr)	__asm __volatile("fldenv %0" : : "m" (*(addr)))
109 #define	__fnclex()	__asm __volatile("fnclex")
110 #define	__fnstcw(addr)	__asm __volatile("fnstcw %0" : "=m" (*(addr)))
111 #define	__fnstenv(addr)	__asm __volatile("fnstenv %0" : "=m" (*(addr)))
112 #define	__fnstsw(addr)	__asm __volatile("fnstsw %0" : "=m" (*(addr)))
113 #define	__ldmxcsr(addr)	__asm __volatile("ldmxcsr %0" : : "m" (*(addr)))
114 #define	__stmxcsr(addr)	__asm __volatile("stmxcsr %0" : "=m" (*(addr)))
115 
116 /*
117  * Load the control word.  Be careful not to trap if there is a currently
118  * unmasked exception (ones that will become freshly unmasked are not a
119  * problem).  This case must be handled by a save/restore of the
120  * environment or even of the full x87 state.  Accessing the environment
121  * is very inefficient, so only do it when necessary.
122  */
123 static __inline void
124 __fnldcw(unsigned short _cw, unsigned short _newcw)
125 {
126 	struct {
127 		unsigned _cw;
128 		unsigned _other[6];
129 	} _env;
130 	unsigned short _sw;
131 
132 	if ((_cw & FP_MSKS_FLD) != FP_MSKS_FLD) {
133 		__fnstsw(&_sw);
134 		if (((_sw & ~_cw) & FP_STKY_FLD) != 0) {
135 			__fnstenv(&_env);
136 			_env._cw = _newcw;
137 			__fldenv(&_env);
138 			return;
139 		}
140 	}
141 	__fldcw(&_newcw);
142 }
143 
144 #endif/* _X86_X86_IEEEFP_H_ */
145