{
mpz_t *p;
unsigned long int *e;
- unsigned long int nfactors;
+ idx_t nfactors;
};
static void factor (uintmax_t, uintmax_t, struct factors *);
__r1 = (n1); __r0 = (n0); \
affirm (__r1 < __d1); \
__q = 0; \
- for (unsigned int __i = W_TYPE_SIZE; __i > 0; __i--) \
+ for (int __i = W_TYPE_SIZE; __i > 0; __i--) \
{ \
rsh2 (__d1, __d0, __d1, __d0, 1); \
__q <<= 1; \
#ifndef count_leading_zeros
# define count_leading_zeros(count, x) do { \
uintmax_t __clz_x = (x); \
- unsigned int __clz_c; \
+ int __clz_c; \
for (__clz_c = 0; \
(__clz_x & ((uintmax_t) 0xff << (W_TYPE_SIZE - 8))) == 0; \
__clz_c += 8) \
#ifndef count_trailing_zeros
# define count_trailing_zeros(count, x) do { \
uintmax_t __ctz_x = (x); \
- unsigned int __ctz_c = 0; \
+ int __ctz_c = 0; \
while ((__ctz_x & 1) == 0) \
{ \
__ctz_x >>= 1; \
static void
factor_insert_multiplicity (struct factors *factors,
- uintmax_t prime, unsigned int m)
+ uintmax_t prime, int m)
{
- unsigned int nfactors = factors->nfactors;
+ int nfactors = factors->nfactors;
uintmax_t *p = factors->p;
unsigned char *e = factors->e;
static void
mp_factor_clear (struct mp_factors *factors)
{
- for (unsigned int i = 0; i < factors->nfactors; i++)
+ for (idx_t i = 0; i < factors->nfactors; i++)
mpz_clear (factors->p[i]);
free (factors->p);
static void
mp_factor_insert (struct mp_factors *factors, mpz_t prime)
{
- unsigned long int nfactors = factors->nfactors;
- mpz_t *p = factors->p;
- unsigned long int *e = factors->e;
- long i;
+ idx_t nfactors = factors->nfactors;
+ mpz_t *p = factors->p;
+ unsigned long int *e = factors->e;
+ ptrdiff_t i;
/* Locate position for insert new or increment e. */
for (i = nfactors - 1; i >= 0; i--)
if (i < 0 || mpz_cmp (p[i], prime) != 0)
{
- p = xrealloc (p, (nfactors + 1) * sizeof p[0]);
- e = xrealloc (e, (nfactors + 1) * sizeof e[0]);
+ p = xireallocarray (p, nfactors + 1, sizeof p[0]);
+ e = xireallocarray (e, nfactors + 1, sizeof e[0]);
mpz_init (p[nfactors]);
for (long j = nfactors - 1; j > i; j--)
#define MR_REPS 25
static void
-factor_insert_refind (struct factors *factors, uintmax_t p, unsigned int i,
- unsigned int off)
+factor_insert_refind (struct factors *factors, uintmax_t p, int i, int off)
{
- for (unsigned int j = 0; j < off; j++)
+ for (int j = 0; j < off; j++)
p += primes_diff[i + j];
factor_insert (factors, p);
}
{
if (t0 % 2 == 0)
{
- unsigned int cnt;
+ int cnt;
if (t0 == 0)
{
}
uintmax_t p = 3;
- unsigned int i;
+ idx_t i;
for (i = 0; t1 > 0 && i < PRIMES_PTAB_ENTRIES; i++)
{
for (;;)
mp_factor_using_division (mpz_t t, struct mp_factors *factors)
{
mpz_t q;
- unsigned long int p;
+ mp_bitcnt_t p;
devmsg ("[trial division] ");
--p;
}
- p = 3;
- for (unsigned int i = 1; i <= PRIMES_PTAB_ENTRIES;)
+ unsigned long int d = 3;
+ for (idx_t i = 1; i <= PRIMES_PTAB_ENTRIES;)
{
- if (! mpz_divisible_ui_p (t, p))
+ if (! mpz_divisible_ui_p (t, d))
{
- p += primes_diff[i++];
- if (mpz_cmp_ui (t, p * p) < 0)
+ d += primes_diff[i++];
+ if (mpz_cmp_ui (t, d * d) < 0)
break;
}
else
{
- mpz_tdiv_q_ui (t, t, p);
- mp_factor_insert_ui (factors, p);
+ mpz_tdiv_q_ui (t, t, d);
+ mp_factor_insert_ui (factors, d);
}
}
uintmax_t ni, const uintmax_t *one)
{
uintmax_t r1, r0, b1, b0, n1, n0;
- unsigned int i;
+ int i;
uintmax_t e;
b0 = bp[0];
ATTRIBUTE_CONST
static bool
millerrabin (uintmax_t n, uintmax_t ni, uintmax_t b, uintmax_t q,
- unsigned int k, uintmax_t one)
+ int k, uintmax_t one)
{
uintmax_t y = powm (b, q, n, ni, one);
if (y == one || y == nm1)
return true;
- for (unsigned int i = 1; i < k; i++)
+ for (int i = 1; i < k; i++)
{
y = mulredc (y, y, n, ni);
ATTRIBUTE_PURE static bool
millerrabin2 (const uintmax_t *np, uintmax_t ni, const uintmax_t *bp,
- const uintmax_t *qp, unsigned int k, const uintmax_t *one)
+ const uintmax_t *qp, int k, const uintmax_t *one)
{
uintmax_t y1, y0, nm1_1, nm1_0, r1m;
if (y0 == nm1_0 && y1 == nm1_1)
return true;
- for (unsigned int i = 1; i < k; i++)
+ for (int i = 1; i < k; i++)
{
y0 = mulredc2 (&r1m, y1, y0, y1, y0, np[1], np[0], ni);
y1 = r1m;
static bool
mp_millerrabin (mpz_srcptr n, mpz_srcptr nm1, mpz_ptr x, mpz_ptr y,
- mpz_srcptr q, unsigned long int k)
+ mpz_srcptr q, mp_bitcnt_t k)
{
mpz_powm (y, x, q, n);
if (mpz_cmp_ui (y, 1) == 0 || mpz_cmp (y, nm1) == 0)
return true;
- for (unsigned long int i = 1; i < k; i++)
+ for (mp_bitcnt_t i = 1; i < k; i++)
{
mpz_powm_ui (y, y, 2, n);
if (mpz_cmp (y, nm1) == 0)
static bool ATTRIBUTE_PURE
prime_p (uintmax_t n)
{
- int k;
+ mp_bitcnt_t k;
bool is_prime;
uintmax_t a_prim, one, ni;
struct factors factors;
/* Loop until Lucas proves our number prime, or Miller-Rabin proves our
number composite. */
- for (unsigned int r = 0; r < PRIMES_PTAB_ENTRIES; r++)
+ for (idx_t r = 0; r < PRIMES_PTAB_ENTRIES; r++)
{
if (flag_prove_primality)
{
is_prime = true;
- for (unsigned int i = 0; i < factors.nfactors && is_prime; i++)
+ for (int i = 0; i < factors.nfactors && is_prime; i++)
{
is_prime
= powm (a_prim, (n - 1) / factors.p[i], n, ni, one) != one;
uintmax_t one[2];
uintmax_t na[2];
uintmax_t ni;
- unsigned int k;
+ int k;
struct factors factors;
if (n1 == 0)
/* Loop until Lucas proves our number prime, or Miller-Rabin proves our
number composite. */
- for (unsigned int r = 0; r < PRIMES_PTAB_ENTRIES; r++)
+ for (idx_t r = 0; r < PRIMES_PTAB_ENTRIES; r++)
{
bool is_prime;
uintmax_t e[2], y[2];
y[0] = powm2 (&y[1], a_prim, e, na, ni, one);
is_prime = (y[0] != one[0] || y[1] != one[1]);
}
- for (unsigned int i = 0; i < factors.nfactors && is_prime; i++)
+ for (int i = 0; i < factors.nfactors && is_prime; i++)
{
/* FIXME: We always have the factor 2. Do we really need to
handle it here? We have done the same powering as part
mpz_sub_ui (nm1, n, 1);
/* Find q and k, where q is odd and n = 1 + 2**k * q. */
- unsigned long int k = mpz_scan1 (nm1, 0);
+ mp_bitcnt_t k = mpz_scan1 (nm1, 0);
mpz_tdiv_q_2exp (q, nm1, k);
mpz_set_ui (a, 2);
/* Loop until Lucas proves our number prime, or Miller-Rabin proves our
number composite. */
- for (unsigned int r = 0; r < PRIMES_PTAB_ENTRIES; r++)
+ for (idx_t r = 0; r < PRIMES_PTAB_ENTRIES; r++)
{
if (flag_prove_primality)
{
is_prime = true;
- for (unsigned long int i = 0; i < factors.nfactors && is_prime; i++)
+ for (idx_t i = 0; i < factors.nfactors && is_prime; i++)
{
mpz_divexact (tmp, nm1, factors.p[i]);
mpz_powm (tmp, a, tmp, n);
isqrt (uintmax_t n)
{
uintmax_t x;
- unsigned c;
+ int c;
if (n == 0)
return 0;
count_leading_zeros (c, n);
/* Make x > sqrt(n). This will be invariant through the loop. */
- x = (uintmax_t) 1 << ((W_TYPE_SIZE + 1 - c) / 2);
+ x = (uintmax_t) 1 << ((W_TYPE_SIZE + 1 - c) >> 1);
for (;;)
{
static uintmax_t
isqrt2 (uintmax_t nh, uintmax_t nl)
{
- unsigned int shift;
+ int shift;
uintmax_t x;
/* Ensures the remainder fits in an uintmax_t. */
/* Make x > sqrt (n). */
x = isqrt ((nh << shift) + (nl >> (W_TYPE_SIZE - shift))) + 1;
- x <<= (W_TYPE_SIZE - shift) / 2;
+ x <<= (W_TYPE_SIZE - shift) >> 1;
/* Do we need more than one iteration? */
for (;;)
}
/* invtab[i] = floor (0x10000 / (0x100 + i) */
-static const unsigned short invtab[0x81] =
+static short const invtab[0x81] =
{
0x200,
0x1fc, 0x1f8, 0x1f4, 0x1f0, 0x1ec, 0x1e9, 0x1e5, 0x1e1,
#if STAT_SQUFOF
# define Q_FREQ_SIZE 50
/* Element 0 keeps the total */
-static unsigned int q_freq[Q_FREQ_SIZE + 1];
+static int q_freq[Q_FREQ_SIZE + 1];
#endif
#if USE_SQUFOF
https://homes.cerias.purdue.edu/~ssw/squfof.pdf
*/
- static const unsigned int multipliers_1[] =
+ static short const multipliers_1[] =
{ /* = 1 (mod 4) */
105, 165, 21, 385, 33, 5, 77, 1, 0
};
- static const unsigned int multipliers_3[] =
+ static short const multipliers_3[] =
{ /* = 3 (mod 4) */
1155, 15, 231, 35, 3, 55, 7, 11, 0
};
- const unsigned int *m;
-
struct { uintmax_t Q; uintmax_t P; } queue[QUEUE_SIZE];
if (n1 >= ((uintmax_t) 1 << (W_TYPE_SIZE - 2)))
}
/* Select multipliers so we always get n * mu = 3 (mod 4) */
- for (m = (n0 % 4 == 1) ? multipliers_3 : multipliers_1;
+ for (short const *m = (n0 % 4 == 1) ? multipliers_3 : multipliers_1;
*m; m++)
{
uintmax_t S, Dh, Dl, Q1, Q, P, L, L1, B;
unsigned int i;
unsigned int mu = *m;
- unsigned int qpos = 0;
+ int qpos = 0;
affirm (mu * n0 % 4 == 3);
uintmax_t r = is_square (Q);
if (r)
{
- for (unsigned int j = 0; j < qpos; j++)
+ for (int j = 0; j < qpos; j++)
{
if (queue[j].Q == r)
{
static strtol_error
strto2uintmax (uintmax_t *hip, uintmax_t *lop, char const *s)
{
- unsigned int lo_carry;
+ int lo_carry;
uintmax_t hi = 0, lo = 0;
strtol_error err = LONGINT_INVALID;
char const *p = s;
for (;;)
{
- unsigned int c = *p++;
+ unsigned char c = *p++;
if (c == 0)
break;
while (err == LONGINT_OK)
{
- unsigned int c = *s++;
+ unsigned char c = *s++;
if (c == 0)
break;
factor (t1, t0, &factors);
- for (unsigned int j = 0; j < factors.nfactors; j++)
- for (unsigned int k = 0; k < factors.e[j]; k++)
+ for (int j = 0; j < factors.nfactors; j++)
+ for (int k = 0; k < factors.e[j]; k++)
{
lbuf_putc (' ');
print_uintmaxes (0, factors.p[j]);
putchar (':');
mp_factor (t, &factors);
- for (unsigned int j = 0; j < factors.nfactors; j++)
- for (unsigned int k = 0; k < factors.e[j]; k++)
+ for (idx_t j = 0; j < factors.nfactors; j++)
+ for (unsigned long int k = 0; k < factors.e[j]; k++)
{
putchar (' ');
mpz_out_str (stdout, 10, factors.p[j]);
{
double acc_f;
printf ("q freq. cum. freq.(total: %d)\n", q_freq[0]);
- for (unsigned int i = 1, acc_f = 0.0; i <= Q_FREQ_SIZE; i++)
+ for (int i = 1, acc_f = 0.0; i <= Q_FREQ_SIZE; i++)
{
double f = (double) q_freq[i] / q_freq[0];
acc_f += f;