1 /*
2 Copyright 1996-2004, 2009 Free Software Foundation, Inc.
3 
4 This file is part of the GNU MP Library test suite.
5 
6 The GNU MP Library test suite is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 3 of the License,
9 or (at your option) any later version.
10 
11 The GNU MP Library test suite is distributed in the hope that it will be
12 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
14 Public License for more details.
15 
16 You should have received a copy of the GNU General Public License along with
17 the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include "gmp.h"
22 #include "gmp-impl.h"
23 #include "tests.h"
24 
25 #ifdef OPERATION_and_n
26 #define func __gmpn_and_n
27 #define reffunc refmpn_and_n
28 #define funcname "mpn_and_n"
29 #endif
30 
31 #ifdef OPERATION_andn_n
32 #define func __gmpn_andn_n
33 #define reffunc refmpn_andn_n
34 #define funcname "mpn_andn_n"
35 #endif
36 
37 #ifdef OPERATION_nand_n
38 #define func __gmpn_nand_n
39 #define reffunc refmpn_nand_n
40 #define funcname "mpn_nand_n"
41 #endif
42 
43 #ifdef OPERATION_ior_n
44 #define func __gmpn_ior_n
45 #define reffunc refmpn_ior_n
46 #define funcname "mpn_ior_n"
47 #endif
48 
49 #ifdef OPERATION_iorn_n
50 #define func __gmpn_iorn_n
51 #define reffunc refmpn_iorn_n
52 #define funcname "mpn_iorn_n"
53 #endif
54 
55 #ifdef OPERATION_nior_n
56 #define func __gmpn_nior_n
57 #define reffunc refmpn_nior_n
58 #define funcname "mpn_nior_n"
59 #endif
60 
61 #ifdef OPERATION_xor_n
62 #define func __gmpn_xor_n
63 #define reffunc refmpn_xor_n
64 #define funcname "mpn_xor_n"
65 #endif
66 
67 #ifdef OPERATION_xnor_n
68 #define func __gmpn_xnor_n
69 #define reffunc refmpn_xnor_n
70 #define funcname "mpn_xnor_n"
71 #endif
72 
73 #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
74 #include <time.h>
75 
76 int
cputime()77 cputime ()
78 {
79   if (CLOCKS_PER_SEC < 100000)
80     return clock () * 1000 / CLOCKS_PER_SEC;
81   return clock () / (CLOCKS_PER_SEC / 1000);
82 }
83 #else
84 #include <sys/types.h>
85 #include <sys/time.h>
86 #include <sys/resource.h>
87 
88 int
cputime()89 cputime ()
90 {
91   struct rusage rus;
92 
93   getrusage (0, &rus);
94   return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
95 }
96 #endif
97 
98 static void mpn_print (mp_ptr, mp_size_t);
99 
100 #define M * 1000000
101 
102 #ifndef CLOCK
103 #error "Don't know CLOCK of your machine"
104 #endif
105 
106 #ifndef OPS
107 #define OPS (CLOCK/5)
108 #endif
109 #ifndef SIZE
110 #define SIZE 328
111 #endif
112 #ifndef TIMES
113 #define TIMES OPS/(SIZE+1)
114 #endif
115 
116 int
main(int argc,char ** argv)117 main (int argc, char **argv)
118 {
119   mp_ptr s1, s2, dx, dy;
120   int i;
121   long t0, t;
122   unsigned int test;
123   mp_size_t size;
124   unsigned int ntests;
125 
126   s1 = malloc (SIZE * sizeof (mp_limb_t));
127   s2 = malloc (SIZE * sizeof (mp_limb_t));
128   dx = malloc ((SIZE + 2) * sizeof (mp_limb_t));
129   dy = malloc ((SIZE + 2) * sizeof (mp_limb_t));
130 
131   ntests = ~(unsigned) 0;
132   if (argc == 2)
133     ntests = strtol (argv[1], 0, 0);
134 
135   for (test = 1; test <= ntests; test++)
136     {
137 #if TIMES == 1 && ! defined (PRINT)
138       if (test % (SIZE > 100000 ? 1 : 100000 / SIZE) == 0)
139 	{
140 	  printf ("\r%d", test);
141 	  fflush (stdout);
142 	}
143 #endif
144 
145 #ifdef RANDOM
146       size = random () % SIZE + 1;
147 #else
148       size = SIZE;
149 #endif
150 
151       dx[0] = 0x87654321;
152       dy[0] = 0x87654321;
153       dx[size+1] = 0x12345678;
154       dy[size+1] = 0x12345678;
155 
156 #if TIMES != 1
157       mpn_random (s1, size);
158       mpn_random (s2, size);
159 
160       t0 = cputime();
161       for (i = 0; i < TIMES; i++)
162 	func (dx+1, s1, s2, size);
163       t = cputime() - t0;
164       printf (funcname ":    %5ldms (%.3f cycles/limb)\n",
165 	      t, ((double) t * CLOCK) / (TIMES * size * 1000.0));
166 #endif
167 
168 #ifndef NOCHECK
169       mpn_random2 (s1, size);
170       mpn_random2 (s2, size);
171 
172 #ifdef PRINT
173       mpn_print (s1, size);
174       mpn_print (s2, size);
175 #endif
176 
177       /* Put garbage in the destination.  */
178       for (i = 0; i < size; i++)
179 	{
180 	  dx[i+1] = 0xdead;
181 	  dy[i+1] = 0xbeef;
182 	}
183 
184       reffunc (dx+1, s1, s2, size);
185       func (dy+1, s1, s2, size);
186 #ifdef PRINT
187       mpn_print (dx+1, size);
188       mpn_print (dy+1, size);
189 #endif
190       if (mpn_cmp (dx, dy, size+2) != 0
191 	  || dx[0] != 0x87654321 || dx[size+1] != 0x12345678)
192 	{
193 #ifndef PRINT
194 	  mpn_print (dx+1, size);
195 	  mpn_print (dy+1, size);
196 #endif
197 	  printf ("\n");
198 	  if (dy[0] != 0x87654321)
199 	    printf ("clobbered at low end\n");
200 	  if (dy[size+1] != 0x12345678)
201 	    printf ("clobbered at high end\n");
202 	  printf ("TEST NUMBER %u\n", test);
203 	  abort();
204 	}
205 #endif
206     }
207   exit (0);
208 }
209 
210 static void
mpn_print(mp_ptr p,mp_size_t size)211 mpn_print (mp_ptr p, mp_size_t size)
212 {
213   mp_size_t i;
214 
215   for (i = size - 1; i >= 0; i--)
216     {
217 #ifdef _LONG_LONG_LIMB
218       printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)),
219 	      (unsigned long) (p[i] >> (GMP_LIMB_BITS/2)),
220               (int) (sizeof(mp_limb_t)), (unsigned long) (p[i]));
221 #else
222       printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
223 #endif
224 #ifdef SPACE
225       if (i != 0)
226 	printf (" ");
227 #endif
228     }
229   puts ("");
230 }
231