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