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