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 3 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. If not, see http://www.gnu.org/licenses/. */
29 check_one (mpz_srcptr want, mpz_srcptr base, unsigned long exp)
35 MPZ_CHECK_FORMAT (want);
37 mpz_pow_ui (got, base, exp);
38 if (mpz_cmp (got, want))
40 printf ("mpz_pow_ui wrong\n");
41 mpz_trace (" base", base);
42 printf (" exp = %lu (0x%lX)\n", exp, exp);
43 mpz_trace (" got ", got);
44 mpz_trace (" want", want);
49 mpz_pow_ui (got, got, exp);
50 if (mpz_cmp (got, want))
52 printf ("mpz_pow_ui wrong\n");
53 mpz_trace (" base", base);
54 printf (" exp = %lu (0x%lX)\n", exp, exp);
55 mpz_trace (" got ", got);
56 mpz_trace (" want", want);
60 if (mpz_fits_ulong_p (base))
62 unsigned long base_u = mpz_get_ui (base);
63 mpz_ui_pow_ui (got, base_u, exp);
64 if (mpz_cmp (got, want))
66 printf ("mpz_ui_pow_ui wrong\n");
67 printf (" base=%lu (0x%lX)\n", base_u, base_u);
68 printf (" exp = %lu (0x%lX)\n", exp, exp);
69 mpz_trace (" got ", got);
70 mpz_trace (" want", want);
79 check_base (mpz_srcptr base)
85 mpz_set_ui (want, 1L);
87 for (exp = 0; exp < 20; exp++)
89 check_one (want, base, exp);
90 mpz_mul (want, want, base);
118 { "0x1000000000000001" },
120 /* actual size closest to estimate */
122 { "0xFFFFFFFFFFFFFFFF" },
124 /* same after rshift */
127 { "0xFFFFFFFFFFFFFFFF0" },
128 { "0xFFFFFFFFFFFFFFFF00" },
130 /* change from 2 limbs to 1 after rshift */
132 { "0x18000000000000000" },
134 /* change from 3 limbs to 2 after rshift */
135 { "0x18000000100000000" },
136 { "0x180000000000000010000000000000000" },
138 /* handling of absolute value */
140 { "-0x8000000000000000" },
142 /* low zero limb, and size>2, checking argument overlap detection */
143 { "0x3000000000000000300000000000000030000000000000000" },
151 for (i = 0; i < numberof (data); i++)
153 mpz_set_str_or_abort (base, data[i].base, 0);
161 check_random (int reps)
166 unsigned long size_range, exp;
167 gmp_randstate_ptr rands = RANDS;
172 for (i = 0; i < reps; i++)
174 /* exponentially random 0 to 2^13 bits for base */
175 mpz_urandomb (want, rands, 32);
176 size_range = mpz_get_ui (want) % 12 + 2;
177 mpz_urandomb (want, rands, size_range);
178 base_size = mpz_get_ui (want);
179 mpz_rrandomb (base, rands, base_size);
181 /* randomly signed base */
182 mpz_urandomb (want, rands, 2);
183 if ((mpz_get_ui (want) & 1) != 0)
184 mpz_neg (base, base);
186 /* random 5 bits for exponent */
187 mpz_urandomb (want, rands, 5L);
188 exp = mpz_get_ui (want);
190 refmpz_pow_ui (want, base, exp);
191 check_one (want, base, exp);
199 main (int argc, char **argv)
203 /* dummy call to drag in refmpn.o for testing mpz/n_pow_ui.c with
205 refmpn_zero_p (NULL, (mp_size_t) 0);
211 reps = atoi (argv[1]);