]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/powerpc/fpu/fenv_private.h
ade0bfaf5f4053133291662c1bed5d220d7b3ac9
[thirdparty/glibc.git] / sysdeps / powerpc / fpu / fenv_private.h
1 /* Private floating point rounding and exceptions handling. PowerPC version.
2 Copyright (C) 2013-2019 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 <https://www.gnu.org/licenses/>. */
18
19 #ifndef POWERPC_FENV_PRIVATE_H
20 #define POWERPC_FENV_PRIVATE_H 1
21
22 #include <fenv.h>
23 #include <fenv_libc.h>
24 #include <fpu_control.h>
25
26 static __always_inline void
27 libc_feholdexcept_setround_ppc (fenv_t *envp, int r)
28 {
29 fenv_union_t old, new;
30
31 old.fenv = *envp = fegetenv_register ();
32
33 __TEST_AND_ENTER_NON_STOP (old.l, 0ULL);
34
35 /* Clear everything and set the rounding mode. */
36 new.l = r;
37 fesetenv_register (new.fenv);
38 }
39
40 static __always_inline unsigned long long
41 __libc_femergeenv_ppc (const fenv_t *envp, unsigned long long old_mask,
42 unsigned long long new_mask)
43 {
44 fenv_union_t old, new;
45
46 new.fenv = *envp;
47 old.fenv = fegetenv_register ();
48
49 /* Merge bits while masking unwanted bits from new and old env. */
50 new.l = (old.l & old_mask) | (new.l & new_mask);
51
52 __TEST_AND_EXIT_NON_STOP (old.l, new.l);
53 __TEST_AND_ENTER_NON_STOP (old.l, new.l);
54
55 /* If requesting to keep status, replace control, and merge exceptions,
56 and exceptions haven't changed, we can just set new control instead
57 of the whole FPSCR. */
58 if ((old_mask & (FPSCR_CONTROL_MASK|FPSCR_STATUS_MASK|FPSCR_EXCEPTIONS_MASK))
59 == (FPSCR_STATUS_MASK|FPSCR_EXCEPTIONS_MASK) &&
60 (new_mask & (FPSCR_CONTROL_MASK|FPSCR_STATUS_MASK|FPSCR_EXCEPTIONS_MASK))
61 == (FPSCR_CONTROL_MASK|FPSCR_EXCEPTIONS_MASK) &&
62 (old.l & FPSCR_EXCEPTIONS_MASK) == (new.l & FPSCR_EXCEPTIONS_MASK))
63 {
64 fesetenv_mode (new.fenv);
65 }
66 else
67 /* Atomically enable and raise (if appropriate) exceptions set in `new'. */
68 fesetenv_register (new.fenv);
69
70 return old.l;
71 }
72
73 static __always_inline void
74 libc_fesetenv_ppc (const fenv_t *envp)
75 {
76 /* Replace the entire environment. */
77 __libc_femergeenv_ppc (envp, 0LL, -1LL);
78 }
79
80 static __always_inline void
81 libc_feresetround_ppc (fenv_t *envp)
82 {
83 fenv_union_t new = { .fenv = *envp };
84 fegetenv_and_set_rn (new.l & FPSCR_RN_MASK);
85 }
86
87 static __always_inline int
88 libc_feupdateenv_test_ppc (fenv_t *envp, int ex)
89 {
90 return __libc_femergeenv_ppc (envp, ~FPSCR_CONTROL_MASK,
91 ~FPSCR_STATUS_MASK) & ex;
92 }
93
94 static __always_inline void
95 libc_feupdateenv_ppc (fenv_t *e)
96 {
97 libc_feupdateenv_test_ppc (e, 0);
98 }
99
100 #define libc_feholdexceptf libc_feholdexcept_ppc
101 #define libc_feholdexcept libc_feholdexcept_ppc
102 #define libc_feholdexcept_setroundf libc_feholdexcept_setround_ppc
103 #define libc_feholdexcept_setround libc_feholdexcept_setround_ppc
104 #define libc_fetestexceptf libc_fetestexcept_ppc
105 #define libc_fetestexcept libc_fetestexcept_ppc
106 #define libc_fesetroundf libc_fesetround_ppc
107 #define libc_fesetround libc_fesetround_ppc
108 #define libc_fesetenvf libc_fesetenv_ppc
109 #define libc_fesetenv libc_fesetenv_ppc
110 #define libc_feupdateenv_testf libc_feupdateenv_test_ppc
111 #define libc_feupdateenv_test libc_feupdateenv_test_ppc
112 #define libc_feupdateenvf libc_feupdateenv_ppc
113 #define libc_feupdateenv libc_feupdateenv_ppc
114 #define libc_feholdsetroundf libc_feholdsetround_ppc
115 #define libc_feholdsetround libc_feholdsetround_ppc
116 #define libc_feresetroundf libc_feresetround_ppc
117 #define libc_feresetround libc_feresetround_ppc
118
119
120 /* We have support for rounding mode context. */
121 #define HAVE_RM_CTX 1
122
123 static __always_inline void
124 libc_feholdsetround_ppc_ctx (struct rm_ctx *ctx, int r)
125 {
126 fenv_union_t old;
127
128 ctx->env = old.fenv = fegetenv_and_set_rn (r);
129 ctx->updated_status = (r != (old.l & FPSCR_RN_MASK));
130 }
131
132 static __always_inline void
133 libc_feholdsetround_noex_ppc_ctx (struct rm_ctx *ctx, int r)
134 {
135 fenv_union_t old, new;
136
137 old.fenv = fegetenv_register ();
138
139 new.l = (old.l & ~(FPSCR_ENABLES_MASK|FPSCR_RN_MASK)) | r;
140
141 ctx->env = old.fenv;
142 if (__glibc_unlikely (new.l != old.l))
143 {
144 __TEST_AND_ENTER_NON_STOP (old.l, 0ULL);
145 fesetenv_mode (new.fenv);
146 ctx->updated_status = true;
147 }
148 else
149 ctx->updated_status = false;
150 }
151
152 static __always_inline void
153 libc_fesetenv_ppc_ctx (struct rm_ctx *ctx)
154 {
155 libc_fesetenv_ppc (&ctx->env);
156 }
157
158 static __always_inline void
159 libc_feupdateenv_ppc_ctx (struct rm_ctx *ctx)
160 {
161 if (__glibc_unlikely (ctx->updated_status))
162 libc_feresetround_ppc (&ctx->env);
163 }
164
165 static __always_inline void
166 libc_feresetround_ppc_ctx (struct rm_ctx *ctx)
167 {
168 if (__glibc_unlikely (ctx->updated_status))
169 libc_feresetround_ppc (&ctx->env);
170 }
171
172 #define libc_fesetenv_ctx libc_fesetenv_ppc_ctx
173 #define libc_fesetenvf_ctx libc_fesetenv_ppc_ctx
174 #define libc_fesetenvl_ctx libc_fesetenv_ppc_ctx
175 #define libc_feholdsetround_ctx libc_feholdsetround_ppc_ctx
176 #define libc_feholdsetroundf_ctx libc_feholdsetround_ppc_ctx
177 #define libc_feholdsetroundl_ctx libc_feholdsetround_ppc_ctx
178 #define libc_feholdsetround_noex_ctx libc_feholdsetround_noex_ppc_ctx
179 #define libc_feholdsetround_noexf_ctx libc_feholdsetround_noex_ppc_ctx
180 #define libc_feholdsetround_noexl_ctx libc_feholdsetround_noex_ppc_ctx
181 #define libc_feresetround_ctx libc_feresetround_ppc_ctx
182 #define libc_feresetroundf_ctx libc_feresetround_ppc_ctx
183 #define libc_feresetroundl_ctx libc_feresetround_ppc_ctx
184 #define libc_feupdateenv_ctx libc_feupdateenv_ppc_ctx
185 #define libc_feupdateenvf_ctx libc_feupdateenv_ppc_ctx
186 #define libc_feupdateenvl_ctx libc_feupdateenv_ppc_ctx
187
188 #include_next <fenv_private.h>
189
190 #endif