1/* mpz_scan1 -- search for a 1 bit.
2
3Copyright 2000, 2001, 2002, 2004, 2012 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19
20#include "gmp.h"
21#include "gmp-impl.h"
22#include "longlong.h"
23
24
25/* mpn_scan0 can't be used for the inverted u<0 search since there might not
26   be a 0 bit before the end of the data.  mpn_scan1 could be used under u>0
27   (except when in the high limb), but usually the search won't go very far
28   so it seems reasonable to inline that code.  */
29
30mp_bitcnt_t
31mpz_scan1 (mpz_srcptr u, mp_bitcnt_t starting_bit) __GMP_NOTHROW
32{
33  mp_srcptr      u_ptr = PTR(u);
34  mp_size_t      size = SIZ(u);
35  mp_size_t      abs_size = ABS(size);
36  mp_srcptr      u_end = u_ptr + abs_size;
37  mp_size_t      starting_limb = starting_bit / GMP_NUMB_BITS;
38  mp_srcptr      p = u_ptr + starting_limb;
39  mp_limb_t      limb;
40  int            cnt;
41
42  /* Past the end there's no 1 bits for u>=0, or an immediate 1 bit for u<0.
43     Notice this test picks up any u==0 too. */
44  if (starting_limb >= abs_size)
45    return (size >= 0 ? ~(mp_bitcnt_t) 0 : starting_bit);
46
47  limb = *p;
48
49  if (size >= 0)
50    {
51      /* Mask to 0 all bits before starting_bit, thus ignoring them. */
52      limb &= (MP_LIMB_T_MAX << (starting_bit % GMP_NUMB_BITS));
53
54      if (limb == 0)
55	{
56	  /* If it's the high limb which is zero after masking, then there's
57	     no 1 bits after starting_bit.  */
58	  p++;
59	  if (p == u_end)
60	    return ~(mp_bitcnt_t) 0;
61
62	  /* Otherwise search further for a non-zero limb.  The high limb is
63	     non-zero, if nothing else.  */
64	  for (;;)
65	    {
66	      limb = *p;
67	      if (limb != 0)
68		break;
69	      p++;
70	      ASSERT (p < u_end);
71	    }
72	}
73    }
74  else
75    {
76      mp_srcptr  q;
77
78      /* If there's a non-zero limb before ours then we're in the ones
79	 complement region.  Search from *(p-1) downwards since that might
80	 give better cache locality, and since a non-zero in the middle of a
81	 number is perhaps a touch more likely than at the end.  */
82      q = p;
83      while (q != u_ptr)
84	{
85	  q--;
86	  if (*q != 0)
87	    goto inverted;
88	}
89
90      if (limb == 0)
91	{
92	  /* Skip zero limbs, to find the start of twos complement.  The
93	     high limb is non-zero, if nothing else.  This search is
94	     necessary so the -limb is applied at the right spot. */
95	  do
96	    {
97	      p++;
98	      ASSERT (p < u_end);
99	      limb = *p;
100	    }
101	  while (limb == 0);
102
103	  /* Apply twos complement, and look for a 1 bit in that.  Since
104	     limb!=0 here, also have (-limb)!=0 so there's certainly a 1
105	     bit.  */
106	  limb = -limb;
107	  goto got_limb;
108	}
109
110      /* Adjust so ~limb implied by searching for 0 bit becomes -limb.  */
111      limb--;
112
113    inverted:
114      /* Now seeking a 0 bit. */
115
116      /* Mask to 1 all bits before starting_bit, thus ignoring them. */
117      limb |= (CNST_LIMB(1) << (starting_bit % GMP_NUMB_BITS)) - 1;
118
119      /* Search for a limb which is not all ones.  If the end is reached
120	 then the zero immediately past the end is the result.  */
121      while (limb == GMP_NUMB_MAX)
122	{
123	  p++;
124	  if (p == u_end)
125	    return (mp_bitcnt_t) abs_size * GMP_NUMB_BITS;
126	  limb = *p;
127	}
128
129      /* Now seeking low 1 bit. */
130      limb = ~limb;
131    }
132
133 got_limb:
134  ASSERT (limb != 0);
135  count_trailing_zeros (cnt, limb);
136  return (mp_bitcnt_t) (p - u_ptr) * GMP_NUMB_BITS + cnt;
137}
138