1
/* Test mpz_pow_ui and mpz_ui_pow_ui.
3
Copyright 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
5
This file is part of the GNU MP Library.
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
12
The GNU MP Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32
check_one (mpz_srcptr want, mpz_srcptr base, unsigned long exp)
38
mpz_pow_ui (got, base, exp);
39
if (mpz_cmp (got, want))
41
printf ("mpz_pow_ui wrong\n");
42
mpz_trace (" base", base);
43
printf (" exp = %lu (0x%lX)\n", exp, exp);
44
mpz_trace (" got ", got);
45
mpz_trace (" want", want);
50
mpz_pow_ui (got, got, exp);
51
if (mpz_cmp (got, want))
53
printf ("mpz_pow_ui wrong\n");
54
mpz_trace (" base", base);
55
printf (" exp = %lu (0x%lX)\n", exp, exp);
56
mpz_trace (" got ", got);
57
mpz_trace (" want", want);
61
if (mpz_fits_ulong_p (base))
63
unsigned long base_u = mpz_get_ui (base);
64
mpz_ui_pow_ui (got, base_u, exp);
65
if (mpz_cmp (got, want))
67
printf ("mpz_ui_pow_ui wrong\n");
68
printf (" base=%lu (0x%lX)\n", base_u, base_u);
69
printf (" exp = %lu (0x%lX)\n", exp, exp);
70
mpz_trace (" got ", got);
71
mpz_trace (" want", want);
80
check_base (mpz_srcptr base)
86
mpz_set_ui (want, 1L);
88
for (exp = 0; exp < 20; exp++)
90
check_one (want, base, exp);
91
mpz_mul (want, want, base);
100
static const struct {
119
{ "0x1000000000000001" },
121
/* actual size closest to estimate */
123
{ "0xFFFFFFFFFFFFFFFF" },
125
/* same after rshift */
128
{ "0xFFFFFFFFFFFFFFFF0" },
129
{ "0xFFFFFFFFFFFFFFFF00" },
131
/* change from 2 limbs to 1 after rshift */
133
{ "0x18000000000000000" },
135
/* change from 3 limbs to 2 after rshift */
136
{ "0x18000000100000000" },
137
{ "0x180000000000000010000000000000000" },
139
/* handling of absolute value */
141
{ "-0x8000000000000000" },
143
/* low zero limb, and size>2, checking argument overlap detection */
144
{ "0x3000000000000000300000000000000030000000000000000" },
152
for (i = 0; i < numberof (data); i++)
154
mpz_set_str_or_abort (base, data[i].base, 0);
162
check_random (int reps)
167
unsigned long size_range, exp;
168
gmp_randstate_ptr rands = RANDS;
173
for (i = 0; i < reps; i++)
175
/* exponentially random 0 to 2^13 bits for base */
176
mpz_urandomb (want, rands, 32);
177
size_range = mpz_get_ui (want) % 12 + 2;
178
mpz_urandomb (want, rands, size_range);
179
base_size = mpz_get_ui (want);
180
mpz_rrandomb (base, rands, base_size);
182
/* randomly signed base */
183
mpz_urandomb (want, rands, 2);
184
if ((mpz_get_ui (want) & 1) != 0)
185
mpz_neg (base, base);
187
/* random 5 bits for exponent */
188
mpz_urandomb (want, rands, 5L);
189
exp = mpz_get_ui (want);
191
refmpz_pow_ui (want, base, exp);
192
check_one (want, base, exp);
200
main (int argc, char **argv)
204
/* dummy call to drag in refmpn.o for testing mpz/n_pow_ui.c with
206
refmpn_zero_p (NULL, (mp_size_t) 0);
212
reps = atoi (argv[1]);