Merge from vendor branch ZLIB:
[dragonfly.git] / sys / amd64 / 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: src/sys/amd64/include/ieeefp.h,v 1.11 2003/11/08 04:39:22 peter Exp $
36  * $DragonFly: src/sys/amd64/include/Attic/ieeefp.h,v 1.1 2004/02/02 08:05:52 dillon Exp $
37  */
38
39 /*
40  *      IEEE floating point type and constant definitions.
41  */
42
43 #ifndef _MACHINE_IEEEFP_H_
44 #define _MACHINE_IEEEFP_H_
45
46 /*
47  * FP rounding modes
48  */
49 typedef enum {
50         FP_RN=0,        /* round to nearest */
51         FP_RM,          /* round down to minus infinity */
52         FP_RP,          /* round up to plus infinity */
53         FP_RZ           /* truncate */
54 } fp_rnd_t;
55
56 /*
57  * FP precision modes
58  */
59 typedef enum {
60         FP_PS=0,        /* 24 bit (single-precision) */
61         FP_PRS,         /* reserved */
62         FP_PD,          /* 53 bit (double-precision) */
63         FP_PE           /* 64 bit (extended-precision) */
64 } fp_prec_t;
65
66 #define fp_except_t     int
67
68 /*
69  * FP exception masks
70  */
71 #define FP_X_INV        0x01    /* invalid operation */
72 #define FP_X_DNML       0x02    /* denormal */
73 #define FP_X_DZ         0x04    /* zero divide */
74 #define FP_X_OFL        0x08    /* overflow */
75 #define FP_X_UFL        0x10    /* underflow */
76 #define FP_X_IMP        0x20    /* (im)precision */
77 #define FP_X_STK        0x40    /* stack fault */
78
79 /*
80  * FP registers
81  */
82 #define FP_MSKS_REG     0       /* exception masks */
83 #define FP_PRC_REG      0       /* precision */
84 #define FP_RND_REG      0       /* direction */
85 #define FP_STKY_REG     1       /* sticky flags */
86
87 /*
88  * FP register bit field masks
89  */
90 #define FP_MSKS_FLD     0x3f    /* exception masks field */
91 #define FP_PRC_FLD      0x300   /* precision control field */
92 #define FP_RND_FLD      0xc00   /* round control field */
93 #define FP_STKY_FLD     0x3f    /* sticky flags field */
94
95 /*
96  * SSE mxcsr register bit field masks
97  */
98 #define SSE_STKY_FLD    0x3f    /* exception flags */
99 #define SSE_DAZ_FLD     0x40    /* Denormals are zero */
100 #define SSE_MSKS_FLD    0x1f80  /* exception masks field */
101 #define SSE_RND_FLD     0x6000  /* rounding control */
102 #define SSE_FZ_FLD      0x8000  /* flush to zero on underflow */
103
104 /*
105  * FP register bit field offsets
106  */
107 #define FP_MSKS_OFF     0       /* exception masks offset */
108 #define FP_PRC_OFF      8       /* precision control offset */
109 #define FP_RND_OFF      10      /* round control offset */
110 #define FP_STKY_OFF     0       /* sticky flags offset */
111
112 /*
113  * SSE mxcsr register bit field offsets
114  */
115 #define SSE_STKY_OFF    0       /* exception flags offset */
116 #define SSE_DAZ_OFF     6       /* DAZ exception mask offset */
117 #define SSE_MSKS_OFF    7       /* other exception masks offset */
118 #define SSE_RND_OFF     13      /* rounding control offset */
119 #define SSE_FZ_OFF      15      /* flush to zero offset */
120
121 #if defined(__GNUC__) && !defined(__cplusplus)
122
123 #define __fldenv(addr)  __asm __volatile("fldenv %0" : : "m" (*(addr)))
124 #define __fnstenv(addr) __asm __volatile("fnstenv %0" : "=m" (*(addr)))
125 #define __fldcw(addr)   __asm __volatile("fldcw %0" : "=m" (*(addr)))
126 #define __fnstcw(addr)  __asm __volatile("fnstcw %0" : "=m" (*(addr)))
127 #define __fnstsw(addr)  __asm __volatile("fnstsw %0" : "=m" (*(addr)))
128 #define __ldmxcsr(addr) __asm __volatile("ldmxcsr %0" : "=m" (*(addr)))
129 #define __stmxcsr(addr) __asm __volatile("stmxcsr %0" : "=m" (*(addr)))
130
131 /*
132  * General notes about conflicting SSE vs FP status bits.
133  * This code assumes that software will not fiddle with the control
134  * bits of the SSE and x87 in such a way to get them out of sync and
135  * still expect this to work.  Break this at your peril.
136  * Because I based this on the i386 port, the x87 state is used for
137  * the fpget*() functions, and is shadowed into the SSE state for
138  * the fpset*() functions.  For dual source fpget*() functions, I
139  * merge the two together.  I think.
140  */
141
142 /* Set rounding control */
143 static __inline__ fp_rnd_t
144 __fpgetround(void)
145 {
146         unsigned short _cw;
147
148         __fnstcw(&_cw);
149         return ((_cw & FP_RND_FLD) >> FP_RND_OFF);
150 }
151
152 static __inline__ fp_rnd_t
153 __fpsetround(fp_rnd_t _m)
154 {
155         unsigned short _cw;
156         unsigned int _mxcsr;
157         fp_rnd_t _p;
158
159         __fnstcw(&_cw);
160         _p = (_cw & FP_RND_FLD) >> FP_RND_OFF;
161         _cw &= ~FP_RND_FLD;
162         _cw |= (_m << FP_RND_OFF) & FP_RND_FLD;
163         __fldcw(&_cw);
164         __stmxcsr(&_mxcsr);
165         _mxcsr &= ~SSE_RND_FLD;
166         _mxcsr |= (_m << SSE_RND_OFF) & SSE_RND_FLD;
167         __ldmxcsr(&_mxcsr);
168         return (_p);
169 }
170
171 /*
172  * Set precision for fadd/fsub/fsqrt etc x87 instructions
173  * There is no equivalent SSE mode or control.
174  */
175 static __inline__ fp_prec_t
176 __fpgetprec(void)
177 {
178         unsigned short _cw;
179
180         __fnstcw(&_cw);
181         return ((_cw & FP_PRC_FLD) >> FP_PRC_OFF);
182 }
183
184 static __inline__ fp_prec_t
185 __fpsetprec(fp_rnd_t _m)
186 {
187         unsigned short _cw;
188         fp_prec_t _p;
189
190         __fnstcw(&_cw);
191         _p = (_cw & FP_PRC_FLD) >> FP_PRC_OFF;
192         _cw &= ~FP_PRC_FLD;
193         _cw |= (_m << FP_PRC_OFF) & FP_PRC_FLD;
194         __fldcw(&_cw);
195         return (_p);
196 }
197
198 /*
199  * Look at the exception masks
200  * Note that x87 masks are inverse of the fp*() functions
201  * API.  ie: mask = 1 means disable for x87 and SSE, but
202  * for the fp*() api, mask = 1 means enabled.
203  */
204 static __inline__ fp_except_t
205 __fpgetmask(void)
206 {
207         unsigned short _cw;
208
209         __fnstcw(&_cw);
210         return ((~_cw) & FP_MSKS_FLD);
211 }
212
213 static __inline__ fp_except_t
214 __fpsetmask(fp_except_t _m)
215 {
216         unsigned short _cw;
217         unsigned int _mxcsr;
218         fp_except_t _p;
219
220         __fnstcw(&_cw);
221         _p = (~_cw) & FP_MSKS_FLD;
222         _cw &= ~FP_MSKS_FLD;
223         _cw |= (~_m) & FP_MSKS_FLD;
224         __fldcw(&_cw);
225         __stmxcsr(&_mxcsr);
226         /* XXX should we clear non-ieee SSE_DAZ_FLD and SSE_FZ_FLD ? */
227         _mxcsr &= ~SSE_MSKS_FLD;
228         _mxcsr |= ((~_m) << SSE_MSKS_OFF) & SSE_MSKS_FLD;
229         __ldmxcsr(&_mxcsr);
230         return (_p);
231 }
232
233 /* See which sticky exceptions are pending, and reset them */
234 static __inline__ fp_except_t
235 __fpgetsticky(void)
236 {
237         unsigned short _sw;
238         unsigned int _mxcsr;
239         fp_except_t _ex;
240
241         __fnstsw(&_sw);
242         _ex = _sw & FP_STKY_FLD;
243         __stmxcsr(&_mxcsr);
244         _ex |= _mxcsr & SSE_STKY_FLD;
245         return (_ex);
246 }
247
248 /* Note that this should really be called fpresetsticky() */
249 static __inline__ fp_except_t
250 __fpsetsticky(fp_except_t _m)
251 {
252         unsigned _env[7];
253         unsigned int _mxcsr;
254         fp_except_t _p;
255
256         __fnstenv(_env);
257         _p = _env[FP_STKY_REG] & _m;
258         __stmxcsr(&_mxcsr);
259         _p |= _mxcsr & SSE_STKY_FLD;
260         _env[FP_STKY_REG] &= ~_m;
261         __fldenv(_env);
262         _mxcsr &= ~_m;
263         __ldmxcsr(&_mxcsr);
264         return (_p);
265 }
266
267 #endif /* __GNUC__ && !__cplusplus */
268
269 #if !defined(__IEEEFP_NOINLINES__) && !defined(__cplusplus) && defined(__GNUC__)
270
271 #define fpgetround()    __fpgetround()
272 #define fpsetround(_m)  __fpsetround(_m)
273 #define fpgetprec()     __fpgetprec()
274 #define fpsetprec(_m)   __fpsetprec(_m)
275 #define fpgetmask()     __fpgetmask()
276 #define fpsetmask(_m)   __fpsetmask(_m)
277 #define fpgetsticky()   __fpgetsticky()
278 #define fpsetsticky(_m) __fpsetsticky(_m)
279
280 /* Suppress prototypes in the MI header. */
281 #define _IEEEFP_INLINED_        1
282
283 #else /* !__IEEEFP_NOINLINES__ && !__cplusplus && __GNUC__ */
284
285 /* Augment the userland declarations */
286 __BEGIN_DECLS
287 extern fp_prec_t fpgetprec(void);
288 extern fp_prec_t fpsetprec(fp_prec_t);
289 __END_DECLS
290
291 #endif /* !__IEEEFP_NOINLINES__ && !__cplusplus && __GNUC__ */
292
293 #endif /* !_MACHINE_IEEEFP_H_ */