beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / combit.c
blobb6d1daa09eedfe471fa89cd53892e6beb0fd95e9
1 /* mpz_combit -- complement a specified bit.
3 Copyright 2002, 2003, 2012, 2015 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 either:
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.
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.
20 or both in parallel, as here.
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.
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/. */
31 #include "gmp.h"
32 #include "gmp-impl.h"
34 void
35 mpz_combit (mpz_ptr d, mp_bitcnt_t bit_index)
37 mp_size_t dsize = SIZ(d);
38 mp_ptr dp = PTR(d);
40 mp_size_t limb_index = bit_index / GMP_NUMB_BITS;
41 mp_limb_t bit = (CNST_LIMB (1) << (bit_index % GMP_NUMB_BITS));
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;
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)
54 ASSERT (dsize < 0);
55 dsize = -dsize;
57 if (dp[limb_index] & bit)
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];
67 else
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;
77 else
79 /* Simple case: Toggle the bit in the absolute value. */
80 dsize = ABS(dsize);
81 if (limb_index < dsize)
83 mp_limb_t dlimb;
84 dlimb = dp[limb_index] ^ bit;
85 dp[limb_index] = dlimb;
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 */
91 /* high limb became zero, must normalize */
92 MPN_NORMALIZE (dp, limb_index);
93 SIZ (d) = SIZ (d) >= 0 ? limb_index : -limb_index;
96 else
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;