~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/tests/mpz/t-powm_ui.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test mpz_powm_ui, mpz_mul. mpz_mod, mpz_mod_ui, mpz_div_ui.
 
2
 
 
3
Copyright 1991, 1993, 1994, 1996, 1997, 2000, 2001 Free Software Foundation, Inc.
 
4
 
 
5
This file is part of the GNU MP Library.
 
6
 
 
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 2.1 of the License, or (at your
 
10
option) any later version.
 
11
 
 
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.
 
16
 
 
17
You should have received a copy of the GNU Lesser General Public License
 
18
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
 
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
20
MA 02111-1307, USA. */
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
 
 
25
#include "gmp.h"
 
26
#include "gmp-impl.h"
 
27
#include "tests.h"
 
28
 
 
29
void dump_abort _PROTO ((mpz_t, mpz_t));
 
30
void debug_mp _PROTO ((mpz_t, int));
 
31
 
 
32
int
 
33
main (int argc, char **argv)
 
34
{
 
35
  mpz_t base, exp, mod;
 
36
  mpz_t r1, r2, base2;
 
37
  mp_size_t base_size, exp_size, mod_size;
 
38
  unsigned long int exp2;
 
39
  int i;
 
40
  int reps = 400;
 
41
  gmp_randstate_ptr rands;
 
42
  mpz_t bs;
 
43
  unsigned long bsi, size_range;
 
44
 
 
45
  tests_start ();
 
46
  rands = RANDS;
 
47
 
 
48
  mpz_init (bs);
 
49
 
 
50
  if (argc == 2)
 
51
     reps = atoi (argv[1]);
 
52
 
 
53
  mpz_init (base);
 
54
  mpz_init (exp);
 
55
  mpz_init (mod);
 
56
  mpz_init (r1);
 
57
  mpz_init (r2);
 
58
  mpz_init (base2);
 
59
 
 
60
  for (i = 0; i < reps; i++)
 
61
    {
 
62
      mpz_urandomb (bs, rands, 32);
 
63
      size_range = mpz_get_ui (bs) % 13 + 2;
 
64
 
 
65
      do  /* Loop until mathematically well-defined.  */
 
66
        {
 
67
          mpz_urandomb (bs, rands, size_range);
 
68
          base_size = mpz_get_ui (bs);
 
69
          mpz_rrandomb (base, rands, base_size);
 
70
 
 
71
          mpz_urandomb (bs, rands, 6L);
 
72
          exp_size = mpz_get_ui (bs);
 
73
          mpz_rrandomb (exp, rands, exp_size);
 
74
          exp2 = mpz_getlimbn (exp, 0);
 
75
        }
 
76
      while (mpz_cmp_ui (base, 0) == 0 && exp2 == 0);
 
77
 
 
78
      do
 
79
        {
 
80
          mpz_urandomb (bs, rands, size_range);
 
81
          mod_size = mpz_get_ui (bs);
 
82
          mpz_rrandomb (mod, rands, mod_size);
 
83
        }
 
84
      while (mpz_cmp_ui (mod, 0) == 0);
 
85
 
 
86
      mpz_urandomb (bs, rands, 2);
 
87
      bsi = mpz_get_ui (bs);
 
88
      if ((bsi & 1) != 0)
 
89
        mpz_neg (base, base);
 
90
 
 
91
      /* printf ("%ld %ld\n", SIZ (base), SIZ (mod)); */
 
92
 
 
93
#if 0
 
94
      putc ('\n', stderr);
 
95
      debug_mp (base, -16);
 
96
      debug_mp (mod, -16);
 
97
#endif
 
98
 
 
99
      mpz_powm_ui (r1, base, exp2, mod);
 
100
 
 
101
      mpz_set_ui (r2, 1);
 
102
      mpz_set (base2, base);
 
103
 
 
104
      mpz_mod (r2, r2, mod);    /* needed when exp==0 and mod==1 */
 
105
      while (exp2 != 0)
 
106
        {
 
107
          if (exp2 % 2 != 0)
 
108
            {
 
109
              mpz_mul (r2, r2, base2);
 
110
              mpz_mod (r2, r2, mod);
 
111
            }
 
112
          mpz_mul (base2, base2, base2);
 
113
          mpz_mod (base2, base2, mod);
 
114
          exp2 = exp2 / 2;
 
115
        }
 
116
 
 
117
#if 0
 
118
      debug_mp (r1, -16);
 
119
      debug_mp (r2, -16);
 
120
#endif
 
121
 
 
122
      if (mpz_cmp (r1, r2) != 0)
 
123
        {
 
124
          fprintf (stderr, "\ntest %d: Incorrect results for operands:\n", i);
 
125
          debug_mp (base, -16);
 
126
          debug_mp (exp, -16);
 
127
          debug_mp (mod, -16);
 
128
          fprintf (stderr, "mpz_powm result:\n");
 
129
          debug_mp (r1, -16);
 
130
          fprintf (stderr, "reference result:\n");
 
131
          debug_mp (r2, -16);
 
132
          abort ();
 
133
        }
 
134
    }
 
135
 
 
136
  mpz_clear (bs);
 
137
  mpz_clear (base);
 
138
  mpz_clear (exp);
 
139
  mpz_clear (mod);
 
140
  mpz_clear (r1);
 
141
  mpz_clear (r2);
 
142
  mpz_clear (base2);
 
143
 
 
144
  tests_end ();
 
145
  exit (0);
 
146
}
 
147
 
 
148
void
 
149
dump_abort (mpz_t dividend, mpz_t divisor)
 
150
{
 
151
  fprintf (stderr, "ERROR\n");
 
152
  fprintf (stderr, "dividend = "); debug_mp (dividend, -16);
 
153
  fprintf (stderr, "divisor  = "); debug_mp (divisor, -16);
 
154
  abort();
 
155
}
 
156
 
 
157
void
 
158
debug_mp (mpz_t x, int base)
 
159
{
 
160
  mpz_out_str (stderr, base, x); fputc ('\n', stderr);
 
161
}