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