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