Blame mpz/scan0.c

Packit 5c3484
/* mpz_scan0 -- search for a 0 bit.
Packit 5c3484
Packit 5c3484
Copyright 2000-2002, 2004, 2012 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "longlong.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* mpn_scan0 can't be used for the u>0 search since there might not be a 0
Packit 5c3484
   bit before the end of the data.  mpn_scan1 could be used for the inverted
Packit 5c3484
   search under u<0, but usually the search won't go very far so it seems
Packit 5c3484
   reasonable to inline that code.  */
Packit 5c3484
Packit 5c3484
mp_bitcnt_t
Packit 5c3484
mpz_scan0 (mpz_srcptr u, mp_bitcnt_t starting_bit) __GMP_NOTHROW
Packit 5c3484
{
Packit 5c3484
  mp_srcptr      u_ptr = PTR(u);
Packit 5c3484
  mp_size_t      size = SIZ(u);
Packit 5c3484
  mp_size_t      abs_size = ABS(size);
Packit 5c3484
  mp_srcptr      u_end = u_ptr + abs_size;
Packit 5c3484
  mp_size_t      starting_limb = starting_bit / GMP_NUMB_BITS;
Packit 5c3484
  mp_srcptr      p = u_ptr + starting_limb;
Packit 5c3484
  mp_limb_t      limb;
Packit 5c3484
  int            cnt;
Packit 5c3484
Packit 5c3484
  /* When past end, there's an immediate 0 bit for u>=0, or no 0 bits for
Packit 5c3484
     u<0.  Notice this test picks up all cases of u==0 too. */
Packit 5c3484
  if (starting_limb >= abs_size)
Packit 5c3484
    return (size >= 0 ? starting_bit : ~(mp_bitcnt_t) 0);
Packit 5c3484
Packit 5c3484
  limb = *p;
Packit 5c3484
Packit 5c3484
  if (size >= 0)
Packit 5c3484
    {
Packit 5c3484
      /* Mask to 1 all bits before starting_bit, thus ignoring them. */
Packit 5c3484
      limb |= (CNST_LIMB(1) << (starting_bit % GMP_NUMB_BITS)) - 1;
Packit 5c3484
Packit 5c3484
      /* Search for a limb which isn't all ones.  If the end is reached then
Packit 5c3484
	 the zero bit immediately past the end is returned.  */
Packit 5c3484
      while (limb == GMP_NUMB_MAX)
Packit 5c3484
	{
Packit 5c3484
	  p++;
Packit 5c3484
	  if (p == u_end)
Packit 5c3484
	    return (mp_bitcnt_t) abs_size * GMP_NUMB_BITS;
Packit 5c3484
	  limb = *p;
Packit 5c3484
	}
Packit 5c3484
Packit 5c3484
      /* Now seek low 1 bit. */
Packit 5c3484
      limb = ~limb;
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      mp_srcptr  q;
Packit 5c3484
Packit 5c3484
      /* If there's a non-zero limb before ours then we're in the ones
Packit 5c3484
	 complement region.  Search from *(p-1) downwards since that might
Packit 5c3484
	 give better cache locality, and since a non-zero in the middle of a
Packit 5c3484
	 number is perhaps a touch more likely than at the end.  */
Packit 5c3484
      q = p;
Packit 5c3484
      while (q != u_ptr)
Packit 5c3484
	{
Packit 5c3484
	  q--;
Packit 5c3484
	  if (*q != 0)
Packit 5c3484
	    goto inverted;
Packit 5c3484
	}
Packit 5c3484
Packit 5c3484
      /* Adjust so ~limb implied by searching for 1 bit below becomes -limb.
Packit 5c3484
	 If limb==0 here then this isn't the beginning of twos complement
Packit 5c3484
	 inversion, but that doesn't matter because limb==0 is a zero bit
Packit 5c3484
	 immediately (-1 is all ones for below).  */
Packit 5c3484
      limb--;
Packit 5c3484
Packit 5c3484
    inverted:
Packit 5c3484
      /* Now seeking a 1 bit. */
Packit 5c3484
Packit 5c3484
      /* Mask to 0 all bits before starting_bit, thus ignoring them. */
Packit 5c3484
      limb &= (MP_LIMB_T_MAX << (starting_bit % GMP_NUMB_BITS));
Packit 5c3484
Packit 5c3484
      if (limb == 0)
Packit 5c3484
	{
Packit 5c3484
	  /* If the high limb is zero after masking, then no 1 bits past
Packit 5c3484
	     starting_bit.  */
Packit 5c3484
	  p++;
Packit 5c3484
	  if (p == u_end)
Packit 5c3484
	    return ~(mp_bitcnt_t) 0;
Packit 5c3484
Packit 5c3484
	  /* Search further for a non-zero limb.  The high limb is non-zero,
Packit 5c3484
	     if nothing else.  */
Packit 5c3484
	  for (;;)
Packit 5c3484
	    {
Packit 5c3484
	      limb = *p;
Packit 5c3484
	      if (limb != 0)
Packit 5c3484
		break;
Packit 5c3484
	      p++;
Packit 5c3484
	      ASSERT (p < u_end);
Packit 5c3484
	    }
Packit 5c3484
	}
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  ASSERT (limb != 0);
Packit 5c3484
  count_trailing_zeros (cnt, limb);
Packit 5c3484
  return (mp_bitcnt_t) (p - u_ptr) * GMP_NUMB_BITS + cnt;
Packit 5c3484
}