1 /* $NetBSD: fenv.h,v 1.4 2024/10/30 15:56:10 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD: src/lib/msun/alpha/fenv.h,v 1.3 2005/03/16 19:03:44 das Exp $ 29 */ 30 31 #ifndef _ALPHA_FENV_H_ 32 #define _ALPHA_FENV_H_ 33 34 #include <sys/featuretest.h> 35 #include <sys/stdint.h> 36 37 typedef __uint64_t fenv_t; 38 typedef __uint16_t fexcept_t; 39 40 /* Exception flags */ 41 #define FE_INVALID 0x01 42 #define FE_DIVBYZERO 0x02 43 #define FE_OVERFLOW 0x04 44 #define FE_UNDERFLOW 0x08 45 #define FE_INEXACT 0x10 46 #define FE_INTOVF 0x20 /* not maskable */ 47 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INTOVF | \ 48 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW) 49 50 /* Rounding modes */ 51 #define FE_TOWARDZERO 0x00 52 #define FE_DOWNWARD 0x01 53 #define FE_TONEAREST 0x02 54 #define FE_UPWARD 0x03 55 #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \ 56 FE_UPWARD | FE_TOWARDZERO) 57 #define _ROUND_SHIFT 58 58 59 #define _FPUSW_SHIFT 52 60 61 #define __excb() __asm __volatile("excb") 62 #define __mf_fpcr(__cw) __asm __volatile("mf_fpcr %0" : "=f" (*(__cw))) 63 #define __mt_fpcr(__cw) __asm __volatile("mt_fpcr %0" : : "f" (__cw)) 64 65 union __fpcr { 66 double __d; 67 fenv_t __bits; 68 }; 69 70 __BEGIN_DECLS 71 72 /* Default floating-point environment */ 73 extern const fenv_t __fe_dfl_env; 74 #define FE_DFL_ENV (&__fe_dfl_env) 75 76 #if __GNUC_PREREQ__(8, 0) 77 #pragma GCC diagnostic push 78 #pragma GCC diagnostic ignored "-Wshadow" 79 #endif 80 81 static __inline int 82 feclearexcept(int __excepts) 83 { 84 union __fpcr __r; 85 86 __excb(); 87 __mf_fpcr(&__r.__d); 88 __r.__bits &= ~((fenv_t)__excepts << _FPUSW_SHIFT); 89 __mt_fpcr(__r.__d); 90 __excb(); 91 return 0; 92 } 93 94 static __inline int 95 fegetexceptflag(fexcept_t *__flagp, int __excepts) 96 { 97 union __fpcr __r; 98 99 __excb(); 100 __mf_fpcr(&__r.__d); 101 __excb(); 102 *__flagp = (__r.__bits >> _FPUSW_SHIFT) & __excepts; 103 return 0; 104 } 105 106 static __inline int 107 fesetexceptflag(const fexcept_t *__flagp, int __excepts) 108 { 109 union __fpcr __r; 110 fenv_t __xflag, __xexcepts; 111 112 __xflag = (fenv_t)*__flagp << _FPUSW_SHIFT; 113 __xexcepts = (fenv_t)__excepts << _FPUSW_SHIFT; 114 __excb(); 115 __mf_fpcr(&__r.__d); 116 __r.__bits &= ~__xexcepts; 117 __r.__bits |= __xflag & __xexcepts; 118 __mt_fpcr(__r.__d); 119 __excb(); 120 return 0; 121 } 122 123 static __inline int 124 feraiseexcept(int __excepts) 125 { 126 127 /* 128 * XXX Generating exceptions this way does not actually invoke 129 * a userland trap handler when enabled, but neither do 130 * arithmetic operations as far as I can tell. Perhaps there 131 * are more bugs in the kernel trap handler. 132 */ 133 fexcept_t __ex = __excepts; 134 fesetexceptflag(&__ex, __excepts); 135 return 0; 136 } 137 138 static __inline int 139 fetestexcept(int __excepts) 140 { 141 union __fpcr __r; 142 143 __excb(); 144 __mf_fpcr(&__r.__d); 145 __excb(); 146 return (__r.__bits >> _FPUSW_SHIFT) & __excepts; 147 } 148 149 static __inline int 150 fegetround(void) 151 { 152 union __fpcr __r; 153 154 /* 155 * No exception barriers should be required here if we assume 156 * that only fesetround() can change the rounding mode. 157 */ 158 __mf_fpcr(&__r.__d); 159 return (int)(__r.__bits >> _ROUND_SHIFT) & _ROUND_MASK; 160 } 161 162 static __inline int 163 fesetround(int __round) 164 { 165 union __fpcr __r; 166 167 if (__round & ~_ROUND_MASK) 168 return (-1); 169 __excb(); 170 __mf_fpcr(&__r.__d); 171 __r.__bits &= ~((fenv_t)_ROUND_MASK << _ROUND_SHIFT); 172 __r.__bits |= (fenv_t)__round << _ROUND_SHIFT; 173 __mt_fpcr(__r.__d); 174 __excb(); 175 return 0; 176 } 177 178 #if __GNUC_PREREQ__(8, 0) 179 #pragma GCC diagnostic pop 180 #endif 181 182 int fegetenv(fenv_t *); 183 int feholdexcept(fenv_t *); 184 int fesetenv(const fenv_t *); 185 int feupdateenv(const fenv_t *); 186 187 #if defined(_NETBSD_SOURCE) || defined(_GNU_SOURCE) 188 int feenableexcept(int); 189 int fedisableexcept(int); 190 int fegetexcept(void); 191 #endif /* _NETBSD_SOURCE || _GNU_SOURCE */ 192 193 194 __END_DECLS 195 196 #endif /* !_ALPHA_FENV_H_ */ 197