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

« back to all changes in this revision

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