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