Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* mpz_clrbit -- clear a specified bit.  | 
2  |  |  | 
3  |  | Copyright 1991, 1993-1995, 2001, 2002, 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  |  |  | 
33  |  | void  | 
34  |  | mpz_clrbit (mpz_ptr d, mp_bitcnt_t bit_idx)  | 
35  | 0  | { | 
36  | 0  |   mp_size_t dsize = SIZ (d);  | 
37  | 0  |   mp_ptr dp = PTR (d);  | 
38  | 0  |   mp_size_t limb_idx;  | 
39  | 0  |   mp_limb_t mask;  | 
40  |  | 
  | 
41  | 0  |   limb_idx = bit_idx / GMP_NUMB_BITS;  | 
42  | 0  |   mask = CNST_LIMB(1) << (bit_idx % GMP_NUMB_BITS);  | 
43  | 0  |   if (dsize >= 0)  | 
44  | 0  |     { | 
45  | 0  |       if (limb_idx < dsize)  | 
46  | 0  |   { | 
47  | 0  |     mp_limb_t  dlimb;  | 
48  | 0  |     dlimb = dp[limb_idx] & ~mask;  | 
49  | 0  |     dp[limb_idx] = dlimb;  | 
50  |  | 
  | 
51  | 0  |     if (UNLIKELY ((dlimb == 0) + limb_idx == dsize)) /* dsize == limb_idx + 1 */  | 
52  | 0  |       { | 
53  |  |         /* high limb became zero, must normalize */  | 
54  | 0  |         MPN_NORMALIZE (dp, limb_idx);  | 
55  | 0  |         SIZ (d) = limb_idx;  | 
56  | 0  |       }  | 
57  | 0  |   }  | 
58  | 0  |       else  | 
59  | 0  |   ;  | 
60  | 0  |     }  | 
61  | 0  |   else  | 
62  | 0  |     { | 
63  |  |       /* Simulate two's complement arithmetic, i.e. simulate  | 
64  |  |    1. Set OP = ~(OP - 1) [with infinitely many leading ones].  | 
65  |  |    2. clear the bit.  | 
66  |  |    3. Set OP = ~OP + 1.  */  | 
67  |  | 
  | 
68  | 0  |       dsize = -dsize;  | 
69  |  | 
  | 
70  | 0  |       if (limb_idx < dsize)  | 
71  | 0  |   { | 
72  | 0  |     mp_size_t zero_bound;  | 
73  |  |  | 
74  |  |     /* No index upper bound on this loop, we're sure there's a non-zero limb  | 
75  |  |        sooner or later.  */  | 
76  | 0  |     zero_bound = 0;  | 
77  | 0  |     while (dp[zero_bound] == 0)  | 
78  | 0  |       zero_bound++;  | 
79  |  | 
  | 
80  | 0  |     if (limb_idx > zero_bound)  | 
81  | 0  |       { | 
82  | 0  |         dp[limb_idx] |= mask;  | 
83  | 0  |       }  | 
84  | 0  |     else if (limb_idx == zero_bound)  | 
85  | 0  |       { | 
86  | 0  |         mp_limb_t  dlimb;  | 
87  | 0  |         dlimb = (((dp[limb_idx] - 1) | mask) + 1) & GMP_NUMB_MASK;  | 
88  | 0  |         dp[limb_idx] = dlimb;  | 
89  |  | 
  | 
90  | 0  |         if (dlimb == 0)  | 
91  | 0  |     { | 
92  |  |       /* Increment at limb_idx + 1.  Extend the number with a zero limb  | 
93  |  |          for simplicity.  */  | 
94  | 0  |       dp = MPZ_REALLOC (d, dsize + 1);  | 
95  | 0  |       dp[dsize] = 0;  | 
96  | 0  |       MPN_INCR_U (dp + limb_idx + 1, dsize - limb_idx, 1);  | 
97  | 0  |       dsize += dp[dsize];  | 
98  |  | 
  | 
99  | 0  |       SIZ (d) = -dsize;  | 
100  | 0  |     }  | 
101  | 0  |       }  | 
102  | 0  |     else  | 
103  | 0  |       ;  | 
104  | 0  |   }  | 
105  | 0  |       else  | 
106  | 0  |   { | 
107  |  |     /* Ugh.  The bit should be cleared outside of the end of the  | 
108  |  |        number.  We have to increase the size of the number.  */  | 
109  | 0  |     dp = MPZ_REALLOC (d, limb_idx + 1);  | 
110  | 0  |     SIZ (d) = -(limb_idx + 1);  | 
111  | 0  |     MPN_ZERO (dp + dsize, limb_idx - dsize);  | 
112  | 0  |     dp[limb_idx] = mask;  | 
113  | 0  |   }  | 
114  | 0  |     }  | 
115  | 0  | }  |