1 | /** @file
|
---|
2 | * IPRT / No-CRT - fenv.h, AMD64.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
26 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
27 | * additional information or have any questions.
|
---|
28 | * --------------------------------------------------------------------
|
---|
29 | *
|
---|
30 | * This code is based on:
|
---|
31 | *
|
---|
32 | * Copyright (c) 2004-2005 David Schultz <[email protected]>
|
---|
33 | * All rights reserved.
|
---|
34 | *
|
---|
35 | * Redistribution and use in source and binary forms, with or without
|
---|
36 | * modification, are permitted provided that the following conditions
|
---|
37 | * are met:
|
---|
38 | * 1. Redistributions of source code must retain the above copyright
|
---|
39 | * notice, this list of conditions and the following disclaimer.
|
---|
40 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
41 | * notice, this list of conditions and the following disclaimer in the
|
---|
42 | * documentation and/or other materials provided with the distribution.
|
---|
43 | *
|
---|
44 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
---|
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
---|
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
54 | * SUCH DAMAGE.
|
---|
55 | */
|
---|
56 |
|
---|
57 | #ifndef ___iprt_nocrt_amd64_fenv_h
|
---|
58 | #define ___iprt_nocrt_amd64_fenv_h
|
---|
59 |
|
---|
60 | #include <iprt/types.h>
|
---|
61 |
|
---|
62 | typedef struct {
|
---|
63 | struct {
|
---|
64 | uint32_t __control;
|
---|
65 | uint32_t __status;
|
---|
66 | uint32_t __tag;
|
---|
67 | char __other[16];
|
---|
68 | } __x87;
|
---|
69 | uint32_t __mxcsr;
|
---|
70 | } fenv_t;
|
---|
71 |
|
---|
72 | typedef uint16_t fexcept_t;
|
---|
73 |
|
---|
74 | /* Exception flags */
|
---|
75 | #define FE_INVALID 0x01
|
---|
76 | #define FE_DENORMAL 0x02
|
---|
77 | #define FE_DIVBYZERO 0x04
|
---|
78 | #define FE_OVERFLOW 0x08
|
---|
79 | #define FE_UNDERFLOW 0x10
|
---|
80 | #define FE_INEXACT 0x20
|
---|
81 | #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
|
---|
82 | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
|
---|
83 |
|
---|
84 | /* Rounding modes */
|
---|
85 | #define FE_TONEAREST 0x0000
|
---|
86 | #define FE_DOWNWARD 0x0400
|
---|
87 | #define FE_UPWARD 0x0800
|
---|
88 | #define FE_TOWARDZERO 0x0c00
|
---|
89 | #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
|
---|
90 | FE_UPWARD | FE_TOWARDZERO)
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * As compared to the x87 control word, the SSE unit's control word
|
---|
94 | * has the rounding control bits offset by 3 and the exception mask
|
---|
95 | * bits offset by 7.
|
---|
96 | */
|
---|
97 | #define _SSE_ROUND_SHIFT 3
|
---|
98 | #define _SSE_EMASK_SHIFT 7
|
---|
99 |
|
---|
100 | __BEGIN_DECLS
|
---|
101 |
|
---|
102 | /* Default floating-point environment */
|
---|
103 | extern const fenv_t RT_NOCRT(__fe_dfl_env);
|
---|
104 | #define FE_DFL_ENV (&__fe_dfl_env)
|
---|
105 |
|
---|
106 | #define __fldcw(__cw) __asm __volatile("fldcw %0" : : "m" (__cw))
|
---|
107 | #define __fldenv(__env) __asm __volatile("fldenv %0" : : "m" (__env))
|
---|
108 | #define __fnclex() __asm __volatile("fnclex")
|
---|
109 | #define __fnstenv(__env) __asm __volatile("fnstenv %0" : "=m" (*(__env)))
|
---|
110 | #define __fnstcw(__cw) __asm __volatile("fnstcw %0" : "=m" (*(__cw)))
|
---|
111 | #define __fnstsw(__sw) __asm __volatile("fnstsw %0" : "=am" (*(__sw)))
|
---|
112 | #define __fwait() __asm __volatile("fwait")
|
---|
113 | #define __ldmxcsr(__csr) __asm __volatile("ldmxcsr %0" : : "m" (__csr))
|
---|
114 | #define __stmxcsr(__csr) __asm __volatile("stmxcsr %0" : "=m" (*(__csr)))
|
---|
115 |
|
---|
116 | DECLINLINE(int)
|
---|
117 | feclearexcept(int __excepts)
|
---|
118 | {
|
---|
119 | fenv_t __env;
|
---|
120 |
|
---|
121 | if (__excepts == FE_ALL_EXCEPT) {
|
---|
122 | __fnclex();
|
---|
123 | } else {
|
---|
124 | __fnstenv(&__env.__x87);
|
---|
125 | __env.__x87.__status &= ~__excepts;
|
---|
126 | __fldenv(__env.__x87);
|
---|
127 | }
|
---|
128 | __stmxcsr(&__env.__mxcsr);
|
---|
129 | __env.__mxcsr &= ~__excepts;
|
---|
130 | __ldmxcsr(__env.__mxcsr);
|
---|
131 | return (0);
|
---|
132 | }
|
---|
133 |
|
---|
134 | DECLINLINE(int)
|
---|
135 | fegetexceptflag(fexcept_t *__flagp, int __excepts)
|
---|
136 | {
|
---|
137 | int __mxcsr, __status;
|
---|
138 |
|
---|
139 | __stmxcsr(&__mxcsr);
|
---|
140 | __fnstsw(&__status);
|
---|
141 | *__flagp = (__mxcsr | __status) & __excepts;
|
---|
142 | return (0);
|
---|
143 | }
|
---|
144 |
|
---|
145 | int RT_NOCRT(fesetexceptflag)(const fexcept_t *__flagp, int __excepts);
|
---|
146 | int RT_NOCRT(feraiseexcept)(int __excepts);
|
---|
147 |
|
---|
148 | DECLINLINE(int)
|
---|
149 | fetestexcept(int __excepts)
|
---|
150 | {
|
---|
151 | int __mxcsr, __status;
|
---|
152 |
|
---|
153 | __stmxcsr(&__mxcsr);
|
---|
154 | __fnstsw(&__status);
|
---|
155 | return ((__status | __mxcsr) & __excepts);
|
---|
156 | }
|
---|
157 |
|
---|
158 | DECLINLINE(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 | DECLINLINE(int)
|
---|
174 | fesetround(int __round)
|
---|
175 | {
|
---|
176 | int __mxcsr, __control;
|
---|
177 |
|
---|
178 | if (__round & ~_ROUND_MASK)
|
---|
179 | return (-1);
|
---|
180 |
|
---|
181 | __fnstcw(&__control);
|
---|
182 | __control &= ~_ROUND_MASK;
|
---|
183 | __control |= __round;
|
---|
184 | __fldcw(__control);
|
---|
185 |
|
---|
186 | __stmxcsr(&__mxcsr);
|
---|
187 | __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
|
---|
188 | __mxcsr |= __round << _SSE_ROUND_SHIFT;
|
---|
189 | __ldmxcsr(__mxcsr);
|
---|
190 |
|
---|
191 | return (0);
|
---|
192 | }
|
---|
193 |
|
---|
194 | int RT_NOCRT(fegetenv)(fenv_t *__envp);
|
---|
195 | int RT_NOCRT(feholdexcept)(fenv_t *__envp);
|
---|
196 |
|
---|
197 | DECLINLINE(int)
|
---|
198 | fesetenv(const fenv_t *__envp)
|
---|
199 | {
|
---|
200 |
|
---|
201 | __fldenv(__envp->__x87);
|
---|
202 | __ldmxcsr(__envp->__mxcsr);
|
---|
203 | return (0);
|
---|
204 | }
|
---|
205 |
|
---|
206 | int RT_NOCRT(feupdateenv)(const fenv_t *__envp);
|
---|
207 | int RT_NOCRT(feenableexcept)(int __mask);
|
---|
208 | int RT_NOCRT(fedisableexcept)(int __mask);
|
---|
209 |
|
---|
210 | DECLINLINE(int)
|
---|
211 | fegetexcept(void)
|
---|
212 | {
|
---|
213 | int __control;
|
---|
214 |
|
---|
215 | /*
|
---|
216 | * We assume that the masks for the x87 and the SSE unit are
|
---|
217 | * the same.
|
---|
218 | */
|
---|
219 | __fnstcw(&__control);
|
---|
220 | return (~__control & FE_ALL_EXCEPT);
|
---|
221 | }
|
---|
222 |
|
---|
223 | __END_DECLS
|
---|
224 |
|
---|
225 | #ifndef RT_WITHOUT_NOCRT_WRAPPERS
|
---|
226 | # define fesetexceptflag RT_NOCRT(fesetexceptflag)
|
---|
227 | # define feraiseexcept RT_NOCRT(feraiseexcept)
|
---|
228 | # define fegetenv RT_NOCRT(fegetenv)
|
---|
229 | # define feholdexcept RT_NOCRT(feholdexcept)
|
---|
230 | # define feupdateenv RT_NOCRT(feupdateenv)
|
---|
231 | # define feenableexcept RT_NOCRT(feenableexcept)
|
---|
232 | # define fedisableexcept RT_NOCRT(fedisableexcept)
|
---|
233 | # define __fe_dfl_env RT_NOCRT(__fe_dfl_env)
|
---|
234 | #endif
|
---|
235 |
|
---|
236 | #endif /* !__iprt_nocrt_amd64_fenv_h__ */
|
---|