xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpn/t-gcd_22.c (revision 3f351f34c6d827cf017cdcff3543f6ec0c88b420)
1 /* Test mpn_gcd_22.
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 150000
28 #endif
29 
30 static void
31 one_test (mpz_srcptr a, mpz_srcptr b, mpz_srcptr ref)
32 {
33   mp_double_limb_t r = mpn_gcd_22 (mpz_getlimbn (a, 1), mpz_getlimbn (a, 0),
34 				   mpz_getlimbn (b, 1), mpz_getlimbn (b, 0));
35   if (r.d0 != mpz_getlimbn (ref, 0) || r.d1 != mpz_getlimbn (ref, 1))
36     {
37       gmp_fprintf (stderr,
38 		   "gcd_22 (0x%Zx, 0x%Zx) failed, got: g1 = 0x%Mx g0 = %Mx, ref: 0x%Zx\n",
39                    a, b, r.d1, r.d0, ref);
40       abort();
41     }
42 }
43 
44 int
45 main (int argc, char **argv)
46 {
47   mpz_t a, b, ref;
48   int count = COUNT;
49   int test;
50   gmp_randstate_ptr rands;
51 
52   TESTS_REPS (count, argv, argc);
53 
54   tests_start ();
55   rands = RANDS;
56 
57   mpz_init (a);
58   mpz_init (b);
59   mpz_init (ref);
60   for (test = 0; test < count; test++)
61     {
62       mp_bitcnt_t asize = 1 + gmp_urandomm_ui(rands, 2*GMP_NUMB_BITS);
63       mp_bitcnt_t bsize = 1 + gmp_urandomm_ui(rands, 2*GMP_NUMB_BITS);
64       if (test & 1)
65 	{
66 	  mpz_urandomb (a, rands, asize);
67 	  mpz_urandomb (b, rands, bsize);
68 	}
69       else
70 	{
71 	  mpz_rrandomb (a, rands, asize);
72 	  mpz_rrandomb (b, rands, bsize);
73 	}
74 
75       mpz_setbit (a, 0);
76       mpz_setbit (b, 0);
77       refmpz_gcd (ref, a, b);
78       one_test (a, b, ref);
79     }
80 
81   mpz_clear (a);
82   mpz_clear (b);
83   mpz_clear (ref);
84 
85   tests_end ();
86   return 0;
87 }
88