1/* Test mpn_mod_1 variants. 2 3Copyright 2010 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 23#include "gmp.h" 24#include "gmp-impl.h" 25#include "tests.h" 26 27static void 28check_one (mp_srcptr ap, mp_size_t n, mp_limb_t b) 29{ 30 mp_limb_t r_ref = refmpn_mod_1 (ap, n, b); 31 mp_limb_t r; 32 33 if (n >= 2) 34 { 35 mp_limb_t pre[4]; 36 mpn_mod_1_1p_cps (pre, b); 37 r = mpn_mod_1_1p (ap, n, b << pre[1], pre); 38 if (r != r_ref) 39 { 40 printf ("mpn_mod_1_1p failed\n"); 41 goto fail; 42 } 43 } 44 if ((b & GMP_NUMB_HIGHBIT) == 0) 45 { 46 mp_limb_t pre[5]; 47 mpn_mod_1s_2p_cps (pre, b); 48 r = mpn_mod_1s_2p (ap, n, b << pre[1], pre); 49 if (r != r_ref) 50 { 51 printf ("mpn_mod_1s_2p failed\n"); 52 goto fail; 53 } 54 } 55 if (b <= GMP_NUMB_MASK / 4) 56 { 57 mp_limb_t pre[7]; 58 mpn_mod_1s_4p_cps (pre, b); 59 r = mpn_mod_1s_4p (ap, n, b << pre[1], pre); 60 if (r != r_ref) 61 { 62 printf ("mpn_mod_1s_4p failed\n"); 63 goto fail; 64 } 65 } 66 r = mpn_mod_1 (ap, n, b); 67 if (r != r_ref) 68 { 69 printf ("mpn_mod_1 failed\n"); 70 fail: 71 printf ("an = %d, a: ", (int) n); mpn_dump (ap, n); 72 printf ("b : "); mpn_dump (&b, 1); 73 printf ("r (expected): "); mpn_dump (&r_ref, 1); 74 printf ("r (bad) : "); mpn_dump (&r, 1); 75 abort(); 76 } 77} 78 79int 80main (int argc, char **argv) 81{ 82 gmp_randstate_ptr rands; 83 int i; 84 unsigned a_bits; 85 unsigned b_bits; 86 mpz_t a; 87 mpz_t b; 88 89 tests_start (); 90 rands = RANDS; 91 mpz_init (a); 92 mpz_init (b); 93 94 for (i = 0; i < 300; i++) 95 { 96 mp_size_t asize; 97 a_bits = 1 + gmp_urandomm_ui (rands, 1000); 98 b_bits = 1 + gmp_urandomm_ui (rands, GMP_NUMB_BITS); 99 100 mpz_rrandomb (a, rands, a_bits); 101 mpz_rrandomb (b, rands, b_bits); 102 103 asize = SIZ(a); 104 if (!asize) 105 asize = 1; 106 if (mpz_sgn (b) == 0) 107 mpz_set_ui (b, 1); 108 109 check_one (PTR(a), asize, PTR(b)[0]); 110 } 111 112 mpz_clear (a); 113 mpz_clear (b); 114 115 tests_end (); 116 return 0; 117} 118 119