1 /* Test for various Toom squaring functions.
3 Copyright 2009, 2012 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library test suite.
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 Public License for more details.
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite. If not, see http://www.gnu.org/licenses/. */
28 /* Main file is expected to define mpn_toomN_mul, mpn_toomN_sqr_itch,
29 * MIN_AN, MAX_AN and then include this file. */
36 main (int argc, char **argv)
38 mp_ptr ap, refp, pp, scratch;
41 gmp_randstate_ptr rands;
48 count = strtol (argv[1], &end, 0);
49 if (*end || count <= 0)
51 fprintf (stderr, "Invalid test count: %s.\n", argv[1]);
58 if (MAX_AN > MIN_AN) {
61 ap = TMP_ALLOC_LIMBS (MAX_AN);
62 refp = TMP_ALLOC_LIMBS (MAX_AN * 2);
63 pp = 1 + TMP_ALLOC_LIMBS (MAX_AN * 2 + 2);
65 = 1+TMP_ALLOC_LIMBS (mpn_toomN_sqr_itch (MAX_AN) + 2);
67 for (test = 0; test < count; test++)
73 mp_limb_t p_before, p_after, s_before, s_after;
76 + gmp_urandomm_ui (rands, MAX_AN - MIN_AN);
79 mpn_random2 (pp-1, an * 2 + 2);
83 itch = mpn_toomN_sqr_itch (an);
84 ASSERT_ALWAYS (itch <= mpn_toomN_sqr_itch (MAX_AN));
85 mpn_random2 (scratch-1, itch+2);
86 s_before = scratch[-1];
87 s_after = scratch[itch];
89 mpn_toomN_sqr (pp, ap, an, scratch);
90 refmpn_mul (refp, ap, an, ap, an);
91 if (pp[-1] != p_before || pp[an * 2] != p_after
92 || scratch[-1] != s_before || scratch[itch] != s_after
93 || mpn_cmp (refp, pp, an * 2) != 0)
95 printf ("ERROR in test %d, an = %d\n",
97 if (pp[-1] != p_before)
99 printf ("before pp:"); mpn_dump (pp -1, 1);
100 printf ("keep: "); mpn_dump (&p_before, 1);
102 if (pp[an * 2] != p_after)
104 printf ("after pp:"); mpn_dump (pp + an * 2, 1);
105 printf ("keep: "); mpn_dump (&p_after, 1);
107 if (scratch[-1] != s_before)
109 printf ("before scratch:"); mpn_dump (scratch-1, 1);
110 printf ("keep: "); mpn_dump (&s_before, 1);
112 if (scratch[itch] != s_after)
114 printf ("after scratch:"); mpn_dump (scratch + itch, 1);
115 printf ("keep: "); mpn_dump (&s_after, 1);
118 mpn_dump (pp, an * 2);
119 mpn_dump (refp, an * 2);