]>
Commit | Line | Data |
---|---|---|
41e8926a | 1 | /* Private floating point rounding and exceptions handling. PowerPC version. |
b168057a | 2 | Copyright (C) 2013-2015 Free Software Foundation, Inc. |
41e8926a AZ |
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 FENV_PRIVATE_H | |
20 | #define FENV_PRIVATE_H 1 | |
21 | ||
22 | #include <fenv.h> | |
23 | #include <fenv_libc.h> | |
24 | #include <fpu_control.h> | |
25 | ||
26 | #define _FPU_MASK_ALL (_FPU_MASK_ZM | _FPU_MASK_OM | _FPU_MASK_UM \ | |
27 | | _FPU_MASK_XM | _FPU_MASK_IM) | |
28 | ||
29 | /* Mask everything but the rounding moded and non-IEEE arithmetic flags. */ | |
30 | #define _FPU_MASK_ROUNDING 0xffffffff00000007LL | |
31 | ||
32 | /* Mask restore rounding mode and exception enabled. */ | |
33 | #define _FPU_MASK_EXCEPT_ROUND 0xffffffff1fffff00LL | |
34 | ||
35 | /* Mask exception enable but fraction rounded/inexact and FP result/CC | |
36 | bits. */ | |
37 | #define _FPU_MASK_FRAC_INEX_RET_CC 0x1ff80fff | |
38 | ||
39 | static __always_inline void | |
40 | libc_feholdexcept_ppc (fenv_t *envp) | |
41 | { | |
42 | fenv_union_t old, new; | |
43 | ||
44 | old.fenv = *envp = fegetenv_register (); | |
45 | ||
46 | new.l = old.l & _FPU_MASK_ROUNDING; | |
47 | ||
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 (); | |
53 | ||
54 | fesetenv_register (new.fenv); | |
55 | } | |
56 | ||
57 | static __always_inline void | |
58 | libc_fesetround_ppc (int r) | |
59 | { | |
60 | __fesetround (r); | |
61 | } | |
62 | ||
63 | static __always_inline void | |
64 | libc_feholdexcept_setround_ppc (fenv_t *envp, int r) | |
65 | { | |
66 | fenv_union_t old, new; | |
67 | ||
68 | old.fenv = *envp = fegetenv_register (); | |
69 | ||
70 | new.l = (old.l & _FPU_MASK_ROUNDING) | r; | |
71 | ||
72 | if ((old.l & _FPU_MASK_ALL) != 0) | |
73 | (void) __fe_mask_env (); | |
74 | ||
75 | fesetenv_register (new.fenv); | |
76 | } | |
77 | ||
78 | static __always_inline int | |
79 | libc_fetestexcept_ppc (int e) | |
80 | { | |
81 | fenv_union_t u; | |
82 | u.fenv = fegetenv_register (); | |
83 | return u.l & e; | |
84 | } | |
85 | ||
86 | static __always_inline void | |
87 | libc_fesetenv_ppc (const fenv_t *envp) | |
88 | { | |
89 | fenv_union_t old, new; | |
90 | ||
91 | new.fenv = *envp; | |
92 | old.fenv = fegetenv_register (); | |
93 | ||
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 | |
97 | hardware. */ | |
98 | if ((old.l & _FPU_MASK_ALL) == 0 && (new.l & _FPU_MASK_ALL) != 0) | |
bd12ab55 | 99 | (void) __fe_nomask_env_priv (); |
41e8926a AZ |
100 | |
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 | |
104 | generate SIGFPE. */ | |
105 | if ((old.l & _FPU_MASK_ALL) != 0 && (new.l & _FPU_MASK_ALL) == 0) | |
106 | (void) __fe_mask_env (); | |
107 | ||
108 | fesetenv_register (*envp); | |
109 | } | |
110 | ||
111 | static __always_inline int | |
112 | libc_feupdateenv_test_ppc (fenv_t *envp, int ex) | |
113 | { | |
114 | fenv_union_t old, new; | |
115 | ||
116 | new.fenv = *envp; | |
117 | old.fenv = fegetenv_register (); | |
118 | ||
119 | /* Restore rounding mode and exception enable from *envp and merge | |
120 | exceptions. Leave fraction rounded/inexact and FP result/CC bits | |
121 | unchanged. */ | |
122 | new.l = (old.l & _FPU_MASK_EXCEPT_ROUND) | |
123 | | (new.l & _FPU_MASK_FRAC_INEX_RET_CC); | |
124 | ||
125 | if ((old.l & _FPU_MASK_ALL) == 0 && (new.l & _FPU_MASK_ALL) != 0) | |
bd12ab55 | 126 | (void) __fe_nomask_env_priv (); |
41e8926a AZ |
127 | |
128 | if ((old.l & _FPU_MASK_ALL) != 0 && (new.l & _FPU_MASK_ALL) == 0) | |
129 | (void) __fe_mask_env (); | |
130 | ||
131 | fesetenv_register (new.fenv); | |
132 | ||
133 | return old.l & ex; | |
134 | } | |
135 | ||
136 | static __always_inline void | |
137 | libc_feupdateenv_ppc (fenv_t *e) | |
138 | { | |
139 | libc_feupdateenv_test_ppc (e, 0); | |
140 | } | |
141 | ||
142 | static __always_inline void | |
143 | libc_feholdsetround_ppc (fenv_t *e, int r) | |
144 | { | |
145 | fenv_union_t old, new; | |
146 | ||
147 | old.fenv = fegetenv_register (); | |
148 | /* Clear current precision and set newer one. */ | |
149 | new.l = (old.l & ~0x3) | r; | |
150 | *e = old.fenv; | |
151 | ||
152 | if ((old.l & _FPU_MASK_ALL) != 0) | |
153 | (void) __fe_mask_env (); | |
154 | fesetenv_register (new.fenv); | |
155 | } | |
156 | ||
157 | static __always_inline void | |
158 | libc_feresetround_ppc (fenv_t *envp) | |
159 | { | |
160 | fenv_union_t old, new; | |
161 | ||
162 | new.fenv = *envp; | |
163 | old.fenv = fegetenv_register (); | |
164 | ||
165 | /* Restore rounding mode and exception enable from *envp and merge | |
166 | exceptions. Leave fraction rounded/inexact and FP result/CC bits | |
167 | unchanged. */ | |
168 | new.l = (old.l & _FPU_MASK_EXCEPT_ROUND) | |
169 | | (new.l & _FPU_MASK_FRAC_INEX_RET_CC); | |
170 | ||
171 | if ((old.l & _FPU_MASK_ALL) == 0 && (new.l & _FPU_MASK_ALL) != 0) | |
bd12ab55 | 172 | (void) __fe_nomask_env_priv (); |
41e8926a AZ |
173 | |
174 | if ((old.l & _FPU_MASK_ALL) != 0 && (new.l & _FPU_MASK_ALL) == 0) | |
175 | (void) __fe_mask_env (); | |
176 | ||
177 | /* Atomically enable and raise (if appropriate) exceptions set in `new'. */ | |
178 | fesetenv_register (new.fenv); | |
179 | } | |
180 | ||
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 | |
199 | ||
200 | ||
201 | /* We have support for rounding mode context. */ | |
202 | #define HAVE_RM_CTX 1 | |
203 | ||
204 | static __always_inline void | |
205 | libc_feholdexcept_setround_ppc_ctx (struct rm_ctx *ctx, int r) | |
206 | { | |
207 | fenv_union_t old, new; | |
208 | ||
209 | old.fenv = fegetenv_register (); | |
210 | ||
211 | new.l = (old.l & _FPU_MASK_ROUNDING) | r; | |
212 | ctx->env = old.fenv; | |
213 | if (__glibc_unlikely (new.l != old.l)) | |
214 | { | |
215 | if ((old.l & _FPU_MASK_ALL) != 0) | |
216 | (void) __fe_mask_env (); | |
217 | fesetenv_register (new.fenv); | |
218 | ctx->updated_status = true; | |
219 | } | |
220 | else | |
221 | ctx->updated_status = false; | |
222 | } | |
223 | ||
224 | static __always_inline void | |
225 | libc_fesetenv_ppc_ctx (struct rm_ctx *ctx) | |
226 | { | |
227 | libc_fesetenv_ppc (&ctx->env); | |
228 | } | |
229 | ||
230 | static __always_inline void | |
231 | libc_feupdateenv_ppc_ctx (struct rm_ctx *ctx) | |
232 | { | |
233 | if (__glibc_unlikely (ctx->updated_status)) | |
234 | libc_feupdateenv_test_ppc (&ctx->env, 0); | |
235 | } | |
236 | ||
237 | static __always_inline void | |
238 | libc_feholdsetround_ppc_ctx (struct rm_ctx *ctx, int r) | |
239 | { | |
240 | fenv_union_t old, new; | |
241 | ||
242 | old.fenv = fegetenv_register (); | |
243 | new.l = (old.l & ~0x3) | r; | |
244 | ctx->env = old.fenv; | |
245 | if (__glibc_unlikely (new.l != old.l)) | |
246 | { | |
247 | if ((old.l & _FPU_MASK_ALL) != 0) | |
248 | (void) __fe_mask_env (); | |
249 | fesetenv_register (new.fenv); | |
250 | ctx->updated_status = true; | |
251 | } | |
252 | else | |
253 | ctx->updated_status = false; | |
254 | } | |
255 | ||
256 | static __always_inline void | |
257 | libc_feresetround_ppc_ctx (struct rm_ctx *ctx) | |
258 | { | |
259 | if (__glibc_unlikely (ctx->updated_status)) | |
260 | libc_feresetround_ppc (&ctx->env); | |
261 | } | |
262 | ||
41e8926a | 263 | #define libc_feholdexcept_setround_ctx libc_feholdexcept_setround_ppc_ctx |
2cd925f7 AZ |
264 | #define libc_feholdexcept_setroundf_ctx libc_feholdexcept_setround_ppc_ctx |
265 | #define libc_feholdexcept_setroundl_ctx libc_feholdexcept_setround_ppc_ctx | |
41e8926a AZ |
266 | #define libc_fesetenv_ctx libc_fesetenv_ppc_ctx |
267 | #define libc_fesetenvf_ctx libc_fesetenv_ppc_ctx | |
2cd925f7 | 268 | #define libc_fesetenvl_ctx libc_fesetenv_ppc_ctx |
41e8926a AZ |
269 | #define libc_feholdsetround_ctx libc_feholdsetround_ppc_ctx |
270 | #define libc_feholdsetroundf_ctx libc_feholdsetround_ppc_ctx | |
2cd925f7 | 271 | #define libc_feholdsetroundl_ctx libc_feholdsetround_ppc_ctx |
41e8926a AZ |
272 | #define libc_feresetround_ctx libc_feresetround_ppc_ctx |
273 | #define libc_feresetroundf_ctx libc_feresetround_ppc_ctx | |
2cd925f7 | 274 | #define libc_feresetroundl_ctx libc_feresetround_ppc_ctx |
41e8926a | 275 | #define libc_feupdateenv_ctx libc_feupdateenv_ppc_ctx |
2cd925f7 AZ |
276 | #define libc_feupdateenvf_ctx libc_feupdateenv_ppc_ctx |
277 | #define libc_feupdateenvl_ctx libc_feupdateenv_ppc_ctx | |
41e8926a AZ |
278 | |
279 | #endif |