Remove some unnecessary inclusions of <sys/cdefs.h> across the tree.
[dragonfly.git] / lib / libm / arch / x86_64 / fenv.c
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: src/lib/msun/amd64/fenv.c,v 1.5 2010/02/03 20:23:47 kib Exp $
27  */
28
29 #include <sys/types.h>
30 #include <machine/npx.h>
31 #include "fenv.h"
32
33 const fenv_t __fe_dfl_env = {
34         { 0xffff0000 | __INITIAL_FPUCW__,
35           0xffff0000,
36           0xffffffff,
37           { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
39         },
40         __INITIAL_MXCSR__
41 };
42
43 int
44 fesetexceptflag(const fexcept_t *flagp, int excepts)
45 {
46         fenv_t env;
47
48         __fnstenv(&env.__x87);
49         env.__x87.__status &= ~excepts;
50         env.__x87.__status |= *flagp & excepts;
51         __fldenv(env.__x87);
52
53         __stmxcsr(&env.__mxcsr);
54         env.__mxcsr &= ~excepts;
55         env.__mxcsr |= *flagp & excepts;
56         __ldmxcsr(env.__mxcsr);
57
58         return (0);
59 }
60
61 int
62 feraiseexcept(int excepts)
63 {
64         fexcept_t ex = excepts;
65
66         fesetexceptflag(&ex, excepts);
67         __fwait();
68         return (0);
69 }
70
71 int
72 fegetenv(fenv_t *envp)
73 {
74
75         __fnstenv(&envp->__x87);
76         __stmxcsr(&envp->__mxcsr);
77         /*
78          * fnstenv masks all exceptions, so we need to restore the
79          * control word to avoid this side effect.
80          */
81         __fldcw(envp->__x87.__control);
82         return (0);
83 }
84
85 int
86 feholdexcept(fenv_t *envp)
87 {
88         __uint32_t mxcsr;
89
90         __stmxcsr(&mxcsr);
91         __fnstenv(&envp->__x87);
92         __fnclex();
93         envp->__mxcsr = mxcsr;
94         mxcsr &= ~FE_ALL_EXCEPT;
95         mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
96         __ldmxcsr(mxcsr);
97         return (0);
98 }
99
100 int
101 feupdateenv(const fenv_t *envp)
102 {
103         __uint32_t mxcsr;
104         __uint16_t status;
105
106         __fnstsw(&status);
107         __stmxcsr(&mxcsr);
108         fesetenv(envp);
109         feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
110         return (0);
111 }
112
113 int
114 __feenableexcept(int mask)
115 {
116         __uint32_t mxcsr, omask;
117         __uint16_t control;
118
119         mask &= FE_ALL_EXCEPT;
120         __fnstcw(&control);
121         __stmxcsr(&mxcsr);
122         omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
123         control &= ~mask;
124         __fldcw(control);
125         mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
126         __ldmxcsr(mxcsr);
127         return (~omask);
128 }
129
130 int
131 __fedisableexcept(int mask)
132 {
133         __uint32_t mxcsr, omask;
134         __uint16_t control;
135
136         mask &= FE_ALL_EXCEPT;
137         __fnstcw(&control);
138         __stmxcsr(&mxcsr);
139         omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
140         control |= mask;
141         __fldcw(control);
142         mxcsr |= mask << _SSE_EMASK_SHIFT;
143         __ldmxcsr(mxcsr);
144         return (~omask);
145 }
146
147 __weak_reference(__feenableexcept, feenableexcept);
148 __weak_reference(__fedisableexcept, fedisableexcept);