VirtualBox

source: vbox/trunk/include/iprt/nocrt/amd64/fenv.h@ 3631

Last change on this file since 3631 was 3631, checked in by vboxsync, 17 years ago

iprt_hdr_h -> _iprt_hdr_h

File size: 7.2 KB
Line 
1/** @file
2 * innotek Portable Runtime / No-CRT - fenv.h, AMD64.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
19 *
20 * --------------------------------------------------------------------
21 *
22 * This code is based on:
23 *
24 * Copyright (c) 2004-2005 David Schultz <[email protected]>
25 * All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 */
48
49#ifndef ___iprt_nocrt_amd64_fenv_h
50#define ___iprt_nocrt_amd64_fenv_h
51
52#include <iprt/types.h>
53
54typedef struct {
55 struct {
56 uint32_t __control;
57 uint32_t __status;
58 uint32_t __tag;
59 char __other[16];
60 } __x87;
61 uint32_t __mxcsr;
62} fenv_t;
63
64typedef uint16_t fexcept_t;
65
66/* Exception flags */
67#define FE_INVALID 0x01
68#define FE_DENORMAL 0x02
69#define FE_DIVBYZERO 0x04
70#define FE_OVERFLOW 0x08
71#define FE_UNDERFLOW 0x10
72#define FE_INEXACT 0x20
73#define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
74 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
75
76/* Rounding modes */
77#define FE_TONEAREST 0x0000
78#define FE_DOWNWARD 0x0400
79#define FE_UPWARD 0x0800
80#define FE_TOWARDZERO 0x0c00
81#define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
82 FE_UPWARD | FE_TOWARDZERO)
83
84/*
85 * As compared to the x87 control word, the SSE unit's control word
86 * has the rounding control bits offset by 3 and the exception mask
87 * bits offset by 7.
88 */
89#define _SSE_ROUND_SHIFT 3
90#define _SSE_EMASK_SHIFT 7
91
92__BEGIN_DECLS
93
94/* Default floating-point environment */
95extern const fenv_t RT_NOCRT(__fe_dfl_env);
96#define FE_DFL_ENV (&__fe_dfl_env)
97
98#define __fldcw(__cw) __asm __volatile("fldcw %0" : : "m" (__cw))
99#define __fldenv(__env) __asm __volatile("fldenv %0" : : "m" (__env))
100#define __fnclex() __asm __volatile("fnclex")
101#define __fnstenv(__env) __asm __volatile("fnstenv %0" : "=m" (*(__env)))
102#define __fnstcw(__cw) __asm __volatile("fnstcw %0" : "=m" (*(__cw)))
103#define __fnstsw(__sw) __asm __volatile("fnstsw %0" : "=am" (*(__sw)))
104#define __fwait() __asm __volatile("fwait")
105#define __ldmxcsr(__csr) __asm __volatile("ldmxcsr %0" : : "m" (__csr))
106#define __stmxcsr(__csr) __asm __volatile("stmxcsr %0" : "=m" (*(__csr)))
107
108DECLINLINE(int)
109feclearexcept(int __excepts)
110{
111 fenv_t __env;
112
113 if (__excepts == FE_ALL_EXCEPT) {
114 __fnclex();
115 } else {
116 __fnstenv(&__env.__x87);
117 __env.__x87.__status &= ~__excepts;
118 __fldenv(__env.__x87);
119 }
120 __stmxcsr(&__env.__mxcsr);
121 __env.__mxcsr &= ~__excepts;
122 __ldmxcsr(__env.__mxcsr);
123 return (0);
124}
125
126DECLINLINE(int)
127fegetexceptflag(fexcept_t *__flagp, int __excepts)
128{
129 int __mxcsr, __status;
130
131 __stmxcsr(&__mxcsr);
132 __fnstsw(&__status);
133 *__flagp = (__mxcsr | __status) & __excepts;
134 return (0);
135}
136
137int RT_NOCRT(fesetexceptflag)(const fexcept_t *__flagp, int __excepts);
138int RT_NOCRT(feraiseexcept)(int __excepts);
139
140DECLINLINE(int)
141fetestexcept(int __excepts)
142{
143 int __mxcsr, __status;
144
145 __stmxcsr(&__mxcsr);
146 __fnstsw(&__status);
147 return ((__status | __mxcsr) & __excepts);
148}
149
150DECLINLINE(int)
151fegetround(void)
152{
153 int __control;
154
155 /*
156 * We assume that the x87 and the SSE unit agree on the
157 * rounding mode. Reading the control word on the x87 turns
158 * out to be about 5 times faster than reading it on the SSE
159 * unit on an Opteron 244.
160 */
161 __fnstcw(&__control);
162 return (__control & _ROUND_MASK);
163}
164
165DECLINLINE(int)
166fesetround(int __round)
167{
168 int __mxcsr, __control;
169
170 if (__round & ~_ROUND_MASK)
171 return (-1);
172
173 __fnstcw(&__control);
174 __control &= ~_ROUND_MASK;
175 __control |= __round;
176 __fldcw(__control);
177
178 __stmxcsr(&__mxcsr);
179 __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
180 __mxcsr |= __round << _SSE_ROUND_SHIFT;
181 __ldmxcsr(__mxcsr);
182
183 return (0);
184}
185
186int RT_NOCRT(fegetenv)(fenv_t *__envp);
187int RT_NOCRT(feholdexcept)(fenv_t *__envp);
188
189DECLINLINE(int)
190fesetenv(const fenv_t *__envp)
191{
192
193 __fldenv(__envp->__x87);
194 __ldmxcsr(__envp->__mxcsr);
195 return (0);
196}
197
198int RT_NOCRT(feupdateenv)(const fenv_t *__envp);
199int RT_NOCRT(feenableexcept)(int __mask);
200int RT_NOCRT(fedisableexcept)(int __mask);
201
202DECLINLINE(int)
203fegetexcept(void)
204{
205 int __control;
206
207 /*
208 * We assume that the masks for the x87 and the SSE unit are
209 * the same.
210 */
211 __fnstcw(&__control);
212 return (~__control & FE_ALL_EXCEPT);
213}
214
215__END_DECLS
216
217#ifndef RT_WITHOUT_NOCRT_WRAPPERS
218# define fesetexceptflag RT_NOCRT(fesetexceptflag)
219# define feraiseexcept RT_NOCRT(feraiseexcept)
220# define fegetenv RT_NOCRT(fegetenv)
221# define feholdexcept RT_NOCRT(feholdexcept)
222# define feupdateenv RT_NOCRT(feupdateenv)
223# define feenableexcept RT_NOCRT(feenableexcept)
224# define fedisableexcept RT_NOCRT(fedisableexcept)
225# define __fe_dfl_env RT_NOCRT(__fe_dfl_env)
226#endif
227
228#endif /* !__iprt_nocrt_amd64_fenv_h__ */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette