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