Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libgmp / mpf / tests / t-muldiv.c
1 /* Test mpf_mul, mpf_div, mpf_ui_div, and mpf_div_ui.
2
3 Copyright (C) 1996 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 Library General Public License as published by
9 the Free Software Foundation; either version 2 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 Library General Public
15 License for more details.
16
17 You should have received a copy of the GNU Library 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 "gmp.h"
24 #include "gmp-impl.h"
25 #include "urandom.h"
26
27 #ifndef SIZE
28 #define SIZE 16
29 #endif
30
31 main (argc, argv)
32      int argc;
33      char **argv;
34 {
35   mp_size_t size;
36   mp_exp_t exp;
37   int reps = 100000;
38   int i;
39   mpf_t u, v, w, x;
40   mp_size_t bprec = 100;
41   mpf_t rerr, limit_rerr;
42   mp_limb_t ulimb, vlimb;
43   int single_flag;
44
45   if (argc > 1)
46     {
47       reps = strtol (argv[1], 0, 0);
48       if (argc > 2)
49         bprec = strtol (argv[2], 0, 0);
50     }
51
52   mpf_set_default_prec (bprec);
53
54   mpf_init (rerr);
55   mpf_init (limit_rerr);
56
57   mpf_init (u);
58   mpf_init (v);
59   mpf_init (w);
60   mpf_init (x);
61
62   for (i = 0; i < reps; i++)
63     {
64       mp_size_t res_prec;
65
66       res_prec = urandom () % (bprec + 100);
67       mpf_set_prec (w, res_prec);
68       mpf_set_prec (x, res_prec);
69
70       mpf_set_ui (limit_rerr, 1);
71       mpf_div_2exp (limit_rerr, limit_rerr, res_prec);
72
73       single_flag = 0;
74
75       if ((urandom () & 1) != 0)
76         {
77           size = urandom () % (2 * SIZE) - SIZE;
78           exp = urandom () % SIZE;
79           mpf_random2 (u, size, exp);
80         }
81       else
82         {
83           ulimb = urandom ();
84           mpf_set_ui (u, ulimb);
85           single_flag = 1;
86         }
87
88       if ((urandom () & 1) != 0)
89         {
90           size = urandom () % (2 * SIZE) - SIZE;
91           exp = urandom () % SIZE;
92           mpf_random2 (v, size, exp);
93         }
94       else
95         {
96           vlimb = urandom ();
97           mpf_set_ui (v, vlimb);
98           single_flag = 2;
99         }
100
101       if (mpf_sgn (v) == 0)
102         continue;
103
104       mpf_div (w, u, v);
105       mpf_mul (x, w, v);
106       mpf_reldiff (rerr, u, x);
107       if (mpf_cmp (rerr, limit_rerr) > 0)
108         {
109           printf ("ERROR in mpf_mul or mpf_div after %d tests\n", i);
110           printf ("   u = "); mpf_dump (u);
111           printf ("   v = "); mpf_dump (v);
112           printf ("   x = "); mpf_dump (x);
113           printf ("   w = "); mpf_dump (w);
114           abort ();
115         }
116
117       if (single_flag == 2)
118         {
119           mpf_div_ui (x, u, vlimb);
120           mpf_reldiff (rerr, w, x);
121           if (mpf_cmp (rerr, limit_rerr) > 0)
122             {
123               printf ("ERROR in mpf_div or mpf_div_ui after %d tests\n", i);
124               printf ("   u = "); mpf_dump (u);
125               printf ("   v = "); mpf_dump (v);
126               printf ("   x = "); mpf_dump (x);
127               printf ("   w = "); mpf_dump (w);
128               abort ();
129             }
130         }
131
132       if (single_flag == 1)
133         {
134           mpf_ui_div (x, ulimb, v);
135           mpf_reldiff (rerr, w, x);
136           if (mpf_cmp (rerr, limit_rerr) > 0)
137             {
138               printf ("ERROR in mpf_div or mpf_ui_div after %d tests\n", i);
139               printf ("   u = "); mpf_dump (u);
140               printf ("   v = "); mpf_dump (v);
141               printf ("   x = "); mpf_dump (x);
142               printf ("   w = "); mpf_dump (w);
143               abort ();
144             }
145         }
146     }
147
148   exit (0);
149 }
150
151 oo (x)
152      mpf_t x;
153 {
154   mp_size_t i;
155   printf (" exp = %ld\n", x->_mp_exp);
156   printf ("size = %d\n", x->_mp_size);
157   for (i = ABS (x->_mp_size) - 1; i >= 0; i--)
158     printf ("%08lX ", x->_mp_d[i]);
159   printf ("\n");
160   mpf_dump (x);
161 }