2 * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/_types.h>
34 typedef __uint64_t fenv_t;
35 typedef __uint16_t fexcept_t;
38 #define FE_INVALID 0x01
39 #define FE_DENORMAL 0x02
40 #define FE_DIVBYZERO 0x04
41 #define FE_OVERFLOW 0x08
42 #define FE_UNDERFLOW 0x10
43 #define FE_INEXACT 0x20
44 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
45 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
48 #define FE_TONEAREST 0x0000
49 #define FE_DOWNWARD 0x0400
50 #define FE_UPWARD 0x0800
51 #define FE_TOWARDZERO 0x0c00
52 #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
53 FE_UPWARD | FE_TOWARDZERO)
57 /* Default floating-point environment */
58 extern const fenv_t __fe_dfl_env;
59 #define FE_DFL_ENV (&__fe_dfl_env)
61 #define _FPUSW_SHIFT 13
63 #define __stfpsr(__r) __asm __volatile("mov %0=ar.fpsr" : "=r" (*(__r)))
64 #define __ldfpsr(__r) __asm __volatile("mov ar.fpsr=%0;;" : : "r" (__r))
67 feclearexcept(int __excepts)
72 __fpsr &= ~((fenv_t)__excepts << _FPUSW_SHIFT);
78 fegetexceptflag(fexcept_t *__flagp, int __excepts)
83 *__flagp = (fexcept_t)(__fpsr >> _FPUSW_SHIFT) & __excepts;
88 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
93 __fpsr &= ~((fenv_t)__excepts << _FPUSW_SHIFT);
94 __fpsr |= (fenv_t)(__excepts & *__flagp) << _FPUSW_SHIFT;
100 * It is worthwhile to use the inline version of this function iff it
101 * is called with arguments that are compile-time constants (due to
102 * dead code elimination). Unfortunately, gcc isn't smart enough to
103 * figure this out automatically, and there's no way to tell it.
104 * We assume that constant arguments will be the common case.
107 feraiseexcept(int __excepts)
112 * With a compiler that supports the FENV_ACCESS pragma
113 * properly, simple expressions like '0.0 / 0.0' should
114 * be sufficient to generate traps. Unfortunately, we
115 * need to bring a volatile variable into the equation
116 * to prevent incorrect optimizations.
118 if (__excepts & FE_INVALID) {
122 if (__excepts & FE_DIVBYZERO) {
126 if (__excepts & FE_OVERFLOW) {
130 if (__excepts & FE_UNDERFLOW) {
134 if (__excepts & FE_INEXACT) {
142 fetestexcept(int __excepts)
147 return ((__fpsr >> _FPUSW_SHIFT) & __excepts);
157 return (__fpsr & _ROUND_MASK);
161 fesetround(int __round)
165 if (__round & ~_ROUND_MASK)
168 __fpsr &= ~_ROUND_MASK;
175 fegetenv(fenv_t *__envp)
183 feholdexcept(fenv_t *__envp)
189 __fpsr &= ~((fenv_t)FE_ALL_EXCEPT << _FPUSW_SHIFT);
190 __fpsr |= FE_ALL_EXCEPT;
196 fesetenv(const fenv_t *__envp)
203 int feupdateenv(const fenv_t *__envp);
208 feenableexcept(int __mask)
210 fenv_t __newfpsr, __oldfpsr;
212 __stfpsr(&__oldfpsr);
213 __newfpsr = __oldfpsr & ~(__mask & FE_ALL_EXCEPT);
215 return (~__oldfpsr & FE_ALL_EXCEPT);
219 fedisableexcept(int __mask)
221 fenv_t __newfpsr, __oldfpsr;
223 __stfpsr(&__oldfpsr);
224 __newfpsr = __oldfpsr | (__mask & FE_ALL_EXCEPT);
226 return (~__oldfpsr & FE_ALL_EXCEPT);
235 return (~__fpsr & FE_ALL_EXCEPT);
238 #endif /* __BSD_VISIBLE */
242 #endif /* !_FENV_H_ */