beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / scan0.c
blobde24595cfe916013922db567b10e6c07ab6a92af
1 /* mpz_scan0 -- search for a 0 bit.
3 Copyright 2000-2002, 2004, 2012 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 for more details.
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
31 #include "gmp.h"
32 #include "gmp-impl.h"
33 #include "longlong.h"
36 /* mpn_scan0 can't be used for the u>0 search since there might not be a 0
37 bit before the end of the data. mpn_scan1 could be used for the inverted
38 search under u<0, but usually the search won't go very far so it seems
39 reasonable to inline that code. */
41 mp_bitcnt_t
42 mpz_scan0 (mpz_srcptr u, mp_bitcnt_t starting_bit) __GMP_NOTHROW
44 mp_srcptr u_ptr = PTR(u);
45 mp_size_t size = SIZ(u);
46 mp_size_t abs_size = ABS(size);
47 mp_srcptr u_end = u_ptr + abs_size;
48 mp_size_t starting_limb = starting_bit / GMP_NUMB_BITS;
49 mp_srcptr p = u_ptr + starting_limb;
50 mp_limb_t limb;
51 int cnt;
53 /* When past end, there's an immediate 0 bit for u>=0, or no 0 bits for
54 u<0. Notice this test picks up all cases of u==0 too. */
55 if (starting_limb >= abs_size)
56 return (size >= 0 ? starting_bit : ~(mp_bitcnt_t) 0);
58 limb = *p;
60 if (size >= 0)
62 /* Mask to 1 all bits before starting_bit, thus ignoring them. */
63 limb |= (CNST_LIMB(1) << (starting_bit % GMP_NUMB_BITS)) - 1;
65 /* Search for a limb which isn't all ones. If the end is reached then
66 the zero bit immediately past the end is returned. */
67 while (limb == GMP_NUMB_MAX)
69 p++;
70 if (p == u_end)
71 return (mp_bitcnt_t) abs_size * GMP_NUMB_BITS;
72 limb = *p;
75 /* Now seek low 1 bit. */
76 limb = ~limb;
78 else
80 mp_srcptr q;
82 /* If there's a non-zero limb before ours then we're in the ones
83 complement region. Search from *(p-1) downwards since that might
84 give better cache locality, and since a non-zero in the middle of a
85 number is perhaps a touch more likely than at the end. */
86 q = p;
87 while (q != u_ptr)
89 q--;
90 if (*q != 0)
91 goto inverted;
94 /* Adjust so ~limb implied by searching for 1 bit below becomes -limb.
95 If limb==0 here then this isn't the beginning of twos complement
96 inversion, but that doesn't matter because limb==0 is a zero bit
97 immediately (-1 is all ones for below). */
98 limb--;
100 inverted:
101 /* Now seeking a 1 bit. */
103 /* Mask to 0 all bits before starting_bit, thus ignoring them. */
104 limb &= (MP_LIMB_T_MAX << (starting_bit % GMP_NUMB_BITS));
106 if (limb == 0)
108 /* If the high limb is zero after masking, then no 1 bits past
109 starting_bit. */
110 p++;
111 if (p == u_end)
112 return ~(mp_bitcnt_t) 0;
114 /* Search further for a non-zero limb. The high limb is non-zero,
115 if nothing else. */
116 for (;;)
118 limb = *p;
119 if (limb != 0)
120 break;
121 p++;
122 ASSERT (p < u_end);
127 ASSERT (limb != 0);
128 count_trailing_zeros (cnt, limb);
129 return (mp_bitcnt_t) (p - u_ptr) * GMP_NUMB_BITS + cnt;