blob: 9edaf88fda43aa3a9894f8ae432c4ba8230774f5 [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>
Elliott Hughesafe835d2016-04-02 08:36:33 -070031
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 Chupince7add12013-09-20 19:09:55 +040039
Elliott Hughes43bf81e2014-06-09 14:29:25 -070040#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 Hughes770a3492013-10-01 17:57:19 -070049/*
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 Chupin7ba84d32014-02-28 00:36:10 +040061const fenv_t __fe_dfl_env = {
Calin Juravle2d367902014-02-25 14:49:41 +000062 {
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 Chupince7add12013-09-20 19:09:55 +040074};
75
Pavel Chupince7add12013-09-20 19:09:55 +040076
Elliott Hughes770a3492013-10-01 17:57:19 -070077/*
78 * The feclearexcept() function clears the supported floating-point exceptions
79 * represented by `excepts'.
80 */
Pavel Chupince7add12013-09-20 19:09:55 +040081int
Elliott Hughes770a3492013-10-01 17:57:19 -070082feclearexcept(int excepts)
Pavel Chupince7add12013-09-20 19:09:55 +040083{
Calin Juravle2d367902014-02-25 14:49:41 +000084 fenv_t fenv;
85 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +040086
Calin Juravle2d367902014-02-25 14:49:41 +000087 excepts &= FE_ALL_EXCEPT;
Pavel Chupince7add12013-09-20 19:09:55 +040088
Calin Juravle2d367902014-02-25 14:49:41 +000089 /* Store the current x87 floating-point environment */
90 __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -070091
Calin Juravle2d367902014-02-25 14:49:41 +000092 /* Clear the requested floating-point exceptions */
93 fenv.__x87.__status &= ~excepts;
Elliott Hughes770a3492013-10-01 17:57:19 -070094
Calin Juravle2d367902014-02-25 14:49:41 +000095 /* Load the x87 floating-point environent */
96 __asm__ __volatile__ ("fldenv %0" : : "m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -070097
Calin Juravle2d367902014-02-25 14:49:41 +000098 /* Same for SSE environment */
99 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
100 mxcsr &= ~excepts;
101 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Pavel Chupince7add12013-09-20 19:09:55 +0400102
Calin Juravle2d367902014-02-25 14:49:41 +0000103 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400104}
105
Elliott Hughes770a3492013-10-01 17:57:19 -0700106/*
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 */
111int
112fegetexceptflag(fexcept_t *flagp, int excepts)
113{
Calin Juravle2d367902014-02-25 14:49:41 +0000114 unsigned short status;
115 unsigned int mxcsr;
Elliott Hughes770a3492013-10-01 17:57:19 -0700116
Calin Juravle2d367902014-02-25 14:49:41 +0000117 excepts &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700118
Calin Juravle2d367902014-02-25 14:49:41 +0000119 /* Store the current x87 status register */
120 __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
Elliott Hughes770a3492013-10-01 17:57:19 -0700121
Calin Juravle2d367902014-02-25 14:49:41 +0000122 /* Store the MXCSR register */
123 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700124
Calin Juravle2d367902014-02-25 14:49:41 +0000125 /* Store the results in flagp */
126 *flagp = (status | mxcsr) & excepts;
Elliott Hughes770a3492013-10-01 17:57:19 -0700127
Calin Juravle2d367902014-02-25 14:49:41 +0000128 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700129}
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 Chupince7add12013-09-20 19:09:55 +0400141int
142feraiseexcept(int excepts)
143{
Calin Juravle2d367902014-02-25 14:49:41 +0000144 excepts &= FE_ALL_EXCEPT;
Pavel Chupince7add12013-09-20 19:09:55 +0400145
Calin Juravle2d367902014-02-25 14:49:41 +0000146 fesetexceptflag((fexcept_t *)&excepts, excepts);
147 __asm__ __volatile__ ("fwait");
Elliott Hughes770a3492013-10-01 17:57:19 -0700148
Calin Juravle2d367902014-02-25 14:49:41 +0000149 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400150}
151
Elliott Hughes770a3492013-10-01 17:57:19 -0700152/*
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 */
157int
158fesetexceptflag(const fexcept_t *flagp, int excepts)
159{
Calin Juravle2d367902014-02-25 14:49:41 +0000160 fenv_t fenv;
161 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +0400162
Calin Juravle2d367902014-02-25 14:49:41 +0000163 excepts &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700164
Calin Juravle2d367902014-02-25 14:49:41 +0000165 /* Store the current x87 floating-point environment */
166 __asm__ __volatile__ ("fnstenv %0" : "=m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -0700167
Calin Juravle2d367902014-02-25 14:49:41 +0000168 /* Set the requested status flags */
169 fenv.__x87.__status &= ~excepts;
170 fenv.__x87.__status |= *flagp & excepts;
Elliott Hughes770a3492013-10-01 17:57:19 -0700171
Calin Juravle2d367902014-02-25 14:49:41 +0000172 /* Load the x87 floating-point environent */
173 __asm__ __volatile__ ("fldenv %0" : : "m" (fenv));
Elliott Hughes770a3492013-10-01 17:57:19 -0700174
Calin Juravle2d367902014-02-25 14:49:41 +0000175 /* 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 Hughes770a3492013-10-01 17:57:19 -0700180
Calin Juravle2d367902014-02-25 14:49:41 +0000181 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700182}
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 */
189int
190fetestexcept(int excepts)
191{
Calin Juravle2d367902014-02-25 14:49:41 +0000192 unsigned short status;
193 unsigned int mxcsr;
Elliott Hughes770a3492013-10-01 17:57:19 -0700194
Calin Juravle2d367902014-02-25 14:49:41 +0000195 excepts &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700196
Calin Juravle2d367902014-02-25 14:49:41 +0000197 /* Store the current x87 status register */
198 __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
Elliott Hughes770a3492013-10-01 17:57:19 -0700199
Calin Juravle2d367902014-02-25 14:49:41 +0000200 /* Store the MXCSR register state */
201 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700202
Calin Juravle2d367902014-02-25 14:49:41 +0000203 return ((status | mxcsr) & excepts);
Elliott Hughes770a3492013-10-01 17:57:19 -0700204}
205
206/*
207 * The fegetround() function gets the current rounding direction.
208 */
209int
210fegetround(void)
211{
Calin Juravle2d367902014-02-25 14:49:41 +0000212 unsigned short control;
Elliott Hughes770a3492013-10-01 17:57:19 -0700213
Calin Juravle2d367902014-02-25 14:49:41 +0000214 /*
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 Hughes770a3492013-10-01 17:57:19 -0700221
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700222 return (control & X87_ROUND_MASK);
Elliott Hughes770a3492013-10-01 17:57:19 -0700223}
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 */
230int
231fesetround(int round)
232{
Calin Juravle2d367902014-02-25 14:49:41 +0000233 unsigned short control;
234 unsigned int mxcsr;
Elliott Hughes770a3492013-10-01 17:57:19 -0700235
Calin Juravle2d367902014-02-25 14:49:41 +0000236 /* Check whether requested rounding direction is supported */
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700237 if (round & ~X87_ROUND_MASK)
Calin Juravle2d367902014-02-25 14:49:41 +0000238 return (-1);
Elliott Hughes770a3492013-10-01 17:57:19 -0700239
Calin Juravle2d367902014-02-25 14:49:41 +0000240 /* Store the current x87 control word register */
241 __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700242
Calin Juravle2d367902014-02-25 14:49:41 +0000243 /* Set the rounding direction */
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700244 control &= ~X87_ROUND_MASK;
Calin Juravle2d367902014-02-25 14:49:41 +0000245 control |= round;
Elliott Hughes770a3492013-10-01 17:57:19 -0700246
Calin Juravle2d367902014-02-25 14:49:41 +0000247 /* Load the x87 control word register */
248 __asm__ __volatile__ ("fldcw %0" : : "m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700249
Calin Juravle2d367902014-02-25 14:49:41 +0000250 /* Same for the SSE environment */
251 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700252 mxcsr &= ~(X87_ROUND_MASK << SSE_ROUND_SHIFT);
253 mxcsr |= round << SSE_ROUND_SHIFT;
Calin Juravle2d367902014-02-25 14:49:41 +0000254 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700255
Calin Juravle2d367902014-02-25 14:49:41 +0000256 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700257}
258
259/*
260 * The fegetenv() function attempts to store the current floating-point
261 * environment in the object pointed to by envp.
262 */
Pavel Chupince7add12013-09-20 19:09:55 +0400263int
264fegetenv(fenv_t *envp)
265{
Calin Juravle2d367902014-02-25 14:49:41 +0000266 /* Store the current x87 floating-point environment */
267 __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp));
Pavel Chupince7add12013-09-20 19:09:55 +0400268
Calin Juravle2d367902014-02-25 14:49:41 +0000269 /* Store the MXCSR register state */
270 __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700271
Calin Juravle2d367902014-02-25 14:49:41 +0000272 /*
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 Hughes770a3492013-10-01 17:57:19 -0700281
Calin Juravle2d367902014-02-25 14:49:41 +0000282 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400283}
284
Elliott Hughes770a3492013-10-01 17:57:19 -0700285/*
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 Chupince7add12013-09-20 19:09:55 +0400291int
292feholdexcept(fenv_t *envp)
293{
Calin Juravle2d367902014-02-25 14:49:41 +0000294 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +0400295
Calin Juravle2d367902014-02-25 14:49:41 +0000296 /* Store the current x87 floating-point environment */
297 __asm__ __volatile__ ("fnstenv %0" : "=m" (*envp));
Elliott Hughes770a3492013-10-01 17:57:19 -0700298
Calin Juravle2d367902014-02-25 14:49:41 +0000299 /* Clear all exception flags in FPU */
300 __asm__ __volatile__ ("fnclex");
Elliott Hughes770a3492013-10-01 17:57:19 -0700301
Calin Juravle2d367902014-02-25 14:49:41 +0000302 /* Store the MXCSR register state */
303 __asm__ __volatile__ ("stmxcsr %0" : "=m" (envp->__mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700304
Calin Juravle2d367902014-02-25 14:49:41 +0000305 /* Clear exception flags in MXCSR */
306 mxcsr = envp->__mxcsr;
307 mxcsr &= ~FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700308
Calin Juravle2d367902014-02-25 14:49:41 +0000309 /* Mask all exceptions */
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700310 mxcsr |= FE_ALL_EXCEPT << SSE_MASK_SHIFT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700311
Calin Juravle2d367902014-02-25 14:49:41 +0000312 /* Store the MXCSR register */
313 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700314
Calin Juravle2d367902014-02-25 14:49:41 +0000315 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400316}
317
Elliott Hughes770a3492013-10-01 17:57:19 -0700318/*
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 */
326int
327fesetenv(const fenv_t *envp)
328{
Calin Juravle2d367902014-02-25 14:49:41 +0000329 /* Load the x87 floating-point environent */
330 __asm__ __volatile__ ("fldenv %0" : : "m" (*envp));
Pavel Chupince7add12013-09-20 19:09:55 +0400331
Calin Juravle2d367902014-02-25 14:49:41 +0000332 /* Store the MXCSR register */
333 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (envp->__mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700334
Calin Juravle2d367902014-02-25 14:49:41 +0000335 return (0);
Elliott Hughes770a3492013-10-01 17:57:19 -0700336}
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 Chupince7add12013-09-20 19:09:55 +0400346int
347feupdateenv(const fenv_t *envp)
348{
Calin Juravle2d367902014-02-25 14:49:41 +0000349 unsigned short status;
350 unsigned int mxcsr;
Pavel Chupince7add12013-09-20 19:09:55 +0400351
Calin Juravle2d367902014-02-25 14:49:41 +0000352 /* Store the x87 status register */
353 __asm__ __volatile__ ("fnstsw %0" : "=am" (status));
Elliott Hughes770a3492013-10-01 17:57:19 -0700354
Calin Juravle2d367902014-02-25 14:49:41 +0000355 /* Store the MXCSR register */
356 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700357
Calin Juravle2d367902014-02-25 14:49:41 +0000358 /* Install new floating-point environment */
359 fesetenv(envp);
Elliott Hughes770a3492013-10-01 17:57:19 -0700360
Calin Juravle2d367902014-02-25 14:49:41 +0000361 /* Raise any previously accumulated exceptions */
362 feraiseexcept(status | mxcsr);
Elliott Hughes770a3492013-10-01 17:57:19 -0700363
Calin Juravle2d367902014-02-25 14:49:41 +0000364 return (0);
Pavel Chupince7add12013-09-20 19:09:55 +0400365}
366
Elliott Hughes770a3492013-10-01 17:57:19 -0700367/*
368 * The following functions are extentions to the standard
369 */
Pavel Chupince7add12013-09-20 19:09:55 +0400370int
Elliott Hughes770a3492013-10-01 17:57:19 -0700371feenableexcept(int mask)
Pavel Chupince7add12013-09-20 19:09:55 +0400372{
Calin Juravle2d367902014-02-25 14:49:41 +0000373 unsigned int mxcsr, omask;
374 unsigned short control;
Pavel Chupince7add12013-09-20 19:09:55 +0400375
Calin Juravle2d367902014-02-25 14:49:41 +0000376 mask &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700377
Calin Juravle2d367902014-02-25 14:49:41 +0000378 __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
379 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700380
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700381 omask = ~(control | (mxcsr >> SSE_MASK_SHIFT)) & FE_ALL_EXCEPT;
Calin Juravle2d367902014-02-25 14:49:41 +0000382 control &= ~mask;
383 __asm__ __volatile__ ("fldcw %0" : : "m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700384
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700385 mxcsr &= ~(mask << SSE_MASK_SHIFT);
Calin Juravle2d367902014-02-25 14:49:41 +0000386 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700387
Calin Juravle2d367902014-02-25 14:49:41 +0000388 return (omask);
Pavel Chupince7add12013-09-20 19:09:55 +0400389}
390
391int
Elliott Hughes770a3492013-10-01 17:57:19 -0700392fedisableexcept(int mask)
Pavel Chupince7add12013-09-20 19:09:55 +0400393{
Calin Juravle2d367902014-02-25 14:49:41 +0000394 unsigned int mxcsr, omask;
395 unsigned short control;
Pavel Chupince7add12013-09-20 19:09:55 +0400396
Calin Juravle2d367902014-02-25 14:49:41 +0000397 mask &= FE_ALL_EXCEPT;
Elliott Hughes770a3492013-10-01 17:57:19 -0700398
Calin Juravle2d367902014-02-25 14:49:41 +0000399 __asm__ __volatile__ ("fnstcw %0" : "=m" (control));
400 __asm__ __volatile__ ("stmxcsr %0" : "=m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700401
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700402 omask = ~(control | (mxcsr >> SSE_MASK_SHIFT)) & FE_ALL_EXCEPT;
Calin Juravle2d367902014-02-25 14:49:41 +0000403 control |= mask;
404 __asm__ __volatile__ ("fldcw %0" : : "m" (control));
Elliott Hughes770a3492013-10-01 17:57:19 -0700405
Elliott Hughes43bf81e2014-06-09 14:29:25 -0700406 mxcsr |= mask << SSE_MASK_SHIFT;
Calin Juravle2d367902014-02-25 14:49:41 +0000407 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (mxcsr));
Elliott Hughes770a3492013-10-01 17:57:19 -0700408
Calin Juravle2d367902014-02-25 14:49:41 +0000409 return (omask);
Pavel Chupince7add12013-09-20 19:09:55 +0400410}
411
Elliott Hughes770a3492013-10-01 17:57:19 -0700412int
413fegetexcept(void)
414{
Calin Juravle2d367902014-02-25 14:49:41 +0000415 unsigned short control;
Elliott Hughes770a3492013-10-01 17:57:19 -0700416
Calin Juravle2d367902014-02-25 14:49:41 +0000417 /*
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 Hughes770a3492013-10-01 17:57:19 -0700422
Calin Juravle2d367902014-02-25 14:49:41 +0000423 return (~control & FE_ALL_EXCEPT);
Elliott Hughes770a3492013-10-01 17:57:19 -0700424}