]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mips/math_private.h
7f2cdf54598239007da5a13744471af8328fdb1a
[thirdparty/glibc.git] / sysdeps / mips / math_private.h
1 /* Internal math stuff. MIPS version.
2 Copyright (C) 2013-2014 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 _MATH_PRIVATE_H
20
21 #ifdef __mips_nan2008
22 /* MIPS aligned to IEEE 754-2008. */
23 #else
24 /* One of the few architectures where the meaning of the quiet/signaling bit is
25 inverse to IEEE 754-2008 (as well as common practice for IEEE 754-1985). */
26 # define HIGH_ORDER_BIT_IS_SET_FOR_SNAN
27 #endif
28
29 /* Inline functions to speed up the math library implementation. The
30 default versions of these routines are in generic/math_private.h
31 and call fesetround, feholdexcept, etc. These routines use inlined
32 code instead. */
33
34 #ifdef __mips_hard_float
35
36 # include <fenv.h>
37 # include <fenv_libc.h>
38 # include <fpu_control.h>
39
40 # define _FPU_MASK_ALL (_FPU_MASK_V | _FPU_MASK_Z | _FPU_MASK_O \
41 |_FPU_MASK_U | _FPU_MASK_I | FE_ALL_EXCEPT)
42
43 static __always_inline void
44 libc_feholdexcept_mips (fenv_t *envp)
45 {
46 fpu_control_t cw;
47
48 /* Save the current state. */
49 _FPU_GETCW (cw);
50 envp->__fp_control_register = cw;
51
52 /* Clear all exception enable bits and flags. */
53 cw &= ~(_FPU_MASK_ALL);
54 _FPU_SETCW (cw);
55 }
56 # define libc_feholdexcept libc_feholdexcept_mips
57 # define libc_feholdexceptf libc_feholdexcept_mips
58 # define libc_feholdexceptl libc_feholdexcept_mips
59
60 static __always_inline void
61 libc_fesetround_mips (int round)
62 {
63 fpu_control_t cw;
64
65 /* Get current state. */
66 _FPU_GETCW (cw);
67
68 /* Set rounding bits. */
69 cw &= ~_FPU_RC_MASK;
70 cw |= round;
71
72 /* Set new state. */
73 _FPU_SETCW (cw);
74 }
75 # define libc_fesetround libc_fesetround_mips
76 # define libc_fesetroundf libc_fesetround_mips
77 # define libc_fesetroundl libc_fesetround_mips
78
79 static __always_inline void
80 libc_feholdexcept_setround_mips (fenv_t *envp, int round)
81 {
82 fpu_control_t cw;
83
84 /* Save the current state. */
85 _FPU_GETCW (cw);
86 envp->__fp_control_register = cw;
87
88 /* Clear all exception enable bits and flags. */
89 cw &= ~(_FPU_MASK_ALL);
90
91 /* Set rounding bits. */
92 cw &= ~_FPU_RC_MASK;
93 cw |= round;
94
95 /* Set new state. */
96 _FPU_SETCW (cw);
97 }
98 # define libc_feholdexcept_setround libc_feholdexcept_setround_mips
99 # define libc_feholdexcept_setroundf libc_feholdexcept_setround_mips
100 # define libc_feholdexcept_setroundl libc_feholdexcept_setround_mips
101
102 # define libc_feholdsetround libc_feholdexcept_setround_mips
103 # define libc_feholdsetroundf libc_feholdexcept_setround_mips
104 # define libc_feholdsetroundl libc_feholdexcept_setround_mips
105
106 static __always_inline void
107 libc_fesetenv_mips (fenv_t *envp)
108 {
109 fpu_control_t cw;
110
111 /* Read current state to flush fpu pipeline. */
112 _FPU_GETCW (cw);
113
114 _FPU_SETCW (envp->__fp_control_register);
115 }
116 # define libc_fesetenv libc_fesetenv_mips
117 # define libc_fesetenvf libc_fesetenv_mips
118 # define libc_fesetenvl libc_fesetenv_mips
119
120 static __always_inline int
121 libc_feupdateenv_test_mips (fenv_t *envp, int excepts)
122 {
123 /* int ret = fetestexcept (excepts); feupdateenv (envp); return ret; */
124 int cw, temp;
125
126 /* Get current control word. */
127 _FPU_GETCW (cw);
128
129 /* Set flag bits (which are accumulative), and *also* set the
130 cause bits. The setting of the cause bits is what actually causes
131 the hardware to generate the exception, if the corresponding enable
132 bit is set as well. */
133 temp = cw & FE_ALL_EXCEPT;
134 temp |= envp->__fp_control_register | (temp << CAUSE_SHIFT);
135
136 /* Set new state. */
137 _FPU_SETCW (temp);
138
139 return cw & excepts & FE_ALL_EXCEPT;
140 }
141 # define libc_feupdateenv_test libc_feupdateenv_test_mips
142 # define libc_feupdateenv_testf libc_feupdateenv_test_mips
143 # define libc_feupdateenv_testl libc_feupdateenv_test_mips
144
145 static __always_inline void
146 libc_feupdateenv_mips (fenv_t *envp)
147 {
148 libc_feupdateenv_test_mips (envp, 0);
149 }
150 # define libc_feupdateenv libc_feupdateenv_mips
151 # define libc_feupdateenvf libc_feupdateenv_mips
152 # define libc_feupdateenvl libc_feupdateenv_mips
153
154 # define libc_feresetround libc_feupdateenv_mips
155 # define libc_feresetroundf libc_feupdateenv_mips
156 # define libc_feresetroundl libc_feupdateenv_mips
157
158 static __always_inline int
159 libc_fetestexcept_mips (int excepts)
160 {
161 int cw;
162
163 /* Get current control word. */
164 _FPU_GETCW (cw);
165
166 return cw & excepts & FE_ALL_EXCEPT;
167 }
168 # define libc_fetestexcept libc_fetestexcept_mips
169 # define libc_fetestexceptf libc_fetestexcept_mips
170 # define libc_fetestexceptl libc_fetestexcept_mips
171
172 /* Enable support for rounding mode context. */
173 # define HAVE_RM_CTX 1
174
175 static __always_inline void
176 libc_feholdexcept_setround_mips_ctx (struct rm_ctx *ctx, int round)
177 {
178 fpu_control_t old, new;
179
180 /* Save the current state. */
181 _FPU_GETCW (old);
182 ctx->env.__fp_control_register = old;
183
184 /* Clear all exception enable bits and flags. */
185 new = old & ~(_FPU_MASK_ALL);
186
187 /* Set rounding bits. */
188 new = (new & ~_FPU_RC_MASK) | round;
189
190 if (__glibc_unlikely (new != old))
191 {
192 _FPU_SETCW (new);
193 ctx->updated_status = true;
194 }
195 else
196 ctx->updated_status = false;
197 }
198 # define libc_feholdexcept_setround_ctx libc_feholdexcept_setround_mips_ctx
199 # define libc_feholdexcept_setroundf_ctx libc_feholdexcept_setround_mips_ctx
200 # define libc_feholdexcept_setroundl_ctx libc_feholdexcept_setround_mips_ctx
201
202 static __always_inline void
203 libc_fesetenv_mips_ctx (struct rm_ctx *ctx)
204 {
205 libc_fesetenv_mips (&ctx->env);
206 }
207 # define libc_fesetenv_ctx libc_fesetenv_mips_ctx
208 # define libc_fesetenvf_ctx libc_fesetenv_mips_ctx
209 # define libc_fesetenvl_ctx libc_fesetenv_mips_ctx
210
211 static __always_inline void
212 libc_feupdateenv_mips_ctx (struct rm_ctx *ctx)
213 {
214 if (__glibc_unlikely (ctx->updated_status))
215 libc_feupdateenv_test_mips (&ctx->env, 0);
216 }
217 # define libc_feupdateenv_ctx libc_feupdateenv_mips_ctx
218 # define libc_feupdateenvf_ctx libc_feupdateenv_mips_ctx
219 # define libc_feupdateenvl_ctx libc_feupdateenv_mips_ctx
220 # define libc_feresetround_ctx libc_feupdateenv_mips_ctx
221 # define libc_feresetroundf_ctx libc_feupdateenv_mips_ctx
222 # define libc_feresetroundl_ctx libc_feupdateenv_mips_ctx
223
224 static __always_inline void
225 libc_feholdsetround_mips_ctx (struct rm_ctx *ctx, int round)
226 {
227 fpu_control_t old, new;
228
229 /* Save the current state. */
230 _FPU_GETCW (old);
231 ctx->env.__fp_control_register = old;
232
233 /* Set rounding bits. */
234 new = (old & ~_FPU_RC_MASK) | round;
235
236 if (__glibc_unlikely (new != old))
237 {
238 _FPU_SETCW (new);
239 ctx->updated_status = true;
240 }
241 else
242 ctx->updated_status = false;
243 }
244 # define libc_feholdsetround_ctx libc_feholdsetround_mips_ctx
245 # define libc_feholdsetroundf_ctx libc_feholdsetround_mips_ctx
246 # define libc_feholdsetroundl_ctx libc_feholdsetround_mips_ctx
247
248 #endif
249
250 #include_next <math_private.h>
251
252 #endif