Checking if A - B > D

Torbjorn Granlund tege at swox.com
Sun Sep 14 15:14:24 CEST 2003


I and Kalle came up a solution that we think will work.
I didn't look very closely at your solutions, but if
the simple code below doesn't do the job, I will look at
it more closely.

The area tmp needs to be dn limbs long, which might be acceptable.

int
nisse (mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn, mp_srcptr dp, mp_size_t dn)
{
  /* Check an, bn, dn for trivial cases where the sizes of these alone
     can determine A - B < D.  Left as an exercise to the user.  */
  foo;

  /* Here assume that an = bn, dn < an.  */
  for (i = dn; i < an; i++)
    {
      if (ap[i] != bp[i])
	{
	  /* We found a difference at higher significance that MSB of D.  */
	  return NO;
	}
    }

  mpn_sub_n (tmp, a, b, n);	/* cannot give carry out */
  if (mpn_cmp (tmp, dp, n) < 0)
    return YES;
  else
    return NO;
}

-- 
Torbjörn


More information about the gmp-devel mailing list