1 /*- 2 * Copyright (c) 2004 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$ 27 */ 28 29 #ifndef _FENV_H_ 30 #define _FENV_H_ 31 32 #include <sys/_types.h> 33 34 typedef __uint32_t fenv_t; 35 typedef __uint32_t fexcept_t; 36 37 /* Exception flags */ 38 #define FE_INVALID 0x0001 39 #define FE_DIVBYZERO 0x0002 40 #define FE_OVERFLOW 0x0004 41 #define FE_UNDERFLOW 0x0008 42 #define FE_INEXACT 0x0010 43 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | \ 44 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW) 45 46 __BEGIN_DECLS 47 48 /* Default floating-point environment */ 49 extern const fenv_t __fe_dfl_env; 50 #define FE_DFL_ENV (&__fe_dfl_env) 51 52 /* We need to be able to map status flag positions to mask flag positions */ 53 #define _FPUSW_SHIFT 16 54 #define _ENABLE_MASK (FE_ALL_EXCEPT << _FPUSW_SHIFT) 55 56 #define __rfs(__fpsr) __asm("rfs %0" : "=m" (*(__fpsr))) 57 #define __wfs(__fpsr) __asm __volatile("wfs %0" : : "m" (__fpsr)) 58 59 static __inline int 60 feclearexcept(int __excepts) 61 { 62 fexcept_t __fpsr; 63 64 __rfs(&__fpsr); 65 __fpsr &= ~__excepts; 66 __wfs(__fpsr); 67 return (0); 68 } 69 70 static __inline int 71 fegetexceptflag(fexcept_t *__flagp, int __excepts) 72 { 73 fexcept_t __fpsr; 74 75 __rfs(&__fpsr); 76 *__flagp = __fpsr & __excepts; 77 return (0); 78 } 79 80 static __inline int 81 fesetexceptflag(const fexcept_t *__flagp, int __excepts) 82 { 83 fexcept_t __fpsr; 84 85 __rfs(&__fpsr); 86 __fpsr &= ~__excepts; 87 __fpsr |= *__flagp & __excepts; 88 __wfs(__fpsr); 89 return (0); 90 } 91 92 static __inline int 93 feraiseexcept(int __excepts) 94 { 95 fexcept_t __ex = __excepts; 96 97 fesetexceptflag(&__ex, __excepts); /* XXX */ 98 return (0); 99 } 100 101 static __inline int 102 fetestexcept(int __excepts) 103 { 104 fexcept_t __fpsr; 105 106 __rfs(&__fpsr); 107 return (__fpsr & __excepts); 108 } 109 110 static __inline int 111 fegetround(void) 112 { 113 114 /* 115 * Apparently, the rounding mode is specified as part of the 116 * instruction format on ARM, so the dynamic rounding mode is 117 * indeterminate. Some FPUs may differ. 118 */ 119 return (-1); 120 } 121 122 static __inline int 123 fesetround(int __round) 124 { 125 126 return (-1); 127 } 128 129 static __inline int 130 fegetenv(fenv_t *__envp) 131 { 132 133 __rfs(__envp); 134 return (0); 135 } 136 137 static __inline int 138 feholdexcept(fenv_t *__envp) 139 { 140 fenv_t __env; 141 142 __rfs(&__env); 143 *__envp = __env; 144 __env &= ~(FE_ALL_EXCEPT | _ENABLE_MASK); 145 __wfs(__env); 146 return (0); 147 } 148 149 static __inline int 150 fesetenv(const fenv_t *__envp) 151 { 152 153 __wfs(*__envp); 154 return (0); 155 } 156 157 static __inline int 158 feupdateenv(const fenv_t *__envp) 159 { 160 fexcept_t __fpsr; 161 162 __rfs(&__fpsr); 163 __wfs(*__envp); 164 feraiseexcept(__fpsr & FE_ALL_EXCEPT); 165 return (0); 166 } 167 168 #if __BSD_VISIBLE 169 170 static __inline int 171 fesetmask(int __mask) 172 { 173 fenv_t __fpsr; 174 175 __rfs(&__fpsr); 176 __fpsr &= ~_ENABLE_MASK; 177 __fpsr |= __mask << _FPUSW_SHIFT; 178 __wfs(__fpsr); 179 return (0); 180 } 181 182 static __inline int 183 fegetmask(void) 184 { 185 fenv_t __fpsr; 186 187 __rfs(&__fpsr); 188 return ((__fpsr & _ENABLE_MASK) >> _FPUSW_SHIFT); 189 } 190 191 #endif /* __BSD_VISIBLE */ 192 193 __END_DECLS 194 195 #endif /* !_FENV_H_ */ 196