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 __uint64_t fexcept_t;
38 #define FE_INVALID 0x00000200
39 #define FE_DIVBYZERO 0x00000040
40 #define FE_OVERFLOW 0x00000100
41 #define FE_UNDERFLOW 0x00000080
42 #define FE_INEXACT 0x00000020
43 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | \
44 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
49 * We can't just use the hardware bit values here, because that would
50 * make FE_UPWARD and FE_DOWNWARD negative, which is not allowed.
52 #define FE_TONEAREST 0x0
53 #define FE_TOWARDZERO 0x1
55 #define FE_DOWNWARD 0x3
56 #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
57 FE_UPWARD | FE_TOWARDZERO)
58 #define _ROUND_SHIFT 30
62 /* Default floating-point environment */
63 extern const fenv_t __fe_dfl_env;
64 #define FE_DFL_ENV (&__fe_dfl_env)
66 /* We need to be able to map status flag positions to mask flag positions */
67 #define _FPUSW_SHIFT 18
68 #define _ENABLE_MASK (FE_ALL_EXCEPT << _FPUSW_SHIFT)
70 #define __ldxfsr(__r) __asm __volatile("ldx %0, %%fsr" : : "m" (__r))
71 #define __stxfsr(__r) __asm __volatile("stx %%fsr, %0" : "=m" (*(__r)))
74 feclearexcept(int __excepts)
85 fegetexceptflag(fexcept_t *__flagp, int __excepts)
90 *__flagp = __r & __excepts;
95 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
101 __r |= *__flagp & __excepts;
107 * In contrast with the ia64 platform, it seems to be worthwhile to
108 * inline this function on sparc64 even when the arguments are not
109 * compile-time constants. Perhaps this depends on the register window.
112 feraiseexcept(int __excepts)
117 * With a compiler that supports the FENV_ACCESS pragma
118 * properly, simple expressions like '0.0 / 0.0' should
119 * be sufficient to generate traps. Unfortunately, we
120 * need to bring a volatile variable into the equation
121 * to prevent incorrect optimizations.
123 if (__excepts & FE_INVALID) {
127 if (__excepts & FE_DIVBYZERO) {
131 if (__excepts & FE_OVERFLOW) {
135 if (__excepts & FE_UNDERFLOW) {
139 if (__excepts & FE_INEXACT) {
147 fetestexcept(int __excepts)
152 return (__r & __excepts);
161 return ((__r >> _ROUND_SHIFT) & _ROUND_MASK);
165 fesetround(int __round)
169 if (__round & ~_ROUND_MASK)
172 __r &= ~(_ROUND_MASK << _ROUND_SHIFT);
173 __r |= __round << _ROUND_SHIFT;
179 fegetenv(fenv_t *__envp)
187 feholdexcept(fenv_t *__envp)
193 __r &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
199 fesetenv(const fenv_t *__envp)
207 feupdateenv(const fenv_t *__envp)
213 feraiseexcept(__r & FE_ALL_EXCEPT);
220 feenableexcept(int __mask)
222 fenv_t __old_r, __new_r;
225 __new_r = __old_r | ((__mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
227 return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
231 fedisableexcept(int __mask)
233 fenv_t __old_r, __new_r;
236 __new_r = __old_r & ~((__mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
238 return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
247 return ((__r & _ENABLE_MASK) >> _FPUSW_SHIFT);
250 #endif /* __BSD_VISIBLE */
254 #endif /* !_FENV_H_ */