2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2006, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
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 Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 #ifdef OPERATION_mul_1
29 #define func __gmpn_mul_1
30 #define reffunc refmpn_mul_1
31 #define funcname "mpn_mul_1"
34 #ifdef OPERATION_addmul_1
35 #define func __gmpn_addmul_1
36 #define reffunc refmpn_addmul_1
37 #define funcname "mpn_addmul_1"
40 #ifdef OPERATION_submul_1
41 #define func __gmpn_submul_1
42 #define reffunc refmpn_submul_1
43 #define funcname "mpn_submul_1"
46 #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
52 if (CLOCKS_PER_SEC < 100000)
53 return clock () * 1000 / CLOCKS_PER_SEC;
54 return clock () / (CLOCKS_PER_SEC / 1000);
57 #include <sys/types.h>
59 #include <sys/resource.h>
67 return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
71 static void print_posneg (mp_limb_t);
72 static void mpn_print (mp_ptr, mp_size_t);
74 #define LXW ((int) (2 * sizeof (mp_limb_t)))
78 #error "Don't know CLOCK of your machine"
88 #define TIMES OPS/(SIZE+1)
92 main (int argc, char **argv)
95 mp_limb_t cy_ref, cy_try;
104 s1 = malloc (SIZE * sizeof (mp_limb_t));
105 ref = malloc (SIZE * sizeof (mp_limb_t));
106 rp = malloc ((SIZE + 2) * sizeof (mp_limb_t));
109 ntests = ~(unsigned) 0;
111 ntests = strtol (argv[1], 0, 0);
113 for (test = 1; test <= ntests; test++)
115 #if TIMES == 1 && ! defined (PRINT)
116 if (test % (1 + 0x80000 / (SIZE + 20)) == 0)
118 printf ("\r%u", test);
124 size = random () % SIZE + 1;
130 rp[size] = 0x12345678;
135 mpn_random2 (&xlimb, 1);
139 mpn_random (s1, size);
140 mpn_random (rp, size);
142 MPN_COPY (ref, rp, size);
144 for (i = 0; i < TIMES; i++)
145 func (ref, s1, size, xlimb);
147 cyc = ((double) t * CLOCK) / (TIMES * size * 1000.0);
148 printf (funcname ": %5ldms (%.3f cycles/limb) [%.2f Gb/s]\n",
150 CLOCK/cyc*GMP_LIMB_BITS*GMP_LIMB_BITS/1e9);
154 mpn_random2 (s1, size);
156 memset (rp, 0, size * sizeof *rp);
158 mpn_random2 (rp, size);
160 #if defined (PRINT) || defined (XPRINT)
162 mpn_print (&xlimb, 1);
165 #ifndef OPERATION_mul_1
166 printf ("%*s ", (int) (2 * sizeof(mp_limb_t)), "");
167 mpn_print (rp, size);
169 printf ("%*s ", (int) (2 * sizeof(mp_limb_t)), "");
170 mpn_print (s1, size);
173 MPN_COPY (ref, rp, size);
174 cy_ref = reffunc (ref, s1, size, xlimb);
175 cy_try = func (rp, s1, size, xlimb);
178 mpn_print (&cy_ref, 1);
179 mpn_print (ref, size);
180 mpn_print (&cy_try, 1);
181 mpn_print (rp, size);
184 if (cy_ref != cy_try || mpn_cmp (ref, rp, size) != 0
185 || rp[-1] != 0x87654321 || rp[size] != 0x12345678)
187 printf ("\n ref%*s try%*s diff\n", LXW - 3, "", 2 * LXW - 6, "");
188 for (i = 0; i < size; i++)
191 printf ("%0*llX ", LXW, (unsigned long long) ref[i]);
192 printf ("%0*llX ", LXW, (unsigned long long) rp[i]);
193 print_posneg (rp[i] - ref[i]);
197 printf ("%0*llX ", LXW, (unsigned long long) cy_ref);
198 printf ("%0*llX ", LXW, (unsigned long long) cy_try);
199 print_posneg (cy_try - cy_ref);
201 if (rp[-1] != 0x87654321)
202 printf ("clobbered at low end\n");
203 if (rp[size] != 0x12345678)
204 printf ("clobbered at high end\n");
205 printf ("TEST NUMBER %u\n", test);
214 print_posneg (mp_limb_t d)
218 printf (" %*X", LXW, 0);
221 sprintf (buf, "%llX", (unsigned long long) -d);
222 printf ("%*s-%s", LXW - (int) strlen (buf), "", buf);
226 sprintf (buf, "%llX", (unsigned long long) d);
227 printf ("%*s+%s", LXW - (int) strlen (buf), "", buf);
232 mpn_print (mp_ptr p, mp_size_t size)
236 for (i = size - 1; i >= 0; i--)
238 #ifdef _LONG_LONG_LIMB
239 printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)),
240 (unsigned long) (p[i] >> (GMP_LIMB_BITS/2)),
241 (int) (sizeof(mp_limb_t)), (unsigned long) (p[i]));
243 printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);