1 /* mpz_clrbit -- clear a specified bit.
3 Copyright 1991, 1993, 1994, 1995, 2001, 2002 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 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.
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.
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/. */
24 mpz_clrbit (mpz_ptr d, mp_bitcnt_t bit_index)
26 mp_size_t dsize = d->_mp_size;
30 limb_index = bit_index / GMP_NUMB_BITS;
33 if (limb_index < dsize)
36 dlimb = dp[limb_index];
37 dlimb &= ~((mp_limb_t) 1 << (bit_index % GMP_NUMB_BITS));
38 dp[limb_index] = dlimb;
40 if (UNLIKELY (dlimb == 0 && limb_index == dsize-1))
42 /* high limb became zero, must normalize */
45 } while (dsize > 0 && dp[dsize-1] == 0);
56 /* Simulate two's complement arithmetic, i.e. simulate
57 1. Set OP = ~(OP - 1) [with infinitely many leading ones].
59 3. Set OP = ~OP + 1. */
63 /* No upper bound on this loop, we're sure there's a non-zero limb
65 for (zero_bound = 0; ; zero_bound++)
66 if (dp[zero_bound] != 0)
69 if (limb_index > zero_bound)
71 if (limb_index < dsize)
72 dp[limb_index] |= (mp_limb_t) 1 << (bit_index % GMP_NUMB_BITS);
75 /* Ugh. The bit should be cleared outside of the end of the
76 number. We have to increase the size of the number. */
77 if (UNLIKELY (d->_mp_alloc < limb_index + 1))
78 dp = _mpz_realloc (d, limb_index + 1);
80 MPN_ZERO (dp + dsize, limb_index - dsize);
81 dp[limb_index] = (mp_limb_t) 1 << (bit_index % GMP_NUMB_BITS);
82 d->_mp_size = -(limb_index + 1);
85 else if (limb_index == zero_bound)
87 dp[limb_index] = ((((dp[limb_index] - 1)
88 | ((mp_limb_t) 1 << (bit_index % GMP_NUMB_BITS))) + 1)
90 if (dp[limb_index] == 0)
93 for (i = limb_index + 1; i < dsize; i++)
95 dp[i] = (dp[i] + 1) & GMP_NUMB_MASK;
99 /* We got carry all way out beyond the end of D. Increase
100 its size (and allocation if necessary). */
102 if (UNLIKELY (d->_mp_alloc < dsize))
103 dp = _mpz_realloc (d, dsize);
106 d->_mp_size = -dsize;