Upload Tizen:Base source
[external/gmp.git] / tests / devel / tst-addsub.c
1 /* Copyright 1996, 2001 Free Software Foundation, Inc.
2
3 This file is part of the GNU MP Library.
4
5 The GNU MP Library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or (at your
8 option) any later version.
9
10 The GNU MP Library is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
13 License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "gmp.h"
21 #include "gmp-impl.h"
22 #include "tests.h"
23
24 #define ADD 1
25 #define SUB 2
26
27 #ifndef METHOD
28 #define METHOD ADD
29 #endif
30
31 #if METHOD == ADD
32 #define REFCALL refmpn_add_n
33 #define TESTCALL mpn_add_n
34 #endif
35
36 #if METHOD == SUB
37 #define REFCALL refmpn_sub_n
38 #define TESTCALL mpn_sub_n
39 #endif
40
41 #define SIZE 100
42
43 int
44 main (int argc, char **argv)
45 {
46   mp_size_t alloc_size, max_size, size, i, cumul_size;
47   mp_ptr s1, s2, dx, dy;
48   int s1_align, s2_align, d_align;
49   long pass, n_passes;
50   mp_limb_t cx, cy;
51
52   max_size = SIZE;
53   n_passes = 1000000;
54
55   argc--; argv++;
56   if (argc)
57     {
58       max_size = atol (*argv);
59       argc--; argv++;
60     }
61
62   alloc_size = max_size + 32;
63   s1 = malloc (alloc_size * BYTES_PER_MP_LIMB);
64   s2 = malloc (alloc_size * BYTES_PER_MP_LIMB);
65   dx = malloc (alloc_size * BYTES_PER_MP_LIMB);
66   dy = malloc (alloc_size * BYTES_PER_MP_LIMB);
67
68   cumul_size = 0;
69   for (pass = 0; pass < n_passes; pass++)
70     {
71       size = random () % max_size + 1;
72
73       cumul_size += size;
74       if (cumul_size >= 1000000)
75         {
76           cumul_size -= 1000000;
77           printf ("\r%ld", pass); fflush (stdout);
78         }
79       s1_align = random () % 32;
80       s2_align = random () % 32;
81       d_align = random () % 32;
82
83       mpn_random2 (s1 + s1_align, size);
84       mpn_random2 (s2 + s2_align, size);
85
86       for (i = 0; i < alloc_size; i++)
87         dx[i] = dy[i] = i + 0x9876500;
88
89       cx = TESTCALL (dx + d_align, s1 + s1_align, s2 + s2_align, size);
90       cy = REFCALL (dy + d_align, s1 + s1_align, s2 + s2_align, size);
91
92       if (cx != cy || mpn_cmp (dx, dy, alloc_size) != 0)
93         abort ();
94     }
95
96   printf ("%ld passes OK\n", n_passes);
97   exit (0);
98 }