1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/* Test mpz_[cft]div_[qr]_2exp.
Copyright 2001 Free Software Foundation, Inc.
This file is part of the GNU MP Library test suite.
The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
#include <stdio.h>
#include <stdlib.h>
#include "gmp-impl.h"
#include "tests.h"
/* If the remainder is in the correct range and q*d+r is correct, then q
must have rounded correctly. */
void
check_one (mpz_srcptr a, unsigned long d)
{
mpz_t q, r, p, d2exp;
int inplace;
mpz_init (d2exp);
mpz_init (q);
mpz_init (r);
mpz_init (p);
mpz_set_ui (d2exp, 1L);
mpz_mul_2exp (d2exp, d2exp, d);
#define INPLACE(fun,dst,src,d) \
if (inplace) \
{ \
mpz_set (dst, src); \
fun (dst, dst, d); \
} \
else \
fun (dst, src, d);
for (inplace = 0; inplace <= 1; inplace++)
{
INPLACE (mpz_fdiv_q_2exp, q, a, d);
INPLACE (mpz_fdiv_r_2exp, r, a, d);
mpz_mul_2exp (p, q, d);
mpz_add (p, p, r);
if (mpz_sgn (r) < 0 || mpz_cmp (r, d2exp) >= 0)
{
printf ("mpz_fdiv_r_2exp result out of range\n");
goto error;
}
if (mpz_cmp (p, a) != 0)
{
printf ("mpz_fdiv_[qr]_2exp doesn't multiply back\n");
goto error;
}
INPLACE (mpz_cdiv_q_2exp, q, a, d);
INPLACE (mpz_cdiv_r_2exp, r, a, d);
mpz_mul_2exp (p, q, d);
mpz_add (p, p, r);
if (mpz_sgn (r) > 0 || mpz_cmpabs (r, d2exp) >= 0)
{
printf ("mpz_cdiv_r_2exp result out of range\n");
goto error;
}
if (mpz_cmp (p, a) != 0)
{
printf ("mpz_cdiv_[qr]_2exp doesn't multiply back\n");
goto error;
}
INPLACE (mpz_tdiv_q_2exp, q, a, d);
INPLACE (mpz_tdiv_r_2exp, r, a, d);
mpz_mul_2exp (p, q, d);
mpz_add (p, p, r);
if (mpz_sgn (r) != 0 && mpz_sgn (r) != mpz_sgn (a))
{
printf ("mpz_tdiv_r_2exp result wrong sign\n");
goto error;
}
if (mpz_cmpabs (r, d2exp) >= 0)
{
printf ("mpz_tdiv_r_2exp result out of range\n");
goto error;
}
if (mpz_cmp (p, a) != 0)
{
printf ("mpz_tdiv_[qr]_2exp doesn't multiply back\n");
goto error;
}
}
mpz_clear (d2exp);
mpz_clear (q);
mpz_clear (r);
mpz_clear (p);
return;
error:
mpz_trace ("a", a);
printf ("d=%lu\n", d);
mpz_trace ("q", q);
mpz_trace ("r", r);
mpz_trace ("p", p);
mp_trace_base = -16;
mpz_trace ("a", a);
printf ("d=0x%lX\n", d);
mpz_trace ("q", q);
mpz_trace ("r", r);
mpz_trace ("p", p);
abort ();
}
void
check_all (mpz_ptr a, unsigned long d)
{
check_one (a, d);
mpz_neg (a, a);
check_one (a, d);
}
void
check_various (void)
{
static const unsigned long table[] = {
0, 1, 2, 3, 4, 5,
GMP_NUMB_BITS-1, GMP_NUMB_BITS, GMP_NUMB_BITS+1,
2*GMP_NUMB_BITS-1, 2*GMP_NUMB_BITS, 2*GMP_NUMB_BITS+1,
3*GMP_NUMB_BITS-1, 3*GMP_NUMB_BITS, 3*GMP_NUMB_BITS+1,
4*GMP_NUMB_BITS-1, 4*GMP_NUMB_BITS, 4*GMP_NUMB_BITS+1
};
int i, j;
unsigned long n, d;
mpz_t a;
mpz_init (a);
/* a==0, and various d */
mpz_set_ui (a, 0L);
for (i = 0; i < numberof (table); i++)
check_one (a, table[i]);
/* a==2^n, and various d */
for (i = 0; i < numberof (table); i++)
{
n = table[i];
mpz_set_ui (a, 1L);
mpz_mul_2exp (a, a, n);
for (j = 0; j < numberof (table); j++)
{
d = table[j];
check_all (a, d);
}
}
mpz_clear (a);
}
void
check_random (int argc, char *argv[])
{
gmp_randstate_ptr rands = RANDS;
int reps = 100;
mpz_t a;
unsigned long d;
int i;
if (argc == 2)
reps = atoi (argv[1]);
mpz_init (a);
for (i = 0; i < reps; i++)
{
/* exponentially within 2 to 257 bits */
mpz_erandomb (a, rands, urandom () % 8 + 2);
d = urandom () % 256;
check_all (a, d);
}
mpz_clear (a);
}
int
main (int argc, char *argv[])
{
tests_start ();
check_various ();
check_random (argc, argv);
tests_end ();
exit (0);
}
|