Home | History | Annotate | Line # | Download | only in tests
      1 /*
      2 
      3 Copyright 2012, 2014, 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 #include <limits.h>
     21 #include <stdlib.h>
     22 #include <stdio.h>
     23 #include <string.h>
     24 
     25 #include "testutils.h"
     26 
     27 #define MAXBITS 400
     28 #define COUNT 10000
     29 
     30 #define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
     31 #define MAXLIMBS ((MAXBITS + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS)
     32 
     33 void
     34 testmain (int argc, char **argv)
     35 {
     36   unsigned i;
     37   mpz_t a, b, res, res_ui, ref, tz;
     38   mp_limb_t t[2*MAXLIMBS];
     39   mp_size_t an;
     40 
     41   mpz_init (a);
     42   mpz_init (b);
     43   mpz_init (res);
     44   mpz_init (res_ui);
     45   mpz_init (ref);
     46 
     47   for (i = 0; i < COUNT; i++)
     48     {
     49       mini_random_op3 (OP_MUL, MAXBITS, a, b, ref);
     50       mpz_mul (res, a, b);
     51       if (mpz_cmp (res, ref))
     52 	{
     53 	  fprintf (stderr, "mpz_mul failed:\n");
     54 	  dump ("a", a);
     55 	  dump ("b", b);
     56 	  dump ("r", res);
     57 	  dump ("ref", ref);
     58 	  abort ();
     59 	}
     60       if (mpz_size (a) == mpz_size (b))
     61 	{
     62 	  memset (t, 0x55, sizeof(t));
     63 	  an = mpz_size (a);
     64 	  if (an > 0)
     65 	    {
     66 	      mpn_mul_n (t, a->_mp_d, b->_mp_d, an);
     67 
     68 	      mpz_roinit_n (tz, t, 2*an);
     69 	      if (mpz_cmpabs (tz, ref))
     70 		{
     71 		  fprintf (stderr, "mpn_mul_n failed:\n");
     72 		  dump ("a", a);
     73 		  dump ("b", b);
     74 		  dump ("ref", ref);
     75 		  abort ();
     76 		}
     77 	    }
     78 	}
     79       if (mpz_fits_slong_p (b)) {
     80 	mpz_mul_si (res_ui, a, mpz_get_si (b));
     81 	if (mpz_cmp (res_ui, ref))
     82 	  {
     83 	    fprintf (stderr, "mpz_mul_si failed:\n");
     84 	    dump ("a", a);
     85 	    dump ("b", b);
     86 	    dump ("r", res_ui);
     87 	    dump ("ref", ref);
     88 	    abort ();
     89 	  }
     90       }
     91       mini_random_op2 (OP_SQR, MAXBITS, a, ref);
     92       an = mpz_size (a);
     93       if (an > 0)
     94 	{
     95 	  memset (t, 0x33, sizeof(t));
     96 	  mpn_sqr (t, mpz_limbs_read (a), an);
     97 
     98 	  mpz_roinit_n (tz, t, 2*an);
     99 	  if (mpz_cmp (tz, ref))
    100 	    {
    101 	      fprintf (stderr, "mpn (squaring) failed:\n");
    102 	      dump ("a", a);
    103 	      dump ("ref", ref);
    104 	      abort ();
    105 	    }
    106 	}
    107     }
    108   mpz_clear (a);
    109   mpz_clear (b);
    110   mpz_clear (res);
    111   mpz_clear (res_ui);
    112   mpz_clear (ref);
    113 }
    114