Annotation of OpenXM_contrib/gmp/tests/devel/addmul_1.c, Revision 1.1
1.1 ! ohara 1: /*
! 2: Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation,
! 3: Inc.
! 4:
! 5: This file is part of the GNU MP Library.
! 6:
! 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 2.1 of the License, or (at your
! 10: option) any later version.
! 11:
! 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.
! 16:
! 17: You should have received a copy of the GNU Lesser General Public License
! 18: along with the GNU MP Library; see the file COPYING.LIB. If not, write to
! 19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
! 20: MA 02111-1307, USA.
! 21: */
! 22:
! 23: #include <stdio.h>
! 24: #include "gmp.h"
! 25: #include "gmp-impl.h"
! 26: #include "longlong.h"
! 27:
! 28: #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
! 29: #include <time.h>
! 30:
! 31: int
! 32: cputime ()
! 33: {
! 34: if (CLOCKS_PER_SEC < 100000)
! 35: return clock () * 1000 / CLOCKS_PER_SEC;
! 36: return clock () / (CLOCKS_PER_SEC / 1000);
! 37: }
! 38: #else
! 39: #include <sys/types.h>
! 40: #include <sys/time.h>
! 41: #include <sys/resource.h>
! 42:
! 43: int
! 44: cputime ()
! 45: {
! 46: struct rusage rus;
! 47:
! 48: getrusage (0, &rus);
! 49: return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
! 50: }
! 51: #endif
! 52:
! 53: #define M * 1000000
! 54:
! 55: #ifndef CLOCK
! 56: #error "Don't know CLOCK of your machine"
! 57: #endif
! 58:
! 59: #ifndef OPS
! 60: #define OPS (CLOCK/5)
! 61: #endif
! 62: #ifndef SIZE
! 63: #define SIZE 496
! 64: #endif
! 65: #ifndef TIMES
! 66: #define TIMES OPS/SIZE
! 67: #else
! 68: #undef OPS
! 69: #define OPS (SIZE*TIMES)
! 70: #endif
! 71:
! 72: mp_limb_t
! 73: refmpn_addmul_1 (res_ptr, s1_ptr, s1_size, s2_limb)
! 74: register mp_ptr res_ptr;
! 75: register mp_srcptr s1_ptr;
! 76: mp_size_t s1_size;
! 77: register mp_limb_t s2_limb;
! 78: {
! 79: register mp_limb_t cy_limb;
! 80: register mp_size_t j;
! 81: register mp_limb_t prod_high, prod_low;
! 82: register mp_limb_t x;
! 83:
! 84: /* The loop counter and index J goes from -S1_SIZE to -1. This way
! 85: the loop becomes faster. */
! 86: j = -s1_size;
! 87:
! 88: /* Offset the base pointers to compensate for the negative indices. */
! 89: res_ptr -= j;
! 90: s1_ptr -= j;
! 91:
! 92: cy_limb = 0;
! 93: s2_limb <<= GMP_NAIL_BITS;
! 94: do
! 95: {
! 96: umul_ppmm (prod_high, prod_low, s1_ptr[j], s2_limb);
! 97: prod_low >>= GMP_NAIL_BITS;
! 98:
! 99: prod_low = (prod_low + cy_limb) & GMP_NUMB_MASK;
! 100: cy_limb = (prod_low < cy_limb) + prod_high;
! 101:
! 102: x = res_ptr[j];
! 103: prod_low = (x + prod_low) & GMP_NUMB_MASK;
! 104: cy_limb += (prod_low < x);
! 105: res_ptr[j] = prod_low;
! 106: }
! 107: while (++j != 0);
! 108:
! 109: return cy_limb;
! 110: }
! 111:
! 112: main (argc, argv)
! 113: int argc;
! 114: char **argv;
! 115: {
! 116: mp_limb_t s1[SIZE];
! 117: mp_limb_t dx[SIZE+2];
! 118: mp_limb_t dy[SIZE+2];
! 119: mp_limb_t cyx, cyy;
! 120: int i;
! 121: long t0, t;
! 122: unsigned int test;
! 123: mp_limb_t xlimb;
! 124: mp_size_t size;
! 125: double cyc;
! 126: unsigned int ntests;
! 127:
! 128: ntests = ~(unsigned) 0;
! 129: if (argc == 2)
! 130: ntests = strtol (argv[1], 0, 0);
! 131:
! 132: for (test = 1; test <= ntests; test++)
! 133: {
! 134: #if TIMES == 1 && ! defined (PRINT)
! 135: if (test % (SIZE > 10000 ? 1 : 10000 / SIZE) == 0)
! 136: {
! 137: printf ("\r%u", test);
! 138: fflush (stdout);
! 139: }
! 140: #endif
! 141:
! 142: #ifdef RANDOM
! 143: size = random () % SIZE + 1;
! 144: #else
! 145: size = SIZE;
! 146: #endif
! 147:
! 148: dy[size+1] = 0x12345678;
! 149: dy[0] = 0x87654321;
! 150:
! 151: #ifdef FIXED_XLIMB
! 152: xlimb = FIXED_XLIMB;
! 153: #else
! 154: mpn_random2 (&xlimb, 1);
! 155: #endif
! 156:
! 157: #if TIMES != 1
! 158: mpn_random (s1, size);
! 159: mpn_random (dy+1, size);
! 160:
! 161: #ifndef NOCHECK
! 162: MPN_COPY (dx, dy, size+2);
! 163: t0 = cputime();
! 164: for (i = 0; i < TIMES; i++)
! 165: refmpn_addmul_1 (dx+1, s1, size, xlimb);
! 166: t = cputime() - t0;
! 167: cyc = ((double) t * CLOCK) / (OPS * 1000.0);
! 168: printf ("refmpn_addmul_1: %5ldms (%.2f cycles/limb) [%.2f Gb/s]\n",
! 169: t, cyc,
! 170: CLOCK/cyc*BITS_PER_MP_LIMB*BITS_PER_MP_LIMB/1e9);
! 171: #endif
! 172:
! 173: MPN_COPY (dx, dy, size+2);
! 174: t0 = cputime();
! 175: for (i = 0; i < TIMES; i++)
! 176: mpn_addmul_1 (dx+1, s1, size, xlimb);
! 177: t = cputime() - t0;
! 178: cyc = ((double) t * CLOCK) / (OPS * 1000.0);
! 179: printf ("mpn_addmul_1: %5ldms (%.2f cycles/limb) [%.2f Gb/s]\n",
! 180: t, cyc,
! 181: CLOCK/cyc*BITS_PER_MP_LIMB*BITS_PER_MP_LIMB/1e9);
! 182: #endif
! 183:
! 184: mpn_random2 (s1, size);
! 185: mpn_random2 (dy+1, size);
! 186: #if defined (PRINT) || defined (XPRINT)
! 187: printf ("xlimb=");
! 188: mpn_print (&xlimb, 1);
! 189: #endif
! 190: #ifdef PRINT
! 191: printf ("%*s ", (int) (2 * sizeof(mp_limb_t)), "");
! 192: mpn_print (dy+1, size);
! 193: printf ("%*s ", (int) (2 * sizeof(mp_limb_t)), "");
! 194: mpn_print (s1, size);
! 195: #endif
! 196:
! 197: MPN_COPY (dx, dy, size+2);
! 198: cyx = refmpn_addmul_1 (dx+1, s1, size, xlimb);
! 199: cyy = mpn_addmul_1 (dy+1, s1, size, xlimb);
! 200:
! 201: #ifdef 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:
! 208: #ifndef NOCHECK
! 209: if (cyx != cyy || mpn_cmp (dx, dy, size+2) != 0
! 210: || dx[size+1] != 0x12345678 || dx[0] != 0x87654321)
! 211: {
! 212: printf ("\n");
! 213: #ifndef PRINT
! 214: printf ("%*lX ", (int) (2 * sizeof(mp_limb_t)), cyx);
! 215: mpn_print (dx+1, size);
! 216: printf ("%*lX ", (int) (2 * sizeof(mp_limb_t)), cyy);
! 217: mpn_print (dy+1, size);
! 218: #endif
! 219: printf ("%*s ", (int) (2 * sizeof(mp_limb_t)), "DIFF:");
! 220: for (i = size; i != 0; i--)
! 221: {
! 222: mp_limb_t diff = dy[i] ^ dx[i];
! 223: if (diff != 0)
! 224: printf ("%*lX", (int) (2 * sizeof(mp_limb_t)), diff);
! 225: else
! 226: printf ("%*s", (int) (2 * sizeof(mp_limb_t)), "");
! 227: #ifdef SPACE
! 228: if (i != 0)
! 229: printf (" ");
! 230: #endif
! 231: }
! 232: printf ("\n");
! 233: if (dy[0] != 0x87654321)
! 234: printf ("clobbered at low end\n");
! 235: if (dy[size+1] != 0x12345678)
! 236: printf ("clobbered at high end\n");
! 237: printf ("TEST NUMBER %u\n", test);
! 238: abort();
! 239: }
! 240: #endif
! 241: #ifdef ONE
! 242: return 0;
! 243: #endif
! 244: }
! 245: }
! 246:
! 247: mpn_print (mp_ptr p, mp_size_t size)
! 248: {
! 249: mp_size_t i;
! 250:
! 251: for (i = size - 1; i >= 0; i--)
! 252: {
! 253: #ifdef _LONG_LONG_LIMB
! 254: printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)),
! 255: (unsigned long) (p[i] >> (BITS_PER_MP_LIMB/2)),
! 256: (int) (sizeof(mp_limb_t)), (unsigned long) (p[i]));
! 257: #else
! 258: printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
! 259: #endif
! 260: #ifdef SPACE
! 261: if (i != 0)
! 262: printf (" ");
! 263: #endif
! 264: }
! 265: puts ("");
! 266: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>