(root)/
gmp-6.3.0/
tests/
mpn/
toom-sqr-shared.h
       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 https://www.gnu.org/licenses/.  */
      19  
      20  
      21  #include <stdlib.h>
      22  #include <stdio.h>
      23  
      24  #include "gmp-impl.h"
      25  #include "tests.h"
      26  
      27  /* Main file is expected to define mpn_toomN_mul, mpn_toomN_sqr_itch,
      28   * MIN_AN, MAX_AN and then include this file. */
      29  
      30  #ifndef COUNT
      31  #define COUNT 2000
      32  #endif
      33  
      34  #ifdef MORE_SQR_TESTS
      35  void MORE_SQR_TESTS (gmp_randstate_ptr);
      36  #endif
      37  
      38  int
      39  main (int argc, char **argv)
      40  {
      41    mp_ptr ap, refp, pp, scratch;
      42    int count = COUNT;
      43    int test;
      44    gmp_randstate_ptr rands;
      45    TMP_DECL;
      46    TMP_MARK;
      47  
      48    TESTS_REPS (count, argv, argc);
      49  
      50    tests_start ();
      51  
      52    if (MAX_AN > MIN_AN) {
      53      rands = RANDS;
      54  
      55      ap = TMP_ALLOC_LIMBS (MAX_AN);
      56      refp = TMP_ALLOC_LIMBS (MAX_AN * 2);
      57      pp = 1 + TMP_ALLOC_LIMBS (MAX_AN * 2 + 2);
      58      scratch
      59        = 1+TMP_ALLOC_LIMBS (mpn_toomN_sqr_itch (MAX_AN) + 2);
      60  
      61      for (test = 0; test < count; test++)
      62        {
      63  	mp_size_t an;
      64  	mp_size_t itch;
      65  	mp_limb_t p_before, p_after, s_before, s_after;
      66  
      67  	an = MIN_AN
      68  	  + gmp_urandomm_ui (rands, MAX_AN - MIN_AN);
      69  
      70  	mpn_random2 (ap, an);
      71  	mpn_random2 (pp-1, an * 2 + 2);
      72  	p_before = pp[-1];
      73  	p_after = pp[an * 2];
      74  
      75  	itch = mpn_toomN_sqr_itch (an);
      76  	ASSERT_ALWAYS (itch <= mpn_toomN_sqr_itch (MAX_AN));
      77  	mpn_random2 (scratch-1, itch+2);
      78  	s_before = scratch[-1];
      79  	s_after = scratch[itch];
      80  
      81  	mpn_toomN_sqr (pp, ap, an, scratch);
      82  	refmpn_mul (refp, ap, an, ap, an);
      83  	if (pp[-1] != p_before || pp[an * 2] != p_after
      84  	    || scratch[-1] != s_before || scratch[itch] != s_after
      85  	    || mpn_cmp (refp, pp, an * 2) != 0)
      86  	  {
      87  	    printf ("ERROR in test %d, an = %d\n",
      88  		    test, (int) an);
      89  	    if (pp[-1] != p_before)
      90  	      {
      91  		printf ("before pp:"); mpn_dump (pp -1, 1);
      92  		printf ("keep:   "); mpn_dump (&p_before, 1);
      93  	      }
      94  	    if (pp[an * 2] != p_after)
      95  	      {
      96  		printf ("after pp:"); mpn_dump (pp + an * 2, 1);
      97  		printf ("keep:   "); mpn_dump (&p_after, 1);
      98  	      }
      99  	    if (scratch[-1] != s_before)
     100  	      {
     101  		printf ("before scratch:"); mpn_dump (scratch-1, 1);
     102  		printf ("keep:   "); mpn_dump (&s_before, 1);
     103  	      }
     104  	    if (scratch[itch] != s_after)
     105  	      {
     106  		printf ("after scratch:"); mpn_dump (scratch + itch, 1);
     107  		printf ("keep:   "); mpn_dump (&s_after, 1);
     108  	      }
     109  	    mpn_dump (ap, an);
     110  	    mpn_dump (pp, an * 2);
     111  	    mpn_dump (refp, an * 2);
     112  
     113  	    abort();
     114  	  }
     115        }
     116      TMP_FREE;
     117  
     118  #ifdef MORE_SQR_TESTS
     119    MORE_SQR_TESTS (rands);
     120  #endif
     121    }
     122  
     123    tests_end ();
     124    return 0;
     125  }