Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 1 | /* $OpenBSD: fenv.c,v 1.3 2012/12/05 23:20:02 deraadt Exp $ */ |
| 2 | /* $NetBSD: fenv.c,v 1.1 2010/07/31 21:47:53 joerg Exp $ */ |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 3 | |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 4 | /*- |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 5 | * Copyright (c) 2004-2005 David Schultz <das (at) FreeBSD.ORG> |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 28 | */ |
| 29 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 30 | #include <fenv.h> |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 31 | #include <machine/fpu.h> |
| 32 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 33 | /* |
| 34 | * The following constant represents the default floating-point environment |
| 35 | * (that is, the one installed at program startup) and has type pointer to |
| 36 | * const-qualified fenv_t. |
| 37 | * |
| 38 | * It can be used as an argument to the functions within the <fenv.h> header |
| 39 | * that manage the floating-point environment, namely fesetenv() and |
| 40 | * feupdateenv(). |
| 41 | * |
| 42 | * x87 fpu registers are 16bit wide. The upper bits, 31-16, are marked as |
| 43 | * RESERVED. |
| 44 | */ |
Pavel Chupin | 7ba84d3 | 2014-02-28 00:36:10 +0400 | [diff] [blame] | 45 | const fenv_t __fe_dfl_env = { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 46 | { |
| 47 | 0xffff0000 | __INITIAL_NPXCW__, /* Control word register */ |
| 48 | 0xffff0000, /* Status word register */ |
| 49 | 0xffffffff, /* Tag word register */ |
| 50 | { |
| 51 | 0x00000000, |
| 52 | 0x00000000, |
| 53 | 0x00000000, |
| 54 | 0xffff0000 |
| 55 | } |
| 56 | }, |
| 57 | __INITIAL_MXCSR__ /* MXCSR register */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 58 | }; |
| 59 | |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 60 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 61 | /* |
| 62 | * The feclearexcept() function clears the supported floating-point exceptions |
| 63 | * represented by `excepts'. |
| 64 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 65 | int |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 66 | feclearexcept(int excepts) |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 67 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 68 | fenv_t fenv; |
| 69 | unsigned int mxcsr; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 70 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 71 | excepts &= FE_ALL_EXCEPT; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 72 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 73 | /* Store the current x87 floating-point environment */ |
| 74 | __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 75 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 76 | /* Clear the requested floating-point exceptions */ |
| 77 | fenv.__x87.__status &= ~excepts; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 78 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 79 | /* Load the x87 floating-point environent */ |
| 80 | __asm__ __volatile__ ("fldenv %0" : : "m" (fenv)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 81 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 82 | /* Same for SSE environment */ |
| 83 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
| 84 | mxcsr &= ~excepts; |
| 85 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 86 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 87 | return (0); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 88 | } |
| 89 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 90 | /* |
| 91 | * The fegetexceptflag() function stores an implementation-defined |
| 92 | * representation of the states of the floating-point status flags indicated by |
| 93 | * the argument excepts in the object pointed to by the argument flagp. |
| 94 | */ |
| 95 | int |
| 96 | fegetexceptflag(fexcept_t *flagp, int excepts) |
| 97 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 98 | unsigned short status; |
| 99 | unsigned int mxcsr; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 100 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 101 | excepts &= FE_ALL_EXCEPT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 102 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 103 | /* Store the current x87 status register */ |
| 104 | __asm__ __volatile__ ("fnstsw %0" : "=am" (status)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 105 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 106 | /* Store the MXCSR register */ |
| 107 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 108 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 109 | /* Store the results in flagp */ |
| 110 | *flagp = (status | mxcsr) & excepts; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 111 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 112 | return (0); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * The feraiseexcept() function raises the supported floating-point exceptions |
| 117 | * represented by the argument `excepts'. |
| 118 | * |
| 119 | * The standard explicitly allows us to execute an instruction that has the |
| 120 | * exception as a side effect, but we choose to manipulate the status register |
| 121 | * directly. |
| 122 | * |
| 123 | * The validation of input is being deferred to fesetexceptflag(). |
| 124 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 125 | int |
| 126 | feraiseexcept(int excepts) |
| 127 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 128 | excepts &= FE_ALL_EXCEPT; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 129 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 130 | fesetexceptflag((fexcept_t *)&excepts, excepts); |
| 131 | __asm__ __volatile__ ("fwait"); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 132 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 133 | return (0); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 134 | } |
| 135 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 136 | /* |
| 137 | * This function sets the floating-point status flags indicated by the argument |
| 138 | * `excepts' to the states stored in the object pointed to by `flagp'. It does |
| 139 | * NOT raise any floating-point exceptions, but only sets the state of the flags. |
| 140 | */ |
| 141 | int |
| 142 | fesetexceptflag(const fexcept_t *flagp, int excepts) |
| 143 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 144 | fenv_t fenv; |
| 145 | unsigned int mxcsr; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 146 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 147 | excepts &= FE_ALL_EXCEPT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 148 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 149 | /* Store the current x87 floating-point environment */ |
| 150 | __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 151 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 152 | /* Set the requested status flags */ |
| 153 | fenv.__x87.__status &= ~excepts; |
| 154 | fenv.__x87.__status |= *flagp & excepts; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 155 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 156 | /* Load the x87 floating-point environent */ |
| 157 | __asm__ __volatile__ ("fldenv %0" : : "m" (fenv)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 158 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 159 | /* Same for SSE environment */ |
| 160 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
| 161 | mxcsr &= ~excepts; |
| 162 | mxcsr |= *flagp & excepts; |
| 163 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 164 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 165 | return (0); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
| 169 | * The fetestexcept() function determines which of a specified subset of the |
| 170 | * floating-point exception flags are currently set. The `excepts' argument |
| 171 | * specifies the floating-point status flags to be queried. |
| 172 | */ |
| 173 | int |
| 174 | fetestexcept(int excepts) |
| 175 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 176 | unsigned short status; |
| 177 | unsigned int mxcsr; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 178 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 179 | excepts &= FE_ALL_EXCEPT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 180 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 181 | /* Store the current x87 status register */ |
| 182 | __asm__ __volatile__ ("fnstsw %0" : "=am" (status)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 183 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 184 | /* Store the MXCSR register state */ |
| 185 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 186 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 187 | return ((status | mxcsr) & excepts); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* |
| 191 | * The fegetround() function gets the current rounding direction. |
| 192 | */ |
| 193 | int |
| 194 | fegetround(void) |
| 195 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 196 | unsigned short control; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 197 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 198 | /* |
| 199 | * We assume that the x87 and the SSE unit agree on the |
| 200 | * rounding mode. Reading the control word on the x87 turns |
| 201 | * out to be about 5 times faster than reading it on the SSE |
| 202 | * unit on an Opteron 244. |
| 203 | */ |
| 204 | __asm__ __volatile__ ("fnstcw %0" : "=m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 205 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 206 | return (control & _X87_ROUND_MASK); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /* |
| 210 | * The fesetround() function establishes the rounding direction represented by |
| 211 | * its argument `round'. If the argument is not equal to the value of a rounding |
| 212 | * direction macro, the rounding direction is not changed. |
| 213 | */ |
| 214 | int |
| 215 | fesetround(int round) |
| 216 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 217 | unsigned short control; |
| 218 | unsigned int mxcsr; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 219 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 220 | /* Check whether requested rounding direction is supported */ |
| 221 | if (round & ~_X87_ROUND_MASK) |
| 222 | return (-1); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 223 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 224 | /* Store the current x87 control word register */ |
| 225 | __asm__ __volatile__ ("fnstcw %0" : "=m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 226 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 227 | /* Set the rounding direction */ |
| 228 | control &= ~_X87_ROUND_MASK; |
| 229 | control |= round; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 230 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 231 | /* Load the x87 control word register */ |
| 232 | __asm__ __volatile__ ("fldcw %0" : : "m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 233 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 234 | /* Same for the SSE environment */ |
| 235 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
| 236 | mxcsr &= ~(_X87_ROUND_MASK << _SSE_ROUND_SHIFT); |
| 237 | mxcsr |= round << _SSE_ROUND_SHIFT; |
| 238 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 239 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 240 | return (0); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
| 244 | * The fegetenv() function attempts to store the current floating-point |
| 245 | * environment in the object pointed to by envp. |
| 246 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 247 | int |
| 248 | fegetenv(fenv_t *envp) |
| 249 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 250 | /* Store the current x87 floating-point environment */ |
| 251 | __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp)); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 252 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 253 | /* Store the MXCSR register state */ |
| 254 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 255 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 256 | /* |
| 257 | * When an FNSTENV instruction is executed, all pending exceptions are |
| 258 | * essentially lost (either the x87 FPU status register is cleared or |
| 259 | * all exceptions are masked). |
| 260 | * |
| 261 | * 8.6 X87 FPU EXCEPTION SYNCHRONIZATION - |
| 262 | * Intel(R) 64 and IA-32 Architectures Softare Developer's Manual - Vol1 |
| 263 | */ |
| 264 | __asm__ __volatile__ ("fldcw %0" : : "m" (envp->__x87.__control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 265 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 266 | return (0); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 267 | } |
| 268 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 269 | /* |
| 270 | * The feholdexcept() function saves the current floating-point environment |
| 271 | * in the object pointed to by envp, clears the floating-point status flags, and |
| 272 | * then installs a non-stop (continue on floating-point exceptions) mode, if |
| 273 | * available, for all floating-point exceptions. |
| 274 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 275 | int |
| 276 | feholdexcept(fenv_t *envp) |
| 277 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 278 | unsigned int mxcsr; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 279 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 280 | /* Store the current x87 floating-point environment */ |
| 281 | __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 282 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 283 | /* Clear all exception flags in FPU */ |
| 284 | __asm__ __volatile__ ("fnclex"); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 285 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 286 | /* Store the MXCSR register state */ |
| 287 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 288 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 289 | /* Clear exception flags in MXCSR */ |
| 290 | mxcsr = envp->__mxcsr; |
| 291 | mxcsr &= ~FE_ALL_EXCEPT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 292 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 293 | /* Mask all exceptions */ |
| 294 | mxcsr |= FE_ALL_EXCEPT << _SSE_MASK_SHIFT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 295 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 296 | /* Store the MXCSR register */ |
| 297 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 298 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 299 | return (0); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 300 | } |
| 301 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 302 | /* |
| 303 | * The fesetenv() function attempts to establish the floating-point environment |
| 304 | * represented by the object pointed to by envp. The argument `envp' points |
| 305 | * to an object set by a call to fegetenv() or feholdexcept(), or equal a |
| 306 | * floating-point environment macro. The fesetenv() function does not raise |
| 307 | * floating-point exceptions, but only installs the state of the floating-point |
| 308 | * status flags represented through its argument. |
| 309 | */ |
| 310 | int |
| 311 | fesetenv(const fenv_t *envp) |
| 312 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 313 | /* Load the x87 floating-point environent */ |
| 314 | __asm__ __volatile__ ("fldenv %0" : : "m" (*envp)); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 315 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 316 | /* Store the MXCSR register */ |
| 317 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (envp->__mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 318 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 319 | return (0); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* |
| 323 | * The feupdateenv() function saves the currently raised floating-point |
| 324 | * exceptions in its automatic storage, installs the floating-point environment |
| 325 | * represented by the object pointed to by `envp', and then raises the saved |
| 326 | * floating-point exceptions. The argument `envp' shall point to an object set |
| 327 | * by a call to feholdexcept() or fegetenv(), or equal a floating-point |
| 328 | * environment macro. |
| 329 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 330 | int |
| 331 | feupdateenv(const fenv_t *envp) |
| 332 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 333 | unsigned short status; |
| 334 | unsigned int mxcsr; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 335 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 336 | /* Store the x87 status register */ |
| 337 | __asm__ __volatile__ ("fnstsw %0" : "=am" (status)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 338 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 339 | /* Store the MXCSR register */ |
| 340 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 341 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 342 | /* Install new floating-point environment */ |
| 343 | fesetenv(envp); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 344 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 345 | /* Raise any previously accumulated exceptions */ |
| 346 | feraiseexcept(status | mxcsr); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 347 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 348 | return (0); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 349 | } |
| 350 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 351 | /* |
| 352 | * The following functions are extentions to the standard |
| 353 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 354 | int |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 355 | feenableexcept(int mask) |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 356 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 357 | unsigned int mxcsr, omask; |
| 358 | unsigned short control; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 359 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 360 | mask &= FE_ALL_EXCEPT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 361 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 362 | __asm__ __volatile__ ("fnstcw %0" : "=m" (control)); |
| 363 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 364 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 365 | omask = ~(control | (mxcsr >> _SSE_MASK_SHIFT)) & FE_ALL_EXCEPT; |
| 366 | control &= ~mask; |
| 367 | __asm__ __volatile__ ("fldcw %0" : : "m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 368 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 369 | mxcsr &= ~(mask << _SSE_MASK_SHIFT); |
| 370 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 371 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 372 | return (omask); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | int |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 376 | fedisableexcept(int mask) |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 377 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 378 | unsigned int mxcsr, omask; |
| 379 | unsigned short control; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 380 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 381 | mask &= FE_ALL_EXCEPT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 382 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 383 | __asm__ __volatile__ ("fnstcw %0" : "=m" (control)); |
| 384 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 385 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 386 | omask = ~(control | (mxcsr >> _SSE_MASK_SHIFT)) & FE_ALL_EXCEPT; |
| 387 | control |= mask; |
| 388 | __asm__ __volatile__ ("fldcw %0" : : "m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 389 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 390 | mxcsr |= mask << _SSE_MASK_SHIFT; |
| 391 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 392 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 393 | return (omask); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 394 | } |
| 395 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 396 | int |
| 397 | fegetexcept(void) |
| 398 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 399 | unsigned short control; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 400 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 401 | /* |
| 402 | * We assume that the masks for the x87 and the SSE unit are |
| 403 | * the same. |
| 404 | */ |
| 405 | __asm__ __volatile__ ("fnstcw %0" : "=m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 406 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 407 | return (~control & FE_ALL_EXCEPT); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 408 | } |