gcc/libquadmath/printf/divrem.c

245 lines
5.4 KiB
C
Raw Normal View History

re PR fortran/47642 (real(kind=16) - libquadmath - segfault on amd64 FreeBSD) PR fortran/47642 * libquadmath.texi (quadmath_snprintf): Document. (quadmath_flt128tostr): Remove. * Makefile.am (libquadmath_la_SOURCES): Add printf/*.c. Remove quadmath_io.c, gdtoa/gdtoa.c, gdtoa/g__fmt.c, gdtoa/g_Qfmt.c, gdtoa/dmisc.c and gdtoa/ulp.c. * quadmath.h (quadmath_snprintf): New prototype. (quadmath_flt128tostr): Remove. * quadmath_weak.h (quadmath_snprintf): Add. (quadmath_flt128tostr): Remove. * configure.ac: New AC_CHECK_HEADERS headers: langinfo.h, wchar.h, wctype.h, limits.h, ctype.h, printf.h, errno.h. (AC_USE_SYSTEM_EXTENSIONS): Add. (HAVE_HIDDEN_VISIBILITY, HAVE_PRINTF_HOOKS, USE_LOCALE_SUPPORT, USE_I18N_NUMBER_H): New checks. * quadmath.map (QUADMATH_1.0): Add quadmath_snprintf. Remove quadmath_flt128tostr. * printf/printf_fphex.c: New file. * printf/_itowa.h: New file. * printf/mul_n.c: New file. * printf/quadmath-printf.h: New file. * printf/submul_1.c: New file. * printf/quadmath-printf.c: New file. * printf/gmp-impl.h: New file. * printf/lshift.c: New file. * printf/fpioconst.h: New file. * printf/add_n.c: New file. * printf/cmp.c: New file. * printf/sub_n.c: New file. * printf/mul.c: New file. * printf/divrem.c: New file. * printf/addmul_1.c: New file. * printf/printf_fp.c: New file. * printf/_itoa.h: New file. * printf/fpioconst.c: New file. * printf/_i18n_number.h: New file. * printf/flt1282mpn.c: New file. * printf/rshift.c: New file. * printf/mul_1.c: New file. * quadmath_io.c: Removed. * gdtoa/gdtoa.c: Removed. * gdtoa/g__fmt.c: Removed. * gdtoa/g_Qfmt.c: Removed. * gdtoa/dmisc.c: Removed. * gdtoa/ulp.c: Removed. * config.h.in: Regenerated. * configure: Regenerated. * Makefile.in: Regenerated. * io/write_float.def (DTOAQ): Use quadmath_snprintf instead of quadmath_flt128tostr. * io/transfer128.c (tmp2): Initialize to quadmath_snprintf instead of quadmath_flt128tostr. From-SVN: r170135
2011-02-14 16:34:44 +01:00
/* mpn_divrem -- Divide natural numbers, producing both remainder and
quotient.
Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
re PR fortran/47642 (real(kind=16) - libquadmath - segfault on amd64 FreeBSD) PR fortran/47642 * libquadmath.texi (quadmath_snprintf): Document. (quadmath_flt128tostr): Remove. * Makefile.am (libquadmath_la_SOURCES): Add printf/*.c. Remove quadmath_io.c, gdtoa/gdtoa.c, gdtoa/g__fmt.c, gdtoa/g_Qfmt.c, gdtoa/dmisc.c and gdtoa/ulp.c. * quadmath.h (quadmath_snprintf): New prototype. (quadmath_flt128tostr): Remove. * quadmath_weak.h (quadmath_snprintf): Add. (quadmath_flt128tostr): Remove. * configure.ac: New AC_CHECK_HEADERS headers: langinfo.h, wchar.h, wctype.h, limits.h, ctype.h, printf.h, errno.h. (AC_USE_SYSTEM_EXTENSIONS): Add. (HAVE_HIDDEN_VISIBILITY, HAVE_PRINTF_HOOKS, USE_LOCALE_SUPPORT, USE_I18N_NUMBER_H): New checks. * quadmath.map (QUADMATH_1.0): Add quadmath_snprintf. Remove quadmath_flt128tostr. * printf/printf_fphex.c: New file. * printf/_itowa.h: New file. * printf/mul_n.c: New file. * printf/quadmath-printf.h: New file. * printf/submul_1.c: New file. * printf/quadmath-printf.c: New file. * printf/gmp-impl.h: New file. * printf/lshift.c: New file. * printf/fpioconst.h: New file. * printf/add_n.c: New file. * printf/cmp.c: New file. * printf/sub_n.c: New file. * printf/mul.c: New file. * printf/divrem.c: New file. * printf/addmul_1.c: New file. * printf/printf_fp.c: New file. * printf/_itoa.h: New file. * printf/fpioconst.c: New file. * printf/_i18n_number.h: New file. * printf/flt1282mpn.c: New file. * printf/rshift.c: New file. * printf/mul_1.c: New file. * quadmath_io.c: Removed. * gdtoa/gdtoa.c: Removed. * gdtoa/g__fmt.c: Removed. * gdtoa/g_Qfmt.c: Removed. * gdtoa/dmisc.c: Removed. * gdtoa/ulp.c: Removed. * config.h.in: Regenerated. * configure: Regenerated. * Makefile.in: Regenerated. * io/write_float.def (DTOAQ): Use quadmath_snprintf instead of quadmath_flt128tostr. * io/transfer128.c (tmp2): Initialize to quadmath_snprintf instead of quadmath_flt128tostr. From-SVN: r170135
2011-02-14 16:34:44 +01:00
This file is part of the GNU MP Library.
The GNU MP 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 MP 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 MP Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
libquadmath.texi (FLT128_DIG, [...]): Document. * libquadmath.texi (FLT128_DIG, FLT128_MIN_10_EXP, FLT128_MAX_10_EXP): Document. (strtoflt128): Remove obsolete comment. * configure.ac (HAVE_STRTOULL): New check. * printf/gmp-impl.h (mpn_construct_float128): New prototype, define. * printf/mul_n.c: Include <config.h>. * printf/add_n.c: Likewise. * printf/cmp.c: Likewise. * printf/fpioconst.c: Likewise. * printf/mul_1.c: Likewise. * printf/rshift.c: Likewise. * printf/lshift.c: Likewise. * printf/submul_1.c: Likewise. * printf/sub_n.c: Likewise. * printf/divrem.c: Likewise. * printf/addmul_1.c: Likewise. * printf/mul.c: Likewise. * printf/quadmath-printf.h (isupper, isdigit, tolower): Change to avoid evaluating argument multiple times. (isxdigit): Redefine. * strtod/strtoflt128.c: New file. * strtod/strtod_l.c: New file. * strtod/mpn2flt128.c: New file. * strtod/grouping.h: New file. * strtod/tens_in_limb.c: New file. * gdtoa/arith.h: Removed. * gdtoa/gd_qnan.h: Removed. * gdtoa/gdtoa_fltrnds.h: Removed. * gdtoa/gdtoa.h: Removed. * gdtoa/gdtoaimp.h: Removed. * gdtoa/gethex.c: Removed. * gdtoa/gmisc.c: Removed. * gdtoa/hd_init.c: Removed. * gdtoa/hexnan.c: Removed. * gdtoa/makefile: Removed. * gdtoa/misc.c: Removed. * gdtoa/README.gdtoa: Removed. * gdtoa/smisc.c: Removed. * gdtoa/strtodg.c: Removed. * gdtoa/strtopQ.c: Removed. * gdtoa/sum.c: Removed. * quadmath.h (FLT128_DIG, FLT128_MIN_10_EXP, FLT128_MAX_10_EXP): Define. * Makefile.am (libquadmath_la_SOURCES): Remove gdtoa/*, add strtod/strtoflt128.c, strtod/mpn2flt128.c and strtod/tens_in_limb.c. * config.h.in: Regenerated. * configure: Regenerated. * Makefile.in: Regenerated. From-SVN: r170254
2011-02-17 20:57:18 +01:00
#include <config.h>
re PR fortran/47642 (real(kind=16) - libquadmath - segfault on amd64 FreeBSD) PR fortran/47642 * libquadmath.texi (quadmath_snprintf): Document. (quadmath_flt128tostr): Remove. * Makefile.am (libquadmath_la_SOURCES): Add printf/*.c. Remove quadmath_io.c, gdtoa/gdtoa.c, gdtoa/g__fmt.c, gdtoa/g_Qfmt.c, gdtoa/dmisc.c and gdtoa/ulp.c. * quadmath.h (quadmath_snprintf): New prototype. (quadmath_flt128tostr): Remove. * quadmath_weak.h (quadmath_snprintf): Add. (quadmath_flt128tostr): Remove. * configure.ac: New AC_CHECK_HEADERS headers: langinfo.h, wchar.h, wctype.h, limits.h, ctype.h, printf.h, errno.h. (AC_USE_SYSTEM_EXTENSIONS): Add. (HAVE_HIDDEN_VISIBILITY, HAVE_PRINTF_HOOKS, USE_LOCALE_SUPPORT, USE_I18N_NUMBER_H): New checks. * quadmath.map (QUADMATH_1.0): Add quadmath_snprintf. Remove quadmath_flt128tostr. * printf/printf_fphex.c: New file. * printf/_itowa.h: New file. * printf/mul_n.c: New file. * printf/quadmath-printf.h: New file. * printf/submul_1.c: New file. * printf/quadmath-printf.c: New file. * printf/gmp-impl.h: New file. * printf/lshift.c: New file. * printf/fpioconst.h: New file. * printf/add_n.c: New file. * printf/cmp.c: New file. * printf/sub_n.c: New file. * printf/mul.c: New file. * printf/divrem.c: New file. * printf/addmul_1.c: New file. * printf/printf_fp.c: New file. * printf/_itoa.h: New file. * printf/fpioconst.c: New file. * printf/_i18n_number.h: New file. * printf/flt1282mpn.c: New file. * printf/rshift.c: New file. * printf/mul_1.c: New file. * quadmath_io.c: Removed. * gdtoa/gdtoa.c: Removed. * gdtoa/g__fmt.c: Removed. * gdtoa/g_Qfmt.c: Removed. * gdtoa/dmisc.c: Removed. * gdtoa/ulp.c: Removed. * config.h.in: Regenerated. * configure: Regenerated. * Makefile.in: Regenerated. * io/write_float.def (DTOAQ): Use quadmath_snprintf instead of quadmath_flt128tostr. * io/transfer128.c (tmp2): Initialize to quadmath_snprintf instead of quadmath_flt128tostr. From-SVN: r170135
2011-02-14 16:34:44 +01:00
#include "gmp-impl.h"
/* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
the NSIZE-DSIZE least significant quotient limbs at QP
and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
non-zero, generate that many fraction bits and append them after the
other quotient limbs.
Return the most significant limb of the quotient, this is always 0 or 1.
Preconditions:
0. NSIZE >= DSIZE.
1. The most significant bit of the divisor must be set.
2. QP must either not overlap with the input operands at all, or
QP + DSIZE >= NP must hold true. (This means that it's
possible to put the quotient in the high part of NUM, right after the
remainder in NUM.
3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero. */
mp_limb_t
#if __STDC__
mpn_divrem (mp_ptr qp, mp_size_t qextra_limbs,
mp_ptr np, mp_size_t nsize,
mp_srcptr dp, mp_size_t dsize)
#else
mpn_divrem (qp, qextra_limbs, np, nsize, dp, dsize)
mp_ptr qp;
mp_size_t qextra_limbs;
mp_ptr np;
mp_size_t nsize;
mp_srcptr dp;
mp_size_t dsize;
#endif
{
mp_limb_t most_significant_q_limb = 0;
switch (dsize)
{
case 0:
/* We are asked to divide by zero, so go ahead and do it! (To make
the compiler not remove this statement, return the value.) */
return 1 / dsize;
case 1:
{
mp_size_t i;
mp_limb_t n1;
mp_limb_t d;
d = dp[0];
n1 = np[nsize - 1];
if (n1 >= d)
{
n1 -= d;
most_significant_q_limb = 1;
}
qp += qextra_limbs;
for (i = nsize - 2; i >= 0; i--)
udiv_qrnnd (qp[i], n1, n1, np[i], d);
qp -= qextra_limbs;
for (i = qextra_limbs - 1; i >= 0; i--)
udiv_qrnnd (qp[i], n1, n1, 0, d);
np[0] = n1;
}
break;
case 2:
{
mp_size_t i;
mp_limb_t n1, n0, n2;
mp_limb_t d1, d0;
np += nsize - 2;
d1 = dp[1];
d0 = dp[0];
n1 = np[1];
n0 = np[0];
if (n1 >= d1 && (n1 > d1 || n0 >= d0))
{
sub_ddmmss (n1, n0, n1, n0, d1, d0);
most_significant_q_limb = 1;
}
for (i = qextra_limbs + nsize - 2 - 1; i >= 0; i--)
{
mp_limb_t q;
mp_limb_t r;
if (i >= qextra_limbs)
np--;
else
np[0] = 0;
if (n1 == d1)
{
/* Q should be either 111..111 or 111..110. Need special
treatment of this rare case as normal division would
give overflow. */
q = ~(mp_limb_t) 0;
r = n0 + d1;
if (r < d1) /* Carry in the addition? */
{
add_ssaaaa (n1, n0, r - d0, np[0], 0, d0);
qp[i] = q;
continue;
}
n1 = d0 - (d0 != 0);
n0 = -d0;
}
else
{
udiv_qrnnd (q, r, n1, n0, d1);
umul_ppmm (n1, n0, d0, q);
}
n2 = np[0];
q_test:
if (n1 > r || (n1 == r && n0 > n2))
{
/* The estimated Q was too large. */
q--;
sub_ddmmss (n1, n0, n1, n0, 0, d0);
r += d1;
if (r >= d1) /* If not carry, test Q again. */
goto q_test;
}
qp[i] = q;
sub_ddmmss (n1, n0, r, n2, n1, n0);
}
np[1] = n1;
np[0] = n0;
}
break;
default:
{
mp_size_t i;
mp_limb_t dX, d1, n0;
np += nsize - dsize;
dX = dp[dsize - 1];
d1 = dp[dsize - 2];
n0 = np[dsize - 1];
if (n0 >= dX)
{
if (n0 > dX || mpn_cmp (np, dp, dsize - 1) >= 0)
{
mpn_sub_n (np, np, dp, dsize);
n0 = np[dsize - 1];
most_significant_q_limb = 1;
}
}
for (i = qextra_limbs + nsize - dsize - 1; i >= 0; i--)
{
mp_limb_t q;
mp_limb_t n1, n2;
mp_limb_t cy_limb;
if (i >= qextra_limbs)
{
np--;
n2 = np[dsize];
}
else
{
n2 = np[dsize - 1];
MPN_COPY_DECR (np + 1, np, dsize);
np[0] = 0;
}
if (n0 == dX)
/* This might over-estimate q, but it's probably not worth
the extra code here to find out. */
q = ~(mp_limb_t) 0;
else
{
mp_limb_t r;
udiv_qrnnd (q, r, n0, np[dsize - 1], dX);
umul_ppmm (n1, n0, d1, q);
while (n1 > r || (n1 == r && n0 > np[dsize - 2]))
{
q--;
r += dX;
if (r < dX) /* I.e. "carry in previous addition?" */
break;
n1 -= n0 < d1;
n0 -= d1;
}
}
/* Possible optimization: We already have (q * n0) and (1 * n1)
after the calculation of q. Taking advantage of that, we
could make this loop make two iterations less. */
cy_limb = mpn_submul_1 (np, dp, dsize, q);
if (n2 != cy_limb)
{
mpn_add_n (np, np, dp, dsize);
q--;
}
qp[i] = q;
n0 = np[dsize - 1];
}
}
}
return most_significant_q_limb;
}