1 /* Private floating point rounding and exceptions handling. PowerPC version.
2 Copyright (C) 2013-2015 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 FENV_PRIVATE_H
20 #define FENV_PRIVATE_H 1
23 #include <fenv_libc.h>
24 #include <fpu_control.h>
26 #define _FPU_MASK_ALL (_FPU_MASK_ZM | _FPU_MASK_OM | _FPU_MASK_UM \
27 | _FPU_MASK_XM | _FPU_MASK_IM)
29 /* Mask everything but the rounding moded and non-IEEE arithmetic flags. */
30 #define _FPU_MASK_ROUNDING 0xffffffff00000007LL
32 /* Mask restore rounding mode and exception enabled. */
33 #define _FPU_MASK_EXCEPT_ROUND 0xffffffff1fffff00LL
35 /* Mask exception enable but fraction rounded/inexact and FP result/CC
37 #define _FPU_MASK_FRAC_INEX_RET_CC 0x1ff80fff
39 static __always_inline
void
40 libc_feholdexcept_ppc (fenv_t
*envp
)
42 fenv_union_t old
, new;
44 old
.fenv
= *envp
= fegetenv_register ();
46 new.l
= old
.l
& _FPU_MASK_ROUNDING
;
48 /* If the old env had any enabled exceptions, then mask SIGFPE in the
49 MSR FE0/FE1 bits. This may allow the FPU to run faster because it
50 always takes the default action and can not generate SIGFPE. */
51 if ((old
.l
& _FPU_MASK_ALL
) != 0)
52 (void) __fe_mask_env ();
54 fesetenv_register (new.fenv
);
57 static __always_inline
void
58 libc_fesetround_ppc (int r
)
63 static __always_inline
void
64 libc_feholdexcept_setround_ppc (fenv_t
*envp
, int r
)
66 fenv_union_t old
, new;
68 old
.fenv
= *envp
= fegetenv_register ();
70 new.l
= (old
.l
& _FPU_MASK_ROUNDING
) | r
;
72 if ((old
.l
& _FPU_MASK_ALL
) != 0)
73 (void) __fe_mask_env ();
75 fesetenv_register (new.fenv
);
78 static __always_inline
int
79 libc_fetestexcept_ppc (int e
)
82 u
.fenv
= fegetenv_register ();
86 static __always_inline
void
87 libc_fesetenv_ppc (const fenv_t
*envp
)
89 fenv_union_t old
, new;
92 old
.fenv
= fegetenv_register ();
94 /* If the old env has no enabled exceptions and the new env has any enabled
95 exceptions, then unmask SIGFPE in the MSR FE0/FE1 bits. This will put the
96 hardware into "precise mode" and may cause the FPU to run slower on some
98 if ((old
.l
& _FPU_MASK_ALL
) == 0 && (new.l
& _FPU_MASK_ALL
) != 0)
99 (void) __fe_nomask_env_priv ();
101 /* If the old env had any enabled exceptions and the new env has no enabled
102 exceptions, then mask SIGFPE in the MSR FE0/FE1 bits. This may allow the
103 FPU to run faster because it always takes the default action and can not
105 if ((old
.l
& _FPU_MASK_ALL
) != 0 && (new.l
& _FPU_MASK_ALL
) == 0)
106 (void) __fe_mask_env ();
108 fesetenv_register (*envp
);
111 static __always_inline
int
112 libc_feupdateenv_test_ppc (fenv_t
*envp
, int ex
)
114 fenv_union_t old
, new;
117 old
.fenv
= fegetenv_register ();
119 /* Restore rounding mode and exception enable from *envp and merge
120 exceptions. Leave fraction rounded/inexact and FP result/CC bits
122 new.l
= (old
.l
& _FPU_MASK_EXCEPT_ROUND
)
123 | (new.l
& _FPU_MASK_FRAC_INEX_RET_CC
);
125 if ((old
.l
& _FPU_MASK_ALL
) == 0 && (new.l
& _FPU_MASK_ALL
) != 0)
126 (void) __fe_nomask_env_priv ();
128 if ((old
.l
& _FPU_MASK_ALL
) != 0 && (new.l
& _FPU_MASK_ALL
) == 0)
129 (void) __fe_mask_env ();
131 fesetenv_register (new.fenv
);
136 static __always_inline
void
137 libc_feupdateenv_ppc (fenv_t
*e
)
139 libc_feupdateenv_test_ppc (e
, 0);
142 static __always_inline
void
143 libc_feholdsetround_ppc (fenv_t
*e
, int r
)
145 fenv_union_t old
, new;
147 old
.fenv
= fegetenv_register ();
148 /* Clear current precision and set newer one. */
149 new.l
= (old
.l
& ~0x3) | r
;
152 if ((old
.l
& _FPU_MASK_ALL
) != 0)
153 (void) __fe_mask_env ();
154 fesetenv_register (new.fenv
);
157 static __always_inline
void
158 libc_feresetround_ppc (fenv_t
*envp
)
160 fenv_union_t old
, new;
163 old
.fenv
= fegetenv_register ();
165 /* Restore rounding mode and exception enable from *envp and merge
166 exceptions. Leave fraction rounded/inexact and FP result/CC bits
168 new.l
= (old
.l
& _FPU_MASK_EXCEPT_ROUND
)
169 | (new.l
& _FPU_MASK_FRAC_INEX_RET_CC
);
171 if ((old
.l
& _FPU_MASK_ALL
) == 0 && (new.l
& _FPU_MASK_ALL
) != 0)
172 (void) __fe_nomask_env_priv ();
174 if ((old
.l
& _FPU_MASK_ALL
) != 0 && (new.l
& _FPU_MASK_ALL
) == 0)
175 (void) __fe_mask_env ();
177 /* Atomically enable and raise (if appropriate) exceptions set in `new'. */
178 fesetenv_register (new.fenv
);
181 #define libc_feholdexceptf libc_feholdexcept_ppc
182 #define libc_feholdexcept libc_feholdexcept_ppc
183 #define libc_feholdexcept_setroundf libc_feholdexcept_setround_ppc
184 #define libc_feholdexcept_setround libc_feholdexcept_setround_ppc
185 #define libc_fetestexceptf libc_fetestexcept_ppc
186 #define libc_fetestexcept libc_fetestexcept_ppc
187 #define libc_fesetroundf libc_fesetround_ppc
188 #define libc_fesetround libc_fesetround_ppc
189 #define libc_fesetenvf libc_fesetenv_ppc
190 #define libc_fesetenv libc_fesetenv_ppc
191 #define libc_feupdateenv_testf libc_feupdateenv_test_ppc
192 #define libc_feupdateenv_test libc_feupdateenv_test_ppc
193 #define libc_feupdateenvf libc_feupdateenv_ppc
194 #define libc_feupdateenv libc_feupdateenv_ppc
195 #define libc_feholdsetroundf libc_feholdsetround_ppc
196 #define libc_feholdsetround libc_feholdsetround_ppc
197 #define libc_feresetroundf libc_feresetround_ppc
198 #define libc_feresetround libc_feresetround_ppc
201 /* We have support for rounding mode context. */
202 #define HAVE_RM_CTX 1
204 static __always_inline
void
205 libc_feholdexcept_setround_ppc_ctx (struct rm_ctx
*ctx
, int r
)
207 fenv_union_t old
, new;
209 old
.fenv
= fegetenv_register ();
211 new.l
= (old
.l
& _FPU_MASK_ROUNDING
) | r
;
213 if (__glibc_unlikely (new.l
!= old
.l
))
215 if ((old
.l
& _FPU_MASK_ALL
) != 0)
216 (void) __fe_mask_env ();
217 fesetenv_register (new.fenv
);
218 ctx
->updated_status
= true;
221 ctx
->updated_status
= false;
224 static __always_inline
void
225 libc_fesetenv_ppc_ctx (struct rm_ctx
*ctx
)
227 libc_fesetenv_ppc (&ctx
->env
);
230 static __always_inline
void
231 libc_feupdateenv_ppc_ctx (struct rm_ctx
*ctx
)
233 if (__glibc_unlikely (ctx
->updated_status
))
234 libc_feupdateenv_test_ppc (&ctx
->env
, 0);
237 static __always_inline
void
238 libc_feholdsetround_ppc_ctx (struct rm_ctx
*ctx
, int r
)
240 fenv_union_t old
, new;
242 old
.fenv
= fegetenv_register ();
243 new.l
= (old
.l
& ~0x3) | r
;
245 if (__glibc_unlikely (new.l
!= old
.l
))
247 if ((old
.l
& _FPU_MASK_ALL
) != 0)
248 (void) __fe_mask_env ();
249 fesetenv_register (new.fenv
);
250 ctx
->updated_status
= true;
253 ctx
->updated_status
= false;
256 static __always_inline
void
257 libc_feresetround_ppc_ctx (struct rm_ctx
*ctx
)
259 if (__glibc_unlikely (ctx
->updated_status
))
260 libc_feresetround_ppc (&ctx
->env
);
263 #define libc_feholdexcept_setround_ctx libc_feholdexcept_setround_ppc_ctx
264 #define libc_feholdexcept_setroundf_ctx libc_feholdexcept_setround_ppc_ctx
265 #define libc_feholdexcept_setroundl_ctx libc_feholdexcept_setround_ppc_ctx
266 #define libc_fesetenv_ctx libc_fesetenv_ppc_ctx
267 #define libc_fesetenvf_ctx libc_fesetenv_ppc_ctx
268 #define libc_fesetenvl_ctx libc_fesetenv_ppc_ctx
269 #define libc_feholdsetround_ctx libc_feholdsetround_ppc_ctx
270 #define libc_feholdsetroundf_ctx libc_feholdsetround_ppc_ctx
271 #define libc_feholdsetroundl_ctx libc_feholdsetround_ppc_ctx
272 #define libc_feresetround_ctx libc_feresetround_ppc_ctx
273 #define libc_feresetroundf_ctx libc_feresetround_ppc_ctx
274 #define libc_feresetroundl_ctx libc_feresetround_ppc_ctx
275 #define libc_feupdateenv_ctx libc_feupdateenv_ppc_ctx
276 #define libc_feupdateenvf_ctx libc_feupdateenv_ppc_ctx
277 #define libc_feupdateenvl_ctx libc_feupdateenv_ppc_ctx