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