]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/include/ieeefp.h
Separate fpresetsticky() from the other fpset functions so that the
[FreeBSD/FreeBSD.git] / sys / i386 / include / ieeefp.h
1 /*-
2  * Copyright (c) 2003 Peter Wemm.
3  * Copyright (c) 1990 Andrew Moore, Talke Studio
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by the University of
17  *      California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      from: @(#) ieeefp.h     1.0 (Berkeley) 9/23/93
35  * $FreeBSD$
36  */
37
38 #ifndef _MACHINE_IEEEFP_H_
39 #define _MACHINE_IEEEFP_H_
40
41 /*
42  * IEEE floating point type, constant and function definitions.
43  * XXX: FP*FLD, FP*REG and FP*OFF are undocumented pollution.
44  */
45
46 #ifndef _SYS_CDEFS_H_
47 #error this file needs sys/cdefs.h as a prerequisite
48 #endif
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 and status register numbers (indexes into the env array).
85  */
86 #define FP_MSKS_REG     0       /* exception masks */
87 #define FP_PRC_REG      0       /* precision */
88 #define FP_RND_REG      0       /* direction */
89 #define FP_STKY_REG     1       /* sticky flags */
90
91 /*
92  * FPU control word bit-field masks.
93  */
94 #define FP_MSKS_FLD     0x3f    /* exception masks field */
95 #define FP_PRC_FLD      0x300   /* precision control field */
96 #define FP_RND_FLD      0xc00   /* rounding control field */
97
98 /*
99  * FPU status word bit-field masks.
100  */
101 #define FP_STKY_FLD     0x3f    /* sticky flags field */
102
103 /*
104  * FPU control word bit-field offsets (shift counts).
105  */
106 #define FP_MSKS_OFF     0       /* exception masks offset */
107 #define FP_PRC_OFF      8       /* precision control offset */
108 #define FP_RND_OFF      10      /* rounding control offset */
109
110 /*
111  * FPU status word bit-field offsets (shift counts).
112  */
113 #define FP_STKY_OFF     0       /* sticky flags offset */
114
115 #ifdef __GNUCLIKE_ASM
116
117 #define __fldenv(addr)  __asm __volatile("fldenv %0" : : "m" (*(addr)))
118 #define __fnclex()      __asm __volatile("fnclex")
119 #define __fnstcw(addr)  __asm __volatile("fnstcw %0" : "=m" (*(addr)))
120 #define __fnstenv(addr) __asm __volatile("fnstenv %0" : "=m" (*(addr)))
121 #define __fnstsw(addr)  __asm __volatile("fnstsw %0" : "=m" (*(addr)))
122
123 /*
124  * return the contents of a FP register
125  */
126 static __inline__ int
127 __fpgetreg(int _reg)
128 {
129         unsigned short _mem;
130
131         /*-
132          * This is more efficient than it looks.  The switch gets optimized
133          * away if _reg is constant.
134          *
135          * The default case only supports _reg == 0.  We could handle more
136          * registers (e.g., tags) using fnstenv, but the interface doesn't
137          * support more.
138          */
139         switch(_reg) {
140         default:
141                 __fnstcw(&_mem);
142                 break;
143         case FP_STKY_REG:
144                 __fnstsw(&_mem);
145                 break;
146         }
147         return _mem;
148 }
149
150 /*
151  * set a FP mode; return previous mode
152  */
153 static __inline__ int
154 __fpsetreg(int _m, int _reg, int _fld, int _off)
155 {
156         unsigned _env[7];
157         unsigned _p;
158
159         /*
160          * _reg == 0 could be handled better using fnstcw/fldcw.
161          */
162         __fnstenv(_env);
163         _p =  (_env[_reg] & _fld) >> _off;
164         _env[_reg] = (_env[_reg] & ~_fld) | (_m << _off & _fld);
165         __fldenv(_env);
166         return _p;
167 }
168
169 /*
170  * SysV/386 FP control interface
171  */
172 #define fpgetround()    ((fp_rnd_t)                                     \
173         ((__fpgetreg(FP_RND_REG) & FP_RND_FLD) >> FP_RND_OFF))
174 #define fpsetround(m)   ((fp_rnd_t)                                     \
175         __fpsetreg((m), FP_RND_REG, FP_RND_FLD, FP_RND_OFF))
176 #define fpgetprec()     ((fp_prec_t)                                    \
177         ((__fpgetreg(FP_PRC_REG) & FP_PRC_FLD) >> FP_PRC_OFF))
178 #define fpsetprec(m)    ((fp_prec_t)                                    \
179         __fpsetreg((m), FP_PRC_REG, FP_PRC_FLD, FP_PRC_OFF))
180 #define fpgetmask()     ((fp_except_t)                                  \
181         ((~__fpgetreg(FP_MSKS_REG) & FP_MSKS_FLD) >> FP_MSKS_OFF))
182 #define fpsetmask(m)    ((fp_except_t)                                  \
183         (~__fpsetreg(~(m), FP_MSKS_REG, FP_MSKS_FLD, FP_MSKS_OFF)) &    \
184             (FP_MSKS_FLD >> FP_MSKS_OFF))
185 #define fpgetsticky()   ((fp_except_t)                                  \
186         ((__fpgetreg(FP_STKY_REG) & FP_STKY_FLD) >> FP_STKY_OFF))
187
188 static __inline fp_except_t
189 fpresetsticky(fp_except_t _m)
190 {
191         struct {
192                 unsigned _cw;
193                 unsigned _sw;
194                 unsigned _other[5];
195         } _env;
196         fp_except_t _p;
197
198         _m &= FP_STKY_FLD >> FP_STKY_OFF;
199         _p = fpgetsticky();
200         if ((_p & ~_m) == _p)
201                 return (_p);
202         if ((_p & ~_m) == 0) {
203                 __fnclex();
204                 return (_p);
205         }
206         __fnstenv(&_env);
207         _env._sw &= ~_m;
208         __fldenv(&_env);
209         return (_p);
210 }
211
212 #endif /* __GNUCLIKE_ASM */
213
214 /* Suppress prototypes in the MI header. */
215 #define _IEEEFP_INLINED_        1
216
217 #endif /* !_MACHINE_IEEEFP_H_ */