1/* mpz_combit -- complement a specified bit.
2
3Copyright 2002, 2003, 2012, 2015 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it 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
14or
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
20or both in parallel, as here.
21
22The GNU MP Library is distributed in the hope that it will be useful, but
23WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25for more details.
26
27You should have received copies of the GNU General Public License and the
28GNU Lesser General Public License along with the GNU MP Library.  If not,
29see https://www.gnu.org/licenses/.  */
30
31#include "gmp-impl.h"
32
33void
34mpz_combit (mpz_ptr d, mp_bitcnt_t bit_index)
35{
36  mp_size_t dsize = SIZ(d);
37  mp_ptr dp = PTR(d);
38
39  mp_size_t limb_index = bit_index / GMP_NUMB_BITS;
40  mp_limb_t bit = (CNST_LIMB (1) << (bit_index % GMP_NUMB_BITS));
41
42  /* Check for the most common case: Positive input, no realloc or
43     normalization needed. */
44  if (limb_index + 1 < dsize)
45    dp[limb_index] ^= bit;
46
47  /* Check for the hairy case. d < 0, and we have all zero bits to the
48     right of the bit to toggle. */
49  else if (limb_index < -dsize
50	   && (limb_index == 0 || mpn_zero_p (dp, limb_index))
51	   && (dp[limb_index] & (bit - 1)) == 0)
52    {
53      ASSERT (dsize < 0);
54      dsize = -dsize;
55
56      if (dp[limb_index] & bit)
57	{
58	  /* We toggle the least significant one bit. Corresponds to
59	     an add, with potential carry propagation, on the absolute
60	     value. */
61	  dp = MPZ_REALLOC (d, 1 + dsize);
62	  dp[dsize] = 0;
63	  MPN_INCR_U (dp + limb_index, 1 + dsize - limb_index, bit);
64	  SIZ(d) = - dsize - dp[dsize];
65	}
66      else
67	{
68	  /* We toggle a zero bit, subtract from the absolute value. */
69	  MPN_DECR_U (dp + limb_index, dsize - limb_index, bit);
70	  /* The absolute value shrinked by at most one bit. */
71	  dsize -= dp[dsize - 1] == 0;
72	  ASSERT (dsize > 0 && dp[dsize - 1] != 0);
73	  SIZ (d) = -dsize;
74	}
75    }
76  else
77    {
78      /* Simple case: Toggle the bit in the absolute value. */
79      dsize = ABS(dsize);
80      if (limb_index < dsize)
81	{
82	  mp_limb_t	 dlimb;
83	  dlimb = dp[limb_index] ^ bit;
84	  dp[limb_index] = dlimb;
85
86	  /* Can happen only when limb_index = dsize - 1. Avoid SIZ(d)
87	     bookkeeping in the common case. */
88	  if (UNLIKELY ((dlimb == 0) + limb_index == dsize)) /* dsize == limb_index + 1 */
89	    {
90	      /* high limb became zero, must normalize */
91	      MPN_NORMALIZE (dp, limb_index);
92	      SIZ (d) = SIZ (d) >= 0 ? limb_index : -limb_index;
93	    }
94	}
95      else
96	{
97	  dp = MPZ_REALLOC (d, limb_index + 1);
98	  MPN_ZERO(dp + dsize, limb_index - dsize);
99	  dp[limb_index++] = bit;
100	  SIZ(d) = SIZ(d) >= 0 ? limb_index : -limb_index;
101	}
102    }
103}
104