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