1/*- 2 * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: releng/10.1/lib/msun/ia64/fenv.h 226218 2011-10-10 15:43:09Z das $ 27 */ 28 29#ifndef _IA64_FENV_H_ 30#define _IA64_FENV_H_ 31 32#include <sys/stdint.h> 33 34#ifndef __fenv_static 35#define __fenv_static static 36#endif 37 38typedef __uint64_t fenv_t; 39typedef __uint16_t fexcept_t; 40 41/* Exception flags */ 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) 50 51/* Rounding modes */ 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) 58 59__BEGIN_DECLS 60 61/* Default floating-point environment */ 62extern const fenv_t __fe_dfl_env; 63#define FE_DFL_ENV (&__fe_dfl_env) 64 65#define _FPUSW_SHIFT 13 66 67#define __stfpsr(__r) __asm __volatile("mov %0=ar.fpsr" : "=r" (*(__r))) 68#define __ldfpsr(__r) __asm __volatile("mov ar.fpsr=%0;;" : : "r" (__r)) 69 70#if __GNUC_PREREQ__(8, 0) 71#pragma GCC diagnostic push 72#pragma GCC diagnostic ignored "-Wshadow" 73#endif 74 75__fenv_static inline int 76feclearexcept(int __excepts) 77{ 78 fenv_t __fpsr; 79 80 __stfpsr(&__fpsr); 81 __fpsr &= ~((fenv_t)__excepts << _FPUSW_SHIFT); 82 __ldfpsr(__fpsr); 83 return (0); 84} 85 86__fenv_static inline int 87fegetexceptflag(fexcept_t *__flagp, int __excepts) 88{ 89 fenv_t __fpsr; 90 91 __stfpsr(&__fpsr); 92 *__flagp = (fexcept_t)(__fpsr >> _FPUSW_SHIFT) & __excepts; 93 return (0); 94} 95 96__fenv_static inline int 97fesetexceptflag(const fexcept_t *__flagp, int __excepts) 98{ 99 fenv_t __fpsr; 100 101 __stfpsr(&__fpsr); 102 __fpsr &= ~((fenv_t)__excepts << _FPUSW_SHIFT); 103 __fpsr |= (fenv_t)(__excepts & *__flagp) << _FPUSW_SHIFT; 104 __ldfpsr(__fpsr); 105 return (0); 106} 107 108/* 109 * It is worthwhile to use the inline version of this function iff it 110 * is called with arguments that are compile-time constants (due to 111 * dead code elimination). Unfortunately, gcc isn't smart enough to 112 * figure this out automatically, and there's no way to tell it. 113 * We assume that constant arguments will be the common case. 114 */ 115__fenv_static inline int 116feraiseexcept(int __excepts) 117{ 118 volatile double d; 119 120 /* 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. 126 */ 127 if (__excepts & FE_INVALID) { 128 d = 0.0; 129 d = 0.0 / d; 130 } 131 if (__excepts & FE_DIVBYZERO) { 132 d = 0.0; 133 d = 1.0 / d; 134 } 135 if (__excepts & FE_OVERFLOW) { 136 d = 0x1.ffp1023; 137 d *= 2.0; 138 } 139 if (__excepts & FE_UNDERFLOW) { 140 d = 0x1p-1022; 141 d /= 0x1p1023; 142 } 143 if (__excepts & FE_INEXACT) { 144 d = 0x1p-1022; 145 d += 1.0; 146 } 147 return (0); 148} 149 150__fenv_static inline int 151fetestexcept(int __excepts) 152{ 153 fenv_t __fpsr; 154 155 __stfpsr(&__fpsr); 156 return ((__fpsr >> _FPUSW_SHIFT) & __excepts); 157} 158 159 160__fenv_static inline int 161fegetround(void) 162{ 163 fenv_t __fpsr; 164 165 __stfpsr(&__fpsr); 166 return (__fpsr & _ROUND_MASK); 167} 168 169__fenv_static inline int 170fesetround(int __round) 171{ 172 fenv_t __fpsr; 173 174 if (__round & ~_ROUND_MASK) 175 return (-1); 176 __stfpsr(&__fpsr); 177 __fpsr &= ~_ROUND_MASK; 178 __fpsr |= __round; 179 __ldfpsr(__fpsr); 180 return (0); 181} 182 183__fenv_static inline int 184fegetenv(fenv_t *__envp) 185{ 186 187 __stfpsr(__envp); 188 return (0); 189} 190 191__fenv_static inline int 192feholdexcept(fenv_t *__envp) 193{ 194 fenv_t __fpsr; 195 196 __stfpsr(&__fpsr); 197 *__envp = __fpsr; 198 __fpsr &= ~((fenv_t)FE_ALL_EXCEPT << _FPUSW_SHIFT); 199 __fpsr |= FE_ALL_EXCEPT; 200 __ldfpsr(__fpsr); 201 return (0); 202} 203 204__fenv_static inline int 205fesetenv(const fenv_t *__envp) 206{ 207 208 __ldfpsr(*__envp); 209 return (0); 210} 211 212int feupdateenv(const fenv_t *__envp); 213 214#if __GNUC_PREREQ__(8, 0) 215#pragma GCC diagnostic pop 216#endif 217 218#if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE) 219 220__fenv_static inline int 221feenableexcept(int __mask) 222{ 223 fenv_t __newfpsr, __oldfpsr; 224 225 __stfpsr(&__oldfpsr); 226 __newfpsr = __oldfpsr & ~(__mask & FE_ALL_EXCEPT); 227 __ldfpsr(__newfpsr); 228 return (~__oldfpsr & FE_ALL_EXCEPT); 229} 230 231__fenv_static inline int 232fedisableexcept(int __mask) 233{ 234 fenv_t __newfpsr, __oldfpsr; 235 236 __stfpsr(&__oldfpsr); 237 __newfpsr = __oldfpsr | (__mask & FE_ALL_EXCEPT); 238 __ldfpsr(__newfpsr); 239 return (~__oldfpsr & FE_ALL_EXCEPT); 240} 241 242__fenv_static inline int 243fegetexcept(void) 244{ 245 fenv_t __fpsr; 246 247 __stfpsr(&__fpsr); 248 return (~__fpsr & FE_ALL_EXCEPT); 249} 250 251#endif /* _NETBSD_SOURCE || _GNU_SOURCE */ 252 253__END_DECLS 254 255#endif /* !_IA64_FENV_H_ */ 256