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