1 | /** @file
|
---|
2 | * IPRT / No-CRT - fenv.h, X86.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | * --------------------------------------------------------------------
|
---|
25 | *
|
---|
26 | * This code is based on:
|
---|
27 | *
|
---|
28 | * Copyright (c) 2004-2005 David Schultz <[email protected]>
|
---|
29 | * All rights reserved.
|
---|
30 | *
|
---|
31 | * Redistribution and use in source and binary forms, with or without
|
---|
32 | * modification, are permitted provided that the following conditions
|
---|
33 | * are met:
|
---|
34 | * 1. Redistributions of source code must retain the above copyright
|
---|
35 | * notice, this list of conditions and the following disclaimer.
|
---|
36 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
37 | * notice, this list of conditions and the following disclaimer in the
|
---|
38 | * documentation and/or other materials provided with the distribution.
|
---|
39 | *
|
---|
40 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
---|
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
---|
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
50 | * SUCH DAMAGE.
|
---|
51 | *
|
---|
52 | * $FreeBSD: src/lib/msun/i387/fenv.h,v 1.4 2005/03/17 22:21:46 das Exp $
|
---|
53 | */
|
---|
54 |
|
---|
55 | #ifndef ___iprt_nocrt_x86_fenv_h
|
---|
56 | #define ___iprt_nocrt_x86_fenv_h
|
---|
57 |
|
---|
58 | #include <iprt/types.h>
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * To preserve binary compatibility with FreeBSD 5.3, we pack the
|
---|
62 | * mxcsr into some reserved fields, rather than changing sizeof(fenv_t).
|
---|
63 | */
|
---|
64 | typedef struct {
|
---|
65 | uint16_t __control;
|
---|
66 | uint16_t __mxcsr_hi;
|
---|
67 | uint16_t __status;
|
---|
68 | uint16_t __mxcsr_lo;
|
---|
69 | uint32_t __tag;
|
---|
70 | char __other[16];
|
---|
71 | } fenv_t;
|
---|
72 |
|
---|
73 | #define __get_mxcsr(env) (((env).__mxcsr_hi << 16) | \
|
---|
74 | ((env).__mxcsr_lo))
|
---|
75 | #define __set_mxcsr(env, x) do { \
|
---|
76 | (env).__mxcsr_hi = (uint32_t)(x) >> 16; \
|
---|
77 | (env).__mxcsr_lo = (uint16_t)(x); \
|
---|
78 | } while (0)
|
---|
79 |
|
---|
80 | typedef uint16_t fexcept_t;
|
---|
81 |
|
---|
82 | /* Exception flags */
|
---|
83 | #define FE_INVALID 0x01
|
---|
84 | #define FE_DENORMAL 0x02
|
---|
85 | #define FE_DIVBYZERO 0x04
|
---|
86 | #define FE_OVERFLOW 0x08
|
---|
87 | #define FE_UNDERFLOW 0x10
|
---|
88 | #define FE_INEXACT 0x20
|
---|
89 | #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
|
---|
90 | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
|
---|
91 |
|
---|
92 | /* Rounding modes */
|
---|
93 | #define FE_TONEAREST 0x0000
|
---|
94 | #define FE_DOWNWARD 0x0400
|
---|
95 | #define FE_UPWARD 0x0800
|
---|
96 | #define FE_TOWARDZERO 0x0c00
|
---|
97 | #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
|
---|
98 | FE_UPWARD | FE_TOWARDZERO)
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * As compared to the x87 control word, the SSE unit's control word
|
---|
102 | * has the rounding control bits offset by 3 and the exception mask
|
---|
103 | * bits offset by 7.
|
---|
104 | */
|
---|
105 | #define _SSE_ROUND_SHIFT 3
|
---|
106 | #define _SSE_EMASK_SHIFT 7
|
---|
107 |
|
---|
108 | /* After testing for SSE support once, we cache the result in __has_sse. */
|
---|
109 | enum __sse_support { __SSE_YES, __SSE_NO, __SSE_UNK };
|
---|
110 | extern enum __sse_support RT_NOCRT(__has_sse);
|
---|
111 | int RT_NOCRT(__test_sse)(void);
|
---|
112 | #ifdef __SSE__
|
---|
113 | #define __HAS_SSE() 1
|
---|
114 | #else
|
---|
115 | #define __HAS_SSE() (RT_NOCRT(__has_sse) == __SSE_YES || \
|
---|
116 | (RT_NOCRT(__has_sse) == __SSE_UNK && RT_NOCRT(__test_sse)()))
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | RT_C_DECLS_BEGIN
|
---|
120 |
|
---|
121 | /* Default floating-point environment */
|
---|
122 | extern const fenv_t __fe_dfl_env;
|
---|
123 | #define FE_DFL_ENV (&__fe_dfl_env)
|
---|
124 |
|
---|
125 | #define __fldcw(__cw) __asm __volatile("fldcw %0" : : "m" (__cw))
|
---|
126 | #define __fldenv(__env) __asm __volatile("fldenv %0" : : "m" (__env))
|
---|
127 | #define __fnclex() __asm __volatile("fnclex")
|
---|
128 | #define __fnstenv(__env) __asm __volatile("fnstenv %0" : "=m" (*(__env)))
|
---|
129 | #define __fnstcw(__cw) __asm __volatile("fnstcw %0" : "=m" (*(__cw)))
|
---|
130 | #define __fnstsw(__sw) __asm __volatile("fnstsw %0" : "=am" (*(__sw)))
|
---|
131 | #define __fwait() __asm __volatile("fwait")
|
---|
132 | #define __ldmxcsr(__csr) __asm __volatile("ldmxcsr %0" : : "m" (__csr))
|
---|
133 | #define __stmxcsr(__csr) __asm __volatile("stmxcsr %0" : "=m" (*(__csr)))
|
---|
134 |
|
---|
135 | #if RT_GNUC_PREREQ(4, 6)
|
---|
136 | # pragma GCC diagnostic push
|
---|
137 | # pragma GCC diagnostic ignored "-Wshadow"
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | DECLINLINE(int)
|
---|
141 | feclearexcept(int __excepts)
|
---|
142 | {
|
---|
143 | fenv_t __env;
|
---|
144 | int __mxcsr;
|
---|
145 |
|
---|
146 | if (__excepts == FE_ALL_EXCEPT) {
|
---|
147 | __fnclex();
|
---|
148 | } else {
|
---|
149 | __fnstenv(&__env);
|
---|
150 | __env.__status &= ~__excepts;
|
---|
151 | __fldenv(__env);
|
---|
152 | }
|
---|
153 | if (__HAS_SSE()) {
|
---|
154 | __stmxcsr(&__mxcsr);
|
---|
155 | __mxcsr &= ~__excepts;
|
---|
156 | __ldmxcsr(__mxcsr);
|
---|
157 | }
|
---|
158 | return (0);
|
---|
159 | }
|
---|
160 |
|
---|
161 | DECLINLINE(int)
|
---|
162 | fegetexceptflag(fexcept_t *__flagp, int __excepts)
|
---|
163 | {
|
---|
164 | int __mxcsr, __status;
|
---|
165 |
|
---|
166 | __fnstsw(&__status);
|
---|
167 | if (__HAS_SSE())
|
---|
168 | __stmxcsr(&__mxcsr);
|
---|
169 | else
|
---|
170 | __mxcsr = 0;
|
---|
171 | *__flagp = (__mxcsr | __status) & __excepts;
|
---|
172 | return (0);
|
---|
173 | }
|
---|
174 |
|
---|
175 | int RT_NOCRT(fesetexceptflag)(const fexcept_t *__flagp, int __excepts);
|
---|
176 | int RT_NOCRT(feraiseexcept)(int __excepts);
|
---|
177 |
|
---|
178 | DECLINLINE(int)
|
---|
179 | fetestexcept(int __excepts)
|
---|
180 | {
|
---|
181 | int __mxcsr, __status;
|
---|
182 |
|
---|
183 | __fnstsw(&__status);
|
---|
184 | if (__HAS_SSE())
|
---|
185 | __stmxcsr(&__mxcsr);
|
---|
186 | else
|
---|
187 | __mxcsr = 0;
|
---|
188 | return ((__status | __mxcsr) & __excepts);
|
---|
189 | }
|
---|
190 |
|
---|
191 | DECLINLINE(int)
|
---|
192 | fegetround(void)
|
---|
193 | {
|
---|
194 | int __control;
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * We assume that the x87 and the SSE unit agree on the
|
---|
198 | * rounding mode. Reading the control word on the x87 turns
|
---|
199 | * out to be about 5 times faster than reading it on the SSE
|
---|
200 | * unit on an Opteron 244.
|
---|
201 | */
|
---|
202 | __fnstcw(&__control);
|
---|
203 | return (__control & _ROUND_MASK);
|
---|
204 | }
|
---|
205 |
|
---|
206 | DECLINLINE(int)
|
---|
207 | fesetround(int __round)
|
---|
208 | {
|
---|
209 | int __mxcsr, __control;
|
---|
210 |
|
---|
211 | if (__round & ~_ROUND_MASK)
|
---|
212 | return (-1);
|
---|
213 |
|
---|
214 | __fnstcw(&__control);
|
---|
215 | __control &= ~_ROUND_MASK;
|
---|
216 | __control |= __round;
|
---|
217 | __fldcw(__control);
|
---|
218 |
|
---|
219 | if (__HAS_SSE()) {
|
---|
220 | __stmxcsr(&__mxcsr);
|
---|
221 | __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
|
---|
222 | __mxcsr |= __round << _SSE_ROUND_SHIFT;
|
---|
223 | __ldmxcsr(__mxcsr);
|
---|
224 | }
|
---|
225 |
|
---|
226 | return (0);
|
---|
227 | }
|
---|
228 |
|
---|
229 | int RT_NOCRT(fegetenv)(fenv_t *__envp);
|
---|
230 | int RT_NOCRT(feholdexcept)(fenv_t *__envp);
|
---|
231 |
|
---|
232 | DECLINLINE(int)
|
---|
233 | fesetenv(const fenv_t *__envp)
|
---|
234 | {
|
---|
235 | fenv_t __env = *__envp;
|
---|
236 | int __mxcsr;
|
---|
237 |
|
---|
238 | __mxcsr = __get_mxcsr(__env);
|
---|
239 | __set_mxcsr(__env, 0xffffffff);
|
---|
240 | __fldenv(__env);
|
---|
241 | if (__HAS_SSE())
|
---|
242 | __ldmxcsr(__mxcsr);
|
---|
243 | return (0);
|
---|
244 | }
|
---|
245 |
|
---|
246 | int RT_NOCRT(feupdateenv)(const fenv_t *__envp);
|
---|
247 | int RT_NOCRT(feenableexcept)(int __mask);
|
---|
248 | int RT_NOCRT(fedisableexcept)(int __mask);
|
---|
249 |
|
---|
250 | DECLINLINE(int)
|
---|
251 | fegetexcept(void)
|
---|
252 | {
|
---|
253 | int __control;
|
---|
254 |
|
---|
255 | /*
|
---|
256 | * We assume that the masks for the x87 and the SSE unit are
|
---|
257 | * the same.
|
---|
258 | */
|
---|
259 | __fnstcw(&__control);
|
---|
260 | return (~__control & FE_ALL_EXCEPT);
|
---|
261 | }
|
---|
262 |
|
---|
263 | #if RT_GNUC_PREREQ(4, 6)
|
---|
264 | # pragma GCC diagnostic pop
|
---|
265 | #endif
|
---|
266 |
|
---|
267 | RT_C_DECLS_END
|
---|
268 |
|
---|
269 | #ifndef RT_WIHTOUT_NOCRT_WRAPPERS
|
---|
270 | # define __has_sse RT_NOCRT(__has_sse)
|
---|
271 | # define __test_sse RT_NOCRT(__test_sse)
|
---|
272 | # define __test_sse RT_NOCRT(__test_sse)
|
---|
273 | # define fesetexceptflag RT_NOCRT(fesetexceptflag)
|
---|
274 | # define feraiseexcept RT_NOCRT(feraiseexcept)
|
---|
275 | # define fegetenv RT_NOCRT(fegetenv)
|
---|
276 | # define feholdexcept RT_NOCRT(feholdexcept)
|
---|
277 | # define feupdateenv RT_NOCRT(feupdateenv)
|
---|
278 | # define feenableexcept RT_NOCRT(feenableexcept)
|
---|
279 | # define fedisableexcept RT_NOCRT(fedisableexcept)
|
---|
280 | #endif
|
---|
281 |
|
---|
282 | #endif /* !__iprt_nocrt_x86_fenv_h__ */
|
---|
283 |
|
---|