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>
35 #define __fenv_static static
38 typedef __uint64_t fenv_t;
39 typedef __uint64_t fexcept_t;
42 #define FE_INVALID 0x00000200
43 #define FE_DIVBYZERO 0x00000040
44 #define FE_OVERFLOW 0x00000100
45 #define FE_UNDERFLOW 0x00000080
46 #define FE_INEXACT 0x00000020
47 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | \
48 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
53 * We can't just use the hardware bit values here, because that would
54 * make FE_UPWARD and FE_DOWNWARD negative, which is not allowed.
56 #define FE_TONEAREST 0x0
57 #define FE_TOWARDZERO 0x1
59 #define FE_DOWNWARD 0x3
60 #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
61 FE_UPWARD | FE_TOWARDZERO)
62 #define _ROUND_SHIFT 30
66 /* Default floating-point environment */
67 extern const fenv_t __fe_dfl_env;
68 #define FE_DFL_ENV (&__fe_dfl_env)
70 /* We need to be able to map status flag positions to mask flag positions */
71 #define _FPUSW_SHIFT 18
72 #define _ENABLE_MASK (FE_ALL_EXCEPT << _FPUSW_SHIFT)
74 #define __ldxfsr(__r) __asm __volatile("ldx %0, %%fsr" : : "m" (__r))
75 #define __stxfsr(__r) __asm __volatile("stx %%fsr, %0" : "=m" (*(__r)))
77 __fenv_static __inline int
78 feclearexcept(int __excepts)
88 __fenv_static inline int
89 fegetexceptflag(fexcept_t *__flagp, int __excepts)
94 *__flagp = __r & __excepts;
98 __fenv_static inline int
99 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
105 __r |= *__flagp & __excepts;
111 * In contrast with the ia64 platform, it seems to be worthwhile to
112 * inline this function on sparc64 even when the arguments are not
113 * compile-time constants. Perhaps this depends on the register window.
115 __fenv_static inline int
116 feraiseexcept(int __excepts)
121 * With a compiler that supports the FENV_ACCESS pragma
122 * properly, simple expressions like '0.0 / 0.0' should
123 * be sufficient to generate traps. Unfortunately, we
124 * need to bring a volatile variable into the equation
125 * to prevent incorrect optimizations.
127 if (__excepts & FE_INVALID) {
131 if (__excepts & FE_DIVBYZERO) {
135 if (__excepts & FE_OVERFLOW) {
139 if (__excepts & FE_UNDERFLOW) {
143 if (__excepts & FE_INEXACT) {
150 __fenv_static inline int
151 fetestexcept(int __excepts)
156 return (__r & __excepts);
159 __fenv_static inline int
165 return ((__r >> _ROUND_SHIFT) & _ROUND_MASK);
168 __fenv_static inline int
169 fesetround(int __round)
173 if (__round & ~_ROUND_MASK)
176 __r &= ~(_ROUND_MASK << _ROUND_SHIFT);
177 __r |= __round << _ROUND_SHIFT;
182 __fenv_static inline int
183 fegetenv(fenv_t *__envp)
190 __fenv_static inline int
191 feholdexcept(fenv_t *__envp)
197 __r &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
202 __fenv_static inline int
203 fesetenv(const fenv_t *__envp)
210 __fenv_static inline int
211 feupdateenv(const fenv_t *__envp)
217 feraiseexcept(__r & FE_ALL_EXCEPT);
223 /* We currently provide no external definitions of the functions below. */
226 feenableexcept(int __mask)
228 fenv_t __old_r, __new_r;
231 __new_r = __old_r | ((__mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
233 return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
237 fedisableexcept(int __mask)
239 fenv_t __old_r, __new_r;
242 __new_r = __old_r & ~((__mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
244 return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
253 return ((__r & _ENABLE_MASK) >> _FPUSW_SHIFT);
256 #endif /* __BSD_VISIBLE */
260 #endif /* !_FENV_H_ */