1 /* Test mpz_abs, mpz_add, mpz_cmp, mpz_cmp_ui, mpz_fdiv_qr, mpz_fdiv_q,
4 Copyright 1993, 1994, 1996, 2000, 2001 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 void dump_abort __GMP_PROTO ((mpz_t, mpz_t));
29 void debug_mp __GMP_PROTO ((mpz_t, int));
32 main (int argc, char **argv)
34 mpz_t dividend, divisor;
35 mpz_t quotient, remainder;
36 mpz_t quotient2, remainder2;
38 mp_size_t dividend_size, divisor_size;
41 gmp_randstate_ptr rands;
43 unsigned long bsi, size_range;
51 reps = atoi (argv[1]);
58 mpz_init (remainder2);
61 for (i = 0; i < reps; i++)
63 mpz_urandomb (bs, rands, 32);
64 size_range = mpz_get_ui (bs) % 16 + 2; /* 0..131071 bit operands */
68 mpz_urandomb (bs, rands, size_range);
69 divisor_size = mpz_get_ui (bs);
70 mpz_rrandomb (divisor, rands, divisor_size);
72 while (mpz_sgn (divisor) == 0);
74 mpz_urandomb (bs, rands, size_range);
75 dividend_size = mpz_get_ui (bs) + divisor_size;
76 mpz_rrandomb (dividend, rands, dividend_size);
78 mpz_urandomb (bs, rands, 2);
79 bsi = mpz_get_ui (bs);
81 mpz_neg (dividend, dividend);
83 mpz_neg (divisor, divisor);
85 /* printf ("%ld %ld\n", SIZ (dividend), SIZ (divisor)); */
87 mpz_fdiv_qr (quotient, remainder, dividend, divisor);
88 mpz_fdiv_q (quotient2, dividend, divisor);
89 mpz_fdiv_r (remainder2, dividend, divisor);
91 /* First determine that the quotients and remainders computed
92 with different functions are equal. */
93 if (mpz_cmp (quotient, quotient2) != 0)
94 dump_abort (dividend, divisor);
95 if (mpz_cmp (remainder, remainder2) != 0)
96 dump_abort (dividend, divisor);
98 /* Check if the sign of the quotient is correct. */
99 if (mpz_cmp_ui (quotient, 0) != 0)
100 if ((mpz_cmp_ui (quotient, 0) < 0)
101 != ((mpz_cmp_ui (dividend, 0) ^ mpz_cmp_ui (divisor, 0)) < 0))
102 dump_abort (dividend, divisor);
104 /* Check if the remainder has the same sign as the divisor
105 (quotient rounded towards minus infinity). */
106 if (mpz_cmp_ui (remainder, 0) != 0)
107 if ((mpz_cmp_ui (remainder, 0) < 0) != (mpz_cmp_ui (divisor, 0) < 0))
108 dump_abort (dividend, divisor);
110 mpz_mul (temp, quotient, divisor);
111 mpz_add (temp, temp, remainder);
112 if (mpz_cmp (temp, dividend) != 0)
113 dump_abort (dividend, divisor);
115 mpz_abs (temp, divisor);
116 mpz_abs (remainder, remainder);
117 if (mpz_cmp (remainder, temp) >= 0)
118 dump_abort (dividend, divisor);
122 mpz_clear (dividend);
124 mpz_clear (quotient);
125 mpz_clear (remainder);
126 mpz_clear (quotient2);
127 mpz_clear (remainder2);
135 dump_abort (mpz_t dividend, mpz_t divisor)
137 fprintf (stderr, "ERROR\n");
138 fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
139 fprintf (stderr, "divisor = "); debug_mp (divisor, -16);
144 debug_mp (mpz_t x, int base)
146 mpz_out_str (stderr, base, x); fputc ('\n', stderr);