3 Copyright 2012, 2013 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library test suite.
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 Public License for more details.
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite. If not, see http://www.gnu.org/licenses/. */
24 #include "testutils.h"
30 dump (const char *label, const mpz_t x)
32 char *buf = mpz_get_str (NULL, 16, x);
33 fprintf (stderr, "%s: %s\n", label, buf);
37 typedef void div_qr_func (mpz_t, mpz_t, const mpz_t, const mpz_t);
38 typedef unsigned long div_qr_ui_func (mpz_t, mpz_t, const mpz_t, unsigned long);
39 typedef void div_func (mpz_t, const mpz_t, const mpz_t);
40 typedef unsigned long div_x_ui_func (mpz_t, const mpz_t, unsigned long);
41 typedef unsigned long div_ui_func (const mpz_t, unsigned long);
44 testmain (int argc, char **argv)
47 mpz_t a, b, q, r, rq, rr;
57 for (i = 0; i < COUNT; i++)
60 for (j = 0; j < 3; j++)
62 static const enum hex_random_op ops[3] = { OP_CDIV, OP_FDIV, OP_TDIV };
63 static const char name[3] = { 'c', 'f', 't'};
64 static div_qr_func * const div_qr [3] =
66 mpz_cdiv_qr, mpz_fdiv_qr, mpz_tdiv_qr
68 static div_qr_ui_func *div_qr_ui[3] =
70 mpz_cdiv_qr_ui, mpz_fdiv_qr_ui, mpz_tdiv_qr_ui
72 static div_func * const div_q [3] =
74 mpz_cdiv_q, mpz_fdiv_q, mpz_tdiv_q
76 static div_x_ui_func *div_q_ui[3] =
78 mpz_cdiv_q_ui, mpz_fdiv_q_ui, mpz_tdiv_q_ui
80 static div_func * const div_r [3] =
82 mpz_cdiv_r, mpz_fdiv_r, mpz_tdiv_r
84 static div_x_ui_func *div_r_ui[3] =
86 mpz_cdiv_r_ui, mpz_fdiv_r_ui, mpz_tdiv_r_ui
88 static div_ui_func *div_ui[3] =
90 mpz_cdiv_ui, mpz_fdiv_ui, mpz_tdiv_ui
93 mini_random_op4 (ops[j], MAXBITS, a, b, rq, rr);
94 div_qr[j] (q, r, a, b);
95 if (mpz_cmp (r, rr) || mpz_cmp (q, rq))
97 fprintf (stderr, "mpz_%cdiv_qr failed:\n", name[j]);
110 fprintf (stderr, "mpz_%cdiv_q failed:\n", name[j]);
121 fprintf (stderr, "mpz_%cdiv_r failed:\n", name[j]);
129 if (j == 0) /* do this once, not for all roundings */
131 div_p = mpz_divisible_p (a, b);
132 if ((mpz_sgn (r) == 0) ^ (div_p != 0))
134 fprintf (stderr, "mpz_divisible_p failed:\n");
142 if (j == 0 && mpz_sgn (b) < 0) /* ceil, negative divisor */
147 fprintf (stderr, "mpz_mod failed:\n");
156 if (j == 1 && mpz_sgn (b) > 0) /* floor, positive divisor */
161 fprintf (stderr, "mpz_mod failed:\n");
170 if (mpz_fits_ulong_p (b))
174 rl = div_qr_ui[j] (q, r, a, mpz_get_ui (b));
175 if (rl != mpz_get_ui (rr)
176 || mpz_cmp (r, rr) || mpz_cmp (q, rq))
178 fprintf (stderr, "mpz_%cdiv_qr_ui failed:\n", name[j]);
181 fprintf(stderr, "rl = %lx\n", rl);
190 rl = div_q_ui[j] (q, a, mpz_get_ui (b));
191 if (rl != mpz_get_ui (rr) || mpz_cmp (q, rq))
193 fprintf (stderr, "mpz_%cdiv_q_ui failed:\n", name[j]);
196 fprintf(stderr, "rl = %lx\n", rl);
204 rl = div_r_ui[j] (r, a, mpz_get_ui (b));
205 if (rl != mpz_get_ui (rr) || mpz_cmp (r, rr))
207 fprintf (stderr, "mpz_%cdiv_qr_ui failed:\n", name[j]);
210 fprintf(stderr, "rl = %lx\n", rl);
216 rl = div_ui[j] (a, mpz_get_ui (b));
217 if (rl != mpz_get_ui (rr))
219 fprintf (stderr, "mpz_%cdiv_qr_ui failed:\n", name[j]);
222 fprintf(stderr, "rl = %lx\n", rl);
227 if (j == 0) /* do this once, not for all roundings */
229 div_p = mpz_divisible_ui_p (a, mpz_get_ui (b));
230 if ((mpz_sgn (r) == 0) ^ (div_p != 0))
232 fprintf (stderr, "mpz_divisible_ui_p failed:\n");
240 if (j == 1) /* floor */
242 mpz_mod_ui (r, a, mpz_get_ui (b));
245 fprintf (stderr, "mpz_mod failed:\n");