Annotation of OpenXM_contrib/gmp/mpf/tests/reuse.c, Revision 1.1.1.2
1.1.1.2 ! maekawa 1: /* Test that routines allow reusing a source variable as destination.
! 2:
! 3: Copyright (C) 1996, 2000 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. */
1.1 maekawa 21:
22: #include <stdio.h>
23: #include "gmp.h"
24: #include "gmp-impl.h"
25: #include "urandom.h"
26:
27: #ifndef SIZE
28: #define SIZE 16
29: #endif
30:
31: #ifndef EXPO
32: #define EXPO 32
33: #endif
34:
35: #if __STDC__
36: typedef void (*dss_func) (mpf_ptr, mpf_srcptr, mpf_srcptr);
37: #else
38: typedef void (*dss_func) ();
39: #endif
40:
41: dss_func dss_funcs[] =
42: {
43: mpf_div, mpf_add, mpf_mul, mpf_sub,
44: };
45:
46: char *dss_func_names[] =
47: {
48: "mpf_div", "mpf_add", "mpf_mul", "mpf_sub",
49: };
50:
51: #if __STDC__
52: typedef void (*dsi_func) (mpf_ptr, mpf_srcptr, unsigned long int);
53: #else
54: typedef void (*dsi_func) ();
55: #endif
56:
57: dsi_func dsi_funcs[] =
58: {
59: mpf_div_ui, mpf_add_ui, mpf_mul_ui, mpf_sub_ui,
1.1.1.2 ! maekawa 60: mpf_mul_2exp, mpf_div_2exp
1.1 maekawa 61: };
62:
63: char *dsi_func_names[] =
64: {
65: "mpf_div_ui", "mpf_add_ui", "mpf_mul_ui", "mpf_sub_ui",
1.1.1.2 ! maekawa 66: "mpf_mul_2exp", "mpf_div_2exp"
1.1 maekawa 67: };
68:
69: #if __STDC__
70: typedef void (*dis_func) (mpf_ptr, unsigned long int, mpf_srcptr);
71: #else
72: typedef void (*dis_func) ();
73: #endif
74:
75: dis_func dis_funcs[] =
76: {
77: mpf_ui_div, mpf_ui_sub,
78: };
79:
80: char *dis_func_names[] =
81: {
82: "mpf_ui_div", "mpf_ui_sub",
83: };
84:
85: main (argc, argv)
86: int argc;
87: char **argv;
88: {
89: int i;
90: int pass, reps = 100000;
91: mpf_t in1, in2, out1;
92: unsigned long int in1i, in2i;
93: mpf_t res1, res2, res3;
94: mp_size_t bprec = 100;
95:
96: if (argc > 1)
97: {
98: reps = strtol (argv[1], 0, 0);
99: if (argc > 2)
100: bprec = strtol (argv[2], 0, 0);
101: }
102:
103: mpf_set_default_prec (bprec);
104:
105: mpf_init (in1);
106: mpf_init (in2);
107: mpf_init (out1);
108: mpf_init (res1);
109: mpf_init (res2);
110: mpf_init (res3);
111:
112: for (pass = 1; pass <= reps; pass++)
113: {
114: mpf_random2 (in1, urandom () % SIZE - SIZE/2, urandom () % EXPO);
115: mpf_random2 (in2, urandom () % SIZE - SIZE/2, urandom () % EXPO);
116:
117: for (i = 0; i < sizeof (dss_funcs) / sizeof (dss_func); i++)
118: {
119: /* Don't divide by 0. */
120: if (i == 0 && mpf_cmp_ui (in2, 0) == 0)
121: continue;
122:
123: (dss_funcs[i]) (res1, in1, in2);
124:
125: mpf_set (out1, in1);
126: (dss_funcs[i]) (out1, out1, in2);
127: mpf_set (res2, out1);
128:
129: mpf_set (out1, in2);
130: (dss_funcs[i]) (out1, in1, out1);
131: mpf_set (res3, out1);
132:
133: if (mpf_cmp (res1, res2) != 0)
134: dump_abort (dss_func_names[i], res1, res2);
135: if (mpf_cmp (res1, res3) != 0)
136: dump_abort (dss_func_names[i], res1, res3);
137: }
138:
139: in2i = urandom ();
140: for (i = 0; i < sizeof (dsi_funcs) / sizeof (dsi_func); i++)
141: {
142: /* Don't divide by 0. */
1.1.1.2 ! maekawa 143: if (strcmp (dsi_func_names[i], "mpf_div_ui") == 0 && in2i == 0)
1.1 maekawa 144: continue;
145:
146: (dsi_funcs[i]) (res1, in1, in2i);
147:
148: mpf_set (out1, in1);
149: (dsi_funcs[i]) (out1, out1, in2i);
150: mpf_set (res2, out1);
151:
152: if (mpf_cmp (res1, res2) != 0)
153: dump_abort (dsi_func_names[i], res1, res2);
154: }
155:
156: in1i = urandom ();
157: for (i = 0; i < sizeof (dis_funcs) / sizeof (dis_func); i++)
158: {
159: /* Don't divide by 0. */
1.1.1.2 ! maekawa 160: if (strcmp (dis_func_names[i], "mpf_ui_div") == 0
! 161: && mpf_cmp_ui (in2, 0) == 0)
1.1 maekawa 162: continue;
163:
164: (dis_funcs[i]) (res1, in1i, in2);
165:
166: mpf_set (out1, in2);
1.1.1.2 ! maekawa 167: (dis_funcs[i]) (out1, in1i, out1);
1.1 maekawa 168: mpf_set (res2, out1);
169:
170: if (mpf_cmp (res1, res2) != 0)
171: dump_abort (dis_func_names[i], res1, res2);
172: }
173:
174: }
175:
176: exit (0);
177: }
178:
179: dump_abort (name, res1, res2)
180: char *name;
181: mpf_t res1, res2;
182: {
1.1.1.2 ! maekawa 183: printf ("failure in %s:\n", name);
1.1 maekawa 184: oo (res1);
185: oo (res2);
186: abort ();
187: }
188:
189: oo (x)
190: mpf_t x;
191: {
192: mp_size_t i;
193: printf (" exp = %ld\n", x->_mp_exp);
194: printf ("size = %d\n", x->_mp_size);
195: for (i = ABS (x->_mp_size) - 1; i >= 0; i--)
196: printf ("%08lX ", x->_mp_d[i]);
197: printf ("\n");
198: mpf_dump (x);
199: }
200:
201: #if 0
202: void mpf_abs _PROTO ((mpf_ptr, mpf_srcptr));
203: void mpf_sqrt _PROTO ((mpf_ptr, mpf_srcptr));
204: void mpf_neg _PROTO ((mpf_ptr, mpf_srcptr));
205: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>