powerpc: fix check-before-set in SET_RESTORE_ROUND
[glibc.git] / sysdeps / powerpc / fpu / fenv_private.h
blob984dff90ff0e798a2f883ce21d0b9b196bba04ef
1 /* Private floating point rounding and exceptions handling. PowerPC version.
2 Copyright (C) 2013-2017 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
22 #include <fenv.h>
23 #include <fenv_libc.h>
24 #include <fpu_control.h>
26 /* Mask for the exception enable bits. */
27 #define _FPU_ALL_TRAPS (_FPU_MASK_ZM | _FPU_MASK_OM | _FPU_MASK_UM \
28 | _FPU_MASK_XM | _FPU_MASK_IM)
30 /* Mask the rounding mode bits. */
31 #define _FPU_MASK_RN 0xfffffffffffffffcLL
33 /* Mask everything but the rounding modes and non-IEEE arithmetic flags. */
34 #define _FPU_MASK_NOT_RN_NI 0xffffffff00000807LL
36 /* Mask restore rounding mode and exception enabled. */
37 #define _FPU_MASK_TRAPS_RN 0xffffffffffffff00LL
39 /* Mask FP result flags, preserve fraction rounded/inexact bits. */
40 #define _FPU_MASK_FRAC_INEX_RET_CC 0xfffffffffff80fffLL
42 static __always_inline void
43 __libc_feholdbits_ppc (fenv_t *envp, unsigned long long mask,
44 unsigned long long bits)
46 fenv_union_t old, new;
48 old.fenv = *envp = fegetenv_register ();
50 new.l = (old.l & mask) | bits;
52 /* If the old env had any enabled exceptions, then mask SIGFPE in the
53 MSR FE0/FE1 bits. This may allow the FPU to run faster because it
54 always takes the default action and can not generate SIGFPE. */
55 if ((old.l & _FPU_ALL_TRAPS) != 0)
56 (void) __fe_mask_env ();
58 fesetenv_register (new.fenv);
61 static __always_inline void
62 libc_feholdexcept_ppc (fenv_t *envp)
64 __libc_feholdbits_ppc (envp, _FPU_MASK_NOT_RN_NI, 0LL);
67 static __always_inline void
68 libc_feholdexcept_setround_ppc (fenv_t *envp, int r)
70 __libc_feholdbits_ppc (envp, _FPU_MASK_NOT_RN_NI & _FPU_MASK_RN, r);
73 static __always_inline void
74 libc_fesetround_ppc (int r)
76 __fesetround_inline (r);
79 static __always_inline int
80 libc_fetestexcept_ppc (int e)
82 fenv_union_t u;
83 u.fenv = fegetenv_register ();
84 return u.l & e;
87 static __always_inline void
88 libc_feholdsetround_ppc (fenv_t *e, int r)
90 __libc_feholdbits_ppc (e, _FPU_MASK_TRAPS_RN, r);
93 static __always_inline unsigned long long
94 __libc_femergeenv_ppc (const fenv_t *envp, unsigned long long old_mask,
95 unsigned long long new_mask)
97 fenv_union_t old, new;
99 new.fenv = *envp;
100 old.fenv = fegetenv_register ();
102 /* Merge bits while masking unwanted bits from new and old env. */
103 new.l = (old.l & old_mask) | (new.l & new_mask);
105 /* If the old env has no enabled exceptions and the new env has any enabled
106 exceptions, then unmask SIGFPE in the MSR FE0/FE1 bits. This will put the
107 hardware into "precise mode" and may cause the FPU to run slower on some
108 hardware. */
109 if ((old.l & _FPU_ALL_TRAPS) == 0 && (new.l & _FPU_ALL_TRAPS) != 0)
110 (void) __fe_nomask_env_priv ();
112 /* If the old env had any enabled exceptions and the new env has no enabled
113 exceptions, then mask SIGFPE in the MSR FE0/FE1 bits. This may allow the
114 FPU to run faster because it always takes the default action and can not
115 generate SIGFPE. */
116 if ((old.l & _FPU_ALL_TRAPS) != 0 && (new.l & _FPU_ALL_TRAPS) == 0)
117 (void) __fe_mask_env ();
119 /* Atomically enable and raise (if appropriate) exceptions set in `new'. */
120 fesetenv_register (new.fenv);
122 return old.l;
125 static __always_inline void
126 libc_fesetenv_ppc (const fenv_t *envp)
128 /* Replace the entire environment. */
129 __libc_femergeenv_ppc (envp, 0LL, -1LL);
132 static __always_inline void
133 libc_feresetround_ppc (fenv_t *envp)
135 __libc_femergeenv_ppc (envp, _FPU_MASK_TRAPS_RN, _FPU_MASK_FRAC_INEX_RET_CC);
138 static __always_inline int
139 libc_feupdateenv_test_ppc (fenv_t *envp, int ex)
141 return __libc_femergeenv_ppc (envp, _FPU_MASK_TRAPS_RN,
142 _FPU_MASK_FRAC_INEX_RET_CC) & ex;
145 static __always_inline void
146 libc_feupdateenv_ppc (fenv_t *e)
148 libc_feupdateenv_test_ppc (e, 0);
151 #define libc_feholdexceptf libc_feholdexcept_ppc
152 #define libc_feholdexcept libc_feholdexcept_ppc
153 #define libc_feholdexcept_setroundf libc_feholdexcept_setround_ppc
154 #define libc_feholdexcept_setround libc_feholdexcept_setround_ppc
155 #define libc_fetestexceptf libc_fetestexcept_ppc
156 #define libc_fetestexcept libc_fetestexcept_ppc
157 #define libc_fesetroundf libc_fesetround_ppc
158 #define libc_fesetround libc_fesetround_ppc
159 #define libc_fesetenvf libc_fesetenv_ppc
160 #define libc_fesetenv libc_fesetenv_ppc
161 #define libc_feupdateenv_testf libc_feupdateenv_test_ppc
162 #define libc_feupdateenv_test libc_feupdateenv_test_ppc
163 #define libc_feupdateenvf libc_feupdateenv_ppc
164 #define libc_feupdateenv libc_feupdateenv_ppc
165 #define libc_feholdsetroundf libc_feholdsetround_ppc
166 #define libc_feholdsetround libc_feholdsetround_ppc
167 #define libc_feresetroundf libc_feresetround_ppc
168 #define libc_feresetround libc_feresetround_ppc
171 /* We have support for rounding mode context. */
172 #define HAVE_RM_CTX 1
174 static __always_inline void
175 libc_feholdsetround_ppc_ctx (struct rm_ctx *ctx, int r)
177 fenv_union_t old, new;
179 old.fenv = fegetenv_register ();
181 new.l = (old.l & _FPU_MASK_TRAPS_RN) | r;
183 ctx->env = old.fenv;
184 if (__glibc_unlikely (new.l != old.l))
186 if ((old.l & _FPU_ALL_TRAPS) != 0)
187 (void) __fe_mask_env ();
188 fesetenv_register (new.fenv);
189 ctx->updated_status = true;
191 else
192 ctx->updated_status = false;
195 static __always_inline void
196 libc_fesetenv_ppc_ctx (struct rm_ctx *ctx)
198 libc_fesetenv_ppc (&ctx->env);
201 static __always_inline void
202 libc_feupdateenv_ppc_ctx (struct rm_ctx *ctx)
204 if (__glibc_unlikely (ctx->updated_status))
205 libc_feresetround_ppc (&ctx->env);
208 static __always_inline void
209 libc_feresetround_ppc_ctx (struct rm_ctx *ctx)
211 if (__glibc_unlikely (ctx->updated_status))
212 libc_feresetround_ppc (&ctx->env);
215 #define libc_fesetenv_ctx libc_fesetenv_ppc_ctx
216 #define libc_fesetenvf_ctx libc_fesetenv_ppc_ctx
217 #define libc_fesetenvl_ctx libc_fesetenv_ppc_ctx
218 #define libc_feholdsetround_ctx libc_feholdsetround_ppc_ctx
219 #define libc_feholdsetroundf_ctx libc_feholdsetround_ppc_ctx
220 #define libc_feholdsetroundl_ctx libc_feholdsetround_ppc_ctx
221 #define libc_feresetround_ctx libc_feresetround_ppc_ctx
222 #define libc_feresetroundf_ctx libc_feresetround_ppc_ctx
223 #define libc_feresetroundl_ctx libc_feresetround_ppc_ctx
224 #define libc_feupdateenv_ctx libc_feupdateenv_ppc_ctx
225 #define libc_feupdateenvf_ctx libc_feupdateenv_ppc_ctx
226 #define libc_feupdateenvl_ctx libc_feupdateenv_ppc_ctx
228 #endif