Line | Count | Source (jump to first uncovered line) |
1 | | /* mpz_scan0 -- search for a 0 bit. |
2 | | |
3 | | Copyright 2000-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 either: |
9 | | |
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. |
13 | | |
14 | | or |
15 | | |
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. |
19 | | |
20 | | or both in parallel, as here. |
21 | | |
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. |
26 | | |
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/. */ |
30 | | |
31 | | #include "gmp-impl.h" |
32 | | #include "longlong.h" |
33 | | |
34 | | |
35 | | /* mpn_scan0 can't be used for the u>0 search since there might not be a 0 |
36 | | bit before the end of the data. mpn_scan1 could be used for the inverted |
37 | | search under u<0, but usually the search won't go very far so it seems |
38 | | reasonable to inline that code. */ |
39 | | |
40 | | mp_bitcnt_t |
41 | | mpz_scan0 (mpz_srcptr u, mp_bitcnt_t starting_bit) __GMP_NOTHROW |
42 | 0 | { |
43 | 0 | mp_srcptr u_ptr = PTR(u); |
44 | 0 | mp_size_t size = SIZ(u); |
45 | 0 | mp_size_t abs_size = ABS(size); |
46 | 0 | mp_srcptr u_end = u_ptr + abs_size; |
47 | 0 | mp_size_t starting_limb = starting_bit / GMP_NUMB_BITS; |
48 | 0 | mp_srcptr p = u_ptr + starting_limb; |
49 | 0 | mp_limb_t limb; |
50 | 0 | int cnt; |
51 | | |
52 | | /* When past end, there's an immediate 0 bit for u>=0, or no 0 bits for |
53 | | u<0. Notice this test picks up all cases of u==0 too. */ |
54 | 0 | if (starting_limb >= abs_size) |
55 | 0 | return (size >= 0 ? starting_bit : ~(mp_bitcnt_t) 0); |
56 | | |
57 | 0 | limb = *p; |
58 | |
|
59 | 0 | if (size >= 0) |
60 | 0 | { |
61 | | /* Mask to 1 all bits before starting_bit, thus ignoring them. */ |
62 | 0 | limb |= (CNST_LIMB(1) << (starting_bit % GMP_NUMB_BITS)) - 1; |
63 | | |
64 | | /* Search for a limb which isn't all ones. If the end is reached then |
65 | | the zero bit immediately past the end is returned. */ |
66 | 0 | while (limb == GMP_NUMB_MAX) |
67 | 0 | { |
68 | 0 | p++; |
69 | 0 | if (p == u_end) |
70 | 0 | return (mp_bitcnt_t) abs_size * GMP_NUMB_BITS; |
71 | 0 | limb = *p; |
72 | 0 | } |
73 | | |
74 | | /* Now seek low 1 bit. */ |
75 | 0 | limb = ~limb; |
76 | 0 | } |
77 | 0 | else |
78 | 0 | { |
79 | 0 | mp_srcptr q; |
80 | | |
81 | | /* If there's a non-zero limb before ours then we're in the ones |
82 | | complement region. Search from *(p-1) downwards since that might |
83 | | give better cache locality, and since a non-zero in the middle of a |
84 | | number is perhaps a touch more likely than at the end. */ |
85 | 0 | q = p; |
86 | 0 | while (q != u_ptr) |
87 | 0 | { |
88 | 0 | q--; |
89 | 0 | if (*q != 0) |
90 | 0 | goto inverted; |
91 | 0 | } |
92 | | |
93 | | /* Adjust so ~limb implied by searching for 1 bit below becomes -limb. |
94 | | If limb==0 here then this isn't the beginning of twos complement |
95 | | inversion, but that doesn't matter because limb==0 is a zero bit |
96 | | immediately (-1 is all ones for below). */ |
97 | 0 | limb--; |
98 | |
|
99 | 0 | inverted: |
100 | | /* Now seeking a 1 bit. */ |
101 | | |
102 | | /* Mask to 0 all bits before starting_bit, thus ignoring them. */ |
103 | 0 | limb &= (MP_LIMB_T_MAX << (starting_bit % GMP_NUMB_BITS)); |
104 | |
|
105 | 0 | if (limb == 0) |
106 | 0 | { |
107 | | /* If the high limb is zero after masking, then no 1 bits past |
108 | | starting_bit. */ |
109 | 0 | p++; |
110 | 0 | if (p == u_end) |
111 | 0 | return ~(mp_bitcnt_t) 0; |
112 | | |
113 | | /* Search further for a non-zero limb. The high limb is non-zero, |
114 | | if nothing else. */ |
115 | 0 | for (;;) |
116 | 0 | { |
117 | 0 | limb = *p; |
118 | 0 | if (limb != 0) |
119 | 0 | break; |
120 | 0 | p++; |
121 | 0 | ASSERT (p < u_end); |
122 | 0 | } |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | 0 | ASSERT (limb != 0); |
127 | 0 | count_trailing_zeros (cnt, limb); |
128 | 0 | return (mp_bitcnt_t) (p - u_ptr) * GMP_NUMB_BITS + cnt; |
129 | 0 | } |