[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.2

1.1       maekawa     1: /* mpz_setbit -- set a specified bit.
                      2:
1.1.1.2 ! maekawa     3: Copyright (C) 1991, 1993, 1994, 1995, 1997, 1999 Free Software Foundation,
        !             4: 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: #if __STDC__
                     28: mpz_setbit (mpz_ptr d, unsigned long int bit_index)
                     29: #else
                     30: mpz_setbit (d, bit_index)
                     31:      mpz_ptr d;
                     32:      unsigned long int bit_index;
                     33: #endif
                     34: {
                     35:   mp_size_t dsize = d->_mp_size;
                     36:   mp_ptr dp = d->_mp_d;
                     37:   mp_size_t limb_index;
                     38:
                     39:   limb_index = bit_index / BITS_PER_MP_LIMB;
                     40:   if (dsize >= 0)
                     41:     {
                     42:       if (limb_index < dsize)
                     43:        {
                     44:          dp[limb_index] |= (mp_limb_t) 1 << (bit_index % BITS_PER_MP_LIMB);
                     45:          d->_mp_size = dsize;
                     46:        }
                     47:       else
                     48:        {
                     49:          /* Ugh.  The bit should be set outside of the end of the
                     50:             number.  We have to increase the size of the number.  */
                     51:          if (d->_mp_alloc < limb_index + 1)
                     52:            {
                     53:              _mpz_realloc (d, limb_index + 1);
                     54:              dp = d->_mp_d;
                     55:            }
                     56:          MPN_ZERO (dp + dsize, limb_index - dsize);
                     57:          dp[limb_index] = (mp_limb_t) 1 << (bit_index % BITS_PER_MP_LIMB);
                     58:          d->_mp_size = limb_index + 1;
                     59:        }
                     60:     }
                     61:   else
                     62:     {
                     63:       mp_size_t zero_bound;
                     64:
                     65:       /* Simulate two's complement arithmetic, i.e. simulate
                     66:         1. Set OP = ~(OP - 1) [with infinitely many leading ones].
1.1.1.2 ! maekawa    67:         2. Set the bit.
1.1       maekawa    68:         3. Set OP = ~OP + 1.  */
                     69:
                     70:       dsize = -dsize;
                     71:
                     72:       /* No upper bound on this loop, we're sure there's a non-zero limb
                     73:         sooner ot later.  */
                     74:       for (zero_bound = 0; ; zero_bound++)
                     75:        if (dp[zero_bound] != 0)
                     76:          break;
                     77:
                     78:       if (limb_index > zero_bound)
                     79:        {
                     80:          if (limb_index < dsize)
                     81:            dp[limb_index] &= ~((mp_limb_t) 1 << (bit_index % BITS_PER_MP_LIMB));
                     82:          else
                     83:            ;
                     84:        }
                     85:       else if (limb_index == zero_bound)
                     86:        {
                     87:          dp[limb_index] = ((dp[limb_index] - 1)
                     88:                            & ~((mp_limb_t) 1 << (bit_index % BITS_PER_MP_LIMB))) + 1;
                     89:          if (dp[limb_index] == 0)
                     90:            {
                     91:              mp_size_t i;
                     92:              for (i = limb_index + 1; i < dsize; i++)
                     93:                {
                     94:                  dp[i] += 1;
                     95:                  if (dp[i] != 0)
                     96:                    goto fin;
                     97:                }
                     98:              /* We got carry all way out beyond the end of D.  Increase
                     99:                 its size (and allocation if necessary).  */
                    100:              dsize++;
                    101:              if (d->_mp_alloc < dsize)
                    102:                {
                    103:                  _mpz_realloc (d, dsize);
                    104:                  dp = d->_mp_d;
                    105:                }
                    106:              dp[i] = 1;
                    107:              d->_mp_size = -dsize;
                    108:            fin:;
                    109:            }
                    110:        }
                    111:       else
1.1.1.2 ! maekawa   112:        {
        !           113:          mpn_decr_u (dp + limb_index,
        !           114:                     (mp_limb_t) 1 << (bit_index % BITS_PER_MP_LIMB));
        !           115:          dsize -= dp[dsize - 1] == 0;
        !           116:          d->_mp_size = -dsize;
        !           117:        }
1.1       maekawa   118:     }
                    119: }

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