Merge branch 'vendor/OPENSSH'
[dragonfly.git] / lib / libm / arch / i386 / fenv.h
1 /*-
2  * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/lib/msun/i387/fenv.h,v 1.6 2007/01/06 21:46:23 das Exp $
27  */
28
29 #ifndef _FENV_H_
30 #define _FENV_H_
31
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34
35 /*
36  * To preserve binary compatibility with FreeBSD 5.3, we pack the
37  * mxcsr into some reserved fields, rather than changing sizeof(fenv_t).
38  */
39 typedef struct {
40         __uint16_t      __control;
41         __uint16_t      __status;
42         __uint32_t      __mxcsr;
43         __uint32_t      __tag;
44         char            __other[16];
45 } fenv_t;
46
47 typedef __uint16_t      fexcept_t;
48
49 /* Exception flags */
50 #define FE_INVALID      0x01
51 #define FE_DENORMAL     0x02
52 #define FE_DIVBYZERO    0x04
53 #define FE_OVERFLOW     0x08
54 #define FE_UNDERFLOW    0x10
55 #define FE_INEXACT      0x20
56 #define FE_ALL_EXCEPT   (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
57                          FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
58
59 /* Rounding modes */
60 #define FE_TONEAREST    0x0000
61 #define FE_DOWNWARD     0x0400
62 #define FE_UPWARD       0x0800
63 #define FE_TOWARDZERO   0x0c00
64 #define _ROUND_MASK     (FE_TONEAREST | FE_DOWNWARD | \
65                          FE_UPWARD | FE_TOWARDZERO)
66
67 /*
68  * As compared to the x87 control word, the SSE unit's control word
69  * has the rounding control bits offset by 3 and the exception mask
70  * bits offset by 7.
71  */
72 #define _SSE_ROUND_SHIFT        3
73 #define _SSE_EMASK_SHIFT        7
74
75 __BEGIN_DECLS
76
77 /* After testing for SSE support once, we cache the result in __has_sse. */
78 enum __sse_support { __SSE_YES, __SSE_NO, __SSE_UNK };
79 extern enum __sse_support __has_sse;
80 int __test_sse(void);
81 #ifdef __SSE__
82 #define __HAS_SSE()     1
83 #else
84 #define __HAS_SSE()     (__has_sse == __SSE_YES ||                      \
85                          (__has_sse == __SSE_UNK && __test_sse()))
86 #endif
87
88 /* Default floating-point environment */
89 extern const fenv_t     __fe_dfl_env;
90 #define FE_DFL_ENV      (&__fe_dfl_env)
91
92 #define __fldcw(__cw)           __asm __volatile("fldcw %0" : : "m" (__cw))
93 #define __fldenv(__env)         __asm __volatile("fldenv %0" : : "m" (__env))
94 #define __fldenvx(__env)        __asm __volatile("fldenv %0" : : "m" (__env)  \
95                                 : "st", "st(1)", "st(2)", "st(3)", "st(4)",   \
96                                 "st(5)", "st(6)", "st(7)")
97 #define __fnclex()              __asm __volatile("fnclex")
98 #define __fnstenv(__env)        __asm __volatile("fnstenv %0" : "=m" (*(__env)))
99 #define __fnstcw(__cw)          __asm __volatile("fnstcw %0" : "=m" (*(__cw)))
100 #define __fnstsw(__sw)          __asm __volatile("fnstsw %0" : "=am" (*(__sw)))
101 #define __fwait()               __asm __volatile("fwait")
102 #define __ldmxcsr(__csr)        __asm __volatile("ldmxcsr %0" : : "m" (__csr))
103 #define __stmxcsr(__csr)        __asm __volatile("stmxcsr %0" : "=m" (*(__csr)))
104
105 static __inline int
106 feclearexcept(int __excepts)
107 {
108         fenv_t __env;
109         int __mxcsr;
110
111         if (__excepts == FE_ALL_EXCEPT) {
112                 __fnclex();
113         } else {
114                 __fnstenv(&__env);
115                 __env.__status &= ~__excepts;
116                 __fldenv(__env);
117         }
118         if (__HAS_SSE()) {
119                 __stmxcsr(&__mxcsr);
120                 __mxcsr &= ~__excepts;
121                 __ldmxcsr(__mxcsr);
122         }
123         return (0);
124 }
125
126 static __inline int
127 fegetexceptflag(fexcept_t *__flagp, int __excepts)
128 {
129         __uint32_t __mxcsr;
130         __uint16_t __status;
131
132         __fnstsw(&__status);
133         if (__HAS_SSE())
134                 __stmxcsr(&__mxcsr);
135         else
136                 __mxcsr = 0;
137         *__flagp = (__mxcsr | __status) & __excepts;
138         return (0);
139 }
140
141 int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
142 int feraiseexcept(int __excepts);
143
144 static __inline int
145 fetestexcept(int __excepts)
146 {
147         __uint32_t __mxcsr;
148         __uint16_t __status;
149
150         __fnstsw(&__status);
151         if (__HAS_SSE())
152                 __stmxcsr(&__mxcsr);
153         else
154                 __mxcsr = 0;
155         return ((__status | __mxcsr) & __excepts);
156 }
157
158 static __inline int
159 fegetround(void)
160 {
161         int __control;
162
163         /*
164          * We assume that the x87 and the SSE unit agree on the
165          * rounding mode.  Reading the control word on the x87 turns
166          * out to be about 5 times faster than reading it on the SSE
167          * unit on an Opteron 244.
168          */
169         __fnstcw(&__control);
170         return (__control & _ROUND_MASK);
171 }
172
173 static __inline int
174 fesetround(int __round)
175 {
176         __uint32_t __mxcsr;
177         __uint16_t __control;
178
179         if (__round & ~_ROUND_MASK)
180                 return (-1);
181
182         __fnstcw(&__control);
183         __control &= ~_ROUND_MASK;
184         __control |= __round;
185         __fldcw(__control);
186
187         if (__HAS_SSE()) {
188                 __stmxcsr(&__mxcsr);
189                 __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
190                 __mxcsr |= __round << _SSE_ROUND_SHIFT;
191                 __ldmxcsr(__mxcsr);
192         }
193
194         return (0);
195 }
196
197 int fegetenv(fenv_t *__envp);
198 int feholdexcept(fenv_t *__envp);
199
200 static __inline int
201 fesetenv(const fenv_t *__envp)
202 {
203         fenv_t __env = *__envp;
204         int __mxcsr;
205
206         __mxcsr = __env.__mxcsr;
207         __env.__mxcsr = 0xffffffff;
208         /*
209          * XXX Using fldenvx() instead of fldenv() tells the compiler that this
210          * instruction clobbers the i387 register stack.  This happens because
211          * we restore the tag word from the saved environment.  Normally, this
212          * would happen anyway and we wouldn't care, because the ABI allows
213          * function calls to clobber the i387 regs.  However, fesetenv() is
214          * inlined, so we need to be more careful.
215          */
216         __fldenvx(__env);
217         if (__HAS_SSE())
218                 __ldmxcsr(__mxcsr);
219         return (0);
220 }
221
222 int feupdateenv(const fenv_t *__envp);
223
224 #if __BSD_VISIBLE
225
226 int feenableexcept(int __mask);
227 int fedisableexcept(int __mask);
228
229 static __inline int
230 fegetexcept(void)
231 {
232         __uint16_t __control;
233
234         /*
235          * We assume that the masks for the x87 and the SSE unit are
236          * the same.
237          */
238         __fnstcw(&__control);
239         return (~__control & FE_ALL_EXCEPT);
240 }
241
242 #endif /* __BSD_VISIBLE */
243
244 __END_DECLS
245
246 #endif  /* !_FENV_H_ */