Blame mpz/setbit.c

Packit 5c3484
/* mpz_setbit -- set a specified bit.
Packit 5c3484
Packit 5c3484
Copyright 1991, 1993-1995, 1997, 1999, 2001, 2002, 2012 Free Software
Packit 5c3484
Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpz_setbit (mpz_ptr d, mp_bitcnt_t bit_idx)
Packit 5c3484
{
Packit 5c3484
  mp_size_t dsize = SIZ (d);
Packit 5c3484
  mp_ptr dp = PTR (d);
Packit 5c3484
  mp_size_t limb_idx;
Packit 5c3484
  mp_limb_t mask;
Packit 5c3484
Packit 5c3484
  limb_idx = bit_idx / GMP_NUMB_BITS;
Packit 5c3484
  mask = CNST_LIMB(1) << (bit_idx % GMP_NUMB_BITS);
Packit 5c3484
  if (dsize >= 0)
Packit 5c3484
    {
Packit 5c3484
      if (limb_idx < dsize)
Packit 5c3484
	{
Packit 5c3484
	  dp[limb_idx] |= mask;
Packit 5c3484
	}
Packit 5c3484
      else
Packit 5c3484
	{
Packit 5c3484
	  /* Ugh.  The bit should be set outside of the end of the
Packit 5c3484
	     number.  We have to increase the size of the number.  */
Packit 5c3484
	  dp = MPZ_REALLOC (d, limb_idx + 1);
Packit 5c3484
	  SIZ (d) = limb_idx + 1;
Packit 5c3484
	  MPN_ZERO (dp + dsize, limb_idx - dsize);
Packit 5c3484
	  dp[limb_idx] = mask;
Packit 5c3484
	}
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      /* Simulate two's complement arithmetic, i.e. simulate
Packit 5c3484
	 1. Set OP = ~(OP - 1) [with infinitely many leading ones].
Packit 5c3484
	 2. Set the bit.
Packit 5c3484
	 3. Set OP = ~OP + 1.  */
Packit 5c3484
Packit 5c3484
      dsize = -dsize;
Packit 5c3484
Packit 5c3484
      if (limb_idx < dsize)
Packit 5c3484
	{
Packit 5c3484
	  mp_size_t zero_bound;
Packit 5c3484
	  /* No index upper bound on this loop, we're sure there's a non-zero limb
Packit 5c3484
	     sooner or later.  */
Packit 5c3484
	  zero_bound = 0;
Packit 5c3484
	  while (dp[zero_bound] == 0)
Packit 5c3484
	    zero_bound++;
Packit 5c3484
Packit 5c3484
	  if (limb_idx > zero_bound)
Packit 5c3484
	    {
Packit 5c3484
	      mp_limb_t	 dlimb;
Packit 5c3484
	      dlimb = dp[limb_idx] & ~mask;
Packit 5c3484
	      dp[limb_idx] = dlimb;
Packit 5c3484
Packit 5c3484
	      if (UNLIKELY ((dlimb == 0) + limb_idx == dsize)) /* dsize == limb_idx + 1 */
Packit 5c3484
		{
Packit 5c3484
		  /* high limb became zero, must normalize */
Packit 5c3484
		  MPN_NORMALIZE (dp, limb_idx);
Packit 5c3484
		  SIZ (d) = -limb_idx;
Packit 5c3484
		}
Packit 5c3484
	    }
Packit 5c3484
	  else if (limb_idx == zero_bound)
Packit 5c3484
	    {
Packit 5c3484
	      dp[limb_idx] = ((dp[limb_idx] - 1) & ~mask) + 1;
Packit 5c3484
	      ASSERT (dp[limb_idx] != 0);
Packit 5c3484
	    }
Packit 5c3484
	  else
Packit 5c3484
	    {
Packit 5c3484
	      MPN_DECR_U (dp + limb_idx, dsize - limb_idx, mask);
Packit 5c3484
	      dsize -= dp[dsize - 1] == 0;
Packit 5c3484
	      SIZ (d) = -dsize;
Packit 5c3484
	    }
Packit 5c3484
	}
Packit 5c3484
    }
Packit 5c3484
}