xref: /freebsd/lib/msun/amd64/fenv.c (revision ab0b9f6b3073e6c4d1dfbf07444d7db67a189a96)
1 /*-
2  * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #include <machine/fpu.h>
32 
33 #define	__fenv_static
34 #include "fenv.h"
35 
36 #ifdef __GNUC_GNU_INLINE__
37 #error "This file must be compiled with C99 'inline' semantics"
38 #endif
39 
40 const fenv_t __fe_dfl_env = {
41 	{ 0xffff0000 | __INITIAL_FPUCW__,
42 	  0xffff0000,
43 	  0xffffffff,
44 	  { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
46 	},
47 	__INITIAL_MXCSR__
48 };
49 
50 extern inline int feclearexcept(int __excepts);
51 extern inline int fegetexceptflag(fexcept_t *__flagp, int __excepts);
52 
53 int
54 fesetexceptflag(const fexcept_t *flagp, int excepts)
55 {
56 	fenv_t env;
57 
58 	__fnstenv(&env.__x87);
59 	env.__x87.__status &= ~excepts;
60 	env.__x87.__status |= *flagp & excepts;
61 	__fldenv(env.__x87);
62 
63 	__stmxcsr(&env.__mxcsr);
64 	env.__mxcsr &= ~excepts;
65 	env.__mxcsr |= *flagp & excepts;
66 	__ldmxcsr(env.__mxcsr);
67 
68 	return (0);
69 }
70 
71 int
72 feraiseexcept(int excepts)
73 {
74 	fexcept_t ex = excepts;
75 
76 	fesetexceptflag(&ex, excepts);
77 	__fwait();
78 	return (0);
79 }
80 
81 extern inline int fetestexcept(int __excepts);
82 extern inline int fegetround(void);
83 extern inline int fesetround(int __round);
84 
85 int
86 fegetenv(fenv_t *envp)
87 {
88 
89 	__fnstenv(&envp->__x87);
90 	__stmxcsr(&envp->__mxcsr);
91 	/*
92 	 * fnstenv masks all exceptions, so we need to restore the
93 	 * control word to avoid this side effect.
94 	 */
95 	__fldcw(envp->__x87.__control);
96 	return (0);
97 }
98 
99 int
100 feholdexcept(fenv_t *envp)
101 {
102 	__uint32_t mxcsr;
103 
104 	__stmxcsr(&mxcsr);
105 	__fnstenv(&envp->__x87);
106 	__fnclex();
107 	envp->__mxcsr = mxcsr;
108 	mxcsr &= ~FE_ALL_EXCEPT;
109 	mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
110 	__ldmxcsr(mxcsr);
111 	return (0);
112 }
113 
114 extern inline int fesetenv(const fenv_t *__envp);
115 
116 int
117 feupdateenv(const fenv_t *envp)
118 {
119 	__uint32_t mxcsr;
120 	__uint16_t status;
121 
122 	__fnstsw(&status);
123 	__stmxcsr(&mxcsr);
124 	fesetenv(envp);
125 	feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
126 	return (0);
127 }
128 
129 int
130 __feenableexcept(int mask)
131 {
132 	__uint32_t mxcsr, omask;
133 	__uint16_t control;
134 
135 	mask &= FE_ALL_EXCEPT;
136 	__fnstcw(&control);
137 	__stmxcsr(&mxcsr);
138 	omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
139 	control &= ~mask;
140 	__fldcw(control);
141 	mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
142 	__ldmxcsr(mxcsr);
143 	return (omask);
144 }
145 
146 int
147 __fedisableexcept(int mask)
148 {
149 	__uint32_t mxcsr, omask;
150 	__uint16_t control;
151 
152 	mask &= FE_ALL_EXCEPT;
153 	__fnstcw(&control);
154 	__stmxcsr(&mxcsr);
155 	omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
156 	control |= mask;
157 	__fldcw(control);
158 	mxcsr |= mask << _SSE_EMASK_SHIFT;
159 	__ldmxcsr(mxcsr);
160 	return (omask);
161 }
162 
163 __weak_reference(__feenableexcept, feenableexcept);
164 __weak_reference(__fedisableexcept, fedisableexcept);
165