xref: /netbsd-src/external/lgpl3/gmp/dist/tests/mpn/t-brootinv.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Copyright 2012, 2015 Free Software Foundation, Inc.
2 
3 This file is part of the GNU MP Library test suite.
4 
5 The GNU MP Library test suite is free software; you can redistribute it
6 and/or modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 3 of the License,
8 or (at your option) any later version.
9 
10 The GNU MP Library test suite is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
13 Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along with
16 the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
17 
18 
19 #include <stdlib.h>		/* for strtol */
20 #include <stdio.h>		/* for printf */
21 
22 #include "gmp-impl.h"
23 #include "longlong.h"
24 #include "tests/tests.h"
25 
26 #define MAX_LIMBS 150
27 #define COUNT 500
28 
29 int
main(int argc,char ** argv)30 main (int argc, char **argv)
31 {
32   gmp_randstate_ptr rands;
33 
34   mp_ptr ap, rp, pp, app, scratch;
35   int count = COUNT;
36   unsigned i;
37   TMP_DECL;
38 
39   TMP_MARK;
40 
41   TESTS_REPS (count, argv, argc);
42 
43   tests_start ();
44   rands = RANDS;
45 
46   ap = TMP_ALLOC_LIMBS (MAX_LIMBS);
47   rp = TMP_ALLOC_LIMBS (MAX_LIMBS);
48   pp = TMP_ALLOC_LIMBS (MAX_LIMBS);
49   app = TMP_ALLOC_LIMBS (MAX_LIMBS);
50   scratch = TMP_ALLOC_LIMBS (5*MAX_LIMBS);
51 
52   for (i = 0; i < count; i++)
53     {
54       mp_size_t n;
55       mp_limb_t k;
56 
57       n = 1 + gmp_urandomm_ui (rands, MAX_LIMBS);
58 
59       if (i & 1)
60 	mpn_random2 (ap, n);
61       else
62 	mpn_random (ap, n);
63 
64       ap[0] |= 1;
65 
66       if (i < 100)
67 	k = 3 + 2*i;
68       else
69 	{
70 	  mpn_random (&k, 1);
71 	  if (k < 3)
72 	    k = 3;
73 	  else
74 	    k |= 1;
75 	}
76       mpn_brootinv (rp, ap, n, k, scratch);
77       mpn_powlo (pp, rp, &k, 1, n, scratch);
78       mpn_mullo_n (app, ap, pp, n);
79 
80       if (app[0] != 1 || !(n == 1 || mpn_zero_p (app+1, n-1)))
81 	{
82 	  gmp_fprintf (stderr,
83 		       "mpn_brootinv returned bad result: %u limbs\n",
84 		       (unsigned) n);
85 	  gmp_fprintf (stderr, "k     = %Mx\n", k);
86 	  gmp_fprintf (stderr, "a     = %Nx\n", ap, n);
87 	  gmp_fprintf (stderr, "r     = %Nx\n", rp, n);
88 	  gmp_fprintf (stderr, "r^n   = %Nx\n", pp, n);
89 	  gmp_fprintf (stderr, "a r^n = %Nx\n", app, n);
90 	  abort ();
91 	}
92     }
93   TMP_FREE;
94   tests_end ();
95   return 0;
96 }
97