1 /* Test for mullo function.
3 Copyright 2009 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 /* Sizes are up to 2^SIZE_LOG limbs */
37 #define MAX_N (1L << SIZE_LOG)
41 main (int argc, char **argv)
43 mp_ptr ap, bp, refp, pp, scratch;
46 gmp_randstate_ptr rands;
53 count = strtol (argv[1], &end, 0);
54 if (*end || count <= 0)
56 fprintf (stderr, "Invalid test count: %s.\n", argv[1]);
64 #define mpn_mullo_itch(n) (0)
66 ap = TMP_ALLOC_LIMBS (MAX_N);
67 bp = TMP_ALLOC_LIMBS (MAX_N);
68 refp = TMP_ALLOC_LIMBS (MAX_N * 2);
69 pp = 1+TMP_ALLOC_LIMBS (MAX_N + 2);
71 = 1+TMP_ALLOC_LIMBS (mpn_mullo_itch (MAX_N) + 2);
73 for (test = 0; test < count; test++)
79 mp_limb_t p_before, p_after, s_before, s_after;
81 for (size_min = 1; (1L << size_min) < MIN_N; size_min++)
84 /* We generate an in the MIN_N <= n <= (1 << size_range). */
86 + gmp_urandomm_ui (rands, SIZE_LOG + 1 - size_min);
89 + gmp_urandomm_ui (rands, (1L << size_range) + 1 - MIN_N);
93 mpn_random2 (pp-1, n + 2);
97 itch = mpn_mullo_itch (n);
98 ASSERT_ALWAYS (itch <= mpn_mullo_itch (MAX_N));
99 mpn_random2 (scratch-1, itch+2);
100 s_before = scratch[-1];
101 s_after = scratch[itch];
103 mpn_mullo_n (pp, ap, bp, n);
104 mpn_mul_n (refp, ap, bp, n);
105 if (pp[-1] != p_before || pp[n] != p_after
106 || scratch[-1] != s_before || scratch[itch] != s_after
107 || mpn_cmp (refp, pp, n) != 0)
109 printf ("ERROR in test %d, n = %d",
111 if (pp[-1] != p_before)
113 printf ("before pp:"); mpn_dump (pp -1, 1);
114 printf ("keep: "); mpn_dump (&p_before, 1);
116 if (pp[n] != p_after)
118 printf ("after pp:"); mpn_dump (pp + n, 1);
119 printf ("keep: "); mpn_dump (&p_after, 1);
121 if (scratch[-1] != s_before)
123 printf ("before scratch:"); mpn_dump (scratch-1, 1);
124 printf ("keep: "); mpn_dump (&s_before, 1);
126 if (scratch[itch] != s_after)
128 printf ("after scratch:"); mpn_dump (scratch + itch, 1);
129 printf ("keep: "); mpn_dump (&s_after, 1);