1 /* Private floating point rounding and exceptions handling. AArch64 version.
2 Copyright (C) 2014-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef AARCH64_MATH_PRIVATE_H
20 #define AARCH64_MATH_PRIVATE_H 1
23 #include <fpu_control.h>
25 #define math_opt_barrier(x) \
26 ({ __typeof (x) __x = (x); __asm ("" : "+w" (__x)); __x; })
27 #define math_force_eval(x) \
28 ({ __typeof (x) __x = (x); __asm __volatile__ ("" : : "w" (__x)); })
30 extern __always_inline
double
31 __ieee754_sqrt (double d
)
34 asm __volatile__ ("fsqrt %d0, %d1" : "=w" (res
) : "w" (d
));
38 extern __always_inline
float
39 __ieee754_sqrtf (float s
)
42 asm __volatile__ ("fsqrt %s0, %s1" : "=w" (res
) : "w" (s
));
46 static __always_inline
void
47 libc_feholdexcept_aarch64 (fenv_t
*envp
)
50 fpu_control_t new_fpcr
;
59 /* Clear exception flags and set all exceptions to non-stop. */
60 new_fpcr
= fpcr
& ~(FE_ALL_EXCEPT
<< FE_EXCEPT_SHIFT
);
61 new_fpsr
= fpsr
& ~FE_ALL_EXCEPT
;
63 if (__glibc_unlikely (new_fpcr
!= fpcr
))
64 _FPU_SETCW (new_fpcr
);
67 _FPU_SETFPSR (new_fpsr
);
70 #define libc_feholdexcept libc_feholdexcept_aarch64
71 #define libc_feholdexceptf libc_feholdexcept_aarch64
72 #define libc_feholdexceptl libc_feholdexcept_aarch64
74 static __always_inline
void
75 libc_fesetround_aarch64 (int round
)
81 /* Check whether rounding modes are different. */
82 round
= (fpcr
^ round
) & _FPU_FPCR_RM_MASK
;
84 /* Set new rounding mode if different. */
85 if (__glibc_unlikely (round
!= 0))
86 _FPU_SETCW (fpcr
^ round
);
89 #define libc_fesetround libc_fesetround_aarch64
90 #define libc_fesetroundf libc_fesetround_aarch64
91 #define libc_fesetroundl libc_fesetround_aarch64
93 static __always_inline
void
94 libc_feholdexcept_setround_aarch64 (fenv_t
*envp
, int round
)
97 fpu_control_t new_fpcr
;
106 /* Clear exception flags, set all exceptions to non-stop,
107 and set new rounding mode. */
108 new_fpcr
= fpcr
& ~((FE_ALL_EXCEPT
<< FE_EXCEPT_SHIFT
) | _FPU_FPCR_RM_MASK
);
110 new_fpsr
= fpsr
& ~FE_ALL_EXCEPT
;
112 if (__glibc_unlikely (new_fpcr
!= fpcr
))
113 _FPU_SETCW (new_fpcr
);
115 if (new_fpsr
!= fpsr
)
116 _FPU_SETFPSR (new_fpsr
);
119 #define libc_feholdexcept_setround libc_feholdexcept_setround_aarch64
120 #define libc_feholdexcept_setroundf libc_feholdexcept_setround_aarch64
121 #define libc_feholdexcept_setroundl libc_feholdexcept_setround_aarch64
123 static __always_inline
int
124 libc_fetestexcept_aarch64 (int ex
)
129 return fpsr
& ex
& FE_ALL_EXCEPT
;
132 #define libc_fetestexcept libc_fetestexcept_aarch64
133 #define libc_fetestexceptf libc_fetestexcept_aarch64
134 #define libc_fetestexceptl libc_fetestexcept_aarch64
136 static __always_inline
void
137 libc_fesetenv_aarch64 (const fenv_t
*envp
)
140 fpu_control_t new_fpcr
;
143 new_fpcr
= envp
->__fpcr
;
145 if (__glibc_unlikely (fpcr
!= new_fpcr
))
146 _FPU_SETCW (new_fpcr
);
148 _FPU_SETFPSR (envp
->__fpsr
);
151 #define libc_fesetenv libc_fesetenv_aarch64
152 #define libc_fesetenvf libc_fesetenv_aarch64
153 #define libc_fesetenvl libc_fesetenv_aarch64
154 #define libc_feresetround_noex libc_fesetenv_aarch64
155 #define libc_feresetround_noexf libc_fesetenv_aarch64
156 #define libc_feresetround_noexl libc_fesetenv_aarch64
158 static __always_inline
int
159 libc_feupdateenv_test_aarch64 (const fenv_t
*envp
, int ex
)
162 fpu_control_t new_fpcr
;
170 /* Merge current exception flags with the saved fenv. */
171 excepts
= fpsr
& FE_ALL_EXCEPT
;
172 new_fpcr
= envp
->__fpcr
;
173 new_fpsr
= envp
->__fpsr
| excepts
;
175 if (__glibc_unlikely (fpcr
!= new_fpcr
))
176 _FPU_SETCW (new_fpcr
);
178 if (fpsr
!= new_fpsr
)
179 _FPU_SETFPSR (new_fpsr
);
181 /* Raise the exceptions if enabled in the new FP state. */
182 if (__glibc_unlikely (excepts
& (new_fpcr
>> FE_EXCEPT_SHIFT
)))
183 __feraiseexcept (excepts
);
188 #define libc_feupdateenv_test libc_feupdateenv_test_aarch64
189 #define libc_feupdateenv_testf libc_feupdateenv_test_aarch64
190 #define libc_feupdateenv_testl libc_feupdateenv_test_aarch64
192 static __always_inline
void
193 libc_feupdateenv_aarch64 (const fenv_t
*envp
)
195 libc_feupdateenv_test_aarch64 (envp
, 0);
198 #define libc_feupdateenv libc_feupdateenv_aarch64
199 #define libc_feupdateenvf libc_feupdateenv_aarch64
200 #define libc_feupdateenvl libc_feupdateenv_aarch64
202 static __always_inline
void
203 libc_feholdsetround_aarch64 (fenv_t
*envp
, int round
)
213 /* Check whether rounding modes are different. */
214 round
= (fpcr
^ round
) & _FPU_FPCR_RM_MASK
;
216 /* Set new rounding mode if different. */
217 if (__glibc_unlikely (round
!= 0))
218 _FPU_SETCW (fpcr
^ round
);
221 #define libc_feholdsetround libc_feholdsetround_aarch64
222 #define libc_feholdsetroundf libc_feholdsetround_aarch64
223 #define libc_feholdsetroundl libc_feholdsetround_aarch64
225 static __always_inline
void
226 libc_feresetround_aarch64 (fenv_t
*envp
)
233 /* Check whether rounding modes are different. */
234 round
= (envp
->__fpcr
^ fpcr
) & _FPU_FPCR_RM_MASK
;
236 /* Restore the rounding mode if it was changed. */
237 if (__glibc_unlikely (round
!= 0))
238 _FPU_SETCW (fpcr
^ round
);
241 #define libc_feresetround libc_feresetround_aarch64
242 #define libc_feresetroundf libc_feresetround_aarch64
243 #define libc_feresetroundl libc_feresetround_aarch64
245 /* We have support for rounding mode context. */
246 #define HAVE_RM_CTX 1
248 static __always_inline
void
249 libc_feholdsetround_aarch64_ctx (struct rm_ctx
*ctx
, int r
)
255 ctx
->env
.__fpcr
= fpcr
;
257 /* Check whether rounding modes are different. */
258 round
= (fpcr
^ r
) & _FPU_FPCR_RM_MASK
;
259 ctx
->updated_status
= round
!= 0;
261 /* Set the rounding mode if changed. */
262 if (__glibc_unlikely (round
!= 0))
263 _FPU_SETCW (fpcr
^ round
);
266 #define libc_feholdsetround_ctx libc_feholdsetround_aarch64_ctx
267 #define libc_feholdsetroundf_ctx libc_feholdsetround_aarch64_ctx
268 #define libc_feholdsetroundl_ctx libc_feholdsetround_aarch64_ctx
270 static __always_inline
void
271 libc_feresetround_aarch64_ctx (struct rm_ctx
*ctx
)
273 /* Restore the rounding mode if updated. */
274 if (__glibc_unlikely (ctx
->updated_status
))
275 _FPU_SETCW (ctx
->env
.__fpcr
);
278 #define libc_feresetround_ctx libc_feresetround_aarch64_ctx
279 #define libc_feresetroundf_ctx libc_feresetround_aarch64_ctx
280 #define libc_feresetroundl_ctx libc_feresetround_aarch64_ctx
282 static __always_inline
void
283 libc_feholdsetround_noex_aarch64_ctx (struct rm_ctx
*ctx
, int r
)
291 ctx
->env
.__fpcr
= fpcr
;
292 ctx
->env
.__fpsr
= fpsr
;
294 /* Check whether rounding modes are different. */
295 round
= (fpcr
^ r
) & _FPU_FPCR_RM_MASK
;
296 ctx
->updated_status
= round
!= 0;
298 /* Set the rounding mode if changed. */
299 if (__glibc_unlikely (round
!= 0))
300 _FPU_SETCW (fpcr
^ round
);
303 #define libc_feholdsetround_noex_ctx libc_feholdsetround_noex_aarch64_ctx
304 #define libc_feholdsetround_noexf_ctx libc_feholdsetround_noex_aarch64_ctx
305 #define libc_feholdsetround_noexl_ctx libc_feholdsetround_noex_aarch64_ctx
307 static __always_inline
void
308 libc_feresetround_noex_aarch64_ctx (struct rm_ctx
*ctx
)
310 /* Restore the rounding mode if updated. */
311 if (__glibc_unlikely (ctx
->updated_status
))
312 _FPU_SETCW (ctx
->env
.__fpcr
);
314 /* Write new FPSR to restore exception flags. */
315 _FPU_SETFPSR (ctx
->env
.__fpsr
);
318 #define libc_feresetround_noex_ctx libc_feresetround_noex_aarch64_ctx
319 #define libc_feresetround_noexf_ctx libc_feresetround_noex_aarch64_ctx
320 #define libc_feresetround_noexl_ctx libc_feresetround_noex_aarch64_ctx
322 #include_next <math_private.h>