VirtualBox

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

Last change on this file since 4170 was 4170, checked in by vboxsync, 18 years ago

eol-style native.

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

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