xref: /netbsd-src/external/lgpl3/gmp/dist/mpz/clrbit.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* mpz_clrbit -- clear a specified bit.
2 
3 Copyright 1991, 1993-1995, 2001, 2002, 2012 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-impl.h"
32 
33 void
mpz_clrbit(mpz_ptr d,mp_bitcnt_t bit_idx)34 mpz_clrbit (mpz_ptr d, mp_bitcnt_t bit_idx)
35 {
36   mp_size_t dsize = SIZ (d);
37   mp_ptr dp = PTR (d);
38   mp_size_t limb_idx;
39   mp_limb_t mask;
40 
41   limb_idx = bit_idx / GMP_NUMB_BITS;
42   mask = CNST_LIMB(1) << (bit_idx % GMP_NUMB_BITS);
43   if (dsize >= 0)
44     {
45       if (limb_idx < dsize)
46 	{
47 	  mp_limb_t  dlimb;
48 	  dlimb = dp[limb_idx] & ~mask;
49 	  dp[limb_idx] = dlimb;
50 
51 	  if (UNLIKELY ((dlimb == 0) + limb_idx == dsize)) /* dsize == limb_idx + 1 */
52 	    {
53 	      /* high limb became zero, must normalize */
54 	      MPN_NORMALIZE (dp, limb_idx);
55 	      SIZ (d) = limb_idx;
56 	    }
57 	}
58       else
59 	;
60     }
61   else
62     {
63       /* Simulate two's complement arithmetic, i.e. simulate
64 	 1. Set OP = ~(OP - 1) [with infinitely many leading ones].
65 	 2. clear the bit.
66 	 3. Set OP = ~OP + 1.  */
67 
68       dsize = -dsize;
69 
70       if (limb_idx < dsize)
71 	{
72 	  mp_size_t zero_bound;
73 
74 	  /* No index upper bound on this loop, we're sure there's a non-zero limb
75 	     sooner or later.  */
76 	  zero_bound = 0;
77 	  while (dp[zero_bound] == 0)
78 	    zero_bound++;
79 
80 	  if (limb_idx > zero_bound)
81 	    {
82 	      dp[limb_idx] |= mask;
83 	    }
84 	  else if (limb_idx == zero_bound)
85 	    {
86 	      mp_limb_t  dlimb;
87 	      dlimb = (((dp[limb_idx] - 1) | mask) + 1) & GMP_NUMB_MASK;
88 	      dp[limb_idx] = dlimb;
89 
90 	      if (dlimb == 0)
91 		{
92 		  /* Increment at limb_idx + 1.  Extend the number with a zero limb
93 		     for simplicity.  */
94 		  dp = MPZ_REALLOC (d, dsize + 1);
95 		  dp[dsize] = 0;
96 		  MPN_INCR_U (dp + limb_idx + 1, dsize - limb_idx, 1);
97 		  dsize += dp[dsize];
98 
99 		  SIZ (d) = -dsize;
100 		}
101 	    }
102 	  else
103 	    ;
104 	}
105       else
106 	{
107 	  /* Ugh.  The bit should be cleared outside of the end of the
108 	     number.  We have to increase the size of the number.  */
109 	  dp = MPZ_REALLOC (d, limb_idx + 1);
110 	  SIZ (d) = -(limb_idx + 1);
111 	  MPN_ZERO (dp + dsize, limb_idx - dsize);
112 	  dp[limb_idx] = mask;
113 	}
114     }
115 }
116