Files
glibc/sysdeps/generic/gmp-arch.h
Adhemerval Zanella 476e962af7 Add gmp-arch and udiv_qrnnd
To enable “longlong.h” removal, the udiv_qrnnd is moved to a gmp-arch.h
file.  It allows each architecture to implement its own arch-specific
optimizations.  The generic implementation now uses a static inline,
which provides better type checking than the GNU extension to cast the
asm constraint (and it works better with clang).

Most of the architecture uses the generic implementation, which is
expanded from a macro, except for alpha, x86, m68k, sh, and sparc.
I kept that alpha, which uses out-of-the-line implementations and x86,
where there is no easy way to use the div{q} instruction from C code.
For the rest, the compiler generates good enough code.

The hppa also provides arch-specific implementations, but they are not
routed in “longlong.h” and thus never used.

Reviewed-by: Wilco Dijkstra  <Wilco.Dijkstra@arm.com>
2025-11-25 14:52:15 -03:00

101 lines
2.6 KiB
C

/* Multiprecision generic functions.
Copyright (C) 2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#ifndef __GMP_ARCH_H
#define __GMP_ARCH_H
#include <stdint.h>
#include <gmp.h>
#define LL_B ((mp_limb_t) 1 << (BITS_PER_MP_LIMB / 2))
static __always_inline mp_limb_t
ll_lowpart (mp_limb_t t)
{
return t & (LL_B - 1);
}
static __always_inline mp_limb_t
ll_highpart (mp_limb_t t)
{
return t >> (BITS_PER_MP_LIMB / 2);
}
/* udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
denominator) divides a UDWtype, composed by the UWtype integers
HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and places the quotient
in QUOTIENT and the remainder in REMAINDER. HIGH_NUMERATOR must be less
than DENOMINATOR for correct operation. If, in addition, the most
significant bit of DENOMINATOR must be 1, then the pre-processor symbol
UDIV_NEEDS_NORMALIZATION is defined to 1. */
#ifndef udiv_qrnnd
static __always_inline void
udiv_qrnnd_generic (mp_limb_t *q, mp_limb_t *r, mp_limb_t n1, mp_limb_t n0,
mp_limb_t d)
{
mp_limb_t d1 = ll_highpart (d),
d0 = ll_lowpart (d),
q1, q0;
mp_limb_t r1, r0, m;
r1 = n1 % d1;
q1 = n1 / d1;
m = q1 * d0;
r1 = r1 * LL_B | ll_highpart (n0);
if (r1 < m)
{
q1--;
r1 += d;
if (r1 >= d)
if (r1 < m)
{
q1--;
r1 += d;
}
}
r1 -= m;
r0 = r1 % d1;
q0 = r1 / d1;
m = q0 * d0;
r0 = r0 * LL_B | ll_lowpart (n0);
if (r0 < m)
{
q0--;
r0 += d;
if (r0 >= d)
if (r0 < m)
{
q0--;
r0 += d;
}
}
r0 -= m;
*q = q1 * LL_B | q0;
*r = r0;
}
# undef UDIV_NEEDS_NORMALIZATION
# define UDIV_NEEDS_NORMALIZATION 1
# undef udiv_qrnnd
# define udiv_qrnnd(__q, __r, __n1, __n0, __d) \
udiv_qrnnd_generic (&__q, &__r, __n1, __n0, __d)
#endif
#endif /* __GMP_ARCH_H */