1 /* Private floating point rounding and exceptions handling.  C-SKY version.
2    Copyright (C) 2018-2025 Free Software Foundation, Inc.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library.  If not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #ifndef CSKY_FENV_PRIVATE_H
19 #define CSKY_FENV_PRIVATE_H 1
20 
21 #include <fenv.h>
22 #include <fpu_control.h>
23 #include "fenv_libc.h"
24 
25 static __always_inline void
libc_feholdexcept_vfp(fenv_t * envp)26 libc_feholdexcept_vfp (fenv_t *envp)
27 {
28   fpu_control_t fpsr, fpcr;
29 
30   _FPU_GETCW (fpcr);
31   envp->__fpcr = fpcr;
32 
33   _FPU_GETFPSR (fpsr);
34   envp->__fpsr = fpsr;
35 
36   /* Now set all exceptions to non-stop.  */
37   fpcr &= ~FE_ALL_EXCEPT;
38 
39   /* And clear all exception flags.  */
40   fpsr &= ~(FE_ALL_EXCEPT << CAUSE_SHIFT);
41 
42   _FPU_SETFPSR (fpsr);
43 
44   _FPU_SETCW (fpcr);
45 }
46 
47 static __always_inline void
libc_fesetround_vfp(int round)48 libc_fesetround_vfp (int round)
49 {
50   fpu_control_t fpcr;
51 
52   _FPU_GETCW (fpcr);
53 
54   /* Set new rounding mode if different.  */
55   if (unlikely ((fpcr & FE_DOWNWARD) != round))
56     _FPU_SETCW ((fpcr & ~FE_DOWNWARD) | round);
57 }
58 
59 static __always_inline void
libc_feholdexcept_setround_vfp(fenv_t * envp,int round)60 libc_feholdexcept_setround_vfp (fenv_t *envp, int round)
61 {
62   fpu_control_t fpsr, fpcr;
63 
64   _FPU_GETCW (fpcr);
65   envp->__fpcr = fpcr;
66 
67   _FPU_GETFPSR (fpsr);
68   envp->__fpsr = fpsr;
69 
70   /* Clear exception flags, set all exceptions to non-stop,
71      and set new rounding mode.  */
72   fpsr &= ~(FE_ALL_EXCEPT << CAUSE_SHIFT);
73   _FPU_SETFPSR (fpsr);
74 
75   fpcr &= ~(FE_ALL_EXCEPT | FE_DOWNWARD);
76   _FPU_SETCW (fpcr | round);
77 }
78 
79 static __always_inline void
libc_feholdsetround_vfp(fenv_t * envp,int round)80 libc_feholdsetround_vfp (fenv_t *envp, int round)
81 {
82   fpu_control_t fpcr;
83 
84   _FPU_GETCW (fpcr);
85   envp->__fpcr = fpcr;
86 
87   /* Set new rounding mode if different.  */
88   if (unlikely ((fpcr & FE_DOWNWARD) != round))
89     _FPU_SETCW ((fpcr & ~FE_DOWNWARD) | round);
90 }
91 
92 static __always_inline void
libc_feresetround_vfp(fenv_t * envp)93 libc_feresetround_vfp (fenv_t *envp)
94 {
95   fpu_control_t fpcr, round;
96 
97   _FPU_GETCW (fpcr);
98 
99   /* Check whether rounding modes are different.  */
100   round = (envp->__fpcr ^ fpcr) & FE_DOWNWARD;
101 
102   /* Restore the rounding mode if it was changed.  */
103   if (unlikely (round != 0))
104     _FPU_SETCW (fpcr ^ round);
105 }
106 
107 static __always_inline int
libc_fetestexcept_vfp(int ex)108 libc_fetestexcept_vfp (int ex)
109 {
110   fpu_control_t fpsr;
111 
112   _FPU_GETFPSR (fpsr);
113   fpsr = fpsr >> CAUSE_SHIFT;
114   return fpsr & ex & FE_ALL_EXCEPT;
115 }
116 
117 static __always_inline void
libc_fesetenv_vfp(const fenv_t * envp)118 libc_fesetenv_vfp (const fenv_t *envp)
119 {
120   fpu_control_t fpcr, fpsr, new_fpcr, new_fpsr;
121 
122   _FPU_GETCW (fpcr);
123   _FPU_GETFPSR (fpsr);
124 
125   new_fpcr = envp->__fpcr;
126   new_fpsr = envp->__fpsr;
127 
128   if (unlikely (fpsr ^ new_fpsr) != 0)
129     _FPU_SETFPSR (new_fpsr);
130 
131   if (unlikely (fpcr ^ new_fpcr) != 0)
132     _FPU_SETCW (new_fpcr);
133 }
134 
135 static __always_inline int
libc_feupdateenv_test_vfp(const fenv_t * envp,int ex)136 libc_feupdateenv_test_vfp (const fenv_t *envp, int ex)
137 {
138   fpu_control_t fpcr, fpsr, new_fpcr, new_fpsr, excepts;
139 
140   _FPU_GETCW (fpcr);
141   _FPU_GETFPSR (fpsr);
142 
143   /* Merge current exception flags with the saved fenv.  */
144   excepts = (fpsr >> CAUSE_SHIFT) & FE_ALL_EXCEPT;
145   new_fpcr = envp->__fpcr;
146   new_fpsr = envp->__fpsr | (excepts << CAUSE_SHIFT);
147 
148   /* Write FCR and FESR if different.  */
149   if (unlikely (fpsr ^ new_fpsr) != 0)
150     _FPU_SETFPSR (new_fpsr);
151 
152   if (unlikely (fpcr ^ new_fpcr) != 0)
153     _FPU_SETCW (new_fpcr);
154 
155   /* Raise the exceptions if enabled in the new FP state.  */
156   if (unlikely (excepts & new_fpcr))
157     feraiseexcept (excepts);
158 
159   return excepts & ex;
160 }
161 
162 static __always_inline void
libc_feupdateenv_vfp(const fenv_t * envp)163 libc_feupdateenv_vfp (const fenv_t *envp)
164 {
165   libc_feupdateenv_test_vfp (envp, 0);
166 }
167 
168 static __always_inline void
libc_feholdsetround_vfp_ctx(struct rm_ctx * ctx,int r)169 libc_feholdsetround_vfp_ctx (struct rm_ctx *ctx, int r)
170 {
171   fpu_control_t fpcr, fpsr, round;
172 
173   _FPU_GETCW (fpcr);
174   _FPU_GETFPSR (fpsr);
175   ctx->updated_status = false;
176   ctx->env.__fpcr = fpcr;
177   ctx->env.__fpsr = fpsr;
178 
179   /* Check whether rounding modes are different.  */
180   round = (fpcr ^ r) & FE_DOWNWARD;
181 
182   /* Set the rounding mode if changed.  */
183   if (unlikely (round != 0))
184     {
185       ctx->updated_status = true;
186       _FPU_SETCW (fpcr ^ round);
187     }
188 }
189 
190 static __always_inline void
libc_feresetround_vfp_ctx(struct rm_ctx * ctx)191 libc_feresetround_vfp_ctx (struct rm_ctx *ctx)
192 {
193   /* Restore the rounding mode if updated.  */
194   if (unlikely (ctx->updated_status))
195     {
196       fpu_control_t fpcr;
197 
198       _FPU_GETCW (fpcr);
199       fpcr = (fpcr & ~FE_DOWNWARD) | (ctx->env.__fpcr & FE_DOWNWARD);
200       _FPU_SETCW (fpcr);
201     }
202 }
203 
204 static __always_inline void
libc_fesetenv_vfp_ctx(struct rm_ctx * ctx)205 libc_fesetenv_vfp_ctx (struct rm_ctx *ctx)
206 {
207   fpu_control_t fpcr, fpsr, new_fpcr, new_fpsr;
208 
209   _FPU_GETCW (fpcr);
210   _FPU_GETFPSR (fpsr);
211 
212   new_fpcr = ctx->env.__fpcr;
213   new_fpsr = ctx->env.__fpsr;
214 
215   if (unlikely (fpsr ^ new_fpsr) != 0)
216     _FPU_SETFPSR (new_fpsr);
217 
218   if (unlikely (fpcr ^ new_fpcr) != 0)
219     _FPU_SETCW (new_fpcr);
220 }
221 
222 #define libc_feholdexcept  libc_feholdexcept_vfp
223 #define libc_feholdexceptf libc_feholdexcept_vfp
224 #define libc_feholdexceptl libc_feholdexcept_vfp
225 
226 #define libc_fesetround  libc_fesetround_vfp
227 #define libc_fesetroundf libc_fesetround_vfp
228 #define libc_fesetroundl libc_fesetround_vfp
229 
230 #define libc_feresetround  libc_feresetround_vfp
231 #define libc_feresetroundf libc_feresetround_vfp
232 #define libc_feresetroundl libc_feresetround_vfp
233 
234 #define libc_feresetround_noex  libc_fesetenv_vfp
235 #define libc_feresetround_noexf libc_fesetenv_vfp
236 #define libc_feresetround_noexl libc_fesetenv_vfp
237 
238 #define libc_feholdexcept_setround  libc_feholdexcept_setround_vfp
239 #define libc_feholdexcept_setroundf libc_feholdexcept_setround_vfp
240 #define libc_feholdexcept_setroundl libc_feholdexcept_setround_vfp
241 
242 #define libc_feholdsetround  libc_feholdsetround_vfp
243 #define libc_feholdsetroundf libc_feholdsetround_vfp
244 #define libc_feholdsetroundl libc_feholdsetround_vfp
245 
246 #define libc_fetestexcept  libc_fetestexcept_vfp
247 #define libc_fetestexceptf libc_fetestexcept_vfp
248 #define libc_fetestexceptl libc_fetestexcept_vfp
249 
250 #define libc_fesetenv  libc_fesetenv_vfp
251 #define libc_fesetenvf libc_fesetenv_vfp
252 #define libc_fesetenvl libc_fesetenv_vfp
253 
254 #define libc_feupdateenv  libc_feupdateenv_vfp
255 #define libc_feupdateenvf libc_feupdateenv_vfp
256 #define libc_feupdateenvl libc_feupdateenv_vfp
257 
258 #define libc_feupdateenv_test  libc_feupdateenv_test_vfp
259 #define libc_feupdateenv_testf libc_feupdateenv_test_vfp
260 #define libc_feupdateenv_testl libc_feupdateenv_test_vfp
261 
262 /* We have support for rounding mode context.  */
263 #define HAVE_RM_CTX 1
264 
265 #define libc_feholdsetround_ctx		libc_feholdsetround_vfp_ctx
266 #define libc_feresetround_ctx		libc_feresetround_vfp_ctx
267 #define libc_feresetround_noex_ctx	libc_fesetenv_vfp_ctx
268 
269 #define libc_feholdsetroundf_ctx	libc_feholdsetround_vfp_ctx
270 #define libc_feresetroundf_ctx		libc_feresetround_vfp_ctx
271 #define libc_feresetround_noexf_ctx	libc_fesetenv_vfp_ctx
272 
273 #define libc_feholdsetroundl_ctx	libc_feholdsetround_vfp_ctx
274 #define libc_feresetroundl_ctx		libc_feresetround_vfp_ctx
275 #define libc_feresetround_noexl_ctx	libc_fesetenv_vfp_ctx
276 
277 #endif /* CSKY_FENV_PRIVATE_H */
278