Annotation of OpenXM_contrib/gmp/tests/devel/sub_n.c, Revision 1.1.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:
27: #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
28: #include <time.h>
29:
30: int
31: cputime ()
32: {
33: if (CLOCKS_PER_SEC < 100000)
34: return clock () * 1000 / CLOCKS_PER_SEC;
35: return clock () / (CLOCKS_PER_SEC / 1000);
36: }
37: #else
38: #include <sys/types.h>
39: #include <sys/time.h>
40: #include <sys/resource.h>
41:
42: int
43: cputime ()
44: {
45: struct rusage rus;
46:
47: getrusage (0, &rus);
48: return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
49: }
50: #endif
51:
52: #define M * 1000000
53:
54: #ifndef CLOCK
55: #error "Don't know CLOCK of your machine"
56: #endif
57:
58: #ifndef OPS
59: #define OPS (CLOCK/5)
60: #endif
61: #ifndef SIZE
62: #define SIZE 328
63: #endif
64: #ifndef TIMES
65: #define TIMES OPS/SIZE
66: #else
67: #undef OPS
68: #define OPS (SIZE*TIMES)
69: #endif
70:
71:
72: mp_limb_t
73: refmpn_sub_n (mp_ptr res_ptr,
74: mp_srcptr s1_ptr, mp_srcptr s2_ptr, mp_size_t size)
75: {
76: register mp_limb_t x, y, cy;
77: register mp_size_t j;
78:
79: /* The loop counter and index J goes from -SIZE to -1. This way
80: the loop becomes faster. */
81: j = -size;
82:
83: /* Offset the base pointers to compensate for the negative indices. */
84: s1_ptr -= j;
85: s2_ptr -= j;
86: res_ptr -= j;
87:
88: cy = 0;
89: do
90: {
91: y = s2_ptr[j];
92: x = s1_ptr[j];
93: y = (y + cy) & GMP_NUMB_MASK; /* add previous carry to subtrahend */
94: cy = (y < cy); /* get out carry from that addition */
95: y = (x - y) & GMP_NUMB_MASK; /* main subtract */
96: cy = (y > x) + cy; /* get out carry from the subtract, combine */
97: res_ptr[j] = y;
98: }
99: while (++j != 0);
100:
101: return cy;
102: }
103:
104: main (argc, argv)
105: int argc;
106: char **argv;
107: {
108: mp_limb_t s1[SIZE];
109: mp_limb_t s2[SIZE];
110: mp_limb_t dx[SIZE+2];
111: mp_limb_t dy[SIZE+2];
112: int cyx, cyy;
113: int i;
114: long t0, t;
115: unsigned int test;
116: mp_size_t size;
117: unsigned int ntests;
118:
119: ntests = ~(unsigned) 0;
120: if (argc == 2)
121: ntests = strtol (argv[1], 0, 0);
122:
123: for (test = 1; test <= ntests; test++)
124: {
125: #if TIMES == 1 && ! defined (PRINT)
126: if (test % (SIZE > 10000 ? 1 : 10000 / SIZE) == 0)
127: {
128: printf ("\r%d", test);
129: fflush (stdout);
130: }
131: #endif
132:
133: #ifdef RANDOM
134: size = random () % SIZE + 1;
135: #else
136: size = SIZE;
137: #endif
138:
139: dx[0] = 0x87654321;
140: dy[0] = 0x87654321;
141: dx[size+1] = 0x12345678;
142: dy[size+1] = 0x12345678;
143:
144: #if TIMES != 1
145: mpn_random (s1, size);
146: mpn_random (s2, size);
147:
148: #ifndef NOCHECK
149: t0 = cputime();
150: for (i = 0; i < TIMES; i++)
151: refmpn_sub_n (dx+1, s1, s2, size);
152: t = cputime() - t0;
153: printf ("refmpn_sub_n: %5ldms (%.2f cycles/limb)\n",
154: t, ((double) t * CLOCK) / (OPS * 1000.0));
155: #endif
156:
157: t0 = cputime();
158: for (i = 0; i < TIMES; i++)
159: mpn_sub_n (dx+1, s1, s2, size);
160: t = cputime() - t0;
161: printf ("mpn_sub_n: %5ldms (%.2f cycles/limb)\n",
162: t, ((double) t * CLOCK) / (OPS * 1000.0));
163: #endif
164:
165: #ifndef NOCHECK
166: mpn_random2 (s1, size);
167: mpn_random2 (s2, size);
168:
169: #ifdef PRINT
170: mpn_print (s1, size);
171: mpn_print (s2, size);
172: #endif
173:
174: /* Put garbage in the destination. */
175: for (i = 0; i < size; i++)
176: {
177: dx[i+1] = 0xdead;
178: dy[i+1] = 0xbeef;
179: }
180:
181: cyx = refmpn_sub_n (dx+1, s1, s2, size);
182: cyy = mpn_sub_n (dy+1, s1, s2, size);
183: #ifdef PRINT
184: printf ("%d ", cyx); mpn_print (dx+1, size);
185: printf ("%d ", cyy); mpn_print (dy+1, size);
186: #endif
187: if (cyx != cyy || mpn_cmp (dx, dy, size+2) != 0
188: || dx[0] != 0x87654321 || dx[size+1] != 0x12345678)
189: {
190: #ifndef PRINT
191: printf ("%d ", cyx); mpn_print (dx+1, size);
192: printf ("%d ", cyy); mpn_print (dy+1, size);
193: #endif
194: printf ("\n");
195: if (dy[0] != 0x87654321)
196: printf ("clobbered at low end\n");
197: if (dy[size+1] != 0x12345678)
198: printf ("clobbered at high end\n");
199: printf ("TEST NUMBER %u\n", test);
200: abort();
201: }
202: #endif
203: }
204: }
205:
206: mpn_print (mp_ptr p, mp_size_t size)
207: {
208: mp_size_t i;
209:
210: for (i = size - 1; i >= 0; i--)
211: {
212: #ifdef _LONG_LONG_LIMB
213: printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)),
214: (unsigned long) (p[i] >> (BITS_PER_MP_LIMB/2)),
215: (int) (sizeof(mp_limb_t)), (unsigned long) (p[i]));
216: #else
217: printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
218: #endif
219: #ifdef SPACE
220: if (i != 0)
221: printf (" ");
222: #endif
223: }
224: puts ("");
225: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>