~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/tests/devel/sub_n.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright 1996, 1997, 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
#if defined (__m88k__)
 
55
#define CLOCK 20 M
 
56
#elif defined (__i386__)
 
57
#define CLOCK (16666667)
 
58
#elif defined (__m68k__)
 
59
#define CLOCK (20 M)
 
60
#elif defined (_IBMR2)
 
61
#define CLOCK (25 M)
 
62
#elif defined (__sparc__)
 
63
#define CLOCK (20 M)
 
64
#elif defined (__sun__)
 
65
#define CLOCK (20 M)
 
66
#elif defined (__mips)
 
67
#define CLOCK (40 M)
 
68
#elif defined (__hppa__)
 
69
#define CLOCK (50 M)
 
70
#elif defined (__alpha)
 
71
#define CLOCK (133 M)
 
72
#else
 
73
#error "Don't know CLOCK of your machine"
 
74
#endif
 
75
#endif
 
76
 
 
77
#ifndef OPS
 
78
#define OPS (CLOCK/5)
 
79
#endif
 
80
#ifndef SIZE
 
81
#define SIZE 328
 
82
#endif
 
83
#ifndef TIMES
 
84
#define TIMES OPS/SIZE
 
85
#else
 
86
#undef OPS
 
87
#define OPS (SIZE*TIMES)
 
88
#endif
 
89
 
 
90
 
 
91
mp_limb_t
 
92
#if __STDC__
 
93
refmpn_sub_n (mp_ptr res_ptr,
 
94
               mp_srcptr s1_ptr, mp_srcptr s2_ptr, mp_size_t size)
 
95
#else
 
96
refmpn_sub_n (res_ptr, s1_ptr, s2_ptr, size)
 
97
     register mp_ptr res_ptr;
 
98
     register mp_srcptr s1_ptr;
 
99
     register mp_srcptr s2_ptr;
 
100
     mp_size_t size;
 
101
#endif
 
102
{
 
103
  register mp_limb_t x, y, cy;
 
104
  register mp_size_t j;
 
105
 
 
106
  /* The loop counter and index J goes from -SIZE to -1.  This way
 
107
     the loop becomes faster.  */
 
108
  j = -size;
 
109
 
 
110
  /* Offset the base pointers to compensate for the negative indices.  */
 
111
  s1_ptr -= j;
 
112
  s2_ptr -= j;
 
113
  res_ptr -= j;
 
114
 
 
115
  cy = 0;
 
116
  do
 
117
    {
 
118
      y = s2_ptr[j];
 
119
      x = s1_ptr[j];
 
120
      y += cy;                  /* add previous carry to subtrahend */
 
121
      cy = (y < cy);            /* get out carry from that addition */
 
122
      y = x - y;                /* main subtract */
 
123
      cy = (y > x) + cy;        /* get out carry from the subtract, combine */
 
124
      res_ptr[j] = y;
 
125
    }
 
126
  while (++j != 0);
 
127
 
 
128
  return cy;
 
129
}
 
130
 
 
131
main (argc, argv)
 
132
     int argc;
 
133
     char **argv;
 
134
{
 
135
  mp_limb_t s1[SIZE];
 
136
  mp_limb_t s2[SIZE];
 
137
  mp_limb_t dx[SIZE+2];
 
138
  mp_limb_t dy[SIZE+2];
 
139
  int cyx, cyy;
 
140
  int i;
 
141
  long t0, t;
 
142
  int test;
 
143
  mp_size_t size;
 
144
 
 
145
  for (test = 0; ; test++)
 
146
    {
 
147
#if TIMES == 1 && ! defined (PRINT)
 
148
      if (test % (SIZE > 10000 ? 1 : 10000 / SIZE) == 0)
 
149
        {
 
150
          printf ("\r%d", test);
 
151
          fflush (stdout);
 
152
        }
 
153
#endif
 
154
 
 
155
#ifdef RANDOM
 
156
      size = random () % SIZE + 1;
 
157
#else
 
158
      size = SIZE;
 
159
#endif
 
160
 
 
161
      dx[0] = 0x87654321;
 
162
      dy[0] = 0x87654321;
 
163
      dx[size+1] = 0x12345678;
 
164
      dy[size+1] = 0x12345678;
 
165
 
 
166
#if TIMES != 1
 
167
      mpn_random (s1, size);
 
168
      mpn_random (s2, size);
 
169
 
 
170
#ifndef NOCHECK
 
171
      t0 = cputime();
 
172
      for (i = 0; i < TIMES; i++)
 
173
        refmpn_sub_n (dx+1, s1, s2, size);
 
174
      t = cputime() - t0;
 
175
      printf ("refmpn_sub_n: %5ldms (%.2f cycles/limb)\n",
 
176
              t, ((double) t * CLOCK) / (OPS * 1000.0));
 
177
#endif
 
178
 
 
179
      t0 = cputime();
 
180
      for (i = 0; i < TIMES; i++)
 
181
        mpn_sub_n (dx+1, s1, s2, size);
 
182
      t = cputime() - t0;
 
183
      printf ("mpn_sub_n:    %5ldms (%.2f cycles/limb)\n",
 
184
              t, ((double) t * CLOCK) / (OPS * 1000.0));
 
185
#endif
 
186
 
 
187
#ifndef NOCHECK
 
188
      mpn_random2 (s1, size);
 
189
      mpn_random2 (s2, size);
 
190
 
 
191
#ifdef PRINT
 
192
      mpn_print (s1, size);
 
193
      mpn_print (s2, size);
 
194
#endif
 
195
 
 
196
      /* Put garbage in the destination.  */
 
197
      for (i = 0; i < size; i++)
 
198
        {
 
199
          dx[i+1] = 0xdead;
 
200
          dy[i+1] = 0xbeef;
 
201
        }
 
202
 
 
203
      cyx = refmpn_sub_n (dx+1, s1, s2, size);
 
204
      cyy = mpn_sub_n (dy+1, s1, s2, size);
 
205
#ifdef PRINT
 
206
      printf ("%d ", cyx); mpn_print (dx+1, size);
 
207
      printf ("%d ", cyy); mpn_print (dy+1, size);
 
208
#endif
 
209
      if (cyx != cyy || mpn_cmp (dx, dy, size+2) != 0
 
210
          || dx[0] != 0x87654321 || dx[size+1] != 0x12345678)
 
211
        {
 
212
#ifndef PRINT
 
213
          printf ("%d ", cyx); mpn_print (dx+1, size);
 
214
          printf ("%d ", cyy); mpn_print (dy+1, size);
 
215
#endif
 
216
          printf ("TEST NUMBER %d\n", test);
 
217
          abort();
 
218
        }
 
219
#endif
 
220
    }
 
221
}
 
222
 
 
223
mpn_print (mp_ptr p, mp_size_t size)
 
224
{
 
225
  mp_size_t i;
 
226
 
 
227
  for (i = size - 1; i >= 0; i--)
 
228
    {
 
229
#ifdef _LONG_LONG_LIMB
 
230
      printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)),
 
231
              (unsigned long) (p[i] >> (BITS_PER_MP_LIMB/2)),
 
232
              (int) (sizeof(mp_limb_t)), (unsigned long) (p[i]));
 
233
#else
 
234
      printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
 
235
#endif
 
236
#ifdef SPACE
 
237
      if (i != 0)
 
238
        printf (" ");
 
239
#endif
 
240
    }
 
241
  puts ("");
 
242
}