github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/devel/anymul_1.c (about) 1 /* 2 Copyright 1996-2002, 2004, 2006-2008 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 <string.h> 21 #include <stdio.h> 22 #include "gmp.h" 23 #include "gmp-impl.h" 24 #include "longlong.h" 25 #include "tests/tests.h" 26 27 #ifdef OPERATION_mul_1 28 #define func __gmpn_mul_1 29 #define reffunc refmpn_mul_1 30 #define funcname "mpn_mul_1" 31 #endif 32 33 #ifdef OPERATION_addmul_1 34 #define func __gmpn_addmul_1 35 #define reffunc refmpn_addmul_1 36 #define funcname "mpn_addmul_1" 37 #endif 38 39 #ifdef OPERATION_submul_1 40 #define func __gmpn_submul_1 41 #define reffunc refmpn_submul_1 42 #define funcname "mpn_submul_1" 43 #endif 44 45 #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux) 46 #include <time.h> 47 48 int 49 cputime () 50 { 51 if (CLOCKS_PER_SEC < 100000) 52 return clock () * 1000 / CLOCKS_PER_SEC; 53 return clock () / (CLOCKS_PER_SEC / 1000); 54 } 55 #else 56 #include <sys/types.h> 57 #include <sys/time.h> 58 #include <sys/resource.h> 59 60 int 61 cputime () 62 { 63 struct rusage rus; 64 65 getrusage (0, &rus); 66 return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000; 67 } 68 #endif 69 70 static void print_posneg (mp_limb_t); 71 static void mpn_print (mp_ptr, mp_size_t); 72 73 #define LXW ((int) (2 * sizeof (mp_limb_t))) 74 #define M * 1000000 75 76 #ifndef CLOCK 77 #error "Don't know CLOCK of your machine" 78 #endif 79 80 #ifndef OPS 81 #define OPS (CLOCK/5) 82 #endif 83 #ifndef SIZE 84 #define SIZE 496 85 #endif 86 #ifndef TIMES 87 #define TIMES OPS/(SIZE+1) 88 #endif 89 90 int 91 main (int argc, char **argv) 92 { 93 mp_ptr s1, ref, rp; 94 mp_limb_t cy_ref, cy_try; 95 int i; 96 long t0, t; 97 unsigned int test; 98 mp_limb_t xlimb; 99 mp_size_t size; 100 double cyc; 101 unsigned int ntests; 102 103 s1 = malloc (SIZE * sizeof (mp_limb_t)); 104 ref = malloc (SIZE * sizeof (mp_limb_t)); 105 rp = malloc ((SIZE + 2) * sizeof (mp_limb_t)); 106 rp++; 107 108 ntests = ~(unsigned) 0; 109 if (argc == 2) 110 ntests = strtol (argv[1], 0, 0); 111 112 for (test = 1; test <= ntests; test++) 113 { 114 #if TIMES == 1 && ! defined (PRINT) 115 if (test % (1 + 0x80000 / (SIZE + 20)) == 0) 116 { 117 printf ("\r%u", test); 118 fflush (stdout); 119 } 120 #endif 121 122 #ifdef PLAIN_RANDOM 123 #define MPN_RANDOM mpn_random 124 #else 125 #define MPN_RANDOM mpn_random2 126 #endif 127 128 #ifdef RANDOM 129 size = random () % SIZE + 1; 130 #else 131 size = SIZE; 132 #endif 133 134 rp[-1] = 0x87654321; 135 rp[size] = 0x12345678; 136 137 #ifdef FIXED_XLIMB 138 xlimb = FIXED_XLIMB; 139 #else 140 MPN_RANDOM (&xlimb, 1); 141 #endif 142 143 #if TIMES != 1 144 mpn_random (s1, size); 145 mpn_random (rp, size); 146 147 MPN_COPY (ref, rp, size); 148 t0 = cputime(); 149 for (i = 0; i < TIMES; i++) 150 func (ref, s1, size, xlimb); 151 t = cputime() - t0; 152 cyc = ((double) t * CLOCK) / (TIMES * size * 1000.0); 153 printf (funcname ": %5ldms (%.3f cycles/limb) [%.2f Gb/s]\n", 154 t, cyc, 155 CLOCK/cyc*GMP_LIMB_BITS*GMP_LIMB_BITS/1e9); 156 #endif 157 158 #ifndef NOCHECK 159 MPN_RANDOM (s1, size); 160 #ifdef ZERO 161 memset (rp, 0, size * sizeof *rp); 162 #else 163 MPN_RANDOM (rp, size); 164 #endif 165 #if defined (PRINT) || defined (XPRINT) 166 printf ("xlimb="); 167 mpn_print (&xlimb, 1); 168 #endif 169 #ifdef PRINT 170 #ifndef OPERATION_mul_1 171 printf ("%*s ", (int) (2 * sizeof(mp_limb_t)), ""); 172 mpn_print (rp, size); 173 #endif 174 printf ("%*s ", (int) (2 * sizeof(mp_limb_t)), ""); 175 mpn_print (s1, size); 176 #endif 177 178 MPN_COPY (ref, rp, size); 179 cy_ref = reffunc (ref, s1, size, xlimb); 180 cy_try = func (rp, s1, size, xlimb); 181 182 #ifdef PRINT 183 mpn_print (&cy_ref, 1); 184 mpn_print (ref, size); 185 mpn_print (&cy_try, 1); 186 mpn_print (rp, size); 187 #endif 188 189 if (cy_ref != cy_try || mpn_cmp (ref, rp, size) != 0 190 || rp[-1] != 0x87654321 || rp[size] != 0x12345678) 191 { 192 printf ("\n ref%*s try%*s diff\n", LXW - 3, "", 2 * LXW - 6, ""); 193 for (i = 0; i < size; i++) 194 { 195 printf ("%6d: ", i); 196 printf ("%0*llX ", LXW, (unsigned long long) ref[i]); 197 printf ("%0*llX ", LXW, (unsigned long long) rp[i]); 198 print_posneg (rp[i] - ref[i]); 199 printf ("\n"); 200 } 201 printf ("retval: "); 202 printf ("%0*llX ", LXW, (unsigned long long) cy_ref); 203 printf ("%0*llX ", LXW, (unsigned long long) cy_try); 204 print_posneg (cy_try - cy_ref); 205 printf ("\n"); 206 if (rp[-1] != 0x87654321) 207 printf ("clobbered at low end\n"); 208 if (rp[size] != 0x12345678) 209 printf ("clobbered at high end\n"); 210 printf ("TEST NUMBER %u\n", test); 211 abort(); 212 } 213 #endif 214 } 215 exit (0); 216 } 217 218 static void 219 print_posneg (mp_limb_t d) 220 { 221 char buf[LXW + 2]; 222 if (d == 0) 223 printf (" %*X", LXW, 0); 224 else if (-d < d) 225 { 226 sprintf (buf, "%llX", (unsigned long long) -d); 227 printf ("%*s-%s", LXW - (int) strlen (buf), "", buf); 228 } 229 else 230 { 231 sprintf (buf, "%llX", (unsigned long long) d); 232 printf ("%*s+%s", LXW - (int) strlen (buf), "", buf); 233 } 234 } 235 236 static void 237 mpn_print (mp_ptr p, mp_size_t size) 238 { 239 mp_size_t i; 240 241 for (i = size - 1; i >= 0; i--) 242 { 243 #ifdef _LONG_LONG_LIMB 244 printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)), 245 (unsigned long) (p[i] >> (GMP_LIMB_BITS/2)), 246 (int) (sizeof(mp_limb_t)), (unsigned long) (p[i])); 247 #else 248 printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]); 249 #endif 250 #ifdef SPACE 251 if (i != 0) 252 printf (" "); 253 #endif 254 } 255 puts (""); 256 }