xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpn/t-gcd_11.c (revision 122b5006ee1bd67145794b4cde92f4fe4781a5ec)
1 /* Test mpn_gcd_11.
2 
3 Copyright 2019 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 <stdio.h>
21 #include <stdlib.h>
22 
23 #include "gmp-impl.h"
24 #include "tests.h"
25 
26 #ifndef COUNT
27 #define COUNT 500000
28 #endif
29 
30 static void
31 one_test (mp_limb_t a, mp_limb_t b, mp_limb_t ref)
32 {
33   mp_limb_t r = mpn_gcd_11 (a, b);
34   if (r != ref)
35     {
36       gmp_fprintf (stderr,
37 		   "gcd_11 (0x%Mx, 0x%Mx) failed, got: 0x%Mx, ref: 0x%Mx\n",
38 		   a, b, r, ref);
39       abort();
40     }
41 }
42 
43 int
44 main (int argc, char **argv)
45 {
46   mpz_t a, b;
47   int count = COUNT;
48   int test;
49   gmp_randstate_ptr rands;
50 
51   TESTS_REPS (count, argv, argc);
52 
53   tests_start ();
54   rands = RANDS;
55 
56   mpz_init (a);
57   mpz_init (b);
58   for (test = 0; test < count; test++)
59     {
60       mp_limb_t al, bl;
61       mp_bitcnt_t asize = 1 + gmp_urandomm_ui(rands, GMP_NUMB_BITS);
62       mp_bitcnt_t bsize = 1 + gmp_urandomm_ui(rands, GMP_NUMB_BITS);
63       if (test & 1)
64 	{
65 	  mpz_urandomb (a, rands, asize);
66 	  mpz_urandomb (b, rands, bsize);
67 	}
68       else
69 	{
70 	  mpz_rrandomb (a, rands, asize);
71 	  mpz_rrandomb (b, rands, bsize);
72 	}
73 
74       mpz_setbit (a, 0);
75       mpz_setbit (b, 0);
76       al = mpz_getlimbn (a, 0);
77       bl = mpz_getlimbn (b, 0);
78       one_test (al, bl, refmpn_gcd_11 (al, bl));
79     }
80 
81   mpz_clear (a);
82   mpz_clear (b);
83 
84   tests_end ();
85   return 0;
86 }
87