]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/msun/amd64/fenv.h
Import libyaml as libbsdyml (private brand name)
[FreeBSD/FreeBSD.git] / lib / msun / amd64 / 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$
27  */
28
29 #ifndef _FENV_H_
30 #define _FENV_H_
31
32 #include <sys/cdefs.h>
33 #include <sys/_types.h>
34
35 #ifndef __fenv_static
36 #define __fenv_static   static
37 #endif
38
39 typedef struct {
40         struct {
41                 __uint32_t      __control;
42                 __uint32_t      __status;
43                 __uint32_t      __tag;
44                 char            __other[16];
45         } __x87;
46         __uint32_t              __mxcsr;
47 } fenv_t;
48
49 typedef __uint16_t      fexcept_t;
50
51 /* Exception flags */
52 #define FE_INVALID      0x01
53 #define FE_DENORMAL     0x02
54 #define FE_DIVBYZERO    0x04
55 #define FE_OVERFLOW     0x08
56 #define FE_UNDERFLOW    0x10
57 #define FE_INEXACT      0x20
58 #define FE_ALL_EXCEPT   (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
59                          FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
60
61 /* Rounding modes */
62 #define FE_TONEAREST    0x0000
63 #define FE_DOWNWARD     0x0400
64 #define FE_UPWARD       0x0800
65 #define FE_TOWARDZERO   0x0c00
66 #define _ROUND_MASK     (FE_TONEAREST | FE_DOWNWARD | \
67                          FE_UPWARD | FE_TOWARDZERO)
68
69 /*
70  * As compared to the x87 control word, the SSE unit's control word
71  * has the rounding control bits offset by 3 and the exception mask
72  * bits offset by 7.
73  */
74 #define _SSE_ROUND_SHIFT        3
75 #define _SSE_EMASK_SHIFT        7
76
77 __BEGIN_DECLS
78
79 /* Default floating-point environment */
80 extern const fenv_t     __fe_dfl_env;
81 #define FE_DFL_ENV      (&__fe_dfl_env)
82
83 #define __fldcw(__cw)           __asm __volatile("fldcw %0" : : "m" (__cw))
84 #define __fldenv(__env)         __asm __volatile("fldenv %0" : : "m" (__env))
85 #define __fldenvx(__env)        __asm __volatile("fldenv %0" : : "m" (__env)  \
86                                 : "st", "st(1)", "st(2)", "st(3)", "st(4)",   \
87                                 "st(5)", "st(6)", "st(7)")
88 #define __fnclex()              __asm __volatile("fnclex")
89 #define __fnstenv(__env)        __asm __volatile("fnstenv %0" : "=m" (*(__env)))
90 #define __fnstcw(__cw)          __asm __volatile("fnstcw %0" : "=m" (*(__cw)))
91 #define __fnstsw(__sw)          __asm __volatile("fnstsw %0" : "=am" (*(__sw)))
92 #define __fwait()               __asm __volatile("fwait")
93 #define __ldmxcsr(__csr)        __asm __volatile("ldmxcsr %0" : : "m" (__csr))
94 #define __stmxcsr(__csr)        __asm __volatile("stmxcsr %0" : "=m" (*(__csr)))
95
96 __fenv_static inline int
97 feclearexcept(int __excepts)
98 {
99         fenv_t __env;
100
101         if (__excepts == FE_ALL_EXCEPT) {
102                 __fnclex();
103         } else {
104                 __fnstenv(&__env.__x87);
105                 __env.__x87.__status &= ~__excepts;
106                 __fldenv(__env.__x87);
107         }
108         __stmxcsr(&__env.__mxcsr);
109         __env.__mxcsr &= ~__excepts;
110         __ldmxcsr(__env.__mxcsr);
111         return (0);
112 }
113
114 __fenv_static inline int
115 fegetexceptflag(fexcept_t *__flagp, int __excepts)
116 {
117         __uint32_t __mxcsr;
118         __uint16_t __status;
119
120         __stmxcsr(&__mxcsr);
121         __fnstsw(&__status);
122         *__flagp = (__mxcsr | __status) & __excepts;
123         return (0);
124 }
125
126 int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
127 int feraiseexcept(int __excepts);
128
129 __fenv_static inline int
130 fetestexcept(int __excepts)
131 {
132         __uint32_t __mxcsr;
133         __uint16_t __status;
134
135         __stmxcsr(&__mxcsr);
136         __fnstsw(&__status);
137         return ((__status | __mxcsr) & __excepts);
138 }
139
140 __fenv_static inline int
141 fegetround(void)
142 {
143         __uint16_t __control;
144
145         /*
146          * We assume that the x87 and the SSE unit agree on the
147          * rounding mode.  Reading the control word on the x87 turns
148          * out to be about 5 times faster than reading it on the SSE
149          * unit on an Opteron 244.
150          */
151         __fnstcw(&__control);
152         return (__control & _ROUND_MASK);
153 }
154
155 __fenv_static inline int
156 fesetround(int __round)
157 {
158         __uint32_t __mxcsr;
159         __uint16_t __control;
160
161         if (__round & ~_ROUND_MASK)
162                 return (-1);
163
164         __fnstcw(&__control);
165         __control &= ~_ROUND_MASK;
166         __control |= __round;
167         __fldcw(__control);
168
169         __stmxcsr(&__mxcsr);
170         __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
171         __mxcsr |= __round << _SSE_ROUND_SHIFT;
172         __ldmxcsr(__mxcsr);
173
174         return (0);
175 }
176
177 int fegetenv(fenv_t *__envp);
178 int feholdexcept(fenv_t *__envp);
179
180 __fenv_static inline int
181 fesetenv(const fenv_t *__envp)
182 {
183
184         /*
185          * XXX Using fldenvx() instead of fldenv() tells the compiler that this
186          * instruction clobbers the i387 register stack.  This happens because
187          * we restore the tag word from the saved environment.  Normally, this
188          * would happen anyway and we wouldn't care, because the ABI allows
189          * function calls to clobber the i387 regs.  However, fesetenv() is
190          * inlined, so we need to be more careful.
191          */
192         __fldenvx(__envp->__x87);
193         __ldmxcsr(__envp->__mxcsr);
194         return (0);
195 }
196
197 int feupdateenv(const fenv_t *__envp);
198
199 #if __BSD_VISIBLE
200
201 int feenableexcept(int __mask);
202 int fedisableexcept(int __mask);
203
204 /* We currently provide no external definition of fegetexcept(). */
205 static inline int
206 fegetexcept(void)
207 {
208         __uint16_t __control;
209
210         /*
211          * We assume that the masks for the x87 and the SSE unit are
212          * the same.
213          */
214         __fnstcw(&__control);
215         return (~__control & FE_ALL_EXCEPT);
216 }
217
218 #endif /* __BSD_VISIBLE */
219
220 __END_DECLS
221
222 #endif  /* !_FENV_H_ */