[BACK]Return to setbit.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpz

Annotation of OpenXM_contrib/gmp/mpz/setbit.c, Revision 1.1.1.3

1.1       maekawa     1: /* mpz_setbit -- set a specified bit.
                      2:
1.1.1.3 ! ohara       3: Copyright 1991, 1993, 1994, 1995, 1997, 1999, 2001, 2002 Free Software
        !             4: Foundation, Inc.
1.1       maekawa     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
1.1.1.2   maekawa     9: it under the terms of the GNU Lesser General Public License as published by
                     10: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1       maekawa    11: option) any later version.
                     12:
                     13: The GNU MP Library is distributed in the hope that it will be useful, but
                     14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2   maekawa    15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    16: License for more details.
                     17:
1.1.1.2   maekawa    18: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    19: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     21: MA 02111-1307, USA. */
                     22:
                     23: #include "gmp.h"
                     24: #include "gmp-impl.h"
                     25:
                     26: void
                     27: mpz_setbit (mpz_ptr d, unsigned long int bit_index)
                     28: {
                     29:   mp_size_t dsize = d->_mp_size;
                     30:   mp_ptr dp = d->_mp_d;
                     31:   mp_size_t limb_index;
                     32:
1.1.1.3 ! ohara      33:   limb_index = bit_index / GMP_NUMB_BITS;
1.1       maekawa    34:   if (dsize >= 0)
                     35:     {
                     36:       if (limb_index < dsize)
                     37:        {
1.1.1.3 ! ohara      38:          dp[limb_index] |= (mp_limb_t) 1 << (bit_index % GMP_NUMB_BITS);
1.1       maekawa    39:          d->_mp_size = dsize;
                     40:        }
                     41:       else
                     42:        {
                     43:          /* Ugh.  The bit should be set outside of the end of the
                     44:             number.  We have to increase the size of the number.  */
                     45:          if (d->_mp_alloc < limb_index + 1)
                     46:            {
                     47:              _mpz_realloc (d, limb_index + 1);
                     48:              dp = d->_mp_d;
                     49:            }
                     50:          MPN_ZERO (dp + dsize, limb_index - dsize);
1.1.1.3 ! ohara      51:          dp[limb_index] = (mp_limb_t) 1 << (bit_index % GMP_NUMB_BITS);
1.1       maekawa    52:          d->_mp_size = limb_index + 1;
                     53:        }
                     54:     }
                     55:   else
                     56:     {
                     57:       mp_size_t zero_bound;
                     58:
                     59:       /* Simulate two's complement arithmetic, i.e. simulate
                     60:         1. Set OP = ~(OP - 1) [with infinitely many leading ones].
1.1.1.2   maekawa    61:         2. Set the bit.
1.1       maekawa    62:         3. Set OP = ~OP + 1.  */
                     63:
                     64:       dsize = -dsize;
                     65:
                     66:       /* No upper bound on this loop, we're sure there's a non-zero limb
                     67:         sooner ot later.  */
                     68:       for (zero_bound = 0; ; zero_bound++)
                     69:        if (dp[zero_bound] != 0)
                     70:          break;
                     71:
                     72:       if (limb_index > zero_bound)
                     73:        {
                     74:          if (limb_index < dsize)
1.1.1.3 ! ohara      75:             {
        !            76:               dp[limb_index] &= ~((mp_limb_t) 1 << (bit_index % GMP_NUMB_BITS));
        !            77:               MPN_NORMALIZE (dp, dsize);
        !            78:               d->_mp_size = -dsize;
        !            79:             }
1.1       maekawa    80:        }
                     81:       else if (limb_index == zero_bound)
                     82:        {
                     83:          dp[limb_index] = ((dp[limb_index] - 1)
1.1.1.3 ! ohara      84:                            & ~((mp_limb_t) 1 << (bit_index % GMP_NUMB_BITS))) + 1;
1.1       maekawa    85:          if (dp[limb_index] == 0)
                     86:            {
                     87:              mp_size_t i;
                     88:              for (i = limb_index + 1; i < dsize; i++)
                     89:                {
                     90:                  dp[i] += 1;
                     91:                  if (dp[i] != 0)
                     92:                    goto fin;
                     93:                }
                     94:              /* We got carry all way out beyond the end of D.  Increase
                     95:                 its size (and allocation if necessary).  */
                     96:              dsize++;
                     97:              if (d->_mp_alloc < dsize)
                     98:                {
                     99:                  _mpz_realloc (d, dsize);
                    100:                  dp = d->_mp_d;
                    101:                }
                    102:              dp[i] = 1;
                    103:              d->_mp_size = -dsize;
                    104:            fin:;
                    105:            }
                    106:        }
                    107:       else
1.1.1.2   maekawa   108:        {
                    109:          mpn_decr_u (dp + limb_index,
1.1.1.3 ! ohara     110:                     (mp_limb_t) 1 << (bit_index % GMP_NUMB_BITS));
1.1.1.2   maekawa   111:          dsize -= dp[dsize - 1] == 0;
                    112:          d->_mp_size = -dsize;
                    113:        }
1.1       maekawa   114:     }
                    115: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>