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