[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / runtime / gmp / mpz_clrbit.c
1 /* mpz_clrbit -- clear a specified bit.
2
3 Copyright (C) 1991 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 the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 The GNU MP Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with the GNU MP Library; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include "gmp.h"
22 #include "gmp-impl.h"
23
24 #define MPN_NORMALIZE(p, size) \
25   do {                                                                  \
26     mp_size i;                                                          \
27     for (i = (size) - 1; i >= 0; i--)                                   \
28       if ((p)[i] != 0)                                                  \
29         break;                                                          \
30     (size) = i + 1;                                                     \
31   } while (0)
32
33 void
34 #ifdef __STDC__
35 mpz_clrbit (MP_INT *d, unsigned long int bit_index)
36 #else
37 mpz_clrbit (d, bit_index)
38      MP_INT *d;
39      unsigned long int bit_index;
40 #endif
41 {
42   mp_size dsize = d->size;
43   mp_ptr dp = d->d;
44   mp_size limb_index;
45
46   limb_index = bit_index / BITS_PER_MP_LIMB;
47   if (dsize >= 0)
48     {
49       if (limb_index < dsize)
50         {
51           dp[limb_index] &= ~((mp_limb) 1 << (bit_index % BITS_PER_MP_LIMB));
52           MPN_NORMALIZE (dp, dsize);
53           d->size = dsize;
54         }
55       else
56         ;
57     }
58   else
59     {
60       mp_size zero_bound;
61
62       /* Simulate two's complement arithmetic, i.e. simulate
63          1. Set OP = ~(OP - 1) [with infinitely many leading ones].
64          2. clear the bit.
65          3. Set OP = ~OP + 1.  */
66
67       dsize = -dsize;
68
69       /* No upper bound on this loop, we're sure there's a non-zero limb
70          sooner ot later.  */
71       for (zero_bound = 0; ; zero_bound++)
72         if (dp[zero_bound] != 0)
73           break;
74
75       if (limb_index > zero_bound)
76         {
77           if (limb_index < dsize)
78             {
79               dp[limb_index] |= ((mp_limb) 1 << (bit_index % BITS_PER_MP_LIMB));
80             }
81           else
82             {
83               /* Ugh.  The bit should be cleared outside of the end of the
84                  number.  We have to increase the size of the number.  */
85               if (d->alloc < limb_index + 1)
86                 {
87                   _mpz_realloc (d, limb_index + 1);
88                   dp = d->d;
89                 }
90               MPN_ZERO (dp + dsize, limb_index - dsize);
91               dp[limb_index] = ((mp_limb) 1 << (bit_index % BITS_PER_MP_LIMB));
92               d->size = -(limb_index + 1);
93             }
94         }
95       else if (limb_index == zero_bound)
96         {
97           dp[limb_index] = ((dp[limb_index] - 1)
98                             | ((mp_limb) 1 << (bit_index % BITS_PER_MP_LIMB))) + 1;
99           if (dp[limb_index] == 0)
100             {
101               mp_size i;
102               for (i = limb_index + 1; i < dsize; i++)
103                 {
104                   dp[i] += 1;
105                   if (dp[i] != 0)
106                     goto fin;
107                 }
108               /* We got carry all way out beyond the end of D.  Increase
109                  its size (and allocation if necessary).  */
110               dsize++;
111               if (d->alloc < dsize)
112                 {
113                   _mpz_realloc (d, dsize);
114                   dp = d->d;
115                 }
116               dp[i] = 1;
117               d->size = -dsize;
118             fin:;
119             }
120         }
121       else
122         ;
123     }
124 }