Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libgmp / mpn / tests / lshift.c
1 #include <stdio.h>
2 #include "gmp.h"
3 #include "gmp-impl.h"
4 #include "longlong.h"
5
6 #ifndef USG
7 #include <sys/time.h>
8 #include <sys/resource.h>
9
10 unsigned long
11 cputime ()
12 {
13     struct rusage rus;
14
15     getrusage (0, &rus);
16     return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
17 }
18 #else
19 #include <time.h>
20
21 #ifndef CLOCKS_PER_SEC
22 #define CLOCKS_PER_SEC 1000000
23 #endif
24
25 #if CLOCKS_PER_SEC >= 10000
26 #define CLOCK_TO_MILLISEC(cl) ((cl) / (CLOCKS_PER_SEC / 1000))
27 #else
28 #define CLOCK_TO_MILLISEC(cl) ((cl) * 1000 / CLOCKS_PER_SEC)
29 #endif
30
31 unsigned long
32 cputime ()
33 {
34   return CLOCK_TO_MILLISEC (clock ());
35 }
36 #endif
37
38 #define M * 1000000
39
40 #ifndef CLOCK
41 #if defined (__m88k__)
42 #define CLOCK 20 M
43 #elif defined (__i386__)
44 #define CLOCK (16.666667 M)
45 #elif defined (__m68k__)
46 #define CLOCK (20 M)
47 #elif defined (_IBMR2)
48 #define CLOCK (25 M)
49 #elif defined (__sparc__)
50 #define CLOCK (20 M)
51 #elif defined (__sun__)
52 #define CLOCK (20 M)
53 #elif defined (__mips)
54 #define CLOCK (40 M)
55 #elif defined (__hppa__)
56 #define CLOCK (50 M)
57 #elif defined (__alpha)
58 #define CLOCK (133 M)
59 #else
60 #error "Don't know CLOCK of your machine"
61 #endif
62 #endif
63
64 #ifndef OPS
65 #define OPS 10000000
66 #endif
67 #ifndef SIZE
68 #define SIZE 496
69 #endif
70 #ifndef TIMES
71 #define TIMES OPS/SIZE
72 #else
73 #undef OPS
74 #define OPS (SIZE*TIMES)
75 #endif
76
77 mp_limb_t
78 refmpn_lshift (wp, up, usize, cnt)
79      register mp_ptr wp;
80      register mp_srcptr up;
81      mp_size_t usize;
82      register unsigned int cnt;
83 {
84   register mp_limb_t high_limb, low_limb;
85   register unsigned sh_1, sh_2;
86   register mp_size_t i;
87   mp_limb_t retval;
88
89 #ifdef DEBUG
90   if (usize == 0 || cnt == 0)
91     abort ();
92 #endif
93
94   sh_1 = cnt;
95 #if 0
96   if (sh_1 == 0)
97     {
98       if (wp != up)
99         {
100           /* Copy from high end to low end, to allow specified input/output
101              overlapping.  */
102           for (i = usize - 1; i >= 0; i--)
103             wp[i] = up[i];
104         }
105       return 0;
106     }
107 #endif
108
109   wp += 1;
110   sh_2 = BITS_PER_MP_LIMB - sh_1;
111   i = usize - 1;
112   low_limb = up[i];
113   retval = low_limb >> sh_2;
114   high_limb = low_limb;
115   while (--i >= 0)
116     {
117       low_limb = up[i];
118       wp[i] = (high_limb << sh_1) | (low_limb >> sh_2);
119       high_limb = low_limb;
120     }
121   wp[i] = high_limb << sh_1;
122
123   return retval;
124 }
125
126 #ifndef CNT
127 #define CNT 4
128 #endif
129
130 main (argc, argv)
131      int argc;
132      char **argv;
133 {
134   mp_limb_t s1[SIZE];
135   mp_limb_t dx[SIZE+2];
136   mp_limb_t dy[SIZE+2];
137   mp_limb_t cyx, cyy;
138   int i;
139   long t0, t;
140   int test;
141   int cnt = CNT;
142   mp_size_t size;
143
144   for (test = 0; ; test++)
145     {
146 #ifdef RANDOM
147       size = (random () % SIZE + 1);
148 #else
149       size = SIZE;
150 #endif
151       mpn_random2 (s1, size);
152
153       dx[size+1] = 0x12345678;
154       dy[size+1] = 0x12345678;
155       dx[0] = 0x87654321;
156       dy[0] = 0x87654321;
157
158 #ifdef PRINT
159       mpn_print (s1, size);
160 #endif
161       t0 = cputime();
162       for (i = 0; i < TIMES; i++)
163         cyx = refmpn_lshift (dx+1, s1, size, cnt);
164       t = cputime() - t0;
165 #if TIMES != 1
166       printf ("refmpn_lshift: %5ldms (%.2f cycles/limb)\n",
167               t,
168               ((double) t * CLOCK) / (OPS * 1000.0));
169 #endif
170 #ifdef PRINT
171       printf ("%*lX ", (int) (2 * sizeof(mp_limb_t)), cyx); mpn_print (dx+1, size);
172 #endif
173
174       t0 = cputime();
175       for (i = 0; i < TIMES; i++)
176         cyy = mpn_lshift (dx+1, s1, size, cnt);
177       t = cputime() - t0;
178 #if TIMES != 1
179       printf ("mpn_lshift:  %5ldms (%.2f cycles/limb)\n",
180               t,
181               ((double) t * CLOCK) / (OPS * 1000.0));
182 #endif
183 #ifdef PRINT
184       printf ("%*lX ", (int) (2 * sizeof(mp_limb_t)), cyy); mpn_print (dx+1, size);
185 #endif
186
187 #ifndef NOCHECK
188       /* Put garbage in the destination.  */
189       for (i = 1; i <= size; i++)
190         {
191           dx[i] = 0x7654321;
192           dy[i] = 0x1234567;
193         }
194
195       cyx = refmpn_lshift (dx+1, s1, size, cnt);
196       cyy = mpn_lshift (dy+1, s1, size, cnt);
197
198       if (cyx != cyy || mpn_cmp (dx, dy, size+2) != 0
199           || dx[size+1] != 0x12345678 || dx[0] != 0x87654321)
200         {
201 #ifndef PRINT
202           printf ("%*lX ", (int) (2 * sizeof(mp_limb_t)), cyx);
203           mpn_print (dx+1, size);
204           printf ("%*lX ", (int) (2 * sizeof(mp_limb_t)), cyy);
205           mpn_print (dy+1, size);
206 #endif
207           abort();
208         }
209 #endif
210     }
211 }
212
213 mpn_print (mp_ptr p, mp_size_t size)
214 {
215   mp_size_t i;
216
217   for (i = size - 1; i >= 0; i--)
218     {
219       printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
220 #ifdef SPACE
221       if (i != 0)
222         printf (" ");
223 #endif
224     }
225   puts ("");
226 }