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 | 43bf81e | 2014-06-09 14:29:25 -0700 | [diff] [blame] | 33 | #define SSE_MASK_SHIFT 7 |
| 34 | |
| 35 | /* |
| 36 | * The following symbol is simply the bitwise-inclusive OR of all floating-point |
| 37 | * rounding direction constants defined above. |
| 38 | */ |
| 39 | #define X87_ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO) |
| 40 | #define SSE_ROUND_SHIFT 3 |
| 41 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 42 | /* |
| 43 | * The following constant represents the default floating-point environment |
| 44 | * (that is, the one installed at program startup) and has type pointer to |
| 45 | * const-qualified fenv_t. |
| 46 | * |
| 47 | * It can be used as an argument to the functions within the <fenv.h> header |
| 48 | * that manage the floating-point environment, namely fesetenv() and |
| 49 | * feupdateenv(). |
| 50 | * |
| 51 | * x87 fpu registers are 16bit wide. The upper bits, 31-16, are marked as |
| 52 | * RESERVED. |
| 53 | */ |
Pavel Chupin | 7ba84d3 | 2014-02-28 00:36:10 +0400 | [diff] [blame] | 54 | const fenv_t __fe_dfl_env = { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 55 | { |
| 56 | 0xffff0000 | __INITIAL_NPXCW__, /* Control word register */ |
| 57 | 0xffff0000, /* Status word register */ |
| 58 | 0xffffffff, /* Tag word register */ |
| 59 | { |
| 60 | 0x00000000, |
| 61 | 0x00000000, |
| 62 | 0x00000000, |
| 63 | 0xffff0000 |
| 64 | } |
| 65 | }, |
| 66 | __INITIAL_MXCSR__ /* MXCSR register */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 67 | }; |
| 68 | |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 69 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 70 | /* |
| 71 | * The feclearexcept() function clears the supported floating-point exceptions |
| 72 | * represented by `excepts'. |
| 73 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 74 | int |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 75 | feclearexcept(int excepts) |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 76 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 77 | fenv_t fenv; |
| 78 | unsigned int mxcsr; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 79 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 80 | excepts &= FE_ALL_EXCEPT; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 81 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 82 | /* Store the current x87 floating-point environment */ |
| 83 | __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 84 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 85 | /* Clear the requested floating-point exceptions */ |
| 86 | fenv.__x87.__status &= ~excepts; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 87 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 88 | /* Load the x87 floating-point environent */ |
| 89 | __asm__ __volatile__ ("fldenv %0" : : "m" (fenv)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 90 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 91 | /* Same for SSE environment */ |
| 92 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
| 93 | mxcsr &= ~excepts; |
| 94 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 95 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 96 | return (0); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 97 | } |
| 98 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 99 | /* |
| 100 | * The fegetexceptflag() function stores an implementation-defined |
| 101 | * representation of the states of the floating-point status flags indicated by |
| 102 | * the argument excepts in the object pointed to by the argument flagp. |
| 103 | */ |
| 104 | int |
| 105 | fegetexceptflag(fexcept_t *flagp, int excepts) |
| 106 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 107 | unsigned short status; |
| 108 | unsigned int mxcsr; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 109 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 110 | excepts &= FE_ALL_EXCEPT; |
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 | /* Store the current x87 status register */ |
| 113 | __asm__ __volatile__ ("fnstsw %0" : "=am" (status)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 114 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 115 | /* Store the MXCSR register */ |
| 116 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 117 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 118 | /* Store the results in flagp */ |
| 119 | *flagp = (status | mxcsr) & excepts; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 120 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 121 | return (0); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
| 125 | * The feraiseexcept() function raises the supported floating-point exceptions |
| 126 | * represented by the argument `excepts'. |
| 127 | * |
| 128 | * The standard explicitly allows us to execute an instruction that has the |
| 129 | * exception as a side effect, but we choose to manipulate the status register |
| 130 | * directly. |
| 131 | * |
| 132 | * The validation of input is being deferred to fesetexceptflag(). |
| 133 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 134 | int |
| 135 | feraiseexcept(int excepts) |
| 136 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 137 | excepts &= FE_ALL_EXCEPT; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 138 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 139 | fesetexceptflag((fexcept_t *)&excepts, excepts); |
| 140 | __asm__ __volatile__ ("fwait"); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 141 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 142 | return (0); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 143 | } |
| 144 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 145 | /* |
| 146 | * This function sets the floating-point status flags indicated by the argument |
| 147 | * `excepts' to the states stored in the object pointed to by `flagp'. It does |
| 148 | * NOT raise any floating-point exceptions, but only sets the state of the flags. |
| 149 | */ |
| 150 | int |
| 151 | fesetexceptflag(const fexcept_t *flagp, int excepts) |
| 152 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 153 | fenv_t fenv; |
| 154 | unsigned int mxcsr; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 155 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 156 | excepts &= FE_ALL_EXCEPT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 157 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 158 | /* Store the current x87 floating-point environment */ |
| 159 | __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 160 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 161 | /* Set the requested status flags */ |
| 162 | fenv.__x87.__status &= ~excepts; |
| 163 | fenv.__x87.__status |= *flagp & excepts; |
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 | /* Load the x87 floating-point environent */ |
| 166 | __asm__ __volatile__ ("fldenv %0" : : "m" (fenv)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 167 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 168 | /* Same for SSE environment */ |
| 169 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
| 170 | mxcsr &= ~excepts; |
| 171 | mxcsr |= *flagp & excepts; |
| 172 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 173 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 174 | return (0); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
| 178 | * The fetestexcept() function determines which of a specified subset of the |
| 179 | * floating-point exception flags are currently set. The `excepts' argument |
| 180 | * specifies the floating-point status flags to be queried. |
| 181 | */ |
| 182 | int |
| 183 | fetestexcept(int excepts) |
| 184 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 185 | unsigned short status; |
| 186 | unsigned int mxcsr; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 187 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 188 | excepts &= FE_ALL_EXCEPT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 189 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 190 | /* Store the current x87 status register */ |
| 191 | __asm__ __volatile__ ("fnstsw %0" : "=am" (status)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 192 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 193 | /* Store the MXCSR register state */ |
| 194 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 195 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 196 | return ((status | mxcsr) & excepts); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* |
| 200 | * The fegetround() function gets the current rounding direction. |
| 201 | */ |
| 202 | int |
| 203 | fegetround(void) |
| 204 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 205 | unsigned short control; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 206 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 207 | /* |
| 208 | * We assume that the x87 and the SSE unit agree on the |
| 209 | * rounding mode. Reading the control word on the x87 turns |
| 210 | * out to be about 5 times faster than reading it on the SSE |
| 211 | * unit on an Opteron 244. |
| 212 | */ |
| 213 | __asm__ __volatile__ ("fnstcw %0" : "=m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 214 | |
Elliott Hughes | 43bf81e | 2014-06-09 14:29:25 -0700 | [diff] [blame] | 215 | return (control & X87_ROUND_MASK); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* |
| 219 | * The fesetround() function establishes the rounding direction represented by |
| 220 | * its argument `round'. If the argument is not equal to the value of a rounding |
| 221 | * direction macro, the rounding direction is not changed. |
| 222 | */ |
| 223 | int |
| 224 | fesetround(int round) |
| 225 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 226 | unsigned short control; |
| 227 | unsigned int mxcsr; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 228 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 229 | /* Check whether requested rounding direction is supported */ |
Elliott Hughes | 43bf81e | 2014-06-09 14:29:25 -0700 | [diff] [blame] | 230 | if (round & ~X87_ROUND_MASK) |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 231 | return (-1); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 232 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 233 | /* Store the current x87 control word register */ |
| 234 | __asm__ __volatile__ ("fnstcw %0" : "=m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 235 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 236 | /* Set the rounding direction */ |
Elliott Hughes | 43bf81e | 2014-06-09 14:29:25 -0700 | [diff] [blame] | 237 | control &= ~X87_ROUND_MASK; |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 238 | control |= round; |
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 | /* Load the x87 control word register */ |
| 241 | __asm__ __volatile__ ("fldcw %0" : : "m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 242 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 243 | /* Same for the SSE environment */ |
| 244 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
Elliott Hughes | 43bf81e | 2014-06-09 14:29:25 -0700 | [diff] [blame] | 245 | mxcsr &= ~(X87_ROUND_MASK << SSE_ROUND_SHIFT); |
| 246 | mxcsr |= round << SSE_ROUND_SHIFT; |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 247 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 248 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 249 | return (0); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* |
| 253 | * The fegetenv() function attempts to store the current floating-point |
| 254 | * environment in the object pointed to by envp. |
| 255 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 256 | int |
| 257 | fegetenv(fenv_t *envp) |
| 258 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 259 | /* Store the current x87 floating-point environment */ |
| 260 | __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp)); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 261 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 262 | /* Store the MXCSR register state */ |
| 263 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 264 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 265 | /* |
| 266 | * When an FNSTENV instruction is executed, all pending exceptions are |
| 267 | * essentially lost (either the x87 FPU status register is cleared or |
| 268 | * all exceptions are masked). |
| 269 | * |
| 270 | * 8.6 X87 FPU EXCEPTION SYNCHRONIZATION - |
| 271 | * Intel(R) 64 and IA-32 Architectures Softare Developer's Manual - Vol1 |
| 272 | */ |
| 273 | __asm__ __volatile__ ("fldcw %0" : : "m" (envp->__x87.__control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 274 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 275 | return (0); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 276 | } |
| 277 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 278 | /* |
| 279 | * The feholdexcept() function saves the current floating-point environment |
| 280 | * in the object pointed to by envp, clears the floating-point status flags, and |
| 281 | * then installs a non-stop (continue on floating-point exceptions) mode, if |
| 282 | * available, for all floating-point exceptions. |
| 283 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 284 | int |
| 285 | feholdexcept(fenv_t *envp) |
| 286 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 287 | unsigned int mxcsr; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 288 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 289 | /* Store the current x87 floating-point environment */ |
| 290 | __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 291 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 292 | /* Clear all exception flags in FPU */ |
| 293 | __asm__ __volatile__ ("fnclex"); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 294 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 295 | /* Store the MXCSR register state */ |
| 296 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 297 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 298 | /* Clear exception flags in MXCSR */ |
| 299 | mxcsr = envp->__mxcsr; |
| 300 | mxcsr &= ~FE_ALL_EXCEPT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 301 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 302 | /* Mask all exceptions */ |
Elliott Hughes | 43bf81e | 2014-06-09 14:29:25 -0700 | [diff] [blame] | 303 | mxcsr |= FE_ALL_EXCEPT << SSE_MASK_SHIFT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 304 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 305 | /* Store the MXCSR register */ |
| 306 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 307 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 308 | return (0); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 309 | } |
| 310 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 311 | /* |
| 312 | * The fesetenv() function attempts to establish the floating-point environment |
| 313 | * represented by the object pointed to by envp. The argument `envp' points |
| 314 | * to an object set by a call to fegetenv() or feholdexcept(), or equal a |
| 315 | * floating-point environment macro. The fesetenv() function does not raise |
| 316 | * floating-point exceptions, but only installs the state of the floating-point |
| 317 | * status flags represented through its argument. |
| 318 | */ |
| 319 | int |
| 320 | fesetenv(const fenv_t *envp) |
| 321 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 322 | /* Load the x87 floating-point environent */ |
| 323 | __asm__ __volatile__ ("fldenv %0" : : "m" (*envp)); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 324 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 325 | /* Store the MXCSR register */ |
| 326 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (envp->__mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 327 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 328 | return (0); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
| 332 | * The feupdateenv() function saves the currently raised floating-point |
| 333 | * exceptions in its automatic storage, installs the floating-point environment |
| 334 | * represented by the object pointed to by `envp', and then raises the saved |
| 335 | * floating-point exceptions. The argument `envp' shall point to an object set |
| 336 | * by a call to feholdexcept() or fegetenv(), or equal a floating-point |
| 337 | * environment macro. |
| 338 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 339 | int |
| 340 | feupdateenv(const fenv_t *envp) |
| 341 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 342 | unsigned short status; |
| 343 | unsigned int mxcsr; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 344 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 345 | /* Store the x87 status register */ |
| 346 | __asm__ __volatile__ ("fnstsw %0" : "=am" (status)); |
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 | /* Store the MXCSR register */ |
| 349 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 350 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 351 | /* Install new floating-point environment */ |
| 352 | fesetenv(envp); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 353 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 354 | /* Raise any previously accumulated exceptions */ |
| 355 | feraiseexcept(status | mxcsr); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 356 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 357 | return (0); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 358 | } |
| 359 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 360 | /* |
| 361 | * The following functions are extentions to the standard |
| 362 | */ |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 363 | int |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 364 | feenableexcept(int mask) |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 365 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 366 | unsigned int mxcsr, omask; |
| 367 | unsigned short control; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 368 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 369 | mask &= FE_ALL_EXCEPT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 370 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 371 | __asm__ __volatile__ ("fnstcw %0" : "=m" (control)); |
| 372 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 373 | |
Elliott Hughes | 43bf81e | 2014-06-09 14:29:25 -0700 | [diff] [blame] | 374 | omask = ~(control | (mxcsr >> SSE_MASK_SHIFT)) & FE_ALL_EXCEPT; |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 375 | control &= ~mask; |
| 376 | __asm__ __volatile__ ("fldcw %0" : : "m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 377 | |
Elliott Hughes | 43bf81e | 2014-06-09 14:29:25 -0700 | [diff] [blame] | 378 | mxcsr &= ~(mask << SSE_MASK_SHIFT); |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 379 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 380 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 381 | return (omask); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | int |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 385 | fedisableexcept(int mask) |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 386 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 387 | unsigned int mxcsr, omask; |
| 388 | unsigned short control; |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 389 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 390 | mask &= FE_ALL_EXCEPT; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 391 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 392 | __asm__ __volatile__ ("fnstcw %0" : "=m" (control)); |
| 393 | __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 394 | |
Elliott Hughes | 43bf81e | 2014-06-09 14:29:25 -0700 | [diff] [blame] | 395 | omask = ~(control | (mxcsr >> SSE_MASK_SHIFT)) & FE_ALL_EXCEPT; |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 396 | control |= mask; |
| 397 | __asm__ __volatile__ ("fldcw %0" : : "m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 398 | |
Elliott Hughes | 43bf81e | 2014-06-09 14:29:25 -0700 | [diff] [blame] | 399 | mxcsr |= mask << SSE_MASK_SHIFT; |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 400 | __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 401 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 402 | return (omask); |
Pavel Chupin | ce7add1 | 2013-09-20 19:09:55 +0400 | [diff] [blame] | 403 | } |
| 404 | |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 405 | int |
| 406 | fegetexcept(void) |
| 407 | { |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 408 | unsigned short control; |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 409 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 410 | /* |
| 411 | * We assume that the masks for the x87 and the SSE unit are |
| 412 | * the same. |
| 413 | */ |
| 414 | __asm__ __volatile__ ("fnstcw %0" : "=m" (control)); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 415 | |
Calin Juravle | 2d36790 | 2014-02-25 14:49:41 +0000 | [diff] [blame] | 416 | return (~control & FE_ALL_EXCEPT); |
Elliott Hughes | 770a349 | 2013-10-01 17:57:19 -0700 | [diff] [blame] | 417 | } |