xref: /netbsd-src/external/lgpl3/gmp/dist/mpz/combit.c (revision eceb233b9bd0dfebb902ed73b531ae6964fa3f9b)
1 /* mpz_combit -- complement a specified bit.
2 
3 Copyright 2002, 2003, 2012, 2015 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.h"
32 #include "gmp-impl.h"
33 
34 void
35 mpz_combit (mpz_ptr d, mp_bitcnt_t bit_index)
36 {
37   mp_size_t dsize = SIZ(d);
38   mp_ptr dp = PTR(d);
39 
40   mp_size_t limb_index = bit_index / GMP_NUMB_BITS;
41   mp_limb_t bit = (CNST_LIMB (1) << (bit_index % GMP_NUMB_BITS));
42 
43   /* Check for the most common case: Positive input, no realloc or
44      normalization needed. */
45   if (limb_index + 1 < dsize)
46     dp[limb_index] ^= bit;
47 
48   /* Check for the hairy case. d < 0, and we have all zero bits to the
49      right of the bit to toggle. */
50   else if (limb_index < -dsize
51 	   && (limb_index == 0 || mpn_zero_p (dp, limb_index))
52 	   && (dp[limb_index] & (bit - 1)) == 0)
53     {
54       ASSERT (dsize < 0);
55       dsize = -dsize;
56 
57       if (dp[limb_index] & bit)
58 	{
59 	  /* We toggle the least significant one bit. Corresponds to
60 	     an add, with potential carry propagation, on the absolute
61 	     value. */
62 	  dp = MPZ_REALLOC (d, 1 + dsize);
63 	  dp[dsize] = 0;
64 	  MPN_INCR_U (dp + limb_index, 1 + dsize - limb_index, bit);
65 	  SIZ(d) = - dsize - dp[dsize];
66 	}
67       else
68 	{
69 	  /* We toggle a zero bit, subtract from the absolute value. */
70 	  MPN_DECR_U (dp + limb_index, dsize - limb_index, bit);
71 	  /* The absolute value shrinked by at most one bit. */
72 	  dsize -= dp[dsize - 1] == 0;
73 	  ASSERT (dsize > 0 && dp[dsize - 1] != 0);
74 	  SIZ (d) = -dsize;
75 	}
76     }
77   else
78     {
79       /* Simple case: Toggle the bit in the absolute value. */
80       dsize = ABS(dsize);
81       if (limb_index < dsize)
82 	{
83 	  mp_limb_t	 dlimb;
84 	  dlimb = dp[limb_index] ^ bit;
85 	  dp[limb_index] = dlimb;
86 
87 	  /* Can happen only when limb_index = dsize - 1. Avoid SIZ(d)
88 	     bookkeeping in the common case. */
89 	  if (UNLIKELY ((dlimb == 0) + limb_index == dsize)) /* dsize == limb_index + 1 */
90 	    {
91 	      /* high limb became zero, must normalize */
92 	      MPN_NORMALIZE (dp, limb_index);
93 	      SIZ (d) = SIZ (d) >= 0 ? limb_index : -limb_index;
94 	    }
95 	}
96       else
97 	{
98 	  dp = MPZ_REALLOC (d, limb_index + 1);
99 	  MPN_ZERO(dp + dsize, limb_index - dsize);
100 	  dp[limb_index++] = bit;
101 	  SIZ(d) = SIZ(d) >= 0 ? limb_index : -limb_index;
102 	}
103     }
104 }
105