Upload Tizen:Base source
[external/gmp.git] / tests / devel / logops_n.c
1 /*
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2009 Free
3 Software Foundation, 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 3 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.  If not, see http://www.gnu.org/licenses/.  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include "gmp.h"
23 #include "gmp-impl.h"
24 #include "tests.h"
25
26 #ifdef OPERATION_and_n
27 #define func __gmpn_and_n
28 #define reffunc refmpn_and_n
29 #define funcname "mpn_and_n"
30 #endif
31
32 #ifdef OPERATION_andn_n
33 #define func __gmpn_andn_n
34 #define reffunc refmpn_andn_n
35 #define funcname "mpn_andn_n"
36 #endif
37
38 #ifdef OPERATION_nand_n
39 #define func __gmpn_nand_n
40 #define reffunc refmpn_nand_n
41 #define funcname "mpn_nand_n"
42 #endif
43
44 #ifdef OPERATION_ior_n
45 #define func __gmpn_ior_n
46 #define reffunc refmpn_ior_n
47 #define funcname "mpn_ior_n"
48 #endif
49
50 #ifdef OPERATION_iorn_n
51 #define func __gmpn_iorn_n
52 #define reffunc refmpn_iorn_n
53 #define funcname "mpn_iorn_n"
54 #endif
55
56 #ifdef OPERATION_nior_n
57 #define func __gmpn_nior_n
58 #define reffunc refmpn_nior_n
59 #define funcname "mpn_nior_n"
60 #endif
61
62 #ifdef OPERATION_xor_n
63 #define func __gmpn_xor_n
64 #define reffunc refmpn_xor_n
65 #define funcname "mpn_xor_n"
66 #endif
67
68 #ifdef OPERATION_xnor_n
69 #define func __gmpn_xnor_n
70 #define reffunc refmpn_xnor_n
71 #define funcname "mpn_xnor_n"
72 #endif
73
74 #if defined (USG) || defined (__SVR4) || defined (_UNICOS) || defined (__hpux)
75 #include <time.h>
76
77 int
78 cputime ()
79 {
80   if (CLOCKS_PER_SEC < 100000)
81     return clock () * 1000 / CLOCKS_PER_SEC;
82   return clock () / (CLOCKS_PER_SEC / 1000);
83 }
84 #else
85 #include <sys/types.h>
86 #include <sys/time.h>
87 #include <sys/resource.h>
88
89 int
90 cputime ()
91 {
92   struct rusage rus;
93
94   getrusage (0, &rus);
95   return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
96 }
97 #endif
98
99 static void mpn_print (mp_ptr, mp_size_t);
100
101 #define M * 1000000
102
103 #ifndef CLOCK
104 #error "Don't know CLOCK of your machine"
105 #endif
106
107 #ifndef OPS
108 #define OPS (CLOCK/5)
109 #endif
110 #ifndef SIZE
111 #define SIZE 328
112 #endif
113 #ifndef TIMES
114 #define TIMES OPS/(SIZE+1)
115 #endif
116
117 int
118 main (int argc, char **argv)
119 {
120   mp_ptr s1, s2, dx, dy;
121   int i;
122   long t0, t;
123   unsigned int test;
124   mp_size_t size;
125   unsigned int ntests;
126
127   s1 = malloc (SIZE * sizeof (mp_limb_t));
128   s2 = malloc (SIZE * sizeof (mp_limb_t));
129   dx = malloc ((SIZE + 2) * sizeof (mp_limb_t));
130   dy = malloc ((SIZE + 2) * sizeof (mp_limb_t));
131
132   ntests = ~(unsigned) 0;
133   if (argc == 2)
134     ntests = strtol (argv[1], 0, 0);
135
136   for (test = 1; test <= ntests; test++)
137     {
138 #if TIMES == 1 && ! defined (PRINT)
139       if (test % (SIZE > 100000 ? 1 : 100000 / SIZE) == 0)
140         {
141           printf ("\r%d", test);
142           fflush (stdout);
143         }
144 #endif
145
146 #ifdef RANDOM
147       size = random () % SIZE + 1;
148 #else
149       size = SIZE;
150 #endif
151
152       dx[0] = 0x87654321;
153       dy[0] = 0x87654321;
154       dx[size+1] = 0x12345678;
155       dy[size+1] = 0x12345678;
156
157 #if TIMES != 1
158       mpn_random (s1, size);
159       mpn_random (s2, size);
160
161       t0 = cputime();
162       for (i = 0; i < TIMES; i++)
163         func (dx+1, s1, s2, size);
164       t = cputime() - t0;
165       printf (funcname ":    %5ldms (%.3f cycles/limb)\n",
166               t, ((double) t * CLOCK) / (TIMES * size * 1000.0));
167 #endif
168
169 #ifndef NOCHECK
170       mpn_random2 (s1, size);
171       mpn_random2 (s2, size);
172
173 #ifdef PRINT
174       mpn_print (s1, size);
175       mpn_print (s2, size);
176 #endif
177
178       /* Put garbage in the destination.  */
179       for (i = 0; i < size; i++)
180         {
181           dx[i+1] = 0xdead;
182           dy[i+1] = 0xbeef;
183         }
184
185       reffunc (dx+1, s1, s2, size);
186       func (dy+1, s1, s2, size);
187 #ifdef PRINT
188       mpn_print (dx+1, size);
189       mpn_print (dy+1, size);
190 #endif
191       if (mpn_cmp (dx, dy, size+2) != 0
192           || dx[0] != 0x87654321 || dx[size+1] != 0x12345678)
193         {
194 #ifndef PRINT
195           mpn_print (dx+1, size);
196           mpn_print (dy+1, size);
197 #endif
198           printf ("\n");
199           if (dy[0] != 0x87654321)
200             printf ("clobbered at low end\n");
201           if (dy[size+1] != 0x12345678)
202             printf ("clobbered at high end\n");
203           printf ("TEST NUMBER %u\n", test);
204           abort();
205         }
206 #endif
207     }
208   exit (0);
209 }
210
211 static void
212 mpn_print (mp_ptr p, mp_size_t size)
213 {
214   mp_size_t i;
215
216   for (i = size - 1; i >= 0; i--)
217     {
218 #ifdef _LONG_LONG_LIMB
219       printf ("%0*lX%0*lX", (int) (sizeof(mp_limb_t)),
220               (unsigned long) (p[i] >> (GMP_LIMB_BITS/2)),
221               (int) (sizeof(mp_limb_t)), (unsigned long) (p[i]));
222 #else
223       printf ("%0*lX", (int) (2 * sizeof(mp_limb_t)), p[i]);
224 #endif
225 #ifdef SPACE
226       if (i != 0)
227         printf (" ");
228 #endif
229     }
230   puts ("");
231 }