1*0a6a1f1dSLionel Sambuc /* $NetBSD: fenv.c,v 1.2 2014/12/27 17:52:45 martin Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2014 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc * All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*0a6a1f1dSLionel Sambuc * by Matt Thomas of 3am Software Foundry.
9*0a6a1f1dSLionel Sambuc *
10*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
12*0a6a1f1dSLionel Sambuc * are met:
13*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
18*0a6a1f1dSLionel Sambuc *
19*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0a6a1f1dSLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0a6a1f1dSLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0a6a1f1dSLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0a6a1f1dSLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0a6a1f1dSLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0a6a1f1dSLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0a6a1f1dSLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0a6a1f1dSLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0a6a1f1dSLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0a6a1f1dSLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30*0a6a1f1dSLionel Sambuc */
31*0a6a1f1dSLionel Sambuc
32*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
33*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: fenv.c,v 1.2 2014/12/27 17:52:45 martin Exp $");
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc #include <sys/param.h>
36*0a6a1f1dSLionel Sambuc #include <sys/types.h>
37*0a6a1f1dSLionel Sambuc #include <assert.h>
38*0a6a1f1dSLionel Sambuc #include <fenv.h>
39*0a6a1f1dSLionel Sambuc #include <string.h>
40*0a6a1f1dSLionel Sambuc #include <unistd.h>
41*0a6a1f1dSLionel Sambuc #include <inttypes.h>
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambuc #include <aarch64/armreg.h>
44*0a6a1f1dSLionel Sambuc
45*0a6a1f1dSLionel Sambuc const fenv_t __fe_dfl_env = {
46*0a6a1f1dSLionel Sambuc .__fpsr = 0,
47*0a6a1f1dSLionel Sambuc .__fpcr = FPCR_FZ|FPCR_DN|FPCR_RN,
48*0a6a1f1dSLionel Sambuc };
49*0a6a1f1dSLionel Sambuc
50*0a6a1f1dSLionel Sambuc /*
51*0a6a1f1dSLionel Sambuc * The feclearexcept() function shall attempt to clear the supported
52*0a6a1f1dSLionel Sambuc * floating-point exceptions represented by excepts.
53*0a6a1f1dSLionel Sambuc */
54*0a6a1f1dSLionel Sambuc int
feclearexcept(int excepts)55*0a6a1f1dSLionel Sambuc feclearexcept(int excepts)
56*0a6a1f1dSLionel Sambuc {
57*0a6a1f1dSLionel Sambuc #ifndef lint
58*0a6a1f1dSLionel Sambuc _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
59*0a6a1f1dSLionel Sambuc #endif
60*0a6a1f1dSLionel Sambuc unsigned int tmp = reg_fpsr_read() & ~__SHIFTIN(excepts, FPSR_CSUM);
61*0a6a1f1dSLionel Sambuc reg_fpsr_write(tmp);
62*0a6a1f1dSLionel Sambuc return 0;
63*0a6a1f1dSLionel Sambuc }
64*0a6a1f1dSLionel Sambuc
65*0a6a1f1dSLionel Sambuc /*
66*0a6a1f1dSLionel Sambuc * The fegetexceptflag() function shall attempt to store an
67*0a6a1f1dSLionel Sambuc * implementation-defined representation of the states of the floating-point
68*0a6a1f1dSLionel Sambuc * status flags indicated by the argument excepts in the object pointed to by
69*0a6a1f1dSLionel Sambuc * the argument flagp.
70*0a6a1f1dSLionel Sambuc */
71*0a6a1f1dSLionel Sambuc int
fegetexceptflag(fexcept_t * flagp,int excepts)72*0a6a1f1dSLionel Sambuc fegetexceptflag(fexcept_t *flagp, int excepts)
73*0a6a1f1dSLionel Sambuc {
74*0a6a1f1dSLionel Sambuc _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
75*0a6a1f1dSLionel Sambuc *flagp = __SHIFTOUT(reg_fpsr_read(), FPSR_CSUM) & excepts;
76*0a6a1f1dSLionel Sambuc return 0;
77*0a6a1f1dSLionel Sambuc }
78*0a6a1f1dSLionel Sambuc
79*0a6a1f1dSLionel Sambuc /*
80*0a6a1f1dSLionel Sambuc * The feraiseexcept() function shall attempt to raise the supported
81*0a6a1f1dSLionel Sambuc * floating-point exceptions represented by the argument excepts. The order
82*0a6a1f1dSLionel Sambuc * in which these floating-point exceptions are raised is unspecified.
83*0a6a1f1dSLionel Sambuc */
84*0a6a1f1dSLionel Sambuc int
feraiseexcept(int excepts)85*0a6a1f1dSLionel Sambuc feraiseexcept(int excepts)
86*0a6a1f1dSLionel Sambuc {
87*0a6a1f1dSLionel Sambuc #ifndef lint
88*0a6a1f1dSLionel Sambuc _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
89*0a6a1f1dSLionel Sambuc #endif
90*0a6a1f1dSLionel Sambuc #ifdef __SOFTFP__
91*0a6a1f1dSLionel Sambuc excepts &= fpgetsticky();
92*0a6a1f1dSLionel Sambuc
93*0a6a1f1dSLionel Sambuc if (excepts) {
94*0a6a1f1dSLionel Sambuc siginfo_t info;
95*0a6a1f1dSLionel Sambuc memset(&info, 0, sizeof info);
96*0a6a1f1dSLionel Sambuc info.si_signo = SIGFPE;
97*0a6a1f1dSLionel Sambuc info.si_pid = getpid();
98*0a6a1f1dSLionel Sambuc info.si_uid = geteuid();
99*0a6a1f1dSLionel Sambuc if (excepts & FE_UNDERFLOW)
100*0a6a1f1dSLionel Sambuc info.si_code = FPE_FLTUND;
101*0a6a1f1dSLionel Sambuc else if (excepts & FE_OVERFLOW)
102*0a6a1f1dSLionel Sambuc info.si_code = FPE_FLTOVF;
103*0a6a1f1dSLionel Sambuc else if (excepts & FE_DIVBYZERO)
104*0a6a1f1dSLionel Sambuc info.si_code = FPE_FLTDIV;
105*0a6a1f1dSLionel Sambuc else if (excepts & FE_INVALID)
106*0a6a1f1dSLionel Sambuc info.si_code = FPE_FLTINV;
107*0a6a1f1dSLionel Sambuc else if (excepts & FE_INEXACT)
108*0a6a1f1dSLionel Sambuc info.si_code = FPE_FLTRES;
109*0a6a1f1dSLionel Sambuc sigqueueinfo(getpid(), &info);
110*0a6a1f1dSLionel Sambuc }
111*0a6a1f1dSLionel Sambuc #else
112*0a6a1f1dSLionel Sambuc unsigned int fpsr = reg_fpsr_read();
113*0a6a1f1dSLionel Sambuc fpsr = (fpsr & ~FPSR_CSUM) | __SHIFTIN(excepts, FPSR_CSUM);
114*0a6a1f1dSLionel Sambuc reg_fpsr_write(fpsr);
115*0a6a1f1dSLionel Sambuc unsigned int fpcr = reg_fpcr_read();
116*0a6a1f1dSLionel Sambuc fpcr = (fpcr & ~FPCR_ESUM) | __SHIFTIN(excepts, FPCR_ESUM);
117*0a6a1f1dSLionel Sambuc reg_fpcr_write(fpcr);
118*0a6a1f1dSLionel Sambuc #endif
119*0a6a1f1dSLionel Sambuc return 0;
120*0a6a1f1dSLionel Sambuc }
121*0a6a1f1dSLionel Sambuc
122*0a6a1f1dSLionel Sambuc /*
123*0a6a1f1dSLionel Sambuc * The fesetexceptflag() function shall attempt to set the floating-point
124*0a6a1f1dSLionel Sambuc * status flags indicated by the argument excepts to the states stored in the
125*0a6a1f1dSLionel Sambuc * object pointed to by flagp. The value pointed to by flagp shall have been
126*0a6a1f1dSLionel Sambuc * set by a previous call to fegetexceptflag() whose second argument
127*0a6a1f1dSLionel Sambuc * represented at least those floating-point exceptions represented by the
128*0a6a1f1dSLionel Sambuc * argument excepts. This function does not raise floating-point exceptions,
129*0a6a1f1dSLionel Sambuc * but only sets the state of the flags.
130*0a6a1f1dSLionel Sambuc */
131*0a6a1f1dSLionel Sambuc int
fesetexceptflag(const fexcept_t * flagp,int excepts)132*0a6a1f1dSLionel Sambuc fesetexceptflag(const fexcept_t *flagp, int excepts)
133*0a6a1f1dSLionel Sambuc {
134*0a6a1f1dSLionel Sambuc #ifndef lint
135*0a6a1f1dSLionel Sambuc _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
136*0a6a1f1dSLionel Sambuc #endif
137*0a6a1f1dSLionel Sambuc unsigned int fpsr = reg_fpsr_read();
138*0a6a1f1dSLionel Sambuc fpsr &= ~__SHIFTIN(excepts, FPSR_CSUM);
139*0a6a1f1dSLionel Sambuc fpsr |= __SHIFTIN((*flagp & excepts), FPSR_CSUM);
140*0a6a1f1dSLionel Sambuc reg_fpsr_write(fpsr);
141*0a6a1f1dSLionel Sambuc return 0;
142*0a6a1f1dSLionel Sambuc }
143*0a6a1f1dSLionel Sambuc
144*0a6a1f1dSLionel Sambuc /*
145*0a6a1f1dSLionel Sambuc * The fetestexcept() function shall determine which of a specified subset of
146*0a6a1f1dSLionel Sambuc * the floating-point exception flags are currently set. The excepts argument
147*0a6a1f1dSLionel Sambuc * specifies the floating-point status flags to be queried.
148*0a6a1f1dSLionel Sambuc */
149*0a6a1f1dSLionel Sambuc int
fetestexcept(int excepts)150*0a6a1f1dSLionel Sambuc fetestexcept(int excepts)
151*0a6a1f1dSLionel Sambuc {
152*0a6a1f1dSLionel Sambuc _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
153*0a6a1f1dSLionel Sambuc return __SHIFTOUT(reg_fpsr_read(), FPSR_CSUM) & excepts;
154*0a6a1f1dSLionel Sambuc }
155*0a6a1f1dSLionel Sambuc
156*0a6a1f1dSLionel Sambuc /*
157*0a6a1f1dSLionel Sambuc * The fegetround() function shall get the current rounding direction.
158*0a6a1f1dSLionel Sambuc */
159*0a6a1f1dSLionel Sambuc int
fegetround(void)160*0a6a1f1dSLionel Sambuc fegetround(void)
161*0a6a1f1dSLionel Sambuc {
162*0a6a1f1dSLionel Sambuc return __SHIFTOUT(reg_fpcr_read(), FPCR_RMODE);
163*0a6a1f1dSLionel Sambuc }
164*0a6a1f1dSLionel Sambuc
165*0a6a1f1dSLionel Sambuc /*
166*0a6a1f1dSLionel Sambuc * The fesetround() function shall establish the rounding direction represented
167*0a6a1f1dSLionel Sambuc * by its argument round. If the argument is not equal to the value of a
168*0a6a1f1dSLionel Sambuc * rounding direction macro, the rounding direction is not changed.
169*0a6a1f1dSLionel Sambuc */
170*0a6a1f1dSLionel Sambuc int
fesetround(int round)171*0a6a1f1dSLionel Sambuc fesetround(int round)
172*0a6a1f1dSLionel Sambuc {
173*0a6a1f1dSLionel Sambuc #ifndef lint
174*0a6a1f1dSLionel Sambuc _DIAGASSERT(!(round & ~__SHIFTOUT(FPCR_RMODE, FPCR_RMODE)));
175*0a6a1f1dSLionel Sambuc #endif
176*0a6a1f1dSLionel Sambuc unsigned int fpcr = reg_fpcr_read() & ~FPCR_RMODE;
177*0a6a1f1dSLionel Sambuc fpcr |= __SHIFTIN(round, FPCR_RMODE);
178*0a6a1f1dSLionel Sambuc reg_fpcr_write(fpcr);
179*0a6a1f1dSLionel Sambuc return 0;
180*0a6a1f1dSLionel Sambuc }
181*0a6a1f1dSLionel Sambuc
182*0a6a1f1dSLionel Sambuc /*
183*0a6a1f1dSLionel Sambuc * The fegetenv() function shall attempt to store the current floating-point
184*0a6a1f1dSLionel Sambuc * environment in the object pointed to by envp.
185*0a6a1f1dSLionel Sambuc */
186*0a6a1f1dSLionel Sambuc int
fegetenv(fenv_t * envp)187*0a6a1f1dSLionel Sambuc fegetenv(fenv_t *envp)
188*0a6a1f1dSLionel Sambuc {
189*0a6a1f1dSLionel Sambuc envp->__fpcr = reg_fpcr_read();
190*0a6a1f1dSLionel Sambuc envp->__fpsr = reg_fpsr_read();
191*0a6a1f1dSLionel Sambuc return 0;
192*0a6a1f1dSLionel Sambuc }
193*0a6a1f1dSLionel Sambuc
194*0a6a1f1dSLionel Sambuc /*
195*0a6a1f1dSLionel Sambuc * The feholdexcept() function shall save the current floating-point
196*0a6a1f1dSLionel Sambuc * environment in the object pointed to by envp, clear the floating-point
197*0a6a1f1dSLionel Sambuc * status flags, and then install a non-stop (continue on floating-point
198*0a6a1f1dSLionel Sambuc * exceptions) mode, if available, for all floating-point exceptions.
199*0a6a1f1dSLionel Sambuc */
200*0a6a1f1dSLionel Sambuc int
feholdexcept(fenv_t * envp)201*0a6a1f1dSLionel Sambuc feholdexcept(fenv_t *envp)
202*0a6a1f1dSLionel Sambuc {
203*0a6a1f1dSLionel Sambuc envp->__fpsr = reg_fpsr_read();
204*0a6a1f1dSLionel Sambuc envp->__fpcr = reg_fpcr_read();
205*0a6a1f1dSLionel Sambuc reg_fpsr_write(envp->__fpsr & ~FPSR_CSUM);
206*0a6a1f1dSLionel Sambuc reg_fpcr_write(envp->__fpcr & ~FPCR_ESUM);
207*0a6a1f1dSLionel Sambuc return 0;
208*0a6a1f1dSLionel Sambuc }
209*0a6a1f1dSLionel Sambuc
210*0a6a1f1dSLionel Sambuc /*
211*0a6a1f1dSLionel Sambuc * The fesetenv() function shall attempt to establish the floating-point
212*0a6a1f1dSLionel Sambuc * environment represented by the object pointed to by envp. The fesetenv()
213*0a6a1f1dSLionel Sambuc * function does not raise floating-point exceptions, but only installs the
214*0a6a1f1dSLionel Sambuc * state of the floating-point status flags represented through its argument.
215*0a6a1f1dSLionel Sambuc */
216*0a6a1f1dSLionel Sambuc int
fesetenv(const fenv_t * envp)217*0a6a1f1dSLionel Sambuc fesetenv(const fenv_t *envp)
218*0a6a1f1dSLionel Sambuc {
219*0a6a1f1dSLionel Sambuc reg_fpsr_write(envp->__fpsr);
220*0a6a1f1dSLionel Sambuc return 0;
221*0a6a1f1dSLionel Sambuc }
222*0a6a1f1dSLionel Sambuc
223*0a6a1f1dSLionel Sambuc /*
224*0a6a1f1dSLionel Sambuc * The feupdateenv() function shall attempt to save the currently raised
225*0a6a1f1dSLionel Sambuc * floating-point exceptions in its automatic storage, attempt to install the
226*0a6a1f1dSLionel Sambuc * floating-point environment represented by the object pointed to by envp,
227*0a6a1f1dSLionel Sambuc * and then attempt to raise the saved floating-point exceptions.
228*0a6a1f1dSLionel Sambuc */
229*0a6a1f1dSLionel Sambuc int
feupdateenv(const fenv_t * envp)230*0a6a1f1dSLionel Sambuc feupdateenv(const fenv_t *envp)
231*0a6a1f1dSLionel Sambuc {
232*0a6a1f1dSLionel Sambuc #ifndef lint
233*0a6a1f1dSLionel Sambuc _DIAGASSERT(envp != NULL);
234*0a6a1f1dSLionel Sambuc #endif
235*0a6a1f1dSLionel Sambuc reg_fpsr_write(envp->__fpsr);
236*0a6a1f1dSLionel Sambuc reg_fpcr_write(envp->__fpcr);
237*0a6a1f1dSLionel Sambuc
238*0a6a1f1dSLionel Sambuc /* Success */
239*0a6a1f1dSLionel Sambuc return 0;
240*0a6a1f1dSLionel Sambuc }
241*0a6a1f1dSLionel Sambuc
242*0a6a1f1dSLionel Sambuc int
feenableexcept(int excepts)243*0a6a1f1dSLionel Sambuc feenableexcept(int excepts)
244*0a6a1f1dSLionel Sambuc {
245*0a6a1f1dSLionel Sambuc const uint32_t __fpcr = reg_fpcr_read();
246*0a6a1f1dSLionel Sambuc reg_fpcr_write((__fpcr & ~FPCR_ESUM) | __SHIFTIN(excepts, FPCR_ESUM));
247*0a6a1f1dSLionel Sambuc return __SHIFTOUT(__fpcr, FPCR_ESUM);
248*0a6a1f1dSLionel Sambuc }
249*0a6a1f1dSLionel Sambuc
250*0a6a1f1dSLionel Sambuc int
fedisableexcept(int excepts)251*0a6a1f1dSLionel Sambuc fedisableexcept(int excepts)
252*0a6a1f1dSLionel Sambuc {
253*0a6a1f1dSLionel Sambuc const uint32_t __fpcr = reg_fpcr_read();
254*0a6a1f1dSLionel Sambuc reg_fpcr_write(__fpcr & ~__SHIFTIN(excepts, FPCR_ESUM));
255*0a6a1f1dSLionel Sambuc return __SHIFTOUT(__fpcr, FPCR_ESUM);
256*0a6a1f1dSLionel Sambuc }
257*0a6a1f1dSLionel Sambuc
258*0a6a1f1dSLionel Sambuc int
fegetexcept(void)259*0a6a1f1dSLionel Sambuc fegetexcept(void)
260*0a6a1f1dSLionel Sambuc {
261*0a6a1f1dSLionel Sambuc const uint32_t __fpcr = reg_fpcr_read();
262*0a6a1f1dSLionel Sambuc return __SHIFTOUT(__fpcr, FPCR_ESUM);
263*0a6a1f1dSLionel Sambuc }
264