Upload Tizen:Base source
[external/gmp.git] / tests / devel / divmod_1.c
1 /*
2 Copyright 1996, 1998, 2000, 2001, 2007 Free Software Foundation, Inc.
3
4 This file is part of the GNU MP Library.
5
6 The GNU MP Library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or (at your
9 option) any later version.
10
11 The GNU MP Library is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14 License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "gmp.h"
22 #include "gmp-impl.h"
23 #include "tests.h"
24
25 #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
26 #include <time.h>
27
28 int
29 cputime ()
30 {
31   if (CLOCKS_PER_SEC < 100000)
32     return clock () * 1000 / CLOCKS_PER_SEC;
33   return clock () / (CLOCKS_PER_SEC / 1000);
34 }
35 #else
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39
40 int
41 cputime ()
42 {
43   struct rusage rus;
44
45   getrusage (0, &rus);
46   return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
47 }
48 #endif
49
50 static void mpn_print (mp_ptr, mp_size_t);
51
52 #define M * 1000000
53
54 #ifndef CLOCK
55 #error "Don't know CLOCK of your machine"
56 #endif
57
58 #ifndef OPS
59 #define OPS 20000000
60 #endif
61 #ifndef SIZE
62 #define SIZE 1000
63 #endif
64 #ifndef TIMES
65 #define TIMES OPS/SIZE
66 #endif
67
68 #ifndef FSIZE
69 #define FSIZE SIZE
70 #endif
71
72 int
73 main ()
74 {
75   mp_limb_t np[SIZE];
76   mp_limb_t dx[SIZE + FSIZE + 2];
77   mp_limb_t dy[SIZE + FSIZE + 2];
78   mp_limb_t dlimb;
79   mp_size_t nn, fn;
80   mp_limb_t retx, rety;
81   int test;
82 #if TIMES != 1
83   int i;
84   long t0, t;
85   double cyc;
86 #endif
87
88   for (test = 0; ; test++)
89     {
90 #if TIMES == 1 && ! defined (PRINT)
91       if (test % (SIZE > 100000 ? 1 : 100000 / SIZE) == 0)
92         {
93           printf ("\r%u", test);
94           fflush (stdout);
95         }
96 #endif
97
98 #ifdef RANDOM
99       nn = random () % (SIZE + 1);
100       fn = random () % (FSIZE + 1);
101 #else
102       nn = SIZE;
103       fn = FSIZE;
104 #endif
105
106       dx[0] = 0x87654321;
107       dx[nn + fn + 1] = 0x12345678;
108       dy[0] = 0x87654321;
109       dy[nn + fn + 1] = 0x12345678;
110       mpn_random2 (np, nn);
111
112 #ifdef FIXED_DLIMB
113       dlimb = FIXED_DLIMB;
114 #else
115       do
116         {
117           mpn_random2 (&dlimb, 1);
118 #ifdef FORCE_NORM
119           dlimb |= GMP_NUMB_HIGHBIT;
120 #endif
121 #ifdef FORCE_UNNORM
122           dlimb &= GMP_NUMB_MAX >> 1;
123 #endif
124         }
125       while (dlimb == 0);
126 #endif
127
128 #if defined (PRINT) || defined (XPRINT)
129       printf ("N=");
130       mpn_print (np, nn);
131       printf ("D=");
132       mpn_print (&dlimb, 1);
133       printf ("nn=%ld\n", (long) nn);
134 #endif
135
136 #if TIMES != 1
137       t0 = cputime();
138       for (i = 0; i < TIMES; i++)
139         mpn_divrem_1 (dx + 1, 0L, np, nn, dlimb);
140       t = cputime() - t0;
141       cyc = ((double) t * CLOCK) / (TIMES * nn * 1000.0);
142       printf ("mpn_divrem_1 int:    %5ldms (%.3f cycles/limb) [%.2f Gb/s]\n",
143               t, cyc,
144               CLOCK/cyc*GMP_LIMB_BITS*GMP_LIMB_BITS/1e9);
145       t0 = cputime();
146       for (i = 0; i < TIMES; i++)
147         mpn_divrem_1 (dx + 1, fn, np, 0, dlimb);
148       t = cputime() - t0;
149       cyc = ((double) t * CLOCK) / (TIMES * fn * 1000.0);
150       printf ("mpn_divrem_1 frac:   %5ldms (%.3f cycles/limb) [%.2f Gb/s]\n",
151               t, cyc,
152               CLOCK/cyc*GMP_LIMB_BITS*GMP_LIMB_BITS/1e9);
153 #endif
154
155       retx = refmpn_divrem_1 (dx + 1, fn, np, nn, dlimb);
156       rety = mpn_divrem_1 (dy + 1, fn, np, nn, dlimb);
157
158 #ifndef NOCHECK
159       if (retx != rety || mpn_cmp (dx, dy, fn + nn + 2) != 0)
160         {
161           printf ("ERROR in test %d, nn=%ld, fn=%ld\n", test, nn, fn);
162           mpn_print (np, nn);
163           mpn_print (&dlimb, 1);
164           printf ("rq: ");
165           mpn_print (dx + 1, nn + fn);
166           printf ("rr: %*lX\n", (int) (2 * sizeof(mp_limb_t)), retx);
167           printf (" q: ");
168           mpn_print (dy + 1, nn + fn);
169           printf (" r: %*lX\n", (int) (2 * sizeof(mp_limb_t)), rety);
170           if (dy[0] != 0x87654321)
171             printf ("clobbered at low end %*lX\n", (int) (2 * sizeof(mp_limb_t)), dy[0]);
172           if (dy[nn + fn + 1] != 0x12345678)
173             printf ("clobbered at high end\n");
174           abort ();
175         }
176 #endif
177     }
178 }
179
180 static void
181 mpn_print (mp_ptr p, mp_size_t size)
182 {
183   mp_size_t i;
184
185   for (i = size - 1; i >= 0; i--)
186     {
187 #ifdef _LONG_LONG_LIMB
188       printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)),
189               (unsigned long) (p[i] >> (GMP_LIMB_BITS/2)),
190               (int) (sizeof(mp_limb_t)), (unsigned long) (p[i]));
191 #else
192       printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
193 #endif
194 #ifdef SPACE
195       if (i != 0)
196         printf (" ");
197 #endif
198     }
199   puts ("");
200 }