Tizen 2.1 base
[external/gmp.git] / tests / devel / copy.c
1 /*
2 Copyright 1999, 2000, 2001, 2004, 2009 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 3 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.  If not, see http://www.gnu.org/licenses/.  */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include "gmp.h"
22 #include "gmp-impl.h"
23 #include "tests.h"
24
25 #ifdef OPERATION_copyi
26 #define func MPN_COPY_INCR
27 #define reffunc refmpn_copyi
28 #define funcname "MPN_COPY_INCR"
29 #endif
30
31 #ifdef OPERATION_copyd
32 #define func MPN_COPY_DECR
33 #define reffunc refmpn_copyd
34 #define funcname "MPN_COPY_DECR"
35 #endif
36
37 #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
38 #include <time.h>
39
40 int
41 cputime ()
42 {
43   if (CLOCKS_PER_SEC < 100000)
44     return clock () * 1000 / CLOCKS_PER_SEC;
45   return clock () / (CLOCKS_PER_SEC / 1000);
46 }
47 #else
48 #include <sys/types.h>
49 #include <sys/time.h>
50 #include <sys/resource.h>
51
52 int
53 cputime ()
54 {
55   struct rusage rus;
56
57   getrusage (0, &rus);
58   return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
59 }
60 #endif
61
62 static void mpn_print (mp_ptr, mp_size_t);
63
64 #define M * 1000000
65
66 #ifndef CLOCK
67 #error "Don't know CLOCK of your machine"
68 #endif
69
70 #ifndef OPS
71 #define OPS (CLOCK/2)
72 #endif
73 #ifndef SIZE
74 #define SIZE 496
75 #endif
76 #ifndef TIMES
77 #define TIMES OPS/(SIZE+1)
78 #endif
79
80 int
81 main (int argc, char **argv)
82 {
83   mp_ptr s1, dx, dy;
84   int i;
85   long t0, t;
86   unsigned int test;
87   mp_size_t size;
88   unsigned int ntests;
89
90   s1 = malloc (SIZE * sizeof (mp_limb_t));
91   dx = malloc ((SIZE + 2) * sizeof (mp_limb_t));
92   dy = malloc ((SIZE + 2) * sizeof (mp_limb_t));
93
94   ntests = ~(unsigned) 0;
95   if (argc == 2)
96     ntests = strtol (argv[1], 0, 0);
97
98   for (test = 1; test <= ntests; test++)
99     {
100 #if TIMES == 1 && ! defined (PRINT)
101       if (test % (SIZE > 100000 ? 1 : 100000 / SIZE) == 0)
102         {
103           printf ("\r%u", test);
104           fflush (stdout);
105         }
106 #endif
107
108 #ifdef RANDOM
109       size = random () % SIZE + 1;
110 #else
111       size = SIZE;
112 #endif
113
114       dx[0] = 0x87654321;
115       dy[0] = 0x87654321;
116       dx[size+1] = 0x12345678;
117       dy[size+1] = 0x12345678;
118
119 #if TIMES != 1
120       mpn_random (s1, size);
121
122       t0 = cputime();
123       for (i = 0; i < TIMES; i++)
124         func (dx+1, s1, size);
125       t = cputime() - t0;
126       printf (funcname ":    %5ldms (%.3f cycles/limb)\n",
127               t, ((double) t * CLOCK) / (TIMES * size * 1000.0));
128 #endif
129
130 #ifndef NOCHECK
131       mpn_random2 (s1, size);
132
133 #ifdef PRINT
134       mpn_print (s1, size);
135 #endif
136
137       /* Put garbage in the destination.  */
138       for (i = 0; i < size; i++)
139         {
140           dx[i+1] = 0xdead;
141           dy[i+1] = 0xbeef;
142         }
143
144       reffunc (dx+1, s1, size);
145       func (dy+1, s1, size);
146
147 #ifdef PRINT
148       mpn_print (dx+1, size);
149       mpn_print (dy+1, size);
150 #endif
151
152       if (mpn_cmp (dx, dy, size+2) != 0
153           || dx[0] != 0x87654321 || dx[size+1] != 0x12345678)
154         {
155 #ifndef PRINT
156           mpn_print (dx+1, size);
157           mpn_print (dy+1, size);
158 #endif
159           printf ("\n");
160           if (dy[0] != 0x87654321)
161             printf ("clobbered at low end\n");
162           if (dy[size+1] != 0x12345678)
163             printf ("clobbered at high end\n");
164           printf ("TEST NUMBER %u\n", test);
165           abort();
166         }
167 #endif
168     }
169   exit (0);
170 }
171
172 static void
173 mpn_print (mp_ptr p, mp_size_t size)
174 {
175   mp_size_t i;
176
177   for (i = size - 1; i >= 0; i--)
178     {
179 #ifdef _LONG_LONG_LIMB
180       printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)),
181               (unsigned long) (p[i] >> (GMP_LIMB_BITS/2)),
182               (int) (sizeof(mp_limb_t)), (unsigned long) (p[i]));
183 #else
184       printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
185 #endif
186 #ifdef SPACE
187       if (i != 0)
188         printf (" ");
189 #endif
190     }
191   puts ("");
192 }