2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/_types.h>
37 #define __fenv_static static
40 typedef __uint64_t fenv_t;
41 typedef __uint64_t fexcept_t;
44 #define FE_INVALID 0x00000200
45 #define FE_DIVBYZERO 0x00000040
46 #define FE_OVERFLOW 0x00000100
47 #define FE_UNDERFLOW 0x00000080
48 #define FE_INEXACT 0x00000020
49 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | \
50 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
55 * We can't just use the hardware bit values here, because that would
56 * make FE_UPWARD and FE_DOWNWARD negative, which is not allowed.
58 #define FE_TONEAREST 0x0
59 #define FE_TOWARDZERO 0x1
61 #define FE_DOWNWARD 0x3
62 #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
63 FE_UPWARD | FE_TOWARDZERO)
64 #define _ROUND_SHIFT 30
68 /* Default floating-point environment */
69 extern const fenv_t __fe_dfl_env;
70 #define FE_DFL_ENV (&__fe_dfl_env)
72 /* We need to be able to map status flag positions to mask flag positions */
73 #define _FPUSW_SHIFT 18
74 #define _ENABLE_MASK (FE_ALL_EXCEPT << _FPUSW_SHIFT)
76 #define __ldxfsr(__r) __asm __volatile("ldx %0, %%fsr" : : "m" (__r))
77 #define __stxfsr(__r) __asm __volatile("stx %%fsr, %0" : "=m" (*(__r)))
79 __fenv_static __inline int
80 feclearexcept(int __excepts)
90 __fenv_static inline int
91 fegetexceptflag(fexcept_t *__flagp, int __excepts)
96 *__flagp = __r & __excepts;
100 __fenv_static inline int
101 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
107 __r |= *__flagp & __excepts;
113 * It seems to be worthwhile to inline this function even when the
114 * arguments are not compile-time constants. Perhaps this depends
115 * on the register window.
117 __fenv_static inline int
118 feraiseexcept(int __excepts)
123 * With a compiler that supports the FENV_ACCESS pragma
124 * properly, simple expressions like '0.0 / 0.0' should
125 * be sufficient to generate traps. Unfortunately, we
126 * need to bring a volatile variable into the equation
127 * to prevent incorrect optimizations.
129 if (__excepts & FE_INVALID) {
133 if (__excepts & FE_DIVBYZERO) {
137 if (__excepts & FE_OVERFLOW) {
141 if (__excepts & FE_UNDERFLOW) {
145 if (__excepts & FE_INEXACT) {
152 __fenv_static inline int
153 fetestexcept(int __excepts)
158 return (__r & __excepts);
161 __fenv_static inline int
167 return ((__r >> _ROUND_SHIFT) & _ROUND_MASK);
170 __fenv_static inline int
171 fesetround(int __round)
175 if (__round & ~_ROUND_MASK)
178 __r &= ~(_ROUND_MASK << _ROUND_SHIFT);
179 __r |= __round << _ROUND_SHIFT;
184 __fenv_static inline int
185 fegetenv(fenv_t *__envp)
192 __fenv_static inline int
193 feholdexcept(fenv_t *__envp)
199 __r &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
204 __fenv_static inline int
205 fesetenv(const fenv_t *__envp)
212 __fenv_static inline int
213 feupdateenv(const fenv_t *__envp)
219 feraiseexcept(__r & FE_ALL_EXCEPT);
225 /* We currently provide no external definitions of the functions below. */
228 feenableexcept(int __mask)
230 fenv_t __old_r, __new_r;
233 __new_r = __old_r | ((__mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
235 return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
239 fedisableexcept(int __mask)
241 fenv_t __old_r, __new_r;
244 __new_r = __old_r & ~((__mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
246 return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
255 return ((__r & _ENABLE_MASK) >> _FPUSW_SHIFT);
258 #endif /* __BSD_VISIBLE */
262 #endif /* !_FENV_H_ */