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 __uint16_t fexcept_t;
42 #define FE_INVALID 0x01
43 #define FE_DENORMAL 0x02
44 #define FE_DIVBYZERO 0x04
45 #define FE_OVERFLOW 0x08
46 #define FE_UNDERFLOW 0x10
47 #define FE_INEXACT 0x20
48 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
49 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
52 #define FE_TONEAREST 0x0000
53 #define FE_DOWNWARD 0x0400
54 #define FE_UPWARD 0x0800
55 #define FE_TOWARDZERO 0x0c00
56 #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
57 FE_UPWARD | FE_TOWARDZERO)
61 /* Default floating-point environment */
62 extern const fenv_t __fe_dfl_env;
63 #define FE_DFL_ENV (&__fe_dfl_env)
65 #define _FPUSW_SHIFT 13
67 #define __stfpsr(__r) __asm __volatile("mov %0=ar.fpsr" : "=r" (*(__r)))
68 #define __ldfpsr(__r) __asm __volatile("mov ar.fpsr=%0;;" : : "r" (__r))
70 __fenv_static inline int
71 feclearexcept(int __excepts)
76 __fpsr &= ~((fenv_t)__excepts << _FPUSW_SHIFT);
81 __fenv_static inline int
82 fegetexceptflag(fexcept_t *__flagp, int __excepts)
87 *__flagp = (fexcept_t)(__fpsr >> _FPUSW_SHIFT) & __excepts;
91 __fenv_static inline int
92 fesetexceptflag(const fexcept_t *__flagp, int __excepts)
97 __fpsr &= ~((fenv_t)__excepts << _FPUSW_SHIFT);
98 __fpsr |= (fenv_t)(__excepts & *__flagp) << _FPUSW_SHIFT;
104 * It is worthwhile to use the inline version of this function iff it
105 * is called with arguments that are compile-time constants (due to
106 * dead code elimination). Unfortunately, gcc isn't smart enough to
107 * figure this out automatically, and there's no way to tell it.
108 * We assume that constant arguments will be the common case.
110 __fenv_static inline int
111 feraiseexcept(int __excepts)
116 * With a compiler that supports the FENV_ACCESS pragma
117 * properly, simple expressions like '0.0 / 0.0' should
118 * be sufficient to generate traps. Unfortunately, we
119 * need to bring a volatile variable into the equation
120 * to prevent incorrect optimizations.
122 if (__excepts & FE_INVALID) {
126 if (__excepts & FE_DIVBYZERO) {
130 if (__excepts & FE_OVERFLOW) {
134 if (__excepts & FE_UNDERFLOW) {
138 if (__excepts & FE_INEXACT) {
145 __fenv_static inline int
146 fetestexcept(int __excepts)
151 return ((__fpsr >> _FPUSW_SHIFT) & __excepts);
155 __fenv_static inline int
161 return (__fpsr & _ROUND_MASK);
164 __fenv_static inline int
165 fesetround(int __round)
169 if (__round & ~_ROUND_MASK)
172 __fpsr &= ~_ROUND_MASK;
178 __fenv_static inline int
179 fegetenv(fenv_t *__envp)
186 __fenv_static inline int
187 feholdexcept(fenv_t *__envp)
193 __fpsr &= ~((fenv_t)FE_ALL_EXCEPT << _FPUSW_SHIFT);
194 __fpsr |= FE_ALL_EXCEPT;
199 __fenv_static inline int
200 fesetenv(const fenv_t *__envp)
207 int feupdateenv(const fenv_t *__envp);
211 /* We currently provide no external definitions of the functions below. */
214 feenableexcept(int __mask)
216 fenv_t __newfpsr, __oldfpsr;
218 __stfpsr(&__oldfpsr);
219 __newfpsr = __oldfpsr & ~(__mask & FE_ALL_EXCEPT);
221 return (~__oldfpsr & FE_ALL_EXCEPT);
225 fedisableexcept(int __mask)
227 fenv_t __newfpsr, __oldfpsr;
229 __stfpsr(&__oldfpsr);
230 __newfpsr = __oldfpsr | (__mask & FE_ALL_EXCEPT);
232 return (~__oldfpsr & FE_ALL_EXCEPT);
241 return (~__fpsr & FE_ALL_EXCEPT);
244 #endif /* __BSD_VISIBLE */
248 #endif /* !_FENV_H_ */