]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/aarch64/fpu/math_private.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / aarch64 / fpu / math_private.h
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.
4
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.
9
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.
14
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/>. */
18
19 #ifndef AARCH64_MATH_PRIVATE_H
20 #define AARCH64_MATH_PRIVATE_H 1
21
22 #include <fenv.h>
23 #include <fpu_control.h>
24
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)); })
29
30 extern __always_inline double
31 __ieee754_sqrt (double d)
32 {
33 double res;
34 asm __volatile__ ("fsqrt %d0, %d1" : "=w" (res) : "w" (d));
35 return res;
36 }
37
38 extern __always_inline float
39 __ieee754_sqrtf (float s)
40 {
41 float res;
42 asm __volatile__ ("fsqrt %s0, %s1" : "=w" (res) : "w" (s));
43 return res;
44 }
45
46 static __always_inline void
47 libc_feholdexcept_aarch64 (fenv_t *envp)
48 {
49 fpu_control_t fpcr;
50 fpu_control_t new_fpcr;
51 fpu_fpsr_t fpsr;
52 fpu_fpsr_t new_fpsr;
53
54 _FPU_GETCW (fpcr);
55 _FPU_GETFPSR (fpsr);
56 envp->__fpcr = fpcr;
57 envp->__fpsr = fpsr;
58
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;
62
63 if (__glibc_unlikely (new_fpcr != fpcr))
64 _FPU_SETCW (new_fpcr);
65
66 if (new_fpsr != fpsr)
67 _FPU_SETFPSR (new_fpsr);
68 }
69
70 #define libc_feholdexcept libc_feholdexcept_aarch64
71 #define libc_feholdexceptf libc_feholdexcept_aarch64
72 #define libc_feholdexceptl libc_feholdexcept_aarch64
73
74 static __always_inline void
75 libc_fesetround_aarch64 (int round)
76 {
77 fpu_control_t fpcr;
78
79 _FPU_GETCW (fpcr);
80
81 /* Check whether rounding modes are different. */
82 round = (fpcr ^ round) & _FPU_FPCR_RM_MASK;
83
84 /* Set new rounding mode if different. */
85 if (__glibc_unlikely (round != 0))
86 _FPU_SETCW (fpcr ^ round);
87 }
88
89 #define libc_fesetround libc_fesetround_aarch64
90 #define libc_fesetroundf libc_fesetround_aarch64
91 #define libc_fesetroundl libc_fesetround_aarch64
92
93 static __always_inline void
94 libc_feholdexcept_setround_aarch64 (fenv_t *envp, int round)
95 {
96 fpu_control_t fpcr;
97 fpu_control_t new_fpcr;
98 fpu_fpsr_t fpsr;
99 fpu_fpsr_t new_fpsr;
100
101 _FPU_GETCW (fpcr);
102 _FPU_GETFPSR (fpsr);
103 envp->__fpcr = fpcr;
104 envp->__fpsr = fpsr;
105
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);
109 new_fpcr |= round;
110 new_fpsr = fpsr & ~FE_ALL_EXCEPT;
111
112 if (__glibc_unlikely (new_fpcr != fpcr))
113 _FPU_SETCW (new_fpcr);
114
115 if (new_fpsr != fpsr)
116 _FPU_SETFPSR (new_fpsr);
117 }
118
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
122
123 static __always_inline int
124 libc_fetestexcept_aarch64 (int ex)
125 {
126 fpu_fpsr_t fpsr;
127
128 _FPU_GETFPSR (fpsr);
129 return fpsr & ex & FE_ALL_EXCEPT;
130 }
131
132 #define libc_fetestexcept libc_fetestexcept_aarch64
133 #define libc_fetestexceptf libc_fetestexcept_aarch64
134 #define libc_fetestexceptl libc_fetestexcept_aarch64
135
136 static __always_inline void
137 libc_fesetenv_aarch64 (const fenv_t *envp)
138 {
139 fpu_control_t fpcr;
140 fpu_control_t new_fpcr;
141
142 _FPU_GETCW (fpcr);
143 new_fpcr = envp->__fpcr;
144
145 if (__glibc_unlikely (fpcr != new_fpcr))
146 _FPU_SETCW (new_fpcr);
147
148 _FPU_SETFPSR (envp->__fpsr);
149 }
150
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
157
158 static __always_inline int
159 libc_feupdateenv_test_aarch64 (const fenv_t *envp, int ex)
160 {
161 fpu_control_t fpcr;
162 fpu_control_t new_fpcr;
163 fpu_fpsr_t fpsr;
164 fpu_fpsr_t new_fpsr;
165 int excepts;
166
167 _FPU_GETCW (fpcr);
168 _FPU_GETFPSR (fpsr);
169
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;
174
175 if (__glibc_unlikely (fpcr != new_fpcr))
176 _FPU_SETCW (new_fpcr);
177
178 if (fpsr != new_fpsr)
179 _FPU_SETFPSR (new_fpsr);
180
181 /* Raise the exceptions if enabled in the new FP state. */
182 if (__glibc_unlikely (excepts & (new_fpcr >> FE_EXCEPT_SHIFT)))
183 __feraiseexcept (excepts);
184
185 return excepts & ex;
186 }
187
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
191
192 static __always_inline void
193 libc_feupdateenv_aarch64 (const fenv_t *envp)
194 {
195 libc_feupdateenv_test_aarch64 (envp, 0);
196 }
197
198 #define libc_feupdateenv libc_feupdateenv_aarch64
199 #define libc_feupdateenvf libc_feupdateenv_aarch64
200 #define libc_feupdateenvl libc_feupdateenv_aarch64
201
202 static __always_inline void
203 libc_feholdsetround_aarch64 (fenv_t *envp, int round)
204 {
205 fpu_control_t fpcr;
206 fpu_fpsr_t fpsr;
207
208 _FPU_GETCW (fpcr);
209 _FPU_GETFPSR (fpsr);
210 envp->__fpcr = fpcr;
211 envp->__fpsr = fpsr;
212
213 /* Check whether rounding modes are different. */
214 round = (fpcr ^ round) & _FPU_FPCR_RM_MASK;
215
216 /* Set new rounding mode if different. */
217 if (__glibc_unlikely (round != 0))
218 _FPU_SETCW (fpcr ^ round);
219 }
220
221 #define libc_feholdsetround libc_feholdsetround_aarch64
222 #define libc_feholdsetroundf libc_feholdsetround_aarch64
223 #define libc_feholdsetroundl libc_feholdsetround_aarch64
224
225 static __always_inline void
226 libc_feresetround_aarch64 (fenv_t *envp)
227 {
228 fpu_control_t fpcr;
229 int round;
230
231 _FPU_GETCW (fpcr);
232
233 /* Check whether rounding modes are different. */
234 round = (envp->__fpcr ^ fpcr) & _FPU_FPCR_RM_MASK;
235
236 /* Restore the rounding mode if it was changed. */
237 if (__glibc_unlikely (round != 0))
238 _FPU_SETCW (fpcr ^ round);
239 }
240
241 #define libc_feresetround libc_feresetround_aarch64
242 #define libc_feresetroundf libc_feresetround_aarch64
243 #define libc_feresetroundl libc_feresetround_aarch64
244
245 /* We have support for rounding mode context. */
246 #define HAVE_RM_CTX 1
247
248 static __always_inline void
249 libc_feholdsetround_aarch64_ctx (struct rm_ctx *ctx, int r)
250 {
251 fpu_control_t fpcr;
252 int round;
253
254 _FPU_GETCW (fpcr);
255 ctx->env.__fpcr = fpcr;
256
257 /* Check whether rounding modes are different. */
258 round = (fpcr ^ r) & _FPU_FPCR_RM_MASK;
259 ctx->updated_status = round != 0;
260
261 /* Set the rounding mode if changed. */
262 if (__glibc_unlikely (round != 0))
263 _FPU_SETCW (fpcr ^ round);
264 }
265
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
269
270 static __always_inline void
271 libc_feresetround_aarch64_ctx (struct rm_ctx *ctx)
272 {
273 /* Restore the rounding mode if updated. */
274 if (__glibc_unlikely (ctx->updated_status))
275 _FPU_SETCW (ctx->env.__fpcr);
276 }
277
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
281
282 static __always_inline void
283 libc_feholdsetround_noex_aarch64_ctx (struct rm_ctx *ctx, int r)
284 {
285 fpu_control_t fpcr;
286 fpu_fpsr_t fpsr;
287 int round;
288
289 _FPU_GETCW (fpcr);
290 _FPU_GETFPSR (fpsr);
291 ctx->env.__fpcr = fpcr;
292 ctx->env.__fpsr = fpsr;
293
294 /* Check whether rounding modes are different. */
295 round = (fpcr ^ r) & _FPU_FPCR_RM_MASK;
296 ctx->updated_status = round != 0;
297
298 /* Set the rounding mode if changed. */
299 if (__glibc_unlikely (round != 0))
300 _FPU_SETCW (fpcr ^ round);
301 }
302
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
306
307 static __always_inline void
308 libc_feresetround_noex_aarch64_ctx (struct rm_ctx *ctx)
309 {
310 /* Restore the rounding mode if updated. */
311 if (__glibc_unlikely (ctx->updated_status))
312 _FPU_SETCW (ctx->env.__fpcr);
313
314 /* Write new FPSR to restore exception flags. */
315 _FPU_SETFPSR (ctx->env.__fpsr);
316 }
317
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
321
322 #include_next <math_private.h>
323
324 #endif