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