File: mpz_misc.C

package info (click to toggle)
sfs 1%3A0.8-0%2Bpre20060720.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,668 kB
  • ctags: 14,317
  • sloc: cpp: 78,358; ansic: 15,494; sh: 9,540; yacc: 786; makefile: 706; perl: 676; lex: 553; python: 146; sed: 70
file content (193 lines) | stat: -rw-r--r-- 4,226 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* $Id: mpz_misc.C,v 1.7 2005/09/28 18:37:50 dm Exp $ */

/*
 *
 * Copyright (C) 1998 David Mazieres (dm@uun.org)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#include "bigint.h"
#include "msb.h"

#undef min
#define min(a, b) (((a) < (b)) ? (a) : (b))

size_t
(mpz_sizeinbase2) (const MP_INT *mp)
{
  size_t i;
  for (i = ABS (mp->_mp_size); i-- > 0;) {
    mp_limb_t v;
    if ((v = mp->_mp_d[i]))
      return 8 * sizeof (mp_limb_t) * i + fls (v);
  }
  return 0;
}

void
_mpz_fixsize (MP_INT *r)
{
  mp_limb_t *sp = r->_mp_d;
  mp_limb_t *ep = sp + ABS (r->_mp_size);
  while (ep > sp && !ep[-1])
    ep--;
  r->_mp_size = r->_mp_size < 0 ? sp - ep : ep - sp;
  _mpz_assert (r);
}

void
mpz_umod_2exp (MP_INT *r, const MP_INT *a, u_long b)
{
  size_t nlimbs;
  const mp_limb_t *ap, *ae;
  mp_limb_t *rp, *re;

  if (a->_mp_size >= 0) {
    mpz_tdiv_r_2exp (r, a, b);
    return;
  }

  nlimbs = ((b + (8 * sizeof (mp_limb_t) - 1))
	    / (8 * sizeof (mp_limb_t)));
  if ((size_t) r->_mp_alloc < nlimbs)
    _mpz_realloc (r, nlimbs);
  ap = a->_mp_d;
  ae = ap + min ((size_t) ABS (a->_mp_size), nlimbs);
  rp = r->_mp_d;

  while (ap < ae)
    if ((*rp++ = -*ap++))
      goto nocarry;
  r->_mp_size = 0;
  return;

 nocarry:
  while (ap < ae)
    *rp++ = ~*ap++;
  re = r->_mp_d + nlimbs;
  while (rp < re)
    *rp++ = ~(mp_limb_t) 0;
  re[-1] &= ~(mp_limb_t) 0 >> ((8*sizeof (mp_limb_t) - b)
			       % (8*sizeof (mp_limb_t)));
  r->_mp_size = nlimbs;
  _mpz_fixsize (r);
}

int
mpz_getbit (const MP_INT *mp, size_t bit)
{
  long limb = bit / mpz_bitsperlimb;
  long nlimbs = mp->_mp_size;
  if (mp->_mp_size >= 0) {
    if (limb >= nlimbs)
      return 0;
    return mp->_mp_d[limb] >> bit % mpz_bitsperlimb & 1;
  }
  else {
    int carry;
    mp_limb_t *p, *e;

    nlimbs = -nlimbs;
    if (limb >= nlimbs)
      return 1;

    carry = 1;
    p = mp->_mp_d;
    e = p + limb;
    for (; p < e; p++)
      if (*p) {
	carry = 0;
	break;
      }
    return (~*e + carry) >> bit % mpz_bitsperlimb & 1;
  }
}

#if SIZEOF_LONG < 8
void
mpz_set_u64 (MP_INT *mp, u_int64_t val)
{
  if (mp->_mp_alloc * sizeof (mp_limb_t) < 8)
    _mpz_realloc (mp, (8 + sizeof (mp_limb_t) - 1) / sizeof (mp_limb_t));
  u_int i = 0;
  while (val) {
    mp->_mp_d[i++] = val;
    val >>= 8 * sizeof (mp_limb_t);
  }
  mp->_mp_size = i;
}

void
mpz_set_s64 (MP_INT *mp, int64_t val)
{
  if (val < 0) {
    mpz_set_u64 (mp, -val);
    mp->_mp_size = -mp->_mp_size;
  }
  else
    mpz_set_u64 (mp, val);
}

u_int64_t
mpz_get_u64 (const MP_INT *mp)
{
  int i = ABS (mp->_mp_size);
  if (!i)
    return 0;
#if GMP_LIMB_SIZE >= 8
  u_int64_t ret = mp->_mp_d[0];
#else /* GMP_LIMB_SIZE < 8 */
  u_int64_t ret = mp->_mp_d[--i];
  while (i > 0)
    ret = (ret << 8 * sizeof (mp_limb_t)) | mp->_mp_d[--i];
#endif /* GMP_LIMB_SIZE < 8 */
  if (mp->_mp_size > 0)
    return ret;
  return ~(ret - 1);
}

int64_t
mpz_get_s64 (const MP_INT *mp)
{
  int i = ABS (mp->_mp_size);
  if (!i)
    return 0;
#if GMP_LIMB_SIZE >= 8
  int64_t ret = mp->_mp_d[0];
#else /* GMP_LIMB_SIZE < 8 */
  int64_t ret = mp->_mp_d[--i];
  while (i > 0)
    ret = (ret << 8 * sizeof (mp_limb_t)) | mp->_mp_d[--i];
  if (mp->_mp_size > 0)
    return ret;
#endif /* GMP_LIMB_SIZE < 8 */
  return ~(ret - 1);
}
#endif /* SIZEOF_LONG < 8 */

/* To be called from the debugger */
extern "C" void mpz_dump (const MP_INT *);
void
mpz_dump (const MP_INT *mp)
{
  char *str = (char *) xmalloc (mpz_sizeinbase (mp, 16) + 3);
  mpz_get_str (str, 16, mp);
  strcat (str, "\n");
  write (2, str, strlen (str));
  xfree (str);
}