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