1 /* $OpenBSD: fenv.c,v 1.4 2014/04/18 15:09:52 guenther Exp $ */ 2 3 /* 4 * Copyright (c) 2011 Martynas Venckus <martynas@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <fenv.h> 20 21 extern unsigned int __fpscr_values[2]; 22 23 /* 24 * The following constant represents the default floating-point environment 25 * (that is, the one installed at program startup) and has type pointer to 26 * const-qualified fenv_t. 27 * 28 * It can be used as an argument to the functions within the <fenv.h> header 29 * that manage the floating-point environment, namely fesetenv() and 30 * feupdateenv(). 31 */ 32 fenv_t __fe_dfl_env = 0xc0000; 33 34 /* 35 * The feclearexcept() function clears the supported floating-point exceptions 36 * represented by `excepts'. 37 */ 38 int 39 feclearexcept(int excepts) 40 { 41 unsigned int fpscr; 42 43 excepts &= FE_ALL_EXCEPT; 44 45 /* Store the current floating-point status and control register */ 46 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 47 48 /* Clear the requested floating-point exceptions */ 49 fpscr &= ~excepts; 50 __fpscr_values[0] &= ~excepts; 51 __fpscr_values[1] &= ~excepts; 52 53 /* Load the floating-point status and control register */ 54 __asm__ volatile ("lds %0, fpscr" : : "r" (fpscr)); 55 56 return (0); 57 } 58 59 /* 60 * The fegetexceptflag() function stores an implementation-defined 61 * representation of the states of the floating-point status flags indicated by 62 * the argument excepts in the object pointed to by the argument flagp. 63 */ 64 int 65 fegetexceptflag(fexcept_t *flagp, int excepts) 66 { 67 unsigned int fpscr; 68 69 excepts &= FE_ALL_EXCEPT; 70 71 /* Store the current floating-point status and control register */ 72 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 73 74 /* Store the results in flagp */ 75 *flagp = fpscr & excepts; 76 77 return (0); 78 } 79 80 /* 81 * The feraiseexcept() function raises the supported floating-point exceptions 82 * represented by the argument `excepts'. 83 */ 84 int 85 feraiseexcept(int excepts) 86 { 87 volatile double d; 88 89 excepts &= FE_ALL_EXCEPT; 90 91 /* 92 * With a compiler that supports the FENV_ACCESS pragma 93 * properly, simple expressions like '0.0 / 0.0' should 94 * be sufficient to generate traps. Unfortunately, we 95 * need to bring a volatile variable into the equation 96 * to prevent incorrect optimizations. 97 */ 98 if (excepts & FE_INVALID) { 99 d = 0.0; 100 d = 0.0 / d; 101 } 102 if (excepts & FE_DIVBYZERO) { 103 d = 0.0; 104 d = 1.0 / d; 105 } 106 if (excepts & FE_OVERFLOW) { 107 d = 0x1.ffp1023; 108 d *= 2.0; 109 } 110 if (excepts & FE_UNDERFLOW) { 111 d = 0x1p-1022; 112 d /= 0x1p1023; 113 } 114 if (excepts & FE_INEXACT) { 115 d = 0x1p-1022; 116 d += 1.0; 117 } 118 119 return (0); 120 } 121 122 /* 123 * This function sets the floating-point status flags indicated by the argument 124 * `excepts' to the states stored in the object pointed to by `flagp'. It does 125 * NOT raise any floating-point exceptions, but only sets the state of the flags. 126 */ 127 int 128 fesetexceptflag(const fexcept_t *flagp, int excepts) 129 { 130 unsigned int fpscr; 131 132 excepts &= FE_ALL_EXCEPT; 133 134 /* Store the current floating-point status and control register */ 135 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 136 137 /* Set the requested status flags */ 138 fpscr &= ~excepts; 139 __fpscr_values[0] &= ~excepts; 140 __fpscr_values[1] &= ~excepts; 141 142 fpscr |= *flagp & excepts; 143 __fpscr_values[0] |= *flagp & excepts; 144 __fpscr_values[1] |= *flagp & excepts; 145 146 /* Load the floating-point status and control register */ 147 __asm__ volatile ("lds %0, fpscr" : : "r" (fpscr)); 148 149 return (0); 150 } 151 152 /* 153 * The fetestexcept() function determines which of a specified subset of the 154 * floating-point exception flags are currently set. The `excepts' argument 155 * specifies the floating-point status flags to be queried. 156 */ 157 int 158 fetestexcept(int excepts) 159 { 160 unsigned int fpscr; 161 162 excepts &= FE_ALL_EXCEPT; 163 164 /* Store the current floating-point status and control register */ 165 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 166 167 return (fpscr & excepts); 168 } 169 170 /* 171 * The fegetround() function gets the current rounding direction. 172 */ 173 int 174 fegetround(void) 175 { 176 unsigned int fpscr; 177 178 /* Store the current floating-point status and control register */ 179 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 180 181 return (fpscr & _ROUND_MASK); 182 } 183 184 /* 185 * The fesetround() function establishes the rounding direction represented by 186 * its argument `round'. If the argument is not equal to the value of a rounding 187 * direction macro, the rounding direction is not changed. 188 */ 189 int 190 fesetround(int round) 191 { 192 unsigned int fpscr; 193 194 /* Check whether requested rounding direction is supported */ 195 if (round & ~_ROUND_MASK) 196 return (-1); 197 198 /* Store the current floating-point status and control register */ 199 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 200 201 /* Set the rounding direction */ 202 fpscr &= ~_ROUND_MASK; 203 __fpscr_values[0] &= ~_ROUND_MASK; 204 __fpscr_values[1] &= ~_ROUND_MASK; 205 206 fpscr |= round; 207 __fpscr_values[0] |= round; 208 __fpscr_values[1] |= round; 209 210 /* Load the floating-point status and control register */ 211 __asm__ volatile ("lds %0, fpscr" : : "r" (fpscr)); 212 213 return (0); 214 } 215 216 /* 217 * The fegetenv() function attempts to store the current floating-point 218 * environment in the object pointed to by envp. 219 */ 220 int 221 fegetenv(fenv_t *envp) 222 { 223 /* Store the current floating-point status and control register */ 224 __asm__ volatile ("sts fpscr, %0" : "=r" (*envp)); 225 226 return (0); 227 } 228 229 /* 230 * The feholdexcept() function saves the current floating-point environment 231 * in the object pointed to by envp, clears the floating-point status flags, and 232 * then installs a non-stop (continue on floating-point exceptions) mode, if 233 * available, for all floating-point exceptions. 234 */ 235 int 236 feholdexcept(fenv_t *envp) 237 { 238 unsigned int fpscr; 239 240 /* Store the current floating-point status and control register */ 241 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 242 243 *envp = fpscr; 244 245 /* Clear exception flags in FPSCR */ 246 fpscr &= ~FE_ALL_EXCEPT; 247 __fpscr_values[0] &= ~FE_ALL_EXCEPT; 248 __fpscr_values[1] &= ~FE_ALL_EXCEPT; 249 250 /* Mask all exceptions */ 251 fpscr &= ~(FE_ALL_EXCEPT << _MASK_SHIFT); 252 __fpscr_values[0] &= ~(FE_ALL_EXCEPT << _MASK_SHIFT); 253 __fpscr_values[1] &= ~(FE_ALL_EXCEPT << _MASK_SHIFT); 254 255 /* Load the floating-point status and control register */ 256 __asm__ volatile ("lds %0, fpscr" : : "r" (fpscr)); 257 258 return (0); 259 } 260 261 /* 262 * The fesetenv() function attempts to establish the floating-point environment 263 * represented by the object pointed to by envp. The argument `envp' points 264 * to an object set by a call to fegetenv() or feholdexcept(), or equal a 265 * floating-point environment macro. The fesetenv() function does not raise 266 * floating-point exceptions, but only installs the state of the floating-point 267 * status flags represented through its argument. 268 */ 269 int 270 fesetenv(const fenv_t *envp) 271 { 272 unsigned int fpscr; 273 274 /* Store the current floating-point status and control register */ 275 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 276 277 /* Set the requested flags */ 278 fpscr &= ~((FE_ALL_EXCEPT << _MASK_SHIFT) | _ROUND_MASK | 279 FE_ALL_EXCEPT); 280 __fpscr_values[0] &= ~((FE_ALL_EXCEPT << _MASK_SHIFT) | _ROUND_MASK | 281 FE_ALL_EXCEPT); 282 __fpscr_values[1] &= ~((FE_ALL_EXCEPT << _MASK_SHIFT) | _ROUND_MASK | 283 FE_ALL_EXCEPT); 284 285 fpscr |= *envp & ((FE_ALL_EXCEPT << _MASK_SHIFT) | _ROUND_MASK | 286 FE_ALL_EXCEPT); 287 __fpscr_values[0] |= *envp & ((FE_ALL_EXCEPT << _MASK_SHIFT) | 288 _ROUND_MASK | FE_ALL_EXCEPT); 289 __fpscr_values[1] |= *envp & ((FE_ALL_EXCEPT << _MASK_SHIFT) | 290 _ROUND_MASK | FE_ALL_EXCEPT); 291 292 /* Load the floating-point status and control register */ 293 __asm__ volatile ("lds %0, fpscr" : : "r" (fpscr)); 294 295 return (0); 296 } 297 298 /* 299 * The feupdateenv() function saves the currently raised floating-point 300 * exceptions in its automatic storage, installs the floating-point environment 301 * represented by the object pointed to by `envp', and then raises the saved 302 * floating-point exceptions. The argument `envp' shall point to an object set 303 * by a call to feholdexcept() or fegetenv(), or equal a floating-point 304 * environment macro. 305 */ 306 int 307 feupdateenv(const fenv_t *envp) 308 { 309 unsigned int fpscr; 310 311 /* Store the current floating-point status and control register */ 312 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 313 314 /* Install new floating-point environment */ 315 fesetenv(envp); 316 317 /* Raise any previously accumulated exceptions */ 318 feraiseexcept(fpscr); 319 320 return (0); 321 } 322 323 /* 324 * The following functions are extentions to the standard 325 */ 326 int 327 feenableexcept(int mask) 328 { 329 unsigned int fpscr, omask; 330 331 mask &= FE_ALL_EXCEPT; 332 333 /* Store the current floating-point status and control register */ 334 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 335 336 omask = (fpscr >> _MASK_SHIFT) & FE_ALL_EXCEPT; 337 fpscr |= mask << _MASK_SHIFT; 338 __fpscr_values[0] |= mask << _MASK_SHIFT; 339 __fpscr_values[1] |= mask << _MASK_SHIFT; 340 341 /* Load the floating-point status and control register */ 342 __asm__ volatile ("lds %0, fpscr" : : "r" (fpscr)); 343 344 return (omask); 345 346 } 347 348 int 349 fedisableexcept(int mask) 350 { 351 unsigned int fpscr, omask; 352 353 mask &= FE_ALL_EXCEPT; 354 355 /* Store the current floating-point status and control register */ 356 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 357 358 omask = (fpscr >> _MASK_SHIFT) & FE_ALL_EXCEPT; 359 fpscr &= ~(mask << _MASK_SHIFT); 360 __fpscr_values[0] &= ~(mask << _MASK_SHIFT); 361 __fpscr_values[1] &= ~(mask << _MASK_SHIFT); 362 363 /* Load the floating-point status and control register */ 364 __asm__ volatile ("lds %0, fpscr" : : "r" (fpscr)); 365 366 return (omask); 367 } 368 369 int 370 fegetexcept(void) 371 { 372 unsigned int fpscr; 373 374 /* Store the current floating-point status and control register */ 375 __asm__ volatile ("sts fpscr, %0" : "=r" (fpscr)); 376 377 return ((fpscr >> _MASK_SHIFT) & FE_ALL_EXCEPT); 378 } 379