Austin Schuh | bb1338c | 2024-06-15 19:31:16 -0700 | [diff] [blame] | 1 | /* Test mpz_mul, mpz_divexact. |
| 2 | |
| 3 | Copyright 1996, 2001, 2002 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of the GNU MP Library test suite. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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 https://www.gnu.org/licenses/. */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | |
| 23 | #include "gmp-impl.h" |
| 24 | #include "tests.h" |
| 25 | |
| 26 | int |
| 27 | main (int argc, char **argv) |
| 28 | { |
| 29 | mpz_t op1, op2; |
| 30 | mpz_t prod, quot; |
| 31 | mp_size_t size; |
| 32 | int i; |
| 33 | int reps = 5000; |
| 34 | gmp_randstate_ptr rands; |
| 35 | mpz_t bs; |
| 36 | unsigned long bsi, size_range; |
| 37 | |
| 38 | tests_start (); |
| 39 | TESTS_REPS (reps, argv, argc); |
| 40 | |
| 41 | rands = RANDS; |
| 42 | |
| 43 | mp_trace_base = -16; |
| 44 | |
| 45 | mpz_init (bs); |
| 46 | |
| 47 | mpz_init (op1); |
| 48 | mpz_init (op2); |
| 49 | mpz_init (prod); |
| 50 | mpz_init (quot); |
| 51 | |
| 52 | for (i = 0; i < reps; i++) |
| 53 | { |
| 54 | mpz_urandomb (bs, rands, 32); |
| 55 | size_range = mpz_get_ui (bs) % 17 + 2; /* 0..2047 bit operands */ |
| 56 | |
| 57 | mpz_urandomb (bs, rands, size_range); |
| 58 | size = mpz_get_ui (bs); |
| 59 | mpz_rrandomb (op1, rands, size); |
| 60 | |
| 61 | do |
| 62 | { |
| 63 | mpz_urandomb (bs, rands, size_range); |
| 64 | size = mpz_get_ui (bs); |
| 65 | mpz_rrandomb (op2, rands, size); |
| 66 | } |
| 67 | while (mpz_sgn (op2) == 0); |
| 68 | |
| 69 | mpz_urandomb (bs, rands, 2); |
| 70 | bsi = mpz_get_ui (bs); |
| 71 | if ((bsi & 1) != 0) |
| 72 | mpz_neg (op1, op1); |
| 73 | if ((bsi & 2) != 0) |
| 74 | mpz_neg (op2, op2); |
| 75 | |
| 76 | mpz_mul (prod, op1, op2); |
| 77 | |
| 78 | mpz_divexact (quot, prod, op2); |
| 79 | MPZ_CHECK_FORMAT (quot); |
| 80 | |
| 81 | if (mpz_cmp (quot, op1) != 0) |
| 82 | { |
| 83 | printf ("Wrong results:\n"); |
| 84 | mpz_trace (" got ", quot); |
| 85 | mpz_trace (" want ", op1); |
| 86 | mpz_trace (" dividend", prod); |
| 87 | mpz_trace (" divisor ", op2); |
| 88 | abort (); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | mpz_clear (bs); |
| 93 | mpz_clear (op1); |
| 94 | mpz_clear (op2); |
| 95 | mpz_clear (prod); |
| 96 | mpz_clear (quot); |
| 97 | |
| 98 | tests_end (); |
| 99 | exit (0); |
| 100 | } |