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

     1  /* Test for mulmid function.
     2  
     3  Copyright 2011 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 9
    31  #endif
    32  
    33  #ifndef COUNT
    34  #define COUNT 5000
    35  #endif
    36  
    37  #define MAX_N (1L << SIZE_LOG)
    38  
    39  int
    40  main (int argc, char **argv)
    41  {
    42    mp_ptr ap, bp, rp, refp;
    43    gmp_randstate_ptr rands;
    44    int test;
    45    TMP_DECL;
    46    TMP_MARK;
    47  
    48    tests_start ();
    49    rands = RANDS;
    50  
    51    ap = TMP_ALLOC_LIMBS (MAX_N);
    52    bp = TMP_ALLOC_LIMBS (MAX_N);
    53    rp = TMP_ALLOC_LIMBS (MAX_N + 2);
    54    refp = TMP_ALLOC_LIMBS (MAX_N + 2);
    55  
    56    for (test = 0; test < COUNT; test++)
    57      {
    58        mp_size_t an, bn, rn;
    59        unsigned size_log;
    60  
    61        size_log = 1 + gmp_urandomm_ui (rands, SIZE_LOG);
    62        an = 1 + gmp_urandomm_ui(rands, 1L << size_log);
    63  
    64        size_log = 1 + gmp_urandomm_ui (rands, SIZE_LOG);
    65        bn = 1 + gmp_urandomm_ui(rands, 1L << size_log);
    66  
    67        /* Make sure an >= bn */
    68        if (an < bn)
    69  	MP_SIZE_T_SWAP (an, bn);
    70  
    71        mpn_random2 (ap, an);
    72        mpn_random2 (bp, bn);
    73  
    74        refmpn_mulmid (refp, ap, an, bp, bn);
    75        mpn_mulmid (rp, ap, an, bp, bn);
    76  
    77        rn = an + 3 - bn;
    78        if (mpn_cmp (refp, rp, rn))
    79  	{
    80  	  printf ("ERROR in test %d, an = %d, bn = %d, rn = %d\n",
    81  		  test, (int) an, (int) bn, (int) rn);
    82  	  printf("a: "); mpn_dump (ap, an);
    83  	  printf("b: "); mpn_dump (bp, bn);
    84  	  printf("r:   "); mpn_dump (rp, rn);
    85  	  printf("ref: "); mpn_dump (refp, rn);
    86  
    87  	  abort();
    88  	}
    89      }
    90    TMP_FREE;
    91    tests_end ();
    92    return 0;
    93  }