blob: e57ac8d9ae58005a8d1e32730d1c0e240c6d0751 [file] [log] [blame]
Austin Schuhbb1338c2024-06-15 19:31:16 -07001/* 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-impl.h"
25#include "tests.h"
26
27/* Main file is expected to define mpn_toomN_mul, mpn_toomN_sqr_itch,
28 * MIN_AN, MAX_AN and then include this file. */
29
30#ifndef COUNT
31#define COUNT 2000
32#endif
33
34int
35main (int argc, char **argv)
36{
37 mp_ptr ap, refp, pp, scratch;
38 int count = COUNT;
39 int test;
40 gmp_randstate_ptr rands;
41 TMP_DECL;
42 TMP_MARK;
43
44 TESTS_REPS (count, argv, argc);
45
46 tests_start ();
47
48 if (MAX_AN > MIN_AN) {
49 rands = RANDS;
50
51 ap = TMP_ALLOC_LIMBS (MAX_AN);
52 refp = TMP_ALLOC_LIMBS (MAX_AN * 2);
53 pp = 1 + TMP_ALLOC_LIMBS (MAX_AN * 2 + 2);
54 scratch
55 = 1+TMP_ALLOC_LIMBS (mpn_toomN_sqr_itch (MAX_AN) + 2);
56
57 for (test = 0; test < count; test++)
58 {
59 mp_size_t an;
60 mp_size_t itch;
61 mp_limb_t p_before, p_after, s_before, s_after;
62
63 an = MIN_AN
64 + gmp_urandomm_ui (rands, MAX_AN - MIN_AN);
65
66 mpn_random2 (ap, an);
67 mpn_random2 (pp-1, an * 2 + 2);
68 p_before = pp[-1];
69 p_after = pp[an * 2];
70
71 itch = mpn_toomN_sqr_itch (an);
72 ASSERT_ALWAYS (itch <= mpn_toomN_sqr_itch (MAX_AN));
73 mpn_random2 (scratch-1, itch+2);
74 s_before = scratch[-1];
75 s_after = scratch[itch];
76
77 mpn_toomN_sqr (pp, ap, an, scratch);
78 refmpn_mul (refp, ap, an, ap, an);
79 if (pp[-1] != p_before || pp[an * 2] != p_after
80 || scratch[-1] != s_before || scratch[itch] != s_after
81 || mpn_cmp (refp, pp, an * 2) != 0)
82 {
83 printf ("ERROR in test %d, an = %d\n",
84 test, (int) an);
85 if (pp[-1] != p_before)
86 {
87 printf ("before pp:"); mpn_dump (pp -1, 1);
88 printf ("keep: "); mpn_dump (&p_before, 1);
89 }
90 if (pp[an * 2] != p_after)
91 {
92 printf ("after pp:"); mpn_dump (pp + an * 2, 1);
93 printf ("keep: "); mpn_dump (&p_after, 1);
94 }
95 if (scratch[-1] != s_before)
96 {
97 printf ("before scratch:"); mpn_dump (scratch-1, 1);
98 printf ("keep: "); mpn_dump (&s_before, 1);
99 }
100 if (scratch[itch] != s_after)
101 {
102 printf ("after scratch:"); mpn_dump (scratch + itch, 1);
103 printf ("keep: "); mpn_dump (&s_after, 1);
104 }
105 mpn_dump (ap, an);
106 mpn_dump (pp, an * 2);
107 mpn_dump (refp, an * 2);
108
109 abort();
110 }
111 }
112 TMP_FREE;
113 }
114
115 tests_end ();
116 return 0;
117}