|
| 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$ |
| 27 | + */ |
| 28 | + |
| 29 | +#ifndef _FENV_H_ |
| 30 | +#define _FENV_H_ |
| 31 | + |
| 32 | +#include <stdint.h> |
| 33 | +#include "cdefs-compat.h" |
| 34 | + |
| 35 | +#ifndef __fenv_static |
| 36 | +#define __fenv_static static |
| 37 | +#endif |
| 38 | + |
| 39 | +typedef uint32_t fenv_t; |
| 40 | +typedef uint32_t fexcept_t; |
| 41 | + |
| 42 | +/* Exception flags */ |
| 43 | +#ifdef __mips_soft_float |
| 44 | +#define _FPUSW_SHIFT 16 |
| 45 | +#define FE_INVALID 0x0001 |
| 46 | +#define FE_DIVBYZERO 0x0002 |
| 47 | +#define FE_OVERFLOW 0x0004 |
| 48 | +#define FE_UNDERFLOW 0x0008 |
| 49 | +#define FE_INEXACT 0x0010 |
| 50 | +#else |
| 51 | +#define _FCSR_CAUSE_SHIFT 10 |
| 52 | +#define FE_INVALID 0x0040 |
| 53 | +#define FE_DIVBYZERO 0x0020 |
| 54 | +#define FE_OVERFLOW 0x0010 |
| 55 | +#define FE_UNDERFLOW 0x0008 |
| 56 | +#define FE_INEXACT 0x0004 |
| 57 | +#endif |
| 58 | +#define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | \ |
| 59 | + FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW) |
| 60 | + |
| 61 | +/* Rounding modes */ |
| 62 | +#define FE_TONEAREST 0x0000 |
| 63 | +#define FE_TOWARDZERO 0x0001 |
| 64 | +#define FE_UPWARD 0x0002 |
| 65 | +#define FE_DOWNWARD 0x0003 |
| 66 | +#define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \ |
| 67 | + FE_UPWARD | FE_TOWARDZERO) |
| 68 | +__BEGIN_DECLS |
| 69 | + |
| 70 | +/* Default floating-point environment */ |
| 71 | +extern const fenv_t __fe_dfl_env; |
| 72 | +#define FE_DFL_ENV (&__fe_dfl_env) |
| 73 | + |
| 74 | +/* We need to be able to map status flag positions to mask flag positions */ |
| 75 | +#define _ENABLE_SHIFT 5 |
| 76 | +#define _ENABLE_MASK (FE_ALL_EXCEPT << _ENABLE_SHIFT) |
| 77 | + |
| 78 | +#ifndef __mips_soft_float |
| 79 | +#define __cfc1(__fcsr) __asm __volatile("cfc1 %0, $31" : "=r" (__fcsr)) |
| 80 | +#define __ctc1(__fcsr) __asm __volatile("ctc1 %0, $31" :: "r" (__fcsr)) |
| 81 | +#endif |
| 82 | + |
| 83 | +#ifdef __mips_soft_float |
| 84 | +int feclearexcept(int __excepts); |
| 85 | +int fegetexceptflag(fexcept_t *__flagp, int __excepts); |
| 86 | +int fesetexceptflag(const fexcept_t *__flagp, int __excepts); |
| 87 | +int feraiseexcept(int __excepts); |
| 88 | +int fetestexcept(int __excepts); |
| 89 | +int fegetround(void); |
| 90 | +int fesetround(int __round); |
| 91 | +int fegetenv(fenv_t *__envp); |
| 92 | +int feholdexcept(fenv_t *__envp); |
| 93 | +int fesetenv(const fenv_t *__envp); |
| 94 | +int feupdateenv(const fenv_t *__envp); |
| 95 | +#else |
| 96 | +__fenv_static inline int |
| 97 | +feclearexcept(int __excepts) |
| 98 | +{ |
| 99 | + fexcept_t fcsr; |
| 100 | + |
| 101 | + __excepts &= FE_ALL_EXCEPT; |
| 102 | + __cfc1(fcsr); |
| 103 | + fcsr &= ~(__excepts | (__excepts << _FCSR_CAUSE_SHIFT)); |
| 104 | + __ctc1(fcsr); |
| 105 | + |
| 106 | + return (0); |
| 107 | +} |
| 108 | + |
| 109 | +__fenv_static inline int |
| 110 | +fegetexceptflag(fexcept_t *__flagp, int __excepts) |
| 111 | +{ |
| 112 | + fexcept_t fcsr; |
| 113 | + |
| 114 | + __excepts &= FE_ALL_EXCEPT; |
| 115 | + __cfc1(fcsr); |
| 116 | + *__flagp = fcsr & __excepts; |
| 117 | + |
| 118 | + return (0); |
| 119 | +} |
| 120 | + |
| 121 | +__fenv_static inline int |
| 122 | +fesetexceptflag(const fexcept_t *__flagp, int __excepts) |
| 123 | +{ |
| 124 | + fexcept_t fcsr; |
| 125 | + |
| 126 | + __excepts &= FE_ALL_EXCEPT; |
| 127 | + __cfc1(fcsr); |
| 128 | + fcsr &= ~__excepts; |
| 129 | + fcsr |= *__flagp & __excepts; |
| 130 | + __ctc1(fcsr); |
| 131 | + |
| 132 | + return (0); |
| 133 | +} |
| 134 | + |
| 135 | +__fenv_static inline int |
| 136 | +feraiseexcept(int __excepts) |
| 137 | +{ |
| 138 | + fexcept_t fcsr; |
| 139 | + |
| 140 | + __excepts &= FE_ALL_EXCEPT; |
| 141 | + __cfc1(fcsr); |
| 142 | + fcsr |= __excepts | (__excepts << _FCSR_CAUSE_SHIFT); |
| 143 | + __ctc1(fcsr); |
| 144 | + |
| 145 | + return (0); |
| 146 | +} |
| 147 | + |
| 148 | +__fenv_static inline int |
| 149 | +fetestexcept(int __excepts) |
| 150 | +{ |
| 151 | + fexcept_t fcsr; |
| 152 | + |
| 153 | + __excepts &= FE_ALL_EXCEPT; |
| 154 | + __cfc1(fcsr); |
| 155 | + |
| 156 | + return (fcsr & __excepts); |
| 157 | +} |
| 158 | + |
| 159 | +__fenv_static inline int |
| 160 | +fegetround(void) |
| 161 | +{ |
| 162 | + fexcept_t fcsr; |
| 163 | + |
| 164 | + __cfc1(fcsr); |
| 165 | + |
| 166 | + return (fcsr & _ROUND_MASK); |
| 167 | +} |
| 168 | + |
| 169 | +__fenv_static inline int |
| 170 | +fesetround(int __round) |
| 171 | +{ |
| 172 | + fexcept_t fcsr; |
| 173 | + |
| 174 | + if (__round & ~_ROUND_MASK) |
| 175 | + return (-1); |
| 176 | + |
| 177 | + __cfc1(fcsr); |
| 178 | + fcsr &= ~_ROUND_MASK; |
| 179 | + fcsr |= __round; |
| 180 | + __ctc1(fcsr); |
| 181 | + |
| 182 | + return (0); |
| 183 | +} |
| 184 | + |
| 185 | +__fenv_static inline int |
| 186 | +fegetenv(fenv_t *__envp) |
| 187 | +{ |
| 188 | + |
| 189 | + __cfc1(*__envp); |
| 190 | + |
| 191 | + return (0); |
| 192 | +} |
| 193 | + |
| 194 | +__fenv_static inline int |
| 195 | +feholdexcept(fenv_t *__envp) |
| 196 | +{ |
| 197 | + fexcept_t fcsr; |
| 198 | + |
| 199 | + __cfc1(fcsr); |
| 200 | + *__envp = fcsr; |
| 201 | + fcsr &= ~(FE_ALL_EXCEPT | _ENABLE_MASK); |
| 202 | + __ctc1(fcsr); |
| 203 | + |
| 204 | + return (0); |
| 205 | +} |
| 206 | + |
| 207 | +__fenv_static inline int |
| 208 | +fesetenv(const fenv_t *__envp) |
| 209 | +{ |
| 210 | + |
| 211 | + __ctc1(*__envp); |
| 212 | + |
| 213 | + return (0); |
| 214 | +} |
| 215 | + |
| 216 | +__fenv_static inline int |
| 217 | +feupdateenv(const fenv_t *__envp) |
| 218 | +{ |
| 219 | + fexcept_t fcsr; |
| 220 | + |
| 221 | + __cfc1(fcsr); |
| 222 | + fesetenv(__envp); |
| 223 | + feraiseexcept(fcsr); |
| 224 | + |
| 225 | + return (0); |
| 226 | +} |
| 227 | +#endif /* !__mips_soft_float */ |
| 228 | + |
| 229 | +#if __BSD_VISIBLE |
| 230 | + |
| 231 | +/* We currently provide no external definitions of the functions below. */ |
| 232 | + |
| 233 | +#ifdef __mips_soft_float |
| 234 | +int feenableexcept(int __mask); |
| 235 | +int fedisableexcept(int __mask); |
| 236 | +int fegetexcept(void); |
| 237 | +#else |
| 238 | +static inline int |
| 239 | +feenableexcept(int __mask) |
| 240 | +{ |
| 241 | + fenv_t __old_fcsr, __new_fcsr; |
| 242 | + |
| 243 | + __cfc1(__old_fcsr); |
| 244 | + __new_fcsr = __old_fcsr | (__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT; |
| 245 | + __ctc1(__new_fcsr); |
| 246 | + |
| 247 | + return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT); |
| 248 | +} |
| 249 | + |
| 250 | +static inline int |
| 251 | +fedisableexcept(int __mask) |
| 252 | +{ |
| 253 | + fenv_t __old_fcsr, __new_fcsr; |
| 254 | + |
| 255 | + __cfc1(__old_fcsr); |
| 256 | + __new_fcsr = __old_fcsr & ~((__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT); |
| 257 | + __ctc1(__new_fcsr); |
| 258 | + |
| 259 | + return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT); |
| 260 | +} |
| 261 | + |
| 262 | +static inline int |
| 263 | +fegetexcept(void) |
| 264 | +{ |
| 265 | + fexcept_t fcsr; |
| 266 | + |
| 267 | + __cfc1(fcsr); |
| 268 | + |
| 269 | + return ((fcsr & _ENABLE_MASK) >> _ENABLE_SHIFT); |
| 270 | +} |
| 271 | + |
| 272 | +#endif /* !__mips_soft_float */ |
| 273 | + |
| 274 | +#endif /* __BSD_VISIBLE */ |
| 275 | + |
| 276 | +__END_DECLS |
| 277 | + |
| 278 | +#endif /* !_FENV_H_ */ |
0 commit comments