Convert to keyserv, telnetd and telnet to libcrypto's BIGNUM
[dragonfly.git] / contrib / libgmp / mpn / tests / sub_n.c
1 #include <stdio.h>
2 #include "gmp.h"
3 #include "gmp-impl.h"
4
5 #ifndef USG
6 #include <sys/time.h>
7 #include <sys/resource.h>
8
9 unsigned long
10 cputime ()
11 {
12     struct rusage rus;
13
14     getrusage (0, &rus);
15     return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
16 }
17 #else
18 #include <time.h>
19
20 #ifndef CLOCKS_PER_SEC
21 #define CLOCKS_PER_SEC 1000000
22 #endif
23
24 #if CLOCKS_PER_SEC >= 10000
25 #define CLOCK_TO_MILLISEC(cl) ((cl) / (CLOCKS_PER_SEC / 1000))
26 #else
27 #define CLOCK_TO_MILLISEC(cl) ((cl) * 1000 / CLOCKS_PER_SEC)
28 #endif
29
30 unsigned long
31 cputime ()
32 {
33   return CLOCK_TO_MILLISEC (clock ());
34 }
35 #endif
36
37 #define M * 1000000
38
39 #ifndef CLOCK
40 #if defined (__m88k__)
41 #define CLOCK 20 M
42 #elif defined (__i386__)
43 #define CLOCK (16.666667 M)
44 #elif defined (__m68k__)
45 #define CLOCK (20 M)
46 #elif defined (_IBMR2)
47 #define CLOCK (25 M)
48 #elif defined (__sparc__)
49 #define CLOCK (20 M)
50 #elif defined (__sun__)
51 #define CLOCK (20 M)
52 #elif defined (__mips)
53 #define CLOCK (40 M)
54 #elif defined (__hppa__)
55 #define CLOCK (50 M)
56 #elif defined (__alpha)
57 #define CLOCK (133 M)
58 #else
59 #error "Don't know CLOCK of your machine"
60 #endif
61 #endif
62
63 #ifndef OPS
64 #define OPS 10000000
65 #endif
66 #ifndef SIZE
67 #define SIZE 328
68 #endif
69 #ifndef TIMES
70 #define TIMES OPS/SIZE
71 #else
72 #undef OPS
73 #define OPS (SIZE*TIMES)
74 #endif
75
76
77 mp_limb_t
78 #if __STDC__
79 refmpn_sub_n (mp_ptr res_ptr,
80                mp_srcptr s1_ptr, mp_srcptr s2_ptr, mp_size_t size)
81 #else
82 refmpn_sub_n (res_ptr, s1_ptr, s2_ptr, size)
83      register mp_ptr res_ptr;
84      register mp_srcptr s1_ptr;
85      register mp_srcptr s2_ptr;
86      mp_size_t size;
87 #endif
88 {
89   register mp_limb_t x, y, cy;
90   register mp_size_t j;
91
92   /* The loop counter and index J goes from -SIZE to -1.  This way
93      the loop becomes faster.  */
94   j = -size;
95
96   /* Offset the base pointers to compensate for the negative indices.  */
97   s1_ptr -= j;
98   s2_ptr -= j;
99   res_ptr -= j;
100
101   cy = 0;
102   do
103     {
104       y = s2_ptr[j];
105       x = s1_ptr[j];
106       y += cy;                  /* add previous carry to subtrahend */
107       cy = (y < cy);            /* get out carry from that addition */
108       y = x - y;                /* main subtract */
109       cy = (y > x) + cy;        /* get out carry from the subtract, combine */
110       res_ptr[j] = y;
111     }
112   while (++j != 0);
113
114   return cy;
115 }
116
117 main (argc, argv)
118      int argc;
119      char **argv;
120 {
121   mp_limb_t s1[SIZE];
122   mp_limb_t s2[SIZE];
123   mp_limb_t dx[SIZE+1];
124   mp_limb_t dy[SIZE+1];
125   int cyx, cyy;
126   int i;
127   long t0, t;
128   int test;
129   mp_size_t size;
130
131   for (test = 0; ; test++)
132     {
133 #ifdef RANDOM
134       size = (random () % SIZE + 1);
135 #else
136       size = SIZE;
137 #endif
138
139       mpn_random2 (s1, size);
140       mpn_random2 (s2, size);
141
142       dx[size] = 0x12345678;
143       dy[size] = 0x12345678;
144
145 #ifdef PRINT
146       mpn_print (s1, size);
147       mpn_print (s2, size);
148 #endif
149       t0 = cputime();
150       for (i = 0; i < TIMES; i++)
151         cyx = refmpn_sub_n (dx, s1, s2, size);
152       t = cputime() - t0;
153 #if TIMES != 1
154       printf ("refmpn_sub_n:   %ldms (%.2f cycles/limb)\n",
155               t,
156               ((double) t * CLOCK) / (OPS * 1000.0));
157 #endif
158 #ifdef PRINT
159       printf ("%d ", cyx); mpn_print (dx, size);
160 #endif
161
162       t0 = cputime();
163       for (i = 0; i < TIMES; i++)
164         cyx = mpn_sub_n (dx, s1, s2, size);
165       t = cputime() - t0;
166 #if TIMES != 1
167       printf ("mpn_sub_n:   %ldms (%.2f cycles/limb)\n",
168               t,
169               ((double) t * CLOCK) / (OPS * 1000.0));
170 #endif
171 #ifdef PRINT
172       printf ("%d ", cyx); mpn_print (dx, size);
173 #endif
174
175 #ifndef NOCHECK
176       /* Put garbage in the destination.  */
177       for (i = 0; i < size; i++)
178         {
179           dx[i] = 0x7654321;
180           dy[i] = 0x1234567;
181         }
182
183       cyx = refmpn_sub_n (dx, s1, s2, size);
184       cyy = mpn_sub_n (dy, s1, s2, size);
185       if (cyx != cyy || mpn_cmp (dx, dy, size) != 0
186           || dx[size] != 0x12345678 || dy[size] != 0x12345678)
187         {
188 #ifndef PRINT
189           printf ("%d ", cyx); mpn_print (dx, size);
190           printf ("%d ", cyy); mpn_print (dy, size);
191 #endif
192           abort();
193         }
194 #endif
195     }
196 }
197
198 mpn_print (mp_ptr p, mp_size_t size)
199 {
200   mp_size_t i;
201
202   for (i = size - 1; i >= 0; i--)
203     {
204       printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
205 #ifdef SPACE
206       if (i != 0)
207         printf (" ");
208 #endif
209     }
210   puts ("");
211 }