1 /* Internal math stuff.
2 Copyright (C) 2022-2024 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 <https://www.gnu.org/licenses/>. */
19 #ifndef LOONGARCH_MATH_PRIVATE_H
20 #define LOONGARCH_MATH_PRIVATE_H 1
22 /* Inline functions to speed up the math library implementation. The
23 default versions of these routines are in generic/math_private.h
24 and call fesetround, feholdexcept, etc. These routines use inlined
27 #ifdef __loongarch_hard_float
30 #include <fenv_libc.h>
31 #include <fpu_control.h>
33 #define _FPU_MASK_ALL \
34 (_FPU_MASK_V | _FPU_MASK_Z | _FPU_MASK_O | _FPU_MASK_U | _FPU_MASK_I \
37 static __always_inline
void
38 libc_feholdexcept_loongarch (fenv_t
*envp
)
42 /* Save the current state. */
44 envp
->__fp_control_register
= cw
;
46 /* Clear all exception enable bits and flags. */
47 cw
&= ~(_FPU_MASK_ALL
);
50 #define libc_feholdexcept libc_feholdexcept_loongarch
51 #define libc_feholdexceptf libc_feholdexcept_loongarch
52 #define libc_feholdexceptl libc_feholdexcept_loongarch
54 static __always_inline
void
55 libc_fesetround_loongarch (int round
)
59 /* Get current state. */
62 /* Set rounding bits. */
69 #define libc_fesetround libc_fesetround_loongarch
70 #define libc_fesetroundf libc_fesetround_loongarch
71 #define libc_fesetroundl libc_fesetround_loongarch
73 static __always_inline
void
74 libc_feholdexcept_setround_loongarch (fenv_t
*envp
, int round
)
78 /* Save the current state. */
80 envp
->__fp_control_register
= cw
;
82 /* Clear all exception enable bits and flags. */
83 cw
&= ~(_FPU_MASK_ALL
);
85 /* Set rounding bits. */
92 #define libc_feholdexcept_setround libc_feholdexcept_setround_loongarch
93 #define libc_feholdexcept_setroundf libc_feholdexcept_setround_loongarch
94 #define libc_feholdexcept_setroundl libc_feholdexcept_setround_loongarch
96 #define libc_feholdsetround libc_feholdexcept_setround_loongarch
97 #define libc_feholdsetroundf libc_feholdexcept_setround_loongarch
98 #define libc_feholdsetroundl libc_feholdexcept_setround_loongarch
100 static __always_inline
void
101 libc_fesetenv_loongarch (fenv_t
*envp
)
103 fpu_control_t cw
__attribute__ ((unused
));
105 /* Read current state to flush fpu pipeline. */
108 _FPU_SETCW (envp
->__fp_control_register
);
110 #define libc_fesetenv libc_fesetenv_loongarch
111 #define libc_fesetenvf libc_fesetenv_loongarch
112 #define libc_fesetenvl libc_fesetenv_loongarch
114 static __always_inline
int
115 libc_feupdateenv_test_loongarch (fenv_t
*envp
, int excepts
)
117 /* int ret = fetestexcept (excepts); feupdateenv (envp); return ret; */
120 /* Get current control word. */
123 /* Set flag bits (which are accumulative), and *also* set the
124 cause bits. The setting of the cause bits is what actually causes
125 the hardware to generate the exception, if the corresponding enable
126 bit is set as well. */
127 temp
= cw
& FE_ALL_EXCEPT
;
128 temp
|= envp
->__fp_control_register
| (temp
<< CAUSE_SHIFT
);
133 return cw
& excepts
& FE_ALL_EXCEPT
;
135 #define libc_feupdateenv_test libc_feupdateenv_test_loongarch
136 #define libc_feupdateenv_testf libc_feupdateenv_test_loongarch
137 #define libc_feupdateenv_testl libc_feupdateenv_test_loongarch
139 static __always_inline
void
140 libc_feupdateenv_loongarch (fenv_t
*envp
)
142 libc_feupdateenv_test_loongarch (envp
, 0);
144 #define libc_feupdateenv libc_feupdateenv_loongarch
145 #define libc_feupdateenvf libc_feupdateenv_loongarch
146 #define libc_feupdateenvl libc_feupdateenv_loongarch
148 #define libc_feresetround libc_feupdateenv_loongarch
149 #define libc_feresetroundf libc_feupdateenv_loongarch
150 #define libc_feresetroundl libc_feupdateenv_loongarch
152 static __always_inline
int
153 libc_fetestexcept_loongarch (int excepts
)
157 /* Get current control word. */
160 return cw
& excepts
& FE_ALL_EXCEPT
;
162 #define libc_fetestexcept libc_fetestexcept_loongarch
163 #define libc_fetestexceptf libc_fetestexcept_loongarch
164 #define libc_fetestexceptl libc_fetestexcept_loongarch
166 /* Enable support for rounding mode context. */
167 #define HAVE_RM_CTX 1
169 static __always_inline
void
170 libc_feholdexcept_setround_loongarch_ctx (struct rm_ctx
*ctx
, int round
)
172 fpu_control_t old
, new;
174 /* Save the current state. */
176 ctx
->env
.__fp_control_register
= old
;
178 /* Clear all exception enable bits and flags. */
179 new = old
& ~(_FPU_MASK_ALL
);
181 /* Set rounding bits. */
182 new = (new & ~_FPU_RC_MASK
) | round
;
184 if (__glibc_unlikely (new != old
))
187 ctx
->updated_status
= true;
190 ctx
->updated_status
= false;
192 #define libc_feholdexcept_setround_ctx libc_feholdexcept_setround_loongarch_ctx
193 #define libc_feholdexcept_setroundf_ctx \
194 libc_feholdexcept_setround_loongarch_ctx
195 #define libc_feholdexcept_setroundl_ctx \
196 libc_feholdexcept_setround_loongarch_ctx
198 static __always_inline
void
199 libc_fesetenv_loongarch_ctx (struct rm_ctx
*ctx
)
201 libc_fesetenv_loongarch (&ctx
->env
);
203 #define libc_fesetenv_ctx libc_fesetenv_loongarch_ctx
204 #define libc_fesetenvf_ctx libc_fesetenv_loongarch_ctx
205 #define libc_fesetenvl_ctx libc_fesetenv_loongarch_ctx
207 static __always_inline
void
208 libc_feupdateenv_loongarch_ctx (struct rm_ctx
*ctx
)
210 if (__glibc_unlikely (ctx
->updated_status
))
211 libc_feupdateenv_test_loongarch (&ctx
->env
, 0);
213 #define libc_feupdateenv_ctx libc_feupdateenv_loongarch_ctx
214 #define libc_feupdateenvf_ctx libc_feupdateenv_loongarch_ctx
215 #define libc_feupdateenvl_ctx libc_feupdateenv_loongarch_ctx
216 #define libc_feresetround_ctx libc_feupdateenv_loongarch_ctx
217 #define libc_feresetroundf_ctx libc_feupdateenv_loongarch_ctx
218 #define libc_feresetroundl_ctx libc_feupdateenv_loongarch_ctx
220 static __always_inline
void
221 libc_feholdsetround_loongarch_ctx (struct rm_ctx
*ctx
, int round
)
223 fpu_control_t old
, new;
225 /* Save the current state. */
227 ctx
->env
.__fp_control_register
= old
;
229 /* Set rounding bits. */
230 new = (old
& ~_FPU_RC_MASK
) | round
;
232 if (__glibc_unlikely (new != old
))
235 ctx
->updated_status
= true;
238 ctx
->updated_status
= false;
240 #define libc_feholdsetround_ctx libc_feholdsetround_loongarch_ctx
241 #define libc_feholdsetroundf_ctx libc_feholdsetround_loongarch_ctx
242 #define libc_feholdsetroundl_ctx libc_feholdsetround_loongarch_ctx
246 #include_next <math_private.h>