udiv_qr_3by2 vs divappr

Niels Möller nisse at lysator.liu.se
Sun May 6 12:01:07 UTC 2018


nisse at lysator.liu.se (Niels Möller) writes:

> This needs more analysis. 

I'm attaching my notes, and below is a version of sbpi1_div_qr.c which
seems to pass tests (have had t-div running with random seeds for a few
hours, in a 32-bit build).

Still unclear (i) if it's correct for d0 = 0, and (ii) exactly what
conditions we need to exclude divappr2 from return a q that spills over
from B-1 to 0 the limit in the code, {n1, n0} >= {dn-1, d0} was a guess
but it might be correct.

Regards,
/Niels

/* mpn_sbpi1_div_qr -- Schoolbook division using the Möller-Granlund 3/2
   division algorithm.

   Contributed to the GNU project by Torbjorn Granlund.

   THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.  IT IS ONLY
   SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
   GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.

Copyright 2007, 2009 Free Software Foundation, Inc.

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 either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

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 General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library.  If not,
see https://www.gnu.org/licenses/.  */


#include "gmp-impl.h"
#include "longlong.h"

static inline mp_limb_t
divappr_2 (mp_limb_t n2, mp_limb_t n1,
         mp_limb_t d1, mp_limb_t d0, mp_limb_t dinv)
{
  mp_limb_t q1, q0, r1, t1, t0, mask;

  umul_ppmm (q1, q0, n2, dinv);
  add_ssaaaa (q1, q0, q1, q0, n2, n1);

  umul_ppmm (t1, t0, q1, d0);
  q1++; /* May overflow */
  r1 = n1 - d1 * q1 - t1 - 1 - (t0 > -d0);

  mask = - (mp_limb_t) (r1 >= q0);
  q1 += mask;
  r1 += mask & (d1 + 1);
  q1 += (r1 >= d1 - 1);

  return q1;
}

mp_limb_t
mpn_sbpi1_div_qr (mp_ptr qp,
		  mp_ptr np, mp_size_t nn,
		  mp_srcptr dp, mp_size_t dn,
		  mp_limb_t dinv)
{
  mp_limb_t qh;
  mp_size_t i;
  mp_limb_t n1;
  mp_limb_t d1, d0;
  mp_limb_t cy, cy1;
  mp_limb_t q;

  ASSERT (dn > 2);
  ASSERT (nn >= dn);
  ASSERT ((dp[dn-1] & GMP_NUMB_HIGHBIT) != 0);

  np += nn;

  qh = mpn_cmp (np - dn, dp, dn) >= 0;
  if (qh != 0)
    mpn_sub_n (np - dn, np - dn, dp, dn);

  qp += nn - dn;

  dn -= 2;			/* offset dn by 2 for main division loops,
				   saving two iterations in mpn_submul_1.  */
  d1 = dp[dn + 1];
  d0 = dp[dn + 0];

  np -= 2;

  for (i = nn - (dn + 2); i > 0; i--)
    {
      n1 = np[1];

      np--;
      if (UNLIKELY (n1 >= d1 - 1) && (n1 == d1 || np[1] > d0))
	{
	  q = GMP_NUMB_MASK;
	}
      else
	{
	  q = divappr_2 (n1, np[1], d1, d0, dinv);
	}
      cy = mpn_submul_1 (np - dn, dp, dn + 2, q);
      ASSERT_ALWAYS (cy >= n1);

      if (UNLIKELY (cy != n1))
	{
	  ASSERT_CARRY(mpn_add_n (np - dn, np - dn, dp, dn + 2));
	  q--;
	}

      *--qp = q;
    }

  return qh;
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: divappr2.pdf
Type: application/pdf
Size: 116389 bytes
Desc: not available
URL: <https://gmplib.org/list-archives/gmp-devel/attachments/20180506/4c624a0b/attachment-0001.pdf>
-------------- next part --------------

-- 
Niels Möller. PGP-encrypted email is preferred. Keyid 368C6677.
Internet email is subject to wholesale government surveillance.


More information about the gmp-devel mailing list