Tizen 2.1 base
[external/gmp.git] / tests / t-modlinv.c
1 /* Test binvert_limb.
2
3 Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along 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
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 }