1/* Test binvert_limb.
2
3Copyright 2000-2003 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library test suite.
6
7The GNU MP Library test suite is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 3 of the License,
10or (at your option) any later version.
11
12The GNU MP Library test suite is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15Public License for more details.
16
17You should have received a copy of the GNU General Public License along with
18the 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-impl.h"
24#include "longlong.h"
25#include "tests.h"
26
27
28void
29one (mp_limb_t n)
30{
31  mp_limb_t  inv, prod;
32
33  binvert_limb (inv, n);
34  prod = (inv * n) & GMP_NUMB_MASK;
35  if (prod != 1)
36    {
37      printf ("binvert_limb wrong\n");
38      mp_limb_trace ("  n       ", n);
39      mp_limb_trace ("  got     ", inv);
40      mp_limb_trace ("  product ", prod);
41      abort ();
42    }
43}
44
45void
46some (void)
47{
48  int  i;
49  for (i = 0; i < 10000; i++)
50    one (refmpn_random_limb () | 1);
51}
52
53void
54all (void)
55{
56  mp_limb_t  n;
57
58  n = 1;
59  do {
60    one (n);
61    n += 2;
62  } while (n != 1);
63}
64
65
66int
67main (int argc, char *argv[])
68{
69  tests_start ();
70
71  if (argc >= 2 && strcmp (argv[1], "-a") == 0)
72    {
73      /* it's feasible to run all values on a 32-bit limb, but not a 64-bit */
74      all ();
75    }
76  else
77    {
78      some ();
79    }
80
81  tests_end ();
82  exit (0);
83}
84