Annotation of OpenXM_contrib/gmp/mpz/tests/t-gcd2.c, Revision 1.1.1.1
1.1 maekawa 1: /* Test mpz_gcd, mpz_gcdext, mpz_mul, mpz_tdiv_r, mpz_add, mpz_cmp,
2: mpz_cmp_ui, mpz_init_set, mpz_set, mpz_clear.
3:
4: Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
5:
6: This file is part of the GNU MP Library.
7:
8: The GNU MP Library is free software; you can redistribute it and/or modify
9: it under the terms of the GNU Library General Public License as published by
10: the Free Software Foundation; either version 2 of the License, or (at your
11: option) any later version.
12:
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 Library General Public
16: License for more details.
17:
18: You should have received a copy of the GNU Library General Public License
19: along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21: MA 02111-1307, USA. */
22:
23: #include <stdio.h>
24: #include "gmp.h"
25: #include "gmp-impl.h"
26: #include "urandom.h"
27:
28: void mpz_refgcd (), debug_mp ();
29:
30: #ifndef SIZE
31: #define SIZE 128
32: #endif
33:
34: main (argc, argv)
35: int argc;
36: char **argv;
37: {
38: mpz_t op1, op2, x;
39: mpz_t refgcd, gcd, s, t;
40: mpz_t temp1, temp2;
41: mp_size_t op1_size, op2_size, x_size;
42: int i;
43: int reps = 1000;
44:
45: if (argc == 2)
46: reps = atoi (argv[1]);
47:
48: mpz_init (op1);
49: mpz_init (op2);
50: mpz_init (x);
51: mpz_init (refgcd);
52: mpz_init (gcd);
53: mpz_init (temp1);
54: mpz_init (temp2);
55: mpz_init (s);
56: mpz_init (t);
57:
58: for (i = 0; i < reps; i++)
59: {
60: op1_size = urandom () % SIZE - SIZE/2;
61: op2_size = urandom () % SIZE - SIZE/2;
62: x_size = urandom () % SIZE/2;
63:
64: mpz_random2 (op1, op1_size);
65: mpz_random2 (op2, op2_size);
66: mpz_random2 (x, x_size);
67: mpz_mul (op1, op1, x);
68: mpz_mul (op2, op2, x);
69:
70: mpz_refgcd (refgcd, op1, op2);
71:
72: mpz_gcd (gcd, op1, op2);
73: if (mpz_cmp (refgcd, gcd))
74: dump_abort (op1, op2);
75:
76: mpz_gcdext (gcd, s, t, op1, op2);
77: if (mpz_cmp (refgcd, gcd))
78: dump_abort (op1, op2);
79:
80: mpz_mul (temp1, s, op1);
81: mpz_mul (temp2, t, op2);
82: mpz_add (gcd, temp1, temp2);
83: if (mpz_cmp (refgcd, gcd))
84: dump_abort (op1, op2);
85: }
86:
87: exit (0);
88: }
89:
90: void
91: mpz_refgcd (g, x, y)
92: mpz_t g;
93: mpz_t x, y;
94: {
95: mpz_t xx, yy;
96:
97: mpz_init (xx);
98: mpz_init (yy);
99:
100: mpz_abs (xx, x);
101: mpz_abs (yy, y);
102:
103: for (;;)
104: {
105: if (mpz_cmp_ui (yy, 0) == 0)
106: {
107: mpz_set (g, xx);
108: break;
109: }
110: mpz_tdiv_r (xx, xx, yy);
111: if (mpz_cmp_ui (xx, 0) == 0)
112: {
113: mpz_set (g, yy);
114: break;
115: }
116: mpz_tdiv_r (yy, yy, xx);
117: }
118:
119: mpz_clear (xx);
120: mpz_clear (yy);
121: }
122:
123: dump_abort (op1, op2)
124: mpz_t op1, op2;
125: {
126: fprintf (stderr, "ERROR\n");
127: fprintf (stderr, "op1 = "); debug_mp (op1, -16);
128: fprintf (stderr, "op2 = "); debug_mp (op2, -16);
129: abort();
130: }
131:
132: void
133: debug_mp (x, base)
134: mpz_t x;
135: {
136: mpz_out_str (stderr, base, x); fputc ('\n', stderr);
137: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>