github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpn/t-sqrlo.c (about)

     1  /* Test for sqrlo function.
     2  
     3  Copyright 2009, 2015 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.h"
    25  #include "gmp-impl.h"
    26  #include "tests.h"
    27  
    28  /* Sizes are up to 2^SIZE_LOG limbs */
    29  #ifndef SIZE_LOG
    30  #define SIZE_LOG 10
    31  #endif
    32  
    33  #ifndef COUNT
    34  #define COUNT 10000
    35  #endif
    36  
    37  #define MAX_N (1L << SIZE_LOG)
    38  #define MIN_N (1)
    39  
    40  int
    41  main (int argc, char **argv)
    42  {
    43    mp_ptr ap, refp, pp, scratch;
    44    int count = COUNT;
    45    int test;
    46    gmp_randstate_ptr rands;
    47    TMP_DECL;
    48    TMP_MARK;
    49  
    50    if (argc > 1)
    51      {
    52        char *end;
    53        count = strtol (argv[1], &end, 0);
    54        if (*end || count <= 0)
    55  	{
    56  	  fprintf (stderr, "Invalid test count: %s.\n", argv[1]);
    57  	  return 1;
    58  	}
    59      }
    60  
    61    tests_start ();
    62    rands = RANDS;
    63  
    64  #define mpn_sqrlo_itch(n) (0)
    65  
    66    ap = TMP_ALLOC_LIMBS (MAX_N);
    67    refp = TMP_ALLOC_LIMBS (MAX_N * 2);
    68    pp = 1+TMP_ALLOC_LIMBS (MAX_N + 2);
    69    scratch
    70      = 1+TMP_ALLOC_LIMBS (mpn_sqrlo_itch (MAX_N) + 2);
    71  
    72    for (test = 0; test < count; test++)
    73      {
    74        unsigned size_min;
    75        unsigned size_range;
    76        mp_size_t n;
    77        mp_size_t itch;
    78        mp_limb_t p_before, p_after, s_before, s_after;
    79  
    80        for (size_min = 1; (1L << size_min) < MIN_N; size_min++)
    81  	;
    82  
    83        /* We generate an in the MIN_N <= n <= (1 << size_range). */
    84        size_range = size_min
    85  	+ gmp_urandomm_ui (rands, SIZE_LOG + 1 - size_min);
    86  
    87        n = MIN_N
    88  	+ gmp_urandomm_ui (rands, (1L << size_range) + 1 - MIN_N);
    89  
    90        mpn_random2 (ap, n);
    91        mpn_random2 (pp-1, n + 2);
    92        p_before = pp[-1];
    93        p_after = pp[n];
    94  
    95        itch = mpn_sqrlo_itch (n);
    96  #if 0
    97        ASSERT_ALWAYS (itch <= mpn_sqrlo_itch (MAX_N));
    98        mpn_random2 (scratch-1, itch+2);
    99  #endif
   100        s_before = scratch[-1];
   101        s_after = scratch[itch];
   102  
   103        mpn_sqrlo (pp, ap, n);
   104        mpn_sqr (refp, ap, n);
   105        if (pp[-1] != p_before || pp[n] != p_after
   106  	  || scratch[-1] != s_before || scratch[itch] != s_after
   107  	  || mpn_cmp (refp, pp, n) != 0)
   108  	{
   109  	  printf ("ERROR in test %d, n = %d",
   110  		  test, (int) n);
   111  	  if (pp[-1] != p_before)
   112  	    {
   113  	      printf ("before pp:"); mpn_dump (pp -1, 1);
   114  	      printf ("keep:   "); mpn_dump (&p_before, 1);
   115  	    }
   116  	  if (pp[n] != p_after)
   117  	    {
   118  	      printf ("after pp:"); mpn_dump (pp + n, 1);
   119  	      printf ("keep:   "); mpn_dump (&p_after, 1);
   120  	    }
   121  	  if (scratch[-1] != s_before)
   122  	    {
   123  	      printf ("before scratch:"); mpn_dump (scratch-1, 1);
   124  	      printf ("keep:   "); mpn_dump (&s_before, 1);
   125  	    }
   126  	  if (scratch[itch] != s_after)
   127  	    {
   128  	      printf ("after scratch:"); mpn_dump (scratch + itch, 1);
   129  	      printf ("keep:   "); mpn_dump (&s_after, 1);
   130  	    }
   131  	  mpn_dump (ap, n);
   132  	  mpn_dump (pp, n);
   133  	  mpn_dump (refp, n);
   134  
   135  	  abort();
   136  	}
   137      }
   138    TMP_FREE;
   139    tests_end ();
   140    return 0;
   141  }