fenv.c revision 1.7 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.7 2017/03/22 23:11:08 chs Exp $");
32
33 #include <sys/types.h>
34 #include <assert.h>
35 #include <fenv.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <inttypes.h>
39
40 #ifdef __SOFTFP__
41 #error This fenv implementation is only for hardfloat.
42 #endif
43
44 #include <arm/armreg.h>
45 #include <arm/vfpreg.h>
46
47 const fenv_t __fe_dfl_env = VFP_FPSCR_FZ|VFP_FPSCR_DN|VFP_FPSCR_RN;
48
49 /*
50 * The feclearexcept() function shall attempt to clear the supported
51 * floating-point exceptions represented by excepts.
52 */
53 int
54 feclearexcept(int excepts)
55 {
56 #ifndef lint
57 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
58 #endif
59 int tmp = armreg_fpscr_read() & ~__SHIFTIN(excepts, VFP_FPSCR_CSUM);
60 armreg_fpscr_write(tmp);
61 return 0;
62 }
63
64 /*
65 * The fegetexceptflag() function shall attempt to store an
66 * implementation-defined representation of the states of the floating-point
67 * status flags indicated by the argument excepts in the object pointed to by
68 * the argument flagp.
69 */
70 int
71 fegetexceptflag(fexcept_t *flagp, int excepts)
72 {
73
74 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
75 *flagp = __SHIFTOUT(armreg_fpscr_read(), VFP_FPSCR_CSUM) & excepts;
76 return 0;
77 }
78
79 /*
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.
83 */
84 int
85 feraiseexcept(int excepts)
86 {
87 #ifndef lint
88 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
89 #endif
90 int fpscr = armreg_fpscr_read();
91 fpscr |= __SHIFTIN(excepts, VFP_FPSCR_CSUM);
92 armreg_fpscr_write(fpscr);
93 return 0;
94 }
95
96 /*
97 * The fesetexceptflag() function shall attempt to set the floating-point
98 * status flags indicated by the argument excepts to the states stored in the
99 * object pointed to by flagp. The value pointed to by flagp shall have been
100 * set by a previous call to fegetexceptflag() whose second argument
101 * represented at least those floating-point exceptions represented by the
102 * argument excepts. This function does not raise floating-point exceptions,
103 * but only sets the state of the flags.
104 */
105 int
106 fesetexceptflag(const fexcept_t *flagp, int excepts)
107 {
108 #ifndef lint
109 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
110 #endif
111 int fpscr = armreg_fpscr_read();
112 fpscr &= ~__SHIFTIN(excepts, VFP_FPSCR_CSUM);
113 fpscr |= __SHIFTIN((*flagp & excepts), VFP_FPSCR_CSUM);
114 armreg_fpscr_write(fpscr);
115 return 0;
116 }
117
118 int
119 feenableexcept(int excepts)
120 {
121 #ifndef lint
122 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
123 #endif
124 int fpscr = armreg_fpscr_read();
125 armreg_fpscr_write(fpscr | __SHIFTIN((excepts), VFP_FPSCR_ESUM));
126 return __SHIFTOUT(fpscr, VFP_FPSCR_ESUM) & FE_ALL_EXCEPT;
127 }
128
129 int
130 fedisableexcept(int excepts)
131 {
132 #ifndef lint
133 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
134 #endif
135 int fpscr = armreg_fpscr_read();
136 armreg_fpscr_write(fpscr & ~__SHIFTIN((excepts), VFP_FPSCR_ESUM));
137 return __SHIFTOUT(fpscr, VFP_FPSCR_ESUM) & FE_ALL_EXCEPT;
138 }
139
140 /*
141 * The fetestexcept() function shall determine which of a specified subset of
142 * the floating-point exception flags are currently set. The excepts argument
143 * specifies the floating-point status flags to be queried.
144 */
145 int
146 fetestexcept(int excepts)
147 {
148 _DIAGASSERT((except & ~FE_ALL_EXCEPT) == 0);
149 return __SHIFTOUT(armreg_fpscr_read(), VFP_FPSCR_CSUM) & excepts;
150 }
151
152 int
153 fegetexcept(void)
154 {
155 return __SHIFTOUT(armreg_fpscr_read(), VFP_FPSCR_ESUM);
156 }
157
158 /*
159 * The fegetround() function shall get the current rounding direction.
160 */
161 int
162 fegetround(void)
163 {
164 return __SHIFTOUT(armreg_fpscr_read(), VFP_FPSCR_RMODE);
165 }
166
167 /*
168 * The fesetround() function shall establish the rounding direction represented
169 * by its argument round. If the argument is not equal to the value of a
170 * rounding direction macro, the rounding direction is not changed.
171 */
172 int
173 fesetround(int round)
174 {
175 #ifndef lint
176 _DIAGASSERT(!(round & ~__SHIFTOUT(VFP_FPSCR_RMODE, VFP_FPSCR_RMODE)));
177 #endif
178 int fpscr = armreg_fpscr_read() & ~VFP_FPSCR_RMODE;
179 fpscr |= __SHIFTIN(round, VFP_FPSCR_RMODE);
180 armreg_fpscr_write(fpscr);
181 return 0;
182 }
183
184 /*
185 * The fegetenv() function shall attempt to store the current floating-point
186 * environment in the object pointed to by envp.
187 */
188 int
189 fegetenv(fenv_t *envp)
190 {
191
192 *envp = armreg_fpscr_read();
193 return 0;
194 }
195
196 /*
197 * The feholdexcept() function shall save the current floating-point
198 * environment in the object pointed to by envp, clear the floating-point
199 * status flags, and then install a non-stop (continue on floating-point
200 * exceptions) mode, if available, for all floating-point exceptions.
201 */
202 int
203 feholdexcept(fenv_t *envp)
204 {
205
206 *envp = armreg_fpscr_read();
207 armreg_fpscr_write((*envp) & ~(VFP_FPSCR_ESUM|VFP_FPSCR_CSUM));
208 return 0;
209 }
210
211 /*
212 * The fesetenv() function shall attempt to establish the floating-point
213 * environment represented by the object pointed to by envp. The fesetenv()
214 * function does not raise floating-point exceptions, but only installs the
215 * state of the floating-point status flags represented through its argument.
216 */
217 int
218 fesetenv(const fenv_t *envp)
219 {
220
221 armreg_fpscr_write(*envp);
222 return 0;
223 }
224
225 /*
226 * The feupdateenv() function shall attempt to save the currently raised
227 * floating-point exceptions in its automatic storage, attempt to install the
228 * floating-point environment represented by the object pointed to by envp,
229 * and then attempt to raise the saved floating-point exceptions.
230 */
231 int
232 feupdateenv(const fenv_t *envp)
233 {
234 #ifndef lint
235 _DIAGASSERT(envp != NULL);
236 #endif
237 int fpscr = armreg_fpscr_read();
238 fpscr &= ~(VFP_FPSCR_ESUM|VFP_FPSCR_RMODE);
239 fpscr |= *envp;
240 armreg_fpscr_write(fpscr);
241
242 /* Success */
243 return 0;
244 }
245