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