xref: /netbsd-src/external/lgpl3/gmp/dist/tests/t-modlinv.c (revision 33881f779a77dce6440bdc44610d94de75bebefe)
1 /* Test binvert_limb.
2 
3 Copyright 2000-2003 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 #include <string.h>
23 #include "gmp.h"
24 #include "gmp-impl.h"
25 #include "longlong.h"
26 #include "tests.h"
27 
28 
29 void
30 one (mp_limb_t n)
31 {
32   mp_limb_t  inv, prod;
33 
34   binvert_limb (inv, n);
35   prod = (inv * n) & GMP_NUMB_MASK;
36   if (prod != 1)
37     {
38       printf ("binvert_limb wrong\n");
39       mp_limb_trace ("  n       ", n);
40       mp_limb_trace ("  got     ", inv);
41       mp_limb_trace ("  product ", prod);
42       abort ();
43     }
44 }
45 
46 void
47 some (void)
48 {
49   int  i;
50   for (i = 0; i < 10000; i++)
51     one (refmpn_random_limb () | 1);
52 }
53 
54 void
55 all (void)
56 {
57   mp_limb_t  n;
58 
59   n = 1;
60   do {
61     one (n);
62     n += 2;
63   } while (n != 1);
64 }
65 
66 
67 int
68 main (int argc, char *argv[])
69 {
70   tests_start ();
71 
72   if (argc >= 2 && strcmp (argv[1], "-a") == 0)
73     {
74       /* it's feasible to run all values on a 32-bit limb, but not a 64-bit */
75       all ();
76     }
77   else
78     {
79       some ();
80     }
81 
82   tests_end ();
83   exit (0);
84 }
85