Annotation of OpenXM_contrib/gmp/tests/devel/lshift.c, Revision 1.1
1.1 ! ohara 1: /*
! 2: Copyright 1996, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
! 3:
! 4: This file is part of the GNU MP Library.
! 5:
! 6: The GNU MP Library is free software; you can redistribute it and/or modify
! 7: it under the terms of the GNU Lesser General Public License as published by
! 8: the Free Software Foundation; either version 2.1 of the License, or (at your
! 9: option) any later version.
! 10:
! 11: The GNU MP Library is distributed in the hope that it will be useful, but
! 12: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 13: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
! 14: License for more details.
! 15:
! 16: You should have received a copy of the GNU Lesser General Public License
! 17: along with the GNU MP Library; see the file COPYING.LIB. If not, write to
! 18: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
! 19: MA 02111-1307, USA.
! 20: */
! 21:
! 22: #include <stdio.h>
! 23: #include "gmp.h"
! 24: #include "gmp-impl.h"
! 25:
! 26: #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
! 27: #include <time.h>
! 28:
! 29: int
! 30: cputime ()
! 31: {
! 32: if (CLOCKS_PER_SEC < 100000)
! 33: return clock () * 1000 / CLOCKS_PER_SEC;
! 34: return clock () / (CLOCKS_PER_SEC / 1000);
! 35: }
! 36: #else
! 37: #include <sys/types.h>
! 38: #include <sys/time.h>
! 39: #include <sys/resource.h>
! 40:
! 41: int
! 42: cputime ()
! 43: {
! 44: struct rusage rus;
! 45:
! 46: getrusage (0, &rus);
! 47: return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
! 48: }
! 49: #endif
! 50:
! 51: #define M * 1000000
! 52:
! 53: #ifndef CLOCK
! 54: #error "Don't know CLOCK of your machine"
! 55: #endif
! 56:
! 57: #ifndef OPS
! 58: #define OPS (CLOCK/5)
! 59: #endif
! 60: #ifndef SIZE
! 61: #define SIZE 496
! 62: #endif
! 63: #ifndef TIMES
! 64: #define TIMES OPS/SIZE
! 65: #else
! 66: #undef OPS
! 67: #define OPS (SIZE*TIMES)
! 68: #endif
! 69:
! 70: mp_limb_t
! 71: refmpn_lshift (wp, up, usize, cnt)
! 72: register mp_ptr wp;
! 73: register mp_srcptr up;
! 74: mp_size_t usize;
! 75: register unsigned int cnt;
! 76: {
! 77: register mp_limb_t high_limb, low_limb;
! 78: register unsigned sh_1, sh_2;
! 79: register mp_size_t i;
! 80: mp_limb_t retval;
! 81:
! 82: #ifdef DEBUG
! 83: if (usize == 0 || cnt == 0)
! 84: abort ();
! 85: #endif
! 86:
! 87: sh_1 = cnt;
! 88: #if 0
! 89: if (sh_1 == 0)
! 90: {
! 91: if (wp != up)
! 92: {
! 93: /* Copy from high end to low end, to allow specified input/output
! 94: overlapping. */
! 95: for (i = usize - 1; i >= 0; i--)
! 96: wp[i] = up[i];
! 97: }
! 98: return 0;
! 99: }
! 100: #endif
! 101:
! 102: wp += 1;
! 103: sh_2 = BITS_PER_MP_LIMB - sh_1;
! 104: i = usize - 1;
! 105: low_limb = up[i];
! 106: retval = low_limb >> sh_2;
! 107: high_limb = low_limb;
! 108: while (--i >= 0)
! 109: {
! 110: low_limb = up[i];
! 111: wp[i] = (high_limb << sh_1) | (low_limb >> sh_2);
! 112: high_limb = low_limb;
! 113: }
! 114: wp[i] = high_limb << sh_1;
! 115:
! 116: return retval;
! 117: }
! 118:
! 119: #ifndef CNT
! 120: #define CNT 4
! 121: #endif
! 122:
! 123: main (argc, argv)
! 124: int argc;
! 125: char **argv;
! 126: {
! 127: mp_limb_t s1[SIZE];
! 128: mp_limb_t dx[SIZE+2];
! 129: mp_limb_t dy[SIZE+2];
! 130: mp_limb_t cyx, cyy;
! 131: int i;
! 132: long t0, t;
! 133: int test;
! 134: int cnt = CNT;
! 135: mp_size_t size;
! 136:
! 137: for (test = 0; ; test++)
! 138: {
! 139: #if TIMES == 1 && ! defined (PRINT)
! 140: if (test % (SIZE > 10000 ? 1 : 10000 / SIZE) == 0)
! 141: {
! 142: printf ("\r%d", test);
! 143: fflush (stdout);
! 144: }
! 145: #endif
! 146:
! 147: #if TIMES == 1
! 148: cnt = random () % (BITS_PER_MP_LIMB - 1) + 1;
! 149: #endif
! 150:
! 151: #ifdef RANDOM
! 152: size = random () % SIZE + 1;
! 153: #else
! 154: size = SIZE;
! 155: #endif
! 156: mpn_random2 (s1, size);
! 157:
! 158: dx[size+1] = 0x12345678;
! 159: dy[size+1] = 0x12345678;
! 160: dx[0] = 0x87654321;
! 161: dy[0] = 0x87654321;
! 162:
! 163: #if TIMES != 1
! 164: mpn_random (s1, size);
! 165:
! 166: #ifndef NOCHECK
! 167: t0 = cputime();
! 168: for (i = 0; i < TIMES; i++)
! 169: refmpn_lshift (dx+1, s1, size, cnt);
! 170: t = cputime() - t0;
! 171: printf ("refmpn_lshift: %5ldms (%.2f cycles/limb)\n",
! 172: t, ((double) t * CLOCK) / (OPS * 1000.0));
! 173: #endif
! 174:
! 175: t0 = cputime();
! 176: for (i = 0; i < TIMES; i++)
! 177: mpn_lshift (dx+1, s1, size, cnt);
! 178: t = cputime() - t0;
! 179: printf ("mpn_lshift: %5ldms (%.2f cycles/limb)\n",
! 180: t, ((double) t * CLOCK) / (OPS * 1000.0));
! 181: #endif
! 182:
! 183: #ifndef NOCHECK
! 184: mpn_random2 (s1, size);
! 185:
! 186: #ifdef PRINT
! 187: printf ("%-*d ", (int) (2 * sizeof(mp_limb_t)), cnt); mpn_print (s1, size);
! 188: #endif
! 189:
! 190: /* Put garbage in the destination. */
! 191: for (i = 0; i < size; i++)
! 192: {
! 193: dx[i+1] = 0xdead;
! 194: dy[i+1] = 0xbeef;
! 195: }
! 196:
! 197: cyx = refmpn_lshift (dx+1, s1, size, cnt);
! 198: cyy = mpn_lshift (dy+1, s1, size, cnt);
! 199: #ifdef PRINT
! 200: printf ("%*lX ", (int) (2 * sizeof(mp_limb_t)), cyx);
! 201: mpn_print (dx+1, size);
! 202: printf ("%*lX ", (int) (2 * sizeof(mp_limb_t)), cyy);
! 203: mpn_print (dy+1, size);
! 204: #endif
! 205: if (cyx != cyy || mpn_cmp (dx, dy, size+2) != 0
! 206: || dx[0] != 0x87654321 || dx[size+1] != 0x12345678)
! 207: {
! 208: #ifndef PRINT
! 209: printf ("%*lX ", (int) (2 * sizeof(mp_limb_t)), cyx);
! 210: mpn_print (dx+1, size);
! 211: printf ("%*lX ", (int) (2 * sizeof(mp_limb_t)), cyy);
! 212: mpn_print (dy+1, size);
! 213: #endif
! 214: printf ("\n");
! 215: if (dy[0] != 0x87654321)
! 216: printf ("clobbered at low end\n");
! 217: if (dy[size+1] != 0x12345678)
! 218: printf ("clobbered at high end\n");
! 219: printf ("TEST NUMBER %u\n", test);
! 220: abort();
! 221: }
! 222: #endif
! 223: }
! 224: }
! 225:
! 226: mpn_print (mp_ptr p, mp_size_t size)
! 227: {
! 228: mp_size_t i;
! 229:
! 230: for (i = size - 1; i >= 0; i--)
! 231: {
! 232: #ifdef _LONG_LONG_LIMB
! 233: printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)),
! 234: (unsigned long) (p[i] >> (BITS_PER_MP_LIMB/2)),
! 235: (int) (sizeof(mp_limb_t)), (unsigned long) (p[i]));
! 236: #else
! 237: printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
! 238: #endif
! 239: #ifdef SPACE
! 240: if (i != 0)
! 241: printf (" ");
! 242: #endif
! 243: }
! 244: puts ("");
! 245: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>