blob: 4b24ff969cdd7414af299166baf9cb837e24350e [file] [log] [blame]
Calin Juravle2d367902014-02-25 14:49:41 +00001/* $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 Hughes770a3492013-10-01 17:57:19 -07003
Pavel Chupince7add12013-09-20 19:09:55 +04004/*-
Elliott Hughes770a3492013-10-01 17:57:19 -07005 * Copyright (c) 2004-2005 David Schultz <das (at) FreeBSD.ORG>
Pavel Chupince7add12013-09-20 19:09:55 +04006 * 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 Chupince7add12013-09-20 19:09:55 +040028 */
29
Elliott Hughes770a3492013-10-01 17:57:19 -070030#include <fenv.h>
Pavel Chupince7add12013-09-20 19:09:55 +040031#include <machine/fpu.h>
32
Elliott Hughes43bf81e2014-06-09 14:29:25 -070033#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 Hughes770a3492013-10-01 17:57:19 -070042/*
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 Chupin7ba84d32014-02-28 00:36:10 +040054const fenv_t __fe_dfl_env = {
Calin Juravle2d367902014-02-25 14:49:41 +000055 {
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 Chupince7add12013-09-20 19:09:55 +040067};
68
Pavel Chupince7add12013-09-20 19:09:55 +040069
Elliott Hughes770a3492013-10-01 17:57:19 -070070/*
71 * The feclearexcept() function clears the supported floating-point exceptions
72 * represented by `excepts'.
73 */
Pavel Chupince7add12013-09-20 19:09:55 +040074int
Elliott Hughes770a3492013-10-01 17:57:19 -070075feclearexcept(int excepts)
Pavel Chupince7add12013-09-20 19:09:55 +040076{
Calin Juravle2d367902014-02-25 14:49:41 +000077 fenv_t fenv;
78 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +040079
Calin Juravle2d367902014-02-25 14:49:41 +000080 excepts &= FE_ALL_EXCEPT;
Pavel Chupince7add12013-09-20 19:09:55 +040081
Calin Juravle2d367902014-02-25 14:49:41 +000082 /* Store the current x87 floating-point environment */
83 __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -070084
Calin Juravle2d367902014-02-25 14:49:41 +000085 /* Clear the requested floating-point exceptions */
86 fenv.__x87.__status &= ~excepts;
Elliott Hughes770a3492013-10-01 17:57:19 -070087
Calin Juravle2d367902014-02-25 14:49:41 +000088 /* Load the x87 floating-point environent */
89 __asm__ __volatile__ ("fldenv %0" : : "m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -070090
Calin Juravle2d367902014-02-25 14:49:41 +000091 /* Same for SSE environment */
92 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
93 mxcsr &= ~excepts;
94 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Pavel Chupince7add12013-09-20 19:09:55 +040095
Calin Juravle2d367902014-02-25 14:49:41 +000096 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +040097}
98
Elliott Hughes770a3492013-10-01 17:57:19 -070099/*
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 */
104int
105fegetexceptflag(fexcept_t *flagp, int excepts)
106{
Calin Juravle2d367902014-02-25 14:49:41 +0000107 unsigned short status;
108 unsigned int mxcsr;
Elliott Hughes770a3492013-10-01 17:57:19 -0700109
Calin Juravle2d367902014-02-25 14:49:41 +0000110 excepts &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700111
Calin Juravle2d367902014-02-25 14:49:41 +0000112 /* Store the current x87 status register */
113 __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
Elliott Hughes770a3492013-10-01 17:57:19 -0700114
Calin Juravle2d367902014-02-25 14:49:41 +0000115 /* Store the MXCSR register */
116 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700117
Calin Juravle2d367902014-02-25 14:49:41 +0000118 /* Store the results in flagp */
119 *flagp = (status | mxcsr) & excepts;
Elliott Hughes770a3492013-10-01 17:57:19 -0700120
Calin Juravle2d367902014-02-25 14:49:41 +0000121 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700122}
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 Chupince7add12013-09-20 19:09:55 +0400134int
135feraiseexcept(int excepts)
136{
Calin Juravle2d367902014-02-25 14:49:41 +0000137 excepts &= FE_ALL_EXCEPT;
Pavel Chupince7add12013-09-20 19:09:55 +0400138
Calin Juravle2d367902014-02-25 14:49:41 +0000139 fesetexceptflag((fexcept_t *)&excepts, excepts);
140 __asm__ __volatile__ ("fwait");
Elliott Hughes770a3492013-10-01 17:57:19 -0700141
Calin Juravle2d367902014-02-25 14:49:41 +0000142 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400143}
144
Elliott Hughes770a3492013-10-01 17:57:19 -0700145/*
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 */
150int
151fesetexceptflag(const fexcept_t *flagp, int excepts)
152{
Calin Juravle2d367902014-02-25 14:49:41 +0000153 fenv_t fenv;
154 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +0400155
Calin Juravle2d367902014-02-25 14:49:41 +0000156 excepts &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700157
Calin Juravle2d367902014-02-25 14:49:41 +0000158 /* Store the current x87 floating-point environment */
159 __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -0700160
Calin Juravle2d367902014-02-25 14:49:41 +0000161 /* Set the requested status flags */
162 fenv.__x87.__status &= ~excepts;
163 fenv.__x87.__status |= *flagp & excepts;
Elliott Hughes770a3492013-10-01 17:57:19 -0700164
Calin Juravle2d367902014-02-25 14:49:41 +0000165 /* Load the x87 floating-point environent */
166 __asm__ __volatile__ ("fldenv %0" : : "m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -0700167
Calin Juravle2d367902014-02-25 14:49:41 +0000168 /* 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 Hughes770a3492013-10-01 17:57:19 -0700173
Calin Juravle2d367902014-02-25 14:49:41 +0000174 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700175}
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 */
182int
183fetestexcept(int excepts)
184{
Calin Juravle2d367902014-02-25 14:49:41 +0000185 unsigned short status;
186 unsigned int mxcsr;
Elliott Hughes770a3492013-10-01 17:57:19 -0700187
Calin Juravle2d367902014-02-25 14:49:41 +0000188 excepts &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700189
Calin Juravle2d367902014-02-25 14:49:41 +0000190 /* Store the current x87 status register */
191 __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
Elliott Hughes770a3492013-10-01 17:57:19 -0700192
Calin Juravle2d367902014-02-25 14:49:41 +0000193 /* Store the MXCSR register state */
194 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700195
Calin Juravle2d367902014-02-25 14:49:41 +0000196 return ((status | mxcsr) & excepts);
Elliott Hughes770a3492013-10-01 17:57:19 -0700197}
198
199/*
200 * The fegetround() function gets the current rounding direction.
201 */
202int
203fegetround(void)
204{
Calin Juravle2d367902014-02-25 14:49:41 +0000205 unsigned short control;
Elliott Hughes770a3492013-10-01 17:57:19 -0700206
Calin Juravle2d367902014-02-25 14:49:41 +0000207 /*
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 Hughes770a3492013-10-01 17:57:19 -0700214
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700215 return (control & X87_ROUND_MASK);
Elliott Hughes770a3492013-10-01 17:57:19 -0700216}
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 */
223int
224fesetround(int round)
225{
Calin Juravle2d367902014-02-25 14:49:41 +0000226 unsigned short control;
227 unsigned int mxcsr;
Elliott Hughes770a3492013-10-01 17:57:19 -0700228
Calin Juravle2d367902014-02-25 14:49:41 +0000229 /* Check whether requested rounding direction is supported */
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700230 if (round & ~X87_ROUND_MASK)
Calin Juravle2d367902014-02-25 14:49:41 +0000231 return (-1);
Elliott Hughes770a3492013-10-01 17:57:19 -0700232
Calin Juravle2d367902014-02-25 14:49:41 +0000233 /* Store the current x87 control word register */
234 __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700235
Calin Juravle2d367902014-02-25 14:49:41 +0000236 /* Set the rounding direction */
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700237 control &= ~X87_ROUND_MASK;
Calin Juravle2d367902014-02-25 14:49:41 +0000238 control |= round;
Elliott Hughes770a3492013-10-01 17:57:19 -0700239
Calin Juravle2d367902014-02-25 14:49:41 +0000240 /* Load the x87 control word register */
241 __asm__ __volatile__ ("fldcw %0" : : "m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700242
Calin Juravle2d367902014-02-25 14:49:41 +0000243 /* Same for the SSE environment */
244 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700245 mxcsr &= ~(X87_ROUND_MASK << SSE_ROUND_SHIFT);
246 mxcsr |= round << SSE_ROUND_SHIFT;
Calin Juravle2d367902014-02-25 14:49:41 +0000247 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700248
Calin Juravle2d367902014-02-25 14:49:41 +0000249 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700250}
251
252/*
253 * The fegetenv() function attempts to store the current floating-point
254 * environment in the object pointed to by envp.
255 */
Pavel Chupince7add12013-09-20 19:09:55 +0400256int
257fegetenv(fenv_t *envp)
258{
Calin Juravle2d367902014-02-25 14:49:41 +0000259 /* Store the current x87 floating-point environment */
260 __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp));
Pavel Chupince7add12013-09-20 19:09:55 +0400261
Calin Juravle2d367902014-02-25 14:49:41 +0000262 /* Store the MXCSR register state */
263 __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700264
Calin Juravle2d367902014-02-25 14:49:41 +0000265 /*
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 Hughes770a3492013-10-01 17:57:19 -0700274
Calin Juravle2d367902014-02-25 14:49:41 +0000275 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400276}
277
Elliott Hughes770a3492013-10-01 17:57:19 -0700278/*
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 Chupince7add12013-09-20 19:09:55 +0400284int
285feholdexcept(fenv_t *envp)
286{
Calin Juravle2d367902014-02-25 14:49:41 +0000287 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +0400288
Calin Juravle2d367902014-02-25 14:49:41 +0000289 /* Store the current x87 floating-point environment */
290 __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp));
Elliott Hughes770a3492013-10-01 17:57:19 -0700291
Calin Juravle2d367902014-02-25 14:49:41 +0000292 /* Clear all exception flags in FPU */
293 __asm__ __volatile__ ("fnclex");
Elliott Hughes770a3492013-10-01 17:57:19 -0700294
Calin Juravle2d367902014-02-25 14:49:41 +0000295 /* Store the MXCSR register state */
296 __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700297
Calin Juravle2d367902014-02-25 14:49:41 +0000298 /* Clear exception flags in MXCSR */
299 mxcsr = envp->__mxcsr;
300 mxcsr &= ~FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700301
Calin Juravle2d367902014-02-25 14:49:41 +0000302 /* Mask all exceptions */
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700303 mxcsr |= FE_ALL_EXCEPT << SSE_MASK_SHIFT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700304
Calin Juravle2d367902014-02-25 14:49:41 +0000305 /* Store the MXCSR register */
306 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700307
Calin Juravle2d367902014-02-25 14:49:41 +0000308 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400309}
310
Elliott Hughes770a3492013-10-01 17:57:19 -0700311/*
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 */
319int
320fesetenv(const fenv_t *envp)
321{
Calin Juravle2d367902014-02-25 14:49:41 +0000322 /* Load the x87 floating-point environent */
323 __asm__ __volatile__ ("fldenv %0" : : "m" (*envp));
Pavel Chupince7add12013-09-20 19:09:55 +0400324
Calin Juravle2d367902014-02-25 14:49:41 +0000325 /* Store the MXCSR register */
326 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (envp->__mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700327
Calin Juravle2d367902014-02-25 14:49:41 +0000328 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700329}
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 Chupince7add12013-09-20 19:09:55 +0400339int
340feupdateenv(const fenv_t *envp)
341{
Calin Juravle2d367902014-02-25 14:49:41 +0000342 unsigned short status;
343 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +0400344
Calin Juravle2d367902014-02-25 14:49:41 +0000345 /* Store the x87 status register */
346 __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
Elliott Hughes770a3492013-10-01 17:57:19 -0700347
Calin Juravle2d367902014-02-25 14:49:41 +0000348 /* Store the MXCSR register */
349 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700350
Calin Juravle2d367902014-02-25 14:49:41 +0000351 /* Install new floating-point environment */
352 fesetenv(envp);
Elliott Hughes770a3492013-10-01 17:57:19 -0700353
Calin Juravle2d367902014-02-25 14:49:41 +0000354 /* Raise any previously accumulated exceptions */
355 feraiseexcept(status | mxcsr);
Elliott Hughes770a3492013-10-01 17:57:19 -0700356
Calin Juravle2d367902014-02-25 14:49:41 +0000357 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400358}
359
Elliott Hughes770a3492013-10-01 17:57:19 -0700360/*
361 * The following functions are extentions to the standard
362 */
Pavel Chupince7add12013-09-20 19:09:55 +0400363int
Elliott Hughes770a3492013-10-01 17:57:19 -0700364feenableexcept(int mask)
Pavel Chupince7add12013-09-20 19:09:55 +0400365{
Calin Juravle2d367902014-02-25 14:49:41 +0000366 unsigned int mxcsr, omask;
367 unsigned short control;
Pavel Chupince7add12013-09-20 19:09:55 +0400368
Calin Juravle2d367902014-02-25 14:49:41 +0000369 mask &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700370
Calin Juravle2d367902014-02-25 14:49:41 +0000371 __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
372 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700373
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700374 omask = ~(control | (mxcsr >> SSE_MASK_SHIFT)) & FE_ALL_EXCEPT;
Calin Juravle2d367902014-02-25 14:49:41 +0000375 control &= ~mask;
376 __asm__ __volatile__ ("fldcw %0" : : "m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700377
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700378 mxcsr &= ~(mask << SSE_MASK_SHIFT);
Calin Juravle2d367902014-02-25 14:49:41 +0000379 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700380
Calin Juravle2d367902014-02-25 14:49:41 +0000381 return (omask);
Pavel Chupince7add12013-09-20 19:09:55 +0400382}
383
384int
Elliott Hughes770a3492013-10-01 17:57:19 -0700385fedisableexcept(int mask)
Pavel Chupince7add12013-09-20 19:09:55 +0400386{
Calin Juravle2d367902014-02-25 14:49:41 +0000387 unsigned int mxcsr, omask;
388 unsigned short control;
Pavel Chupince7add12013-09-20 19:09:55 +0400389
Calin Juravle2d367902014-02-25 14:49:41 +0000390 mask &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700391
Calin Juravle2d367902014-02-25 14:49:41 +0000392 __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
393 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700394
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700395 omask = ~(control | (mxcsr >> SSE_MASK_SHIFT)) & FE_ALL_EXCEPT;
Calin Juravle2d367902014-02-25 14:49:41 +0000396 control |= mask;
397 __asm__ __volatile__ ("fldcw %0" : : "m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700398
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700399 mxcsr |= mask << SSE_MASK_SHIFT;
Calin Juravle2d367902014-02-25 14:49:41 +0000400 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700401
Calin Juravle2d367902014-02-25 14:49:41 +0000402 return (omask);
Pavel Chupince7add12013-09-20 19:09:55 +0400403}
404
Elliott Hughes770a3492013-10-01 17:57:19 -0700405int
406fegetexcept(void)
407{
Calin Juravle2d367902014-02-25 14:49:41 +0000408 unsigned short control;
Elliott Hughes770a3492013-10-01 17:57:19 -0700409
Calin Juravle2d367902014-02-25 14:49:41 +0000410 /*
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 Hughes770a3492013-10-01 17:57:19 -0700415
Calin Juravle2d367902014-02-25 14:49:41 +0000416 return (~control & FE_ALL_EXCEPT);
Elliott Hughes770a3492013-10-01 17:57:19 -0700417}