xref: /netbsd-src/external/lgpl3/gmp/dist/mpz/setbit.c (revision 4d342c046e3288fb5a1edcd33cfec48c41c80664)
1 /* mpz_setbit -- set a specified bit.
2 
3 Copyright 1991, 1993-1995, 1997, 1999, 2001, 2002, 2012 Free Software
4 Foundation, Inc.
5 
6 This file is part of the GNU MP Library.
7 
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
10 
11   * the GNU Lesser General Public License as published by the Free
12     Software Foundation; either version 3 of the License, or (at your
13     option) any later version.
14 
15 or
16 
17   * the GNU General Public License as published by the Free Software
18     Foundation; either version 2 of the License, or (at your option) any
19     later version.
20 
21 or both in parallel, as here.
22 
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26 for more details.
27 
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library.  If not,
30 see https://www.gnu.org/licenses/.  */
31 
32 #include "gmp.h"
33 #include "gmp-impl.h"
34 
35 void
36 mpz_setbit (mpz_ptr d, mp_bitcnt_t bit_idx)
37 {
38   mp_size_t dsize = SIZ (d);
39   mp_ptr dp = PTR (d);
40   mp_size_t limb_idx;
41   mp_limb_t mask;
42 
43   limb_idx = bit_idx / GMP_NUMB_BITS;
44   mask = CNST_LIMB(1) << (bit_idx % GMP_NUMB_BITS);
45   if (dsize >= 0)
46     {
47       if (limb_idx < dsize)
48 	{
49 	  dp[limb_idx] |= mask;
50 	}
51       else
52 	{
53 	  /* Ugh.  The bit should be set outside of the end of the
54 	     number.  We have to increase the size of the number.  */
55 	  dp = MPZ_REALLOC (d, limb_idx + 1);
56 	  SIZ (d) = limb_idx + 1;
57 	  MPN_ZERO (dp + dsize, limb_idx - dsize);
58 	  dp[limb_idx] = mask;
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. Set 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 	  /* No index upper bound on this loop, we're sure there's a non-zero limb
74 	     sooner or later.  */
75 	  zero_bound = 0;
76 	  while (dp[zero_bound] == 0)
77 	    zero_bound++;
78 
79 	  if (limb_idx > zero_bound)
80 	    {
81 	      mp_limb_t	 dlimb;
82 	      dlimb = dp[limb_idx] & ~mask;
83 	      dp[limb_idx] = dlimb;
84 
85 	      if (UNLIKELY ((dlimb == 0) + limb_idx == dsize)) /* dsize == limb_idx + 1 */
86 		{
87 		  /* high limb became zero, must normalize */
88 		  MPN_NORMALIZE (dp, limb_idx);
89 		  SIZ (d) = -limb_idx;
90 		}
91 	    }
92 	  else if (limb_idx == zero_bound)
93 	    {
94 	      dp[limb_idx] = ((dp[limb_idx] - 1) & ~mask) + 1;
95 	      ASSERT (dp[limb_idx] != 0);
96 	    }
97 	  else
98 	    {
99 	      MPN_DECR_U (dp + limb_idx, dsize - limb_idx, mask);
100 	      dsize -= dp[dsize - 1] == 0;
101 	      SIZ (d) = -dsize;
102 	    }
103 	}
104     }
105 }
106