1/* Test binvert_limb.
2
3Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library.  If not, see http://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
29void
30one (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
46void
47some (void)
48{
49  int  i;
50  for (i = 0; i < 10000; i++)
51    one (refmpn_random_limb () | 1);
52}
53
54void
55all (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
67int
68main (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