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-impl.h" 24 #include "longlong.h" 25 #include "tests.h" 26 27 28 void 29 one (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 45 void 46 some (void) 47 { 48 int i; 49 for (i = 0; i < 10000; i++) 50 one (refmpn_random_limb () | 1); 51 } 52 53 void 54 all (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 66 int 67 main (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