beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / scan1.c
blobe6b759e2bebc4cdc09dc59b9468697db29ecad15
1 /* mpz_scan1 -- search for a 1 bit.
3 Copyright 2000-2002, 2004, 2012, 2015 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 inverted u<0 search since there might not
37 be a 0 bit before the end of the data. mpn_scan1 could be used under u>0
38 (except when in the high limb), but usually the search won't go very far
39 so it seems reasonable to inline that code. */
41 mp_bitcnt_t
42 mpz_scan1 (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 - 1;
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 /* Past the end there's no 1 bits for u>=0, or an immediate 1 bit for u<0.
54 Notice this test picks up any u==0 too. */
55 if (starting_limb >= abs_size)
56 return (size >= 0 ? ~(mp_bitcnt_t) 0 : starting_bit);
58 /* This is an important case, where sign is not relevant! */
59 if (starting_bit == 0)
60 goto short_cut;
62 limb = *p;
64 if (size >= 0)
66 /* Mask to 0 all bits before starting_bit, thus ignoring them. */
67 limb &= (MP_LIMB_T_MAX << (starting_bit % GMP_NUMB_BITS));
69 if (limb == 0)
71 /* If it's the high limb which is zero after masking, then there's
72 no 1 bits after starting_bit. */
73 if (p == u_end)
74 return ~(mp_bitcnt_t) 0;
76 /* Otherwise search further for a non-zero limb. The high limb is
77 non-zero, if nothing else. */
78 search_nonzero:
81 ASSERT (p != u_end);
82 p++;
83 short_cut:
84 limb = *p;
86 while (limb == 0);
89 else
91 /* If there's a non-zero limb before ours then we're in the ones
92 complement region. */
93 if (starting_limb == 0 || mpn_zero_p (u_ptr, starting_limb)) {
94 if (limb == 0)
95 /* Seeking for the first non-zero bit, it is the same for u and -u. */
96 goto search_nonzero;
98 /* Adjust so ~limb implied by searching for 0 bit becomes -limb. */
99 limb--;
102 /* Now seeking a 0 bit. */
104 /* Mask to 1 all bits before starting_bit, thus ignoring them. */
105 limb |= (CNST_LIMB(1) << (starting_bit % GMP_NUMB_BITS)) - 1;
107 /* Search for a limb which is not all ones. If the end is reached
108 then the zero immediately past the end is the result. */
109 while (limb == GMP_NUMB_MAX)
111 if (p == u_end)
112 return (mp_bitcnt_t) abs_size * GMP_NUMB_BITS;
113 p++;
114 limb = *p;
117 /* Now seeking low 1 bit. */
118 limb = ~limb;
121 ASSERT (limb != 0);
122 count_trailing_zeros (cnt, limb);
123 return (mp_bitcnt_t) (p - u_ptr) * GMP_NUMB_BITS + cnt;