diff --git a/gmp-impl.h b/gmp-impl.h index 3d20c432..39566455 100644 --- a/gmp-impl.h +++ b/gmp-impl.h @@ -1227,6 +1227,7 @@ mp_size_t mpn_fft_next_size _PROTO ((mp_size_t pl, int k)) ATTRIBUTE_CONST; #define DC_DIVAPPR_Q_N_ITCH(n) ((n)*4 + 64) #define DC_BDIV_Q_N_ITCH(n) ((n)/2 + 2) +#define DC_BDIV_QR_N_ITCH(n) (n) #define mpn_sb_divrem_mn __MPN(sb_divrem_mn) mp_limb_t mpn_sb_divrem_mn _PROTO ((mp_ptr, mp_ptr, mp_size_t, diff --git a/tests/mpn/Makefile.am b/tests/mpn/Makefile.am index c4da4b2e..c8af3323 100644 --- a/tests/mpn/Makefile.am +++ b/tests/mpn/Makefile.am @@ -31,7 +31,7 @@ check_PROGRAMS = t-asmtype t-aors_1 t-divrem_1 t-fat t-get_d \ t-dc_divappr_q_n t-inv_divappr_q_n t-invert t-sb_div_q t-sb_div_qr \ t-dc_div_q t-dc_div_qr t-dc_divappr_q t-dc_div_qr_n t-inv_divappr_q \ t-inv_div_q t-inv_div_qr t-inv_div_qr_n t-tdiv_qr t-sb_bdiv_q \ - t-sb_bdiv_qr t-dc_bdiv_q_n + t-sb_bdiv_qr t-dc_bdiv_q_n t-dc_bdiv_qr_n if ENABLE_STATIC if ENABLE_SHARED diff --git a/tests/mpn/Makefile.in b/tests/mpn/Makefile.in index 8579de8f..7b995d40 100644 --- a/tests/mpn/Makefile.in +++ b/tests/mpn/Makefile.in @@ -68,7 +68,7 @@ check_PROGRAMS = t-asmtype$(EXEEXT) t-aors_1$(EXEEXT) \ t-inv_div_q$(EXEEXT) t-inv_div_qr$(EXEEXT) \ t-inv_div_qr_n$(EXEEXT) t-tdiv_qr$(EXEEXT) \ t-sb_bdiv_q$(EXEEXT) t-sb_bdiv_qr$(EXEEXT) \ - t-dc_bdiv_q_n$(EXEEXT) $(am__EXEEXT_1) + t-dc_bdiv_q_n$(EXEEXT) t-dc_bdiv_qr_n$(EXEEXT) $(am__EXEEXT_1) @ENABLE_SHARED_TRUE@@ENABLE_STATIC_TRUE@am__append_1 = st_fat st_instrument subdir = tests/mpn DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in @@ -127,6 +127,11 @@ t_dc_bdiv_q_n_OBJECTS = t-dc_bdiv_q_n.$(OBJEXT) t_dc_bdiv_q_n_LDADD = $(LDADD) t_dc_bdiv_q_n_DEPENDENCIES = $(top_builddir)/tests/libtests.la \ $(top_builddir)/libmpir.la +t_dc_bdiv_qr_n_SOURCES = t-dc_bdiv_qr_n.c +t_dc_bdiv_qr_n_OBJECTS = t-dc_bdiv_qr_n.$(OBJEXT) +t_dc_bdiv_qr_n_LDADD = $(LDADD) +t_dc_bdiv_qr_n_DEPENDENCIES = $(top_builddir)/tests/libtests.la \ + $(top_builddir)/libmpir.la t_dc_div_q_SOURCES = t-dc_div_q.c t_dc_div_q_OBJECTS = t-dc_div_q.$(OBJEXT) t_dc_div_q_LDADD = $(LDADD) @@ -326,20 +331,20 @@ LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ $(LDFLAGS) -o $@ SOURCES = $(st_fat_SOURCES) $(st_instrument_SOURCES) t-addadd_n.c \ t-addsub_n.c t-aors_1.c t-asmtype.c t-dc_bdiv_q_n.c \ - t-dc_div_q.c t-dc_div_qr.c t-dc_div_qr_n.c t-dc_divappr_q.c \ - t-dc_divappr_q_n.c t-divebyBm1of.c t-divebyff.c t-divrem_1.c \ - t-fat.c t-get_d.c t-instrument.c t-inv_div_q.c t-inv_div_qr.c \ - t-inv_div_qr_n.c t-inv_divappr_q.c t-inv_divappr_q_n.c \ - t-invert.c t-iord_u.c t-lorrshift1.c t-mp_bases.c \ - t-mullow_basecase.c t-mullowhigh.c t-mulmid.c \ - t-mulmod_2expm1.c t-mulmod_2expp1.c t-neg.c t-perfsqr.c \ - t-redc_basecase.c t-sb_bdiv_q.c t-sb_bdiv_qr.c t-sb_div_q.c \ - t-sb_div_qr.c t-sb_divappr_q.c t-scan.c t-subadd_n.c \ - t-tdiv_q.c t-tdiv_qr.c + t-dc_bdiv_qr_n.c t-dc_div_q.c t-dc_div_qr.c t-dc_div_qr_n.c \ + t-dc_divappr_q.c t-dc_divappr_q_n.c t-divebyBm1of.c \ + t-divebyff.c t-divrem_1.c t-fat.c t-get_d.c t-instrument.c \ + t-inv_div_q.c t-inv_div_qr.c t-inv_div_qr_n.c \ + t-inv_divappr_q.c t-inv_divappr_q_n.c t-invert.c t-iord_u.c \ + t-lorrshift1.c t-mp_bases.c t-mullow_basecase.c t-mullowhigh.c \ + t-mulmid.c t-mulmod_2expm1.c t-mulmod_2expp1.c t-neg.c \ + t-perfsqr.c t-redc_basecase.c t-sb_bdiv_q.c t-sb_bdiv_qr.c \ + t-sb_div_q.c t-sb_div_qr.c t-sb_divappr_q.c t-scan.c \ + t-subadd_n.c t-tdiv_q.c t-tdiv_qr.c DIST_SOURCES = $(am__st_fat_SOURCES_DIST) \ $(am__st_instrument_SOURCES_DIST) t-addadd_n.c t-addsub_n.c \ - t-aors_1.c t-asmtype.c t-dc_bdiv_q_n.c t-dc_div_q.c \ - t-dc_div_qr.c t-dc_div_qr_n.c t-dc_divappr_q.c \ + t-aors_1.c t-asmtype.c t-dc_bdiv_q_n.c t-dc_bdiv_qr_n.c \ + t-dc_div_q.c t-dc_div_qr.c t-dc_div_qr_n.c t-dc_divappr_q.c \ t-dc_divappr_q_n.c t-divebyBm1of.c t-divebyff.c t-divrem_1.c \ t-fat.c t-get_d.c t-instrument.c t-inv_div_q.c t-inv_div_qr.c \ t-inv_div_qr_n.c t-inv_divappr_q.c t-inv_divappr_q_n.c \ @@ -572,6 +577,9 @@ t-asmtype$(EXEEXT): $(t_asmtype_OBJECTS) $(t_asmtype_DEPENDENCIES) t-dc_bdiv_q_n$(EXEEXT): $(t_dc_bdiv_q_n_OBJECTS) $(t_dc_bdiv_q_n_DEPENDENCIES) @rm -f t-dc_bdiv_q_n$(EXEEXT) $(LINK) $(t_dc_bdiv_q_n_OBJECTS) $(t_dc_bdiv_q_n_LDADD) $(LIBS) +t-dc_bdiv_qr_n$(EXEEXT): $(t_dc_bdiv_qr_n_OBJECTS) $(t_dc_bdiv_qr_n_DEPENDENCIES) + @rm -f t-dc_bdiv_qr_n$(EXEEXT) + $(LINK) $(t_dc_bdiv_qr_n_OBJECTS) $(t_dc_bdiv_qr_n_LDADD) $(LIBS) t-dc_div_q$(EXEEXT): $(t_dc_div_q_OBJECTS) $(t_dc_div_q_DEPENDENCIES) @rm -f t-dc_div_q$(EXEEXT) $(LINK) $(t_dc_div_q_OBJECTS) $(t_dc_div_q_LDADD) $(LIBS) diff --git a/tests/mpn/t-dc_bdiv_qr_n.c b/tests/mpn/t-dc_bdiv_qr_n.c new file mode 100644 index 00000000..84e22000 --- /dev/null +++ b/tests/mpn/t-dc_bdiv_qr_n.c @@ -0,0 +1,109 @@ +/* Test mpn_dc_bdiv_qr_n. + +Copyright 2002 Free Software Foundation, Inc. +Copyright 2009, 2010 William Hart + +This file is part of the MPIR Library. + +The MPIR 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 MPIR 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 MPIR Library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +MA 02110-1301, USA. */ + +#include +#include + +#include "mpir.h" +#include "gmp-impl.h" +#include "longlong.h" +#include "tests.h" + +#if defined(_MSC_VER) || defined(__MINGW32__) +#define random rand +#endif + +#define MAX_LIMBS 400 +#define ITERS 10000 + +/* Check divide and conquer hensel division routine. */ +void +check_dc_bdiv_qr_n (void) +{ + mp_limb_t np[2*MAX_LIMBS]; + mp_limb_t np2[2*MAX_LIMBS]; + mp_limb_t rp[2*MAX_LIMBS]; + mp_limb_t dp[2*MAX_LIMBS]; + mp_limb_t qp[2*MAX_LIMBS]; + mp_limb_t tp[DC_BDIV_QR_N_ITCH(MAX_LIMBS)]; + mp_limb_t dip, cy1, cy2; + + mp_size_t nn, rn, dn, qn; + + gmp_randstate_t rands; + + int i, j, s; + gmp_randinit_default(rands); + + for (i = 0; i < ITERS; i++) + { + dn = (random() % (MAX_LIMBS - 2)) + 3; + nn = 2*dn; + qn = dn; + + mpn_rrandom (np, rands, nn); + mpn_rrandom (dp, rands, dn); + + dp[0] |= 1; + + MPN_COPY(np2, np, nn); + + modlimb_invert(dip, dp[0]); + + cy1 = mpn_dc_bdiv_qr_n(qp, np, dp, dn, dip, tp); + + if (qn >= dn) mpn_mul(rp, qp, qn, dp, dn); + else mpn_mul(rp, dp, dn, qp, qn); + + cy2 = mpn_sub_n(rp, np2, rp, nn); + + if (mpn_cmp(rp + qn, np + qn, dn) != 0) + { + printf("failed: quotient wrong!\n"); + printf ("nn = %lu, dn = %lu, qn = %lu\n\n", nn, dn, qn); + gmp_printf (" np: %Nx\n\n", np2, nn); + gmp_printf (" dp: %Nx\n\n", dp, dn); + gmp_printf (" qp: %Nx\n\n", qp, qn); + gmp_printf (" rp: %Nx\n\n", rp, qn); + abort (); + } + + if (cy1 != cy2) + { + printf("failed: carry wrong!\n"); + printf("cy1 = %lx, cy2 = %lx\n", cy1, cy2); + } + } + + gmp_randclear(rands); +} + +int +main (void) +{ + tests_start (); + + check_dc_bdiv_qr_n (); + + tests_end (); + exit (0); +}