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