1 /* mpz_scan0 -- search for a 0 bit.
2
3 Copyright 2000, 2001, 2002, 2004, 2012 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along 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 u>0 search since there might not be a 0
26 bit before the end of the data. mpn_scan1 could be used for the inverted
27 search under u<0, but usually the search won't go very far so it seems
28 reasonable to inline that code. */
29
30 mp_bitcnt_t
mpz_scan0(mpz_srcptr u,mp_bitcnt_t starting_bit)31 mpz_scan0 (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 /* When past end, there's an immediate 0 bit for u>=0, or no 0 bits for
43 u<0. Notice this test picks up all cases of u==0 too. */
44 if (starting_limb >= abs_size)
45 return (size >= 0 ? starting_bit : ~(mp_bitcnt_t) 0);
46
47 limb = *p;
48
49 if (size >= 0)
50 {
51 /* Mask to 1 all bits before starting_bit, thus ignoring them. */
52 limb |= (CNST_LIMB(1) << (starting_bit % GMP_NUMB_BITS)) - 1;
53
54 /* Search for a limb which isn't all ones. If the end is reached then
55 the zero bit immediately past the end is returned. */
56 while (limb == GMP_NUMB_MAX)
57 {
58 p++;
59 if (p == u_end)
60 return (mp_bitcnt_t) abs_size * GMP_NUMB_BITS;
61 limb = *p;
62 }
63
64 /* Now seek low 1 bit. */
65 limb = ~limb;
66 }
67 else
68 {
69 mp_srcptr q;
70
71 /* If there's a non-zero limb before ours then we're in the ones
72 complement region. Search from *(p-1) downwards since that might
73 give better cache locality, and since a non-zero in the middle of a
74 number is perhaps a touch more likely than at the end. */
75 q = p;
76 while (q != u_ptr)
77 {
78 q--;
79 if (*q != 0)
80 goto inverted;
81 }
82
83 /* Adjust so ~limb implied by searching for 1 bit below becomes -limb.
84 If limb==0 here then this isn't the beginning of twos complement
85 inversion, but that doesn't matter because limb==0 is a zero bit
86 immediately (-1 is all ones for below). */
87 limb--;
88
89 inverted:
90 /* Now seeking a 1 bit. */
91
92 /* Mask to 0 all bits before starting_bit, thus ignoring them. */
93 limb &= (MP_LIMB_T_MAX << (starting_bit % GMP_NUMB_BITS));
94
95 if (limb == 0)
96 {
97 /* If the high limb is zero after masking, then no 1 bits past
98 starting_bit. */
99 p++;
100 if (p == u_end)
101 return ~(mp_bitcnt_t) 0;
102
103 /* Search further for a non-zero limb. The high limb is non-zero,
104 if nothing else. */
105 for (;;)
106 {
107 limb = *p;
108 if (limb != 0)
109 break;
110 p++;
111 ASSERT (p < u_end);
112 }
113 }
114 }
115
116 ASSERT (limb != 0);
117 count_trailing_zeros (cnt, limb);
118 return (mp_bitcnt_t) (p - u_ptr) * GMP_NUMB_BITS + cnt;
119 }
120