aboutsummaryrefslogtreecommitdiff
path: root/gmp-6.3.0/mini-gmp/tests/t-mpq_muldiv_2exp.c
blob: 46b2c0c2fba03bfc71b074952864aad9948cc8d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*

Copyright 2012, 2013, 2018 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.

The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#include "testutils.h"
#include "../mini-mpq.h"

#define MAXBITS 300
#define COUNT 10000

static void
_mpq_set_zz (mpq_t q, mpz_t n, mpz_t d)
{
  if (mpz_fits_ulong_p (d) && mpz_fits_slong_p (n))
    {
      mpq_set_si (q, mpz_get_si (n), mpz_get_ui (d));
    }
  else if (mpz_fits_ulong_p (d) && mpz_fits_ulong_p (n))
    {
      mpq_set_ui (q, mpz_get_ui (n), mpz_get_ui (d));
    }
  else
    {
      mpq_set_num (q, n);
      mpq_set_den (q, d);
    }
  mpq_canonicalize (q);
}

void
testmain (int argc, char **argv)
{
  unsigned i;
  mpz_t a, b, t;
  mpq_t aq, rq, tq;
  mp_bitcnt_t e;
  long int e2, t1, t2;

  mpz_init (a);
  mpz_init (b);
  mpz_init (t);
  mpq_init (aq);
  mpq_init (rq);
  mpq_init (tq);

  for (i = 0; i < COUNT; i++)
    {
      do {
	mini_random_bit_op (OP_COMBIT, MAXBITS, a, &e, b);
      } while (mpz_sgn (a) == 0 || mpz_sgn (b) == 0);

      _mpq_set_zz (aq, a, b);
      e2 = mpz_scan1 (a, 0);
      e2-= mpz_scan1 (b, 0);

      mpq_mul_2exp (rq, aq, e);
      t1 = mpz_scan1 (mpq_numref (rq), 0);
      t2 = mpz_scan1 (mpq_denref (rq), 0);
      mpq_neg (tq, rq);
      mpq_div (tq, aq, tq);
      mpq_get_den (t, tq);

      if (e2 + e != t1 - t2 || (t2 != 0 && t1 != 0) || mpz_scan1 (t, 0) != e
	  || mpz_sizeinbase (t, 2) - 1 != e || mpz_cmp_si (mpq_numref (tq), -1) != 0)
	{
	  fprintf (stderr, "mpq_mul_2exp failed: %lu\n", e);
	  dump ("na", a);
	  dump ("da", b);
	  dump ("nr", mpq_numref (rq));
	  dump ("dr", mpq_denref (rq));
	  abort ();
	}

      mpq_div_2exp (rq, aq, e);
      t1 = mpz_scan1 (mpq_numref (rq), 0);
      t2 = mpz_scan1 (mpq_denref (rq), 0);
      mpq_div (aq, aq, rq);
      mpq_get_num (t, aq);

      if (e2 != t1 - t2 + e || (t2 != 0 && t1 != 0) || mpz_scan1 (t, 0) != e
	  || mpz_sizeinbase (t, 2) - 1 != e || mpz_cmp_ui (mpq_denref (aq), 1) != 0)
	{
	  fprintf (stderr, "mpq_div_2exp failed: %lu\n", e);
	  fprintf (stderr, "%li %li %lu %zu\n", e2, t2, mpz_scan1 (t, 0), mpz_sizeinbase (t, 2));
	  dump ("na", a);
	  dump ("da", b);
	  dump ("nr", mpq_numref (rq));
	  dump ("dr", mpq_denref (rq));
	  abort ();
	}

      mpq_set_ui (aq, 0, 1);
      mpq_set_ui (rq, 6, 7);
      mpq_set (tq, aq);
      mpq_div_2exp (rq, aq, e);

      if (!mpq_equal (tq, rq))
	{
	  fprintf (stderr, "mpq_div_2exp failed on zero: %lu\n", e);
	  abort ();
	}

      mpq_set_ui (rq, 7, 6);
      mpq_mul_2exp (rq, aq, e);

      if (!mpq_equal (rq, tq))
	{
	  fprintf (stderr, "mpq_mul_2exp failed on zero: %lu\n", e);
	  abort ();
	}
    }

  mpz_clear (a);
  mpz_clear (b);
  mpz_clear (t);
  mpq_clear (aq);
  mpq_clear (rq);
  mpq_clear (tq);
}