make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / x86_64-all / mlib / fenv.c
blob17e49a77475d970986b55e453546a5f4c5396104
1 /*-
2 * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/lib/msun/amd64/fenv.c,v 1.4 2007/01/05 07:15:26 das Exp $
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #ifndef __AROS__
32 #include <machine/fpu.h>
33 #endif
34 #include <fenv.h>
36 const fenv_t __fe_dfl_env = {
37 { 0xffff0000 | __INITIAL_FPUCW__,
38 0xffff0000,
39 0xffffffff,
40 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
43 __INITIAL_MXCSR__
46 int
47 fesetexceptflag(const fexcept_t *flagp, int excepts)
49 fenv_t env;
51 __fnstenv(&env.__x87);
52 env.__x87.__status &= ~excepts;
53 env.__x87.__status |= *flagp & excepts;
54 __fldenv(env.__x87);
56 __stmxcsr(&env.__mxcsr);
57 env.__mxcsr &= ~excepts;
58 env.__mxcsr |= *flagp & excepts;
59 __ldmxcsr(env.__mxcsr);
61 return (0);
64 int
65 feraiseexcept(int excepts)
67 fexcept_t ex = excepts;
69 fesetexceptflag(&ex, excepts);
70 __fwait();
71 return (0);
74 int
75 fegetenv(fenv_t *envp)
78 __fnstenv(&envp->__x87);
79 __stmxcsr(&envp->__mxcsr);
81 * fnstenv masks all exceptions, so we need to restore the
82 * control word to avoid this side effect.
84 __fldcw(envp->__x87.__control);
85 return (0);
88 int
89 feholdexcept(fenv_t *envp)
91 int mxcsr;
93 __stmxcsr(&mxcsr);
94 __fnstenv(&envp->__x87);
95 __fnclex();
96 envp->__mxcsr = mxcsr;
97 mxcsr &= ~FE_ALL_EXCEPT;
98 mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
99 __ldmxcsr(mxcsr);
100 return (0);
104 feupdateenv(const fenv_t *envp)
106 int mxcsr, status;
108 __fnstsw(&status);
109 __stmxcsr(&mxcsr);
110 fesetenv(envp);
111 feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
112 return (0);
116 __feenableexcept(int mask)
118 int mxcsr, control, omask;
120 mask &= FE_ALL_EXCEPT;
121 __fnstcw(&control);
122 __stmxcsr(&mxcsr);
123 omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
124 control &= ~mask;
125 __fldcw(control);
126 mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
127 __ldmxcsr(mxcsr);
128 return (~omask);
132 __fedisableexcept(int mask)
134 int mxcsr, control, omask;
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);
147 __weak_reference(__feenableexcept, feenableexcept);
148 __weak_reference(__fedisableexcept, fedisableexcept);