1 /* $NetBSD: fenv.c,v 1.2 2014/12/27 17:52:45 martin Exp $ */
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas of 3am Software Foundry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: fenv.c,v 1.2 2014/12/27 17:52:45 martin Exp $");
35 #include <sys/param.h>
36 #include <sys/types.h>
43 #include <aarch64/armreg.h>
45 const fenv_t __fe_dfl_env
= {
47 .__fpcr
= FPCR_FZ
|FPCR_DN
|FPCR_RN
,
51 * The feclearexcept() function shall attempt to clear the supported
52 * floating-point exceptions represented by excepts.
55 feclearexcept(int excepts
)
58 _DIAGASSERT((except
& ~FE_ALL_EXCEPT
) == 0);
60 unsigned int tmp
= reg_fpsr_read() & ~__SHIFTIN(excepts
, FPSR_CSUM
);
66 * The fegetexceptflag() function shall attempt to store an
67 * implementation-defined representation of the states of the floating-point
68 * status flags indicated by the argument excepts in the object pointed to by
72 fegetexceptflag(fexcept_t
*flagp
, int excepts
)
74 _DIAGASSERT((except
& ~FE_ALL_EXCEPT
) == 0);
75 *flagp
= __SHIFTOUT(reg_fpsr_read(), FPSR_CSUM
) & excepts
;
80 * The feraiseexcept() function shall attempt to raise the supported
81 * floating-point exceptions represented by the argument excepts. The order
82 * in which these floating-point exceptions are raised is unspecified.
85 feraiseexcept(int excepts
)
88 _DIAGASSERT((except
& ~FE_ALL_EXCEPT
) == 0);
91 excepts
&= fpgetsticky();
95 memset(&info
, 0, sizeof info
);
96 info
.si_signo
= SIGFPE
;
97 info
.si_pid
= getpid();
98 info
.si_uid
= geteuid();
99 if (excepts
& FE_UNDERFLOW
)
100 info
.si_code
= FPE_FLTUND
;
101 else if (excepts
& FE_OVERFLOW
)
102 info
.si_code
= FPE_FLTOVF
;
103 else if (excepts
& FE_DIVBYZERO
)
104 info
.si_code
= FPE_FLTDIV
;
105 else if (excepts
& FE_INVALID
)
106 info
.si_code
= FPE_FLTINV
;
107 else if (excepts
& FE_INEXACT
)
108 info
.si_code
= FPE_FLTRES
;
109 sigqueueinfo(getpid(), &info
);
112 unsigned int fpsr
= reg_fpsr_read();
113 fpsr
= (fpsr
& ~FPSR_CSUM
) | __SHIFTIN(excepts
, FPSR_CSUM
);
114 reg_fpsr_write(fpsr
);
115 unsigned int fpcr
= reg_fpcr_read();
116 fpcr
= (fpcr
& ~FPCR_ESUM
) | __SHIFTIN(excepts
, FPCR_ESUM
);
117 reg_fpcr_write(fpcr
);
123 * The fesetexceptflag() function shall attempt to set the floating-point
124 * status flags indicated by the argument excepts to the states stored in the
125 * object pointed to by flagp. The value pointed to by flagp shall have been
126 * set by a previous call to fegetexceptflag() whose second argument
127 * represented at least those floating-point exceptions represented by the
128 * argument excepts. This function does not raise floating-point exceptions,
129 * but only sets the state of the flags.
132 fesetexceptflag(const fexcept_t
*flagp
, int excepts
)
135 _DIAGASSERT((except
& ~FE_ALL_EXCEPT
) == 0);
137 unsigned int fpsr
= reg_fpsr_read();
138 fpsr
&= ~__SHIFTIN(excepts
, FPSR_CSUM
);
139 fpsr
|= __SHIFTIN((*flagp
& excepts
), FPSR_CSUM
);
140 reg_fpsr_write(fpsr
);
145 * The fetestexcept() function shall determine which of a specified subset of
146 * the floating-point exception flags are currently set. The excepts argument
147 * specifies the floating-point status flags to be queried.
150 fetestexcept(int excepts
)
152 _DIAGASSERT((except
& ~FE_ALL_EXCEPT
) == 0);
153 return __SHIFTOUT(reg_fpsr_read(), FPSR_CSUM
) & excepts
;
157 * The fegetround() function shall get the current rounding direction.
162 return __SHIFTOUT(reg_fpcr_read(), FPCR_RMODE
);
166 * The fesetround() function shall establish the rounding direction represented
167 * by its argument round. If the argument is not equal to the value of a
168 * rounding direction macro, the rounding direction is not changed.
171 fesetround(int round
)
174 _DIAGASSERT(!(round
& ~__SHIFTOUT(FPCR_RMODE
, FPCR_RMODE
)));
176 unsigned int fpcr
= reg_fpcr_read() & ~FPCR_RMODE
;
177 fpcr
|= __SHIFTIN(round
, FPCR_RMODE
);
178 reg_fpcr_write(fpcr
);
183 * The fegetenv() function shall attempt to store the current floating-point
184 * environment in the object pointed to by envp.
187 fegetenv(fenv_t
*envp
)
189 envp
->__fpcr
= reg_fpcr_read();
190 envp
->__fpsr
= reg_fpsr_read();
195 * The feholdexcept() function shall save the current floating-point
196 * environment in the object pointed to by envp, clear the floating-point
197 * status flags, and then install a non-stop (continue on floating-point
198 * exceptions) mode, if available, for all floating-point exceptions.
201 feholdexcept(fenv_t
*envp
)
203 envp
->__fpsr
= reg_fpsr_read();
204 envp
->__fpcr
= reg_fpcr_read();
205 reg_fpsr_write(envp
->__fpsr
& ~FPSR_CSUM
);
206 reg_fpcr_write(envp
->__fpcr
& ~FPCR_ESUM
);
211 * The fesetenv() function shall attempt to establish the floating-point
212 * environment represented by the object pointed to by envp. The fesetenv()
213 * function does not raise floating-point exceptions, but only installs the
214 * state of the floating-point status flags represented through its argument.
217 fesetenv(const fenv_t
*envp
)
219 reg_fpsr_write(envp
->__fpsr
);
224 * The feupdateenv() function shall attempt to save the currently raised
225 * floating-point exceptions in its automatic storage, attempt to install the
226 * floating-point environment represented by the object pointed to by envp,
227 * and then attempt to raise the saved floating-point exceptions.
230 feupdateenv(const fenv_t
*envp
)
233 _DIAGASSERT(envp
!= NULL
);
235 reg_fpsr_write(envp
->__fpsr
);
236 reg_fpcr_write(envp
->__fpcr
);
243 feenableexcept(int excepts
)
245 const uint32_t __fpcr
= reg_fpcr_read();
246 reg_fpcr_write((__fpcr
& ~FPCR_ESUM
) | __SHIFTIN(excepts
, FPCR_ESUM
));
247 return __SHIFTOUT(__fpcr
, FPCR_ESUM
);
251 fedisableexcept(int excepts
)
253 const uint32_t __fpcr
= reg_fpcr_read();
254 reg_fpcr_write(__fpcr
& ~__SHIFTIN(excepts
, FPCR_ESUM
));
255 return __SHIFTOUT(__fpcr
, FPCR_ESUM
);
261 const uint32_t __fpcr
= reg_fpcr_read();
262 return __SHIFTOUT(__fpcr
, FPCR_ESUM
);