blob: b058de29caef5a117efa4cc66ad2cce4c4dec54c [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 Hughes770a3492013-10-01 17:57:19 -070033/*
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 Chupin7ba84d32014-02-28 00:36:10 +040045const fenv_t __fe_dfl_env = {
Calin Juravle2d367902014-02-25 14:49:41 +000046 {
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 Chupince7add12013-09-20 19:09:55 +040058};
59
Pavel Chupince7add12013-09-20 19:09:55 +040060
Elliott Hughes770a3492013-10-01 17:57:19 -070061/*
62 * The feclearexcept() function clears the supported floating-point exceptions
63 * represented by `excepts'.
64 */
Pavel Chupince7add12013-09-20 19:09:55 +040065int
Elliott Hughes770a3492013-10-01 17:57:19 -070066feclearexcept(int excepts)
Pavel Chupince7add12013-09-20 19:09:55 +040067{
Calin Juravle2d367902014-02-25 14:49:41 +000068 fenv_t fenv;
69 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +040070
Calin Juravle2d367902014-02-25 14:49:41 +000071 excepts &= FE_ALL_EXCEPT;
Pavel Chupince7add12013-09-20 19:09:55 +040072
Calin Juravle2d367902014-02-25 14:49:41 +000073 /* Store the current x87 floating-point environment */
74 __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -070075
Calin Juravle2d367902014-02-25 14:49:41 +000076 /* Clear the requested floating-point exceptions */
77 fenv.__x87.__status &= ~excepts;
Elliott Hughes770a3492013-10-01 17:57:19 -070078
Calin Juravle2d367902014-02-25 14:49:41 +000079 /* Load the x87 floating-point environent */
80 __asm__ __volatile__ ("fldenv %0" : : "m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -070081
Calin Juravle2d367902014-02-25 14:49:41 +000082 /* Same for SSE environment */
83 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
84 mxcsr &= ~excepts;
85 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Pavel Chupince7add12013-09-20 19:09:55 +040086
Calin Juravle2d367902014-02-25 14:49:41 +000087 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +040088}
89
Elliott Hughes770a3492013-10-01 17:57:19 -070090/*
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 */
95int
96fegetexceptflag(fexcept_t *flagp, int excepts)
97{
Calin Juravle2d367902014-02-25 14:49:41 +000098 unsigned short status;
99 unsigned int mxcsr;
Elliott Hughes770a3492013-10-01 17:57:19 -0700100
Calin Juravle2d367902014-02-25 14:49:41 +0000101 excepts &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700102
Calin Juravle2d367902014-02-25 14:49:41 +0000103 /* Store the current x87 status register */
104 __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
Elliott Hughes770a3492013-10-01 17:57:19 -0700105
Calin Juravle2d367902014-02-25 14:49:41 +0000106 /* Store the MXCSR register */
107 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700108
Calin Juravle2d367902014-02-25 14:49:41 +0000109 /* Store the results in flagp */
110 *flagp = (status | mxcsr) & excepts;
Elliott Hughes770a3492013-10-01 17:57:19 -0700111
Calin Juravle2d367902014-02-25 14:49:41 +0000112 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700113}
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 Chupince7add12013-09-20 19:09:55 +0400125int
126feraiseexcept(int excepts)
127{
Calin Juravle2d367902014-02-25 14:49:41 +0000128 excepts &= FE_ALL_EXCEPT;
Pavel Chupince7add12013-09-20 19:09:55 +0400129
Calin Juravle2d367902014-02-25 14:49:41 +0000130 fesetexceptflag((fexcept_t *)&excepts, excepts);
131 __asm__ __volatile__ ("fwait");
Elliott Hughes770a3492013-10-01 17:57:19 -0700132
Calin Juravle2d367902014-02-25 14:49:41 +0000133 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400134}
135
Elliott Hughes770a3492013-10-01 17:57:19 -0700136/*
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 */
141int
142fesetexceptflag(const fexcept_t *flagp, int excepts)
143{
Calin Juravle2d367902014-02-25 14:49:41 +0000144 fenv_t fenv;
145 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +0400146
Calin Juravle2d367902014-02-25 14:49:41 +0000147 excepts &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700148
Calin Juravle2d367902014-02-25 14:49:41 +0000149 /* Store the current x87 floating-point environment */
150 __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -0700151
Calin Juravle2d367902014-02-25 14:49:41 +0000152 /* Set the requested status flags */
153 fenv.__x87.__status &= ~excepts;
154 fenv.__x87.__status |= *flagp & excepts;
Elliott Hughes770a3492013-10-01 17:57:19 -0700155
Calin Juravle2d367902014-02-25 14:49:41 +0000156 /* Load the x87 floating-point environent */
157 __asm__ __volatile__ ("fldenv %0" : : "m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -0700158
Calin Juravle2d367902014-02-25 14:49:41 +0000159 /* 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 Hughes770a3492013-10-01 17:57:19 -0700164
Calin Juravle2d367902014-02-25 14:49:41 +0000165 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700166}
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 */
173int
174fetestexcept(int excepts)
175{
Calin Juravle2d367902014-02-25 14:49:41 +0000176 unsigned short status;
177 unsigned int mxcsr;
Elliott Hughes770a3492013-10-01 17:57:19 -0700178
Calin Juravle2d367902014-02-25 14:49:41 +0000179 excepts &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700180
Calin Juravle2d367902014-02-25 14:49:41 +0000181 /* Store the current x87 status register */
182 __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
Elliott Hughes770a3492013-10-01 17:57:19 -0700183
Calin Juravle2d367902014-02-25 14:49:41 +0000184 /* Store the MXCSR register state */
185 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700186
Calin Juravle2d367902014-02-25 14:49:41 +0000187 return ((status | mxcsr) & excepts);
Elliott Hughes770a3492013-10-01 17:57:19 -0700188}
189
190/*
191 * The fegetround() function gets the current rounding direction.
192 */
193int
194fegetround(void)
195{
Calin Juravle2d367902014-02-25 14:49:41 +0000196 unsigned short control;
Elliott Hughes770a3492013-10-01 17:57:19 -0700197
Calin Juravle2d367902014-02-25 14:49:41 +0000198 /*
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 Hughes770a3492013-10-01 17:57:19 -0700205
Calin Juravle2d367902014-02-25 14:49:41 +0000206 return (control & _X87_ROUND_MASK);
Elliott Hughes770a3492013-10-01 17:57:19 -0700207}
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 */
214int
215fesetround(int round)
216{
Calin Juravle2d367902014-02-25 14:49:41 +0000217 unsigned short control;
218 unsigned int mxcsr;
Elliott Hughes770a3492013-10-01 17:57:19 -0700219
Calin Juravle2d367902014-02-25 14:49:41 +0000220 /* Check whether requested rounding direction is supported */
221 if (round & ~_X87_ROUND_MASK)
222 return (-1);
Elliott Hughes770a3492013-10-01 17:57:19 -0700223
Calin Juravle2d367902014-02-25 14:49:41 +0000224 /* Store the current x87 control word register */
225 __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700226
Calin Juravle2d367902014-02-25 14:49:41 +0000227 /* Set the rounding direction */
228 control &= ~_X87_ROUND_MASK;
229 control |= round;
Elliott Hughes770a3492013-10-01 17:57:19 -0700230
Calin Juravle2d367902014-02-25 14:49:41 +0000231 /* Load the x87 control word register */
232 __asm__ __volatile__ ("fldcw %0" : : "m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700233
Calin Juravle2d367902014-02-25 14:49:41 +0000234 /* 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 Hughes770a3492013-10-01 17:57:19 -0700239
Calin Juravle2d367902014-02-25 14:49:41 +0000240 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700241}
242
243/*
244 * The fegetenv() function attempts to store the current floating-point
245 * environment in the object pointed to by envp.
246 */
Pavel Chupince7add12013-09-20 19:09:55 +0400247int
248fegetenv(fenv_t *envp)
249{
Calin Juravle2d367902014-02-25 14:49:41 +0000250 /* Store the current x87 floating-point environment */
251 __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp));
Pavel Chupince7add12013-09-20 19:09:55 +0400252
Calin Juravle2d367902014-02-25 14:49:41 +0000253 /* Store the MXCSR register state */
254 __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700255
Calin Juravle2d367902014-02-25 14:49:41 +0000256 /*
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 Hughes770a3492013-10-01 17:57:19 -0700265
Calin Juravle2d367902014-02-25 14:49:41 +0000266 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400267}
268
Elliott Hughes770a3492013-10-01 17:57:19 -0700269/*
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 Chupince7add12013-09-20 19:09:55 +0400275int
276feholdexcept(fenv_t *envp)
277{
Calin Juravle2d367902014-02-25 14:49:41 +0000278 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +0400279
Calin Juravle2d367902014-02-25 14:49:41 +0000280 /* Store the current x87 floating-point environment */
281 __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp));
Elliott Hughes770a3492013-10-01 17:57:19 -0700282
Calin Juravle2d367902014-02-25 14:49:41 +0000283 /* Clear all exception flags in FPU */
284 __asm__ __volatile__ ("fnclex");
Elliott Hughes770a3492013-10-01 17:57:19 -0700285
Calin Juravle2d367902014-02-25 14:49:41 +0000286 /* Store the MXCSR register state */
287 __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700288
Calin Juravle2d367902014-02-25 14:49:41 +0000289 /* Clear exception flags in MXCSR */
290 mxcsr = envp->__mxcsr;
291 mxcsr &= ~FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700292
Calin Juravle2d367902014-02-25 14:49:41 +0000293 /* Mask all exceptions */
294 mxcsr |= FE_ALL_EXCEPT << _SSE_MASK_SHIFT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700295
Calin Juravle2d367902014-02-25 14:49:41 +0000296 /* Store the MXCSR register */
297 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700298
Calin Juravle2d367902014-02-25 14:49:41 +0000299 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400300}
301
Elliott Hughes770a3492013-10-01 17:57:19 -0700302/*
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 */
310int
311fesetenv(const fenv_t *envp)
312{
Calin Juravle2d367902014-02-25 14:49:41 +0000313 /* Load the x87 floating-point environent */
314 __asm__ __volatile__ ("fldenv %0" : : "m" (*envp));
Pavel Chupince7add12013-09-20 19:09:55 +0400315
Calin Juravle2d367902014-02-25 14:49:41 +0000316 /* Store the MXCSR register */
317 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (envp->__mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700318
Calin Juravle2d367902014-02-25 14:49:41 +0000319 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700320}
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 Chupince7add12013-09-20 19:09:55 +0400330int
331feupdateenv(const fenv_t *envp)
332{
Calin Juravle2d367902014-02-25 14:49:41 +0000333 unsigned short status;
334 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +0400335
Calin Juravle2d367902014-02-25 14:49:41 +0000336 /* Store the x87 status register */
337 __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
Elliott Hughes770a3492013-10-01 17:57:19 -0700338
Calin Juravle2d367902014-02-25 14:49:41 +0000339 /* Store the MXCSR register */
340 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700341
Calin Juravle2d367902014-02-25 14:49:41 +0000342 /* Install new floating-point environment */
343 fesetenv(envp);
Elliott Hughes770a3492013-10-01 17:57:19 -0700344
Calin Juravle2d367902014-02-25 14:49:41 +0000345 /* Raise any previously accumulated exceptions */
346 feraiseexcept(status | mxcsr);
Elliott Hughes770a3492013-10-01 17:57:19 -0700347
Calin Juravle2d367902014-02-25 14:49:41 +0000348 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400349}
350
Elliott Hughes770a3492013-10-01 17:57:19 -0700351/*
352 * The following functions are extentions to the standard
353 */
Pavel Chupince7add12013-09-20 19:09:55 +0400354int
Elliott Hughes770a3492013-10-01 17:57:19 -0700355feenableexcept(int mask)
Pavel Chupince7add12013-09-20 19:09:55 +0400356{
Calin Juravle2d367902014-02-25 14:49:41 +0000357 unsigned int mxcsr, omask;
358 unsigned short control;
Pavel Chupince7add12013-09-20 19:09:55 +0400359
Calin Juravle2d367902014-02-25 14:49:41 +0000360 mask &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700361
Calin Juravle2d367902014-02-25 14:49:41 +0000362 __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
363 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700364
Calin Juravle2d367902014-02-25 14:49:41 +0000365 omask = ~(control | (mxcsr >> _SSE_MASK_SHIFT)) & FE_ALL_EXCEPT;
366 control &= ~mask;
367 __asm__ __volatile__ ("fldcw %0" : : "m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700368
Calin Juravle2d367902014-02-25 14:49:41 +0000369 mxcsr &= ~(mask << _SSE_MASK_SHIFT);
370 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700371
Calin Juravle2d367902014-02-25 14:49:41 +0000372 return (omask);
Pavel Chupince7add12013-09-20 19:09:55 +0400373}
374
375int
Elliott Hughes770a3492013-10-01 17:57:19 -0700376fedisableexcept(int mask)
Pavel Chupince7add12013-09-20 19:09:55 +0400377{
Calin Juravle2d367902014-02-25 14:49:41 +0000378 unsigned int mxcsr, omask;
379 unsigned short control;
Pavel Chupince7add12013-09-20 19:09:55 +0400380
Calin Juravle2d367902014-02-25 14:49:41 +0000381 mask &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700382
Calin Juravle2d367902014-02-25 14:49:41 +0000383 __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
384 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700385
Calin Juravle2d367902014-02-25 14:49:41 +0000386 omask = ~(control | (mxcsr >> _SSE_MASK_SHIFT)) & FE_ALL_EXCEPT;
387 control |= mask;
388 __asm__ __volatile__ ("fldcw %0" : : "m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700389
Calin Juravle2d367902014-02-25 14:49:41 +0000390 mxcsr |= mask << _SSE_MASK_SHIFT;
391 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700392
Calin Juravle2d367902014-02-25 14:49:41 +0000393 return (omask);
Pavel Chupince7add12013-09-20 19:09:55 +0400394}
395
Elliott Hughes770a3492013-10-01 17:57:19 -0700396int
397fegetexcept(void)
398{
Calin Juravle2d367902014-02-25 14:49:41 +0000399 unsigned short control;
Elliott Hughes770a3492013-10-01 17:57:19 -0700400
Calin Juravle2d367902014-02-25 14:49:41 +0000401 /*
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 Hughes770a3492013-10-01 17:57:19 -0700406
Calin Juravle2d367902014-02-25 14:49:41 +0000407 return (~control & FE_ALL_EXCEPT);
Elliott Hughes770a3492013-10-01 17:57:19 -0700408}