toom-sqr-shared.h revision 1.1.1.1.8.2 1 1.1.1.1.8.2 tls /* Test for various Toom squaring functions.
2 1.1.1.1.8.2 tls
3 1.1.1.1.8.2 tls Copyright 2009, 2012 Free Software Foundation, Inc.
4 1.1.1.1.8.2 tls
5 1.1.1.1.8.2 tls This file is part of the GNU MP Library test suite.
6 1.1.1.1.8.2 tls
7 1.1.1.1.8.2 tls The GNU MP Library test suite is free software; you can redistribute it
8 1.1.1.1.8.2 tls and/or modify it under the terms of the GNU General Public License as
9 1.1.1.1.8.2 tls published by the Free Software Foundation; either version 3 of the License,
10 1.1.1.1.8.2 tls or (at your option) any later version.
11 1.1.1.1.8.2 tls
12 1.1.1.1.8.2 tls The GNU MP Library test suite is distributed in the hope that it will be
13 1.1.1.1.8.2 tls useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1.1.1.8.2 tls MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 1.1.1.1.8.2 tls Public License for more details.
16 1.1.1.1.8.2 tls
17 1.1.1.1.8.2 tls You should have received a copy of the GNU General Public License along with
18 1.1.1.1.8.2 tls the GNU MP Library test suite. If not, see http://www.gnu.org/licenses/. */
19 1.1.1.1.8.2 tls
20 1.1.1.1.8.2 tls
21 1.1.1.1.8.2 tls #include <stdlib.h>
22 1.1.1.1.8.2 tls #include <stdio.h>
23 1.1.1.1.8.2 tls
24 1.1.1.1.8.2 tls #include "gmp.h"
25 1.1.1.1.8.2 tls #include "gmp-impl.h"
26 1.1.1.1.8.2 tls #include "tests.h"
27 1.1.1.1.8.2 tls
28 1.1.1.1.8.2 tls /* Main file is expected to define mpn_toomN_mul, mpn_toomN_sqr_itch,
29 1.1.1.1.8.2 tls * MIN_AN, MAX_AN and then include this file. */
30 1.1.1.1.8.2 tls
31 1.1.1.1.8.2 tls #ifndef COUNT
32 1.1.1.1.8.2 tls #define COUNT 500
33 1.1.1.1.8.2 tls #endif
34 1.1.1.1.8.2 tls
35 1.1.1.1.8.2 tls int
36 1.1.1.1.8.2 tls main (int argc, char **argv)
37 1.1.1.1.8.2 tls {
38 1.1.1.1.8.2 tls mp_ptr ap, refp, pp, scratch;
39 1.1.1.1.8.2 tls int count = COUNT;
40 1.1.1.1.8.2 tls int test;
41 1.1.1.1.8.2 tls gmp_randstate_ptr rands;
42 1.1.1.1.8.2 tls TMP_DECL;
43 1.1.1.1.8.2 tls TMP_MARK;
44 1.1.1.1.8.2 tls
45 1.1.1.1.8.2 tls if (argc > 1)
46 1.1.1.1.8.2 tls {
47 1.1.1.1.8.2 tls char *end;
48 1.1.1.1.8.2 tls count = strtol (argv[1], &end, 0);
49 1.1.1.1.8.2 tls if (*end || count <= 0)
50 1.1.1.1.8.2 tls {
51 1.1.1.1.8.2 tls fprintf (stderr, "Invalid test count: %s.\n", argv[1]);
52 1.1.1.1.8.2 tls return 1;
53 1.1.1.1.8.2 tls }
54 1.1.1.1.8.2 tls }
55 1.1.1.1.8.2 tls
56 1.1.1.1.8.2 tls tests_start ();
57 1.1.1.1.8.2 tls
58 1.1.1.1.8.2 tls if (MAX_AN > MIN_AN) {
59 1.1.1.1.8.2 tls rands = RANDS;
60 1.1.1.1.8.2 tls
61 1.1.1.1.8.2 tls ap = TMP_ALLOC_LIMBS (MAX_AN);
62 1.1.1.1.8.2 tls refp = TMP_ALLOC_LIMBS (MAX_AN * 2);
63 1.1.1.1.8.2 tls pp = 1 + TMP_ALLOC_LIMBS (MAX_AN * 2 + 2);
64 1.1.1.1.8.2 tls scratch
65 1.1.1.1.8.2 tls = 1+TMP_ALLOC_LIMBS (mpn_toomN_sqr_itch (MAX_AN) + 2);
66 1.1.1.1.8.2 tls
67 1.1.1.1.8.2 tls for (test = 0; test < count; test++)
68 1.1.1.1.8.2 tls {
69 1.1.1.1.8.2 tls unsigned size_min;
70 1.1.1.1.8.2 tls unsigned size_range;
71 1.1.1.1.8.2 tls mp_size_t an;
72 1.1.1.1.8.2 tls mp_size_t itch;
73 1.1.1.1.8.2 tls mp_limb_t p_before, p_after, s_before, s_after;
74 1.1.1.1.8.2 tls
75 1.1.1.1.8.2 tls an = MIN_AN
76 1.1.1.1.8.2 tls + gmp_urandomm_ui (rands, MAX_AN - MIN_AN);
77 1.1.1.1.8.2 tls
78 1.1.1.1.8.2 tls mpn_random2 (ap, an);
79 1.1.1.1.8.2 tls mpn_random2 (pp-1, an * 2 + 2);
80 1.1.1.1.8.2 tls p_before = pp[-1];
81 1.1.1.1.8.2 tls p_after = pp[an * 2];
82 1.1.1.1.8.2 tls
83 1.1.1.1.8.2 tls itch = mpn_toomN_sqr_itch (an);
84 1.1.1.1.8.2 tls ASSERT_ALWAYS (itch <= mpn_toomN_sqr_itch (MAX_AN));
85 1.1.1.1.8.2 tls mpn_random2 (scratch-1, itch+2);
86 1.1.1.1.8.2 tls s_before = scratch[-1];
87 1.1.1.1.8.2 tls s_after = scratch[itch];
88 1.1.1.1.8.2 tls
89 1.1.1.1.8.2 tls mpn_toomN_sqr (pp, ap, an, scratch);
90 1.1.1.1.8.2 tls refmpn_mul (refp, ap, an, ap, an);
91 1.1.1.1.8.2 tls if (pp[-1] != p_before || pp[an * 2] != p_after
92 1.1.1.1.8.2 tls || scratch[-1] != s_before || scratch[itch] != s_after
93 1.1.1.1.8.2 tls || mpn_cmp (refp, pp, an * 2) != 0)
94 1.1.1.1.8.2 tls {
95 1.1.1.1.8.2 tls printf ("ERROR in test %d, an = %d\n",
96 1.1.1.1.8.2 tls test, (int) an);
97 1.1.1.1.8.2 tls if (pp[-1] != p_before)
98 1.1.1.1.8.2 tls {
99 1.1.1.1.8.2 tls printf ("before pp:"); mpn_dump (pp -1, 1);
100 1.1.1.1.8.2 tls printf ("keep: "); mpn_dump (&p_before, 1);
101 1.1.1.1.8.2 tls }
102 1.1.1.1.8.2 tls if (pp[an * 2] != p_after)
103 1.1.1.1.8.2 tls {
104 1.1.1.1.8.2 tls printf ("after pp:"); mpn_dump (pp + an * 2, 1);
105 1.1.1.1.8.2 tls printf ("keep: "); mpn_dump (&p_after, 1);
106 1.1.1.1.8.2 tls }
107 1.1.1.1.8.2 tls if (scratch[-1] != s_before)
108 1.1.1.1.8.2 tls {
109 1.1.1.1.8.2 tls printf ("before scratch:"); mpn_dump (scratch-1, 1);
110 1.1.1.1.8.2 tls printf ("keep: "); mpn_dump (&s_before, 1);
111 1.1.1.1.8.2 tls }
112 1.1.1.1.8.2 tls if (scratch[itch] != s_after)
113 1.1.1.1.8.2 tls {
114 1.1.1.1.8.2 tls printf ("after scratch:"); mpn_dump (scratch + itch, 1);
115 1.1.1.1.8.2 tls printf ("keep: "); mpn_dump (&s_after, 1);
116 1.1.1.1.8.2 tls }
117 1.1.1.1.8.2 tls mpn_dump (ap, an);
118 1.1.1.1.8.2 tls mpn_dump (pp, an * 2);
119 1.1.1.1.8.2 tls mpn_dump (refp, an * 2);
120 1.1.1.1.8.2 tls
121 1.1.1.1.8.2 tls abort();
122 1.1.1.1.8.2 tls }
123 1.1.1.1.8.2 tls }
124 1.1.1.1.8.2 tls TMP_FREE;
125 1.1.1.1.8.2 tls }
126 1.1.1.1.8.2 tls
127 1.1.1.1.8.2 tls tests_end ();
128 1.1.1.1.8.2 tls return 0;
129 1.1.1.1.8.2 tls }
130