xref: /netbsd/external/lgpl3/gmp/dist/tests/mpn/t-mulmid.c (revision 671ea119)
1d25e02daSmrg /* Test for mulmid function.
2d25e02daSmrg 
3d25e02daSmrg Copyright 2011 Free Software Foundation, Inc.
4d25e02daSmrg 
5d25e02daSmrg This file is part of the GNU MP Library test suite.
6d25e02daSmrg 
7d25e02daSmrg The GNU MP Library test suite is free software; you can redistribute it
8d25e02daSmrg and/or modify it under the terms of the GNU General Public License as
9d25e02daSmrg published by the Free Software Foundation; either version 3 of the License,
10d25e02daSmrg or (at your option) any later version.
11d25e02daSmrg 
12d25e02daSmrg The GNU MP Library test suite is distributed in the hope that it will be
13d25e02daSmrg useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14d25e02daSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15d25e02daSmrg Public License for more details.
16d25e02daSmrg 
17d25e02daSmrg You should have received a copy of the GNU General Public License along with
18*f81b1c5bSmrg the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
19d25e02daSmrg 
20d25e02daSmrg 
21d25e02daSmrg #include <stdlib.h>
22d25e02daSmrg #include <stdio.h>
23d25e02daSmrg 
24d25e02daSmrg #include "gmp-impl.h"
25d25e02daSmrg #include "tests.h"
26d25e02daSmrg 
27d25e02daSmrg /* Sizes are up to 2^SIZE_LOG limbs */
28d25e02daSmrg #ifndef SIZE_LOG
29d25e02daSmrg #define SIZE_LOG 9
30d25e02daSmrg #endif
31d25e02daSmrg 
32d25e02daSmrg #ifndef COUNT
33d25e02daSmrg #define COUNT 5000
34d25e02daSmrg #endif
35d25e02daSmrg 
36d25e02daSmrg #define MAX_N (1L << SIZE_LOG)
37d25e02daSmrg 
38d25e02daSmrg int
main(int argc,char ** argv)39d25e02daSmrg main (int argc, char **argv)
40d25e02daSmrg {
41d25e02daSmrg   mp_ptr ap, bp, rp, refp;
42d25e02daSmrg   gmp_randstate_ptr rands;
43d25e02daSmrg   int test;
44d25e02daSmrg   TMP_DECL;
45d25e02daSmrg   TMP_MARK;
46d25e02daSmrg 
47d25e02daSmrg   tests_start ();
48d25e02daSmrg   rands = RANDS;
49d25e02daSmrg 
50d25e02daSmrg   ap = TMP_ALLOC_LIMBS (MAX_N);
51d25e02daSmrg   bp = TMP_ALLOC_LIMBS (MAX_N);
52d25e02daSmrg   rp = TMP_ALLOC_LIMBS (MAX_N + 2);
53d25e02daSmrg   refp = TMP_ALLOC_LIMBS (MAX_N + 2);
54d25e02daSmrg 
55d25e02daSmrg   for (test = 0; test < COUNT; test++)
56d25e02daSmrg     {
57d25e02daSmrg       mp_size_t an, bn, rn;
58d25e02daSmrg       unsigned size_log;
59d25e02daSmrg 
60d25e02daSmrg       size_log = 1 + gmp_urandomm_ui (rands, SIZE_LOG);
61d25e02daSmrg       an = 1 + gmp_urandomm_ui(rands, 1L << size_log);
62d25e02daSmrg 
63d25e02daSmrg       size_log = 1 + gmp_urandomm_ui (rands, SIZE_LOG);
64d25e02daSmrg       bn = 1 + gmp_urandomm_ui(rands, 1L << size_log);
65d25e02daSmrg 
66d25e02daSmrg       /* Make sure an >= bn */
67d25e02daSmrg       if (an < bn)
68d25e02daSmrg 	MP_SIZE_T_SWAP (an, bn);
69d25e02daSmrg 
70d25e02daSmrg       mpn_random2 (ap, an);
71d25e02daSmrg       mpn_random2 (bp, bn);
72d25e02daSmrg 
73d25e02daSmrg       refmpn_mulmid (refp, ap, an, bp, bn);
74d25e02daSmrg       mpn_mulmid (rp, ap, an, bp, bn);
75d25e02daSmrg 
76d25e02daSmrg       rn = an + 3 - bn;
77d25e02daSmrg       if (mpn_cmp (refp, rp, rn))
78d25e02daSmrg 	{
79d25e02daSmrg 	  printf ("ERROR in test %d, an = %d, bn = %d, rn = %d\n",
80*f81b1c5bSmrg 		  test, (int) an, (int) bn, (int) rn);
81d25e02daSmrg 	  printf("a: "); mpn_dump (ap, an);
82d25e02daSmrg 	  printf("b: "); mpn_dump (bp, bn);
83d25e02daSmrg 	  printf("r:   "); mpn_dump (rp, rn);
84d25e02daSmrg 	  printf("ref: "); mpn_dump (refp, rn);
85d25e02daSmrg 
86d25e02daSmrg 	  abort();
87d25e02daSmrg 	}
88d25e02daSmrg     }
89d25e02daSmrg   TMP_FREE;
90d25e02daSmrg   tests_end ();
91d25e02daSmrg   return 0;
92d25e02daSmrg }
93