Home | History | Annotate | Line # | Download | only in mpn
toom-sqr-shared.h revision 1.1
      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  mrg the GNU MP Library test suite.  If not, see http://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.h"
     25  1.1  mrg #include "gmp-impl.h"
     26  1.1  mrg #include "tests.h"
     27  1.1  mrg 
     28  1.1  mrg /* Main file is expected to define mpn_toomN_mul, mpn_toomN_sqr_itch,
     29  1.1  mrg  * MIN_AN, MAX_AN and then include this file. */
     30  1.1  mrg 
     31  1.1  mrg #ifndef COUNT
     32  1.1  mrg #define COUNT 500
     33  1.1  mrg #endif
     34  1.1  mrg 
     35  1.1  mrg int
     36  1.1  mrg main (int argc, char **argv)
     37  1.1  mrg {
     38  1.1  mrg   mp_ptr ap, refp, pp, scratch;
     39  1.1  mrg   int count = COUNT;
     40  1.1  mrg   int test;
     41  1.1  mrg   gmp_randstate_ptr rands;
     42  1.1  mrg   TMP_DECL;
     43  1.1  mrg   TMP_MARK;
     44  1.1  mrg 
     45  1.1  mrg   if (argc > 1)
     46  1.1  mrg     {
     47  1.1  mrg       char *end;
     48  1.1  mrg       count = strtol (argv[1], &end, 0);
     49  1.1  mrg       if (*end || count <= 0)
     50  1.1  mrg 	{
     51  1.1  mrg 	  fprintf (stderr, "Invalid test count: %s.\n", argv[1]);
     52  1.1  mrg 	  return 1;
     53  1.1  mrg 	}
     54  1.1  mrg     }
     55  1.1  mrg 
     56  1.1  mrg   tests_start ();
     57  1.1  mrg 
     58  1.1  mrg   if (MAX_AN > MIN_AN) {
     59  1.1  mrg     rands = RANDS;
     60  1.1  mrg 
     61  1.1  mrg     ap = TMP_ALLOC_LIMBS (MAX_AN);
     62  1.1  mrg     refp = TMP_ALLOC_LIMBS (MAX_AN * 2);
     63  1.1  mrg     pp = 1 + TMP_ALLOC_LIMBS (MAX_AN * 2 + 2);
     64  1.1  mrg     scratch
     65  1.1  mrg       = 1+TMP_ALLOC_LIMBS (mpn_toomN_sqr_itch (MAX_AN) + 2);
     66  1.1  mrg 
     67  1.1  mrg     for (test = 0; test < count; test++)
     68  1.1  mrg       {
     69  1.1  mrg 	unsigned size_min;
     70  1.1  mrg 	unsigned size_range;
     71  1.1  mrg 	mp_size_t an;
     72  1.1  mrg 	mp_size_t itch;
     73  1.1  mrg 	mp_limb_t p_before, p_after, s_before, s_after;
     74  1.1  mrg 
     75  1.1  mrg 	an = MIN_AN
     76  1.1  mrg 	  + gmp_urandomm_ui (rands, MAX_AN - MIN_AN);
     77  1.1  mrg 
     78  1.1  mrg 	mpn_random2 (ap, an);
     79  1.1  mrg 	mpn_random2 (pp-1, an * 2 + 2);
     80  1.1  mrg 	p_before = pp[-1];
     81  1.1  mrg 	p_after = pp[an * 2];
     82  1.1  mrg 
     83  1.1  mrg 	itch = mpn_toomN_sqr_itch (an);
     84  1.1  mrg 	ASSERT_ALWAYS (itch <= mpn_toomN_sqr_itch (MAX_AN));
     85  1.1  mrg 	mpn_random2 (scratch-1, itch+2);
     86  1.1  mrg 	s_before = scratch[-1];
     87  1.1  mrg 	s_after = scratch[itch];
     88  1.1  mrg 
     89  1.1  mrg 	mpn_toomN_sqr (pp, ap, an, scratch);
     90  1.1  mrg 	refmpn_mul (refp, ap, an, ap, an);
     91  1.1  mrg 	if (pp[-1] != p_before || pp[an * 2] != p_after
     92  1.1  mrg 	    || scratch[-1] != s_before || scratch[itch] != s_after
     93  1.1  mrg 	    || mpn_cmp (refp, pp, an * 2) != 0)
     94  1.1  mrg 	  {
     95  1.1  mrg 	    printf ("ERROR in test %d, an = %d\n",
     96  1.1  mrg 		    test, (int) an);
     97  1.1  mrg 	    if (pp[-1] != p_before)
     98  1.1  mrg 	      {
     99  1.1  mrg 		printf ("before pp:"); mpn_dump (pp -1, 1);
    100  1.1  mrg 		printf ("keep:   "); mpn_dump (&p_before, 1);
    101  1.1  mrg 	      }
    102  1.1  mrg 	    if (pp[an * 2] != p_after)
    103  1.1  mrg 	      {
    104  1.1  mrg 		printf ("after pp:"); mpn_dump (pp + an * 2, 1);
    105  1.1  mrg 		printf ("keep:   "); mpn_dump (&p_after, 1);
    106  1.1  mrg 	      }
    107  1.1  mrg 	    if (scratch[-1] != s_before)
    108  1.1  mrg 	      {
    109  1.1  mrg 		printf ("before scratch:"); mpn_dump (scratch-1, 1);
    110  1.1  mrg 		printf ("keep:   "); mpn_dump (&s_before, 1);
    111  1.1  mrg 	      }
    112  1.1  mrg 	    if (scratch[itch] != s_after)
    113  1.1  mrg 	      {
    114  1.1  mrg 		printf ("after scratch:"); mpn_dump (scratch + itch, 1);
    115  1.1  mrg 		printf ("keep:   "); mpn_dump (&s_after, 1);
    116  1.1  mrg 	      }
    117  1.1  mrg 	    mpn_dump (ap, an);
    118  1.1  mrg 	    mpn_dump (pp, an * 2);
    119  1.1  mrg 	    mpn_dump (refp, an * 2);
    120  1.1  mrg 
    121  1.1  mrg 	    abort();
    122  1.1  mrg 	  }
    123  1.1  mrg       }
    124  1.1  mrg     TMP_FREE;
    125  1.1  mrg   }
    126  1.1  mrg 
    127  1.1  mrg   tests_end ();
    128  1.1  mrg   return 0;
    129  1.1  mrg }
    130