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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/* Test file for:
mpfr_fits_sint_p, mpfr_fits_slong_p, mpfr_fits_sshort_p,
mpfr_fits_uint_p, mpfr_fits_ulong_p, mpfr_fits_ushort_p
Copyright 2004-2019 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.
This file is part of the GNU MPFR Library.
The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 MPFR Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
/* Needed due to the inclusion of mpfr-intmax.h */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "mpfr-intmax.h"
#include "mpfr-test.h"
#define FTEST_AUX(N,NOT,FCT) \
do \
{ \
__gmpfr_flags = ex_flags; \
if (NOT FCT (x, (mpfr_rnd_t) r)) \
{ \
printf ("Error %d for %s, rnd = %s and x = ", \
N, #FCT, \
mpfr_print_rnd_mode ((mpfr_rnd_t) r)); \
mpfr_dump (x); \
exit (1); \
} \
if (__gmpfr_flags != ex_flags) \
{ \
mpfr_flags_t flags = __gmpfr_flags; \
printf ("Flags error %d for %s, rnd = %s and x = ", \
N, #FCT, \
mpfr_print_rnd_mode ((mpfr_rnd_t) r)); \
mpfr_dump(x); \
printf ("Expected flags:"); \
flags_out (ex_flags); \
printf ("Got flags: "); \
flags_out (flags); \
exit (1); \
} \
} \
while (0)
#define FTEST(N,NOT,FCT) \
do \
{ \
mpfr_exp_t e; \
FTEST_AUX (N,NOT,FCT); \
if (MPFR_IS_SINGULAR (x)) \
break; \
e = mpfr_get_exp (x); \
set_emin (e); \
set_emax (e); \
FTEST_AUX (N,NOT,FCT); \
set_emin (emin); \
set_emax (emax); \
} \
while (0)
#define CHECK_ALL(N,NOT) \
do \
{ \
FTEST (N, NOT, mpfr_fits_ulong_p); \
FTEST (N, NOT, mpfr_fits_slong_p); \
FTEST (N, NOT, mpfr_fits_uint_p); \
FTEST (N, NOT, mpfr_fits_sint_p); \
FTEST (N, NOT, mpfr_fits_ushort_p); \
FTEST (N, NOT, mpfr_fits_sshort_p); \
} \
while (0)
#define CHECK_MAX(N,NOT) \
do \
{ \
FTEST (N, NOT, mpfr_fits_uintmax_p); \
FTEST (N, NOT, mpfr_fits_intmax_p); \
} \
while (0)
/* V is a non-zero limit for the type (*_MIN for a signed type or *_MAX).
* If V is positive, then test V, V + 1/4, V + 3/4 and V + 1.
* If V is negative, then test V, V - 1/4, V - 3/4 and V - 1.
*/
#define CHECK_LIM(N,V,SET,FCT) \
do \
{ \
SET (x, V, MPFR_RNDN); \
FTEST (N, !, FCT); \
mpfr_set_si_2exp (y, (V) < 0 ? -1 : 1, -2, MPFR_RNDN); \
mpfr_add (x, x, y, MPFR_RNDN); \
FTEST (N+1, (r == MPFR_RNDN || \
MPFR_IS_LIKE_RNDZ (r, (V) < 0)) ^ !!, FCT); \
mpfr_add (x, x, y, MPFR_RNDN); \
mpfr_add (x, x, y, MPFR_RNDN); \
FTEST (N+3, MPFR_IS_LIKE_RNDZ (r, (V) < 0) ^ !!, FCT); \
mpfr_add (x, x, y, MPFR_RNDN); \
FTEST (N+4, !!, FCT); \
} \
while (0)
int
main (void)
{
mpfr_exp_t emin, emax;
mpfr_t x, y;
mpfr_flags_t flags[2] = { 0, MPFR_FLAGS_ALL }, ex_flags;
int i, r, fi;
tests_start_mpfr ();
emin = mpfr_get_emin ();
emax = mpfr_get_emax ();
mpfr_init2 (x, sizeof (unsigned long) * CHAR_BIT + 2);
mpfr_init2 (y, 8);
RND_LOOP (r)
for (fi = 0; fi < numberof (flags); fi++)
{
ex_flags = flags[fi];
/* Check NaN */
mpfr_set_nan (x);
CHECK_ALL (1, !!);
/* Check +Inf */
mpfr_set_inf (x, 1);
CHECK_ALL (2, !!);
/* Check -Inf */
mpfr_set_inf (x, -1);
CHECK_ALL (3, !!);
/* Check +0 */
mpfr_set_zero (x, 1);
CHECK_ALL (4, !);
/* Check -0 */
mpfr_set_zero (x, -1);
CHECK_ALL (5, !);
/* Check small positive op */
mpfr_set_str1 (x, "1@-1");
CHECK_ALL (6, !);
/* Check 17 */
mpfr_set_ui (x, 17, MPFR_RNDN);
CHECK_ALL (7, !);
/* Check large values (no fit) */
mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN);
mpfr_mul_2ui (x, x, 1, MPFR_RNDN);
CHECK_ALL (8, !!);
mpfr_mul_2ui (x, x, 40, MPFR_RNDN);
CHECK_ALL (9, !!);
/* Check a non-integer number just below a power of two. */
mpfr_set_ui_2exp (x, 255, -2, MPFR_RNDN);
CHECK_ALL (10, !);
/* Check the limits of the types (except 0 for unsigned types) */
CHECK_LIM (20, ULONG_MAX, mpfr_set_ui, mpfr_fits_ulong_p);
CHECK_LIM (30, LONG_MAX, mpfr_set_si, mpfr_fits_slong_p);
CHECK_LIM (35, LONG_MIN, mpfr_set_si, mpfr_fits_slong_p);
CHECK_LIM (40, UINT_MAX, mpfr_set_ui, mpfr_fits_uint_p);
CHECK_LIM (50, INT_MAX, mpfr_set_si, mpfr_fits_sint_p);
CHECK_LIM (55, INT_MIN, mpfr_set_si, mpfr_fits_sint_p);
CHECK_LIM (60, USHRT_MAX, mpfr_set_ui, mpfr_fits_ushort_p);
CHECK_LIM (70, SHRT_MAX, mpfr_set_si, mpfr_fits_sshort_p);
CHECK_LIM (75, SHRT_MIN, mpfr_set_si, mpfr_fits_sshort_p);
/* Check negative op */
for (i = 1; i <= 4; i++)
{
int inv;
mpfr_set_si_2exp (x, -i, -2, MPFR_RNDN);
/* for RNDF, it fits if it fits when rounding away from zero */
mpfr_rint (y, x, r != MPFR_RNDF ? (mpfr_rnd_t) r : MPFR_RNDA);
inv = MPFR_NOTZERO (y);
FTEST (80, inv ^ !, mpfr_fits_ulong_p);
FTEST (81, !, mpfr_fits_slong_p);
FTEST (82, inv ^ !, mpfr_fits_uint_p);
FTEST (83, !, mpfr_fits_sint_p);
FTEST (84, inv ^ !, mpfr_fits_ushort_p);
FTEST (85, !, mpfr_fits_sshort_p);
}
}
#ifdef _MPFR_H_HAVE_INTMAX_T
mpfr_set_prec (x, sizeof (uintmax_t) * CHAR_BIT + 2);
RND_LOOP (r)
{
/* Check NaN */
mpfr_set_nan (x);
CHECK_MAX (1, !!);
/* Check +Inf */
mpfr_set_inf (x, 1);
CHECK_MAX (2, !!);
/* Check -Inf */
mpfr_set_inf (x, -1);
CHECK_MAX (3, !!);
/* Check +0 */
mpfr_set_zero (x, 1);
CHECK_MAX (4, !);
/* Check -0 */
mpfr_set_zero (x, -1);
CHECK_MAX (5, !);
/* Check small positive op */
mpfr_set_str1 (x, "1@-1");
CHECK_MAX (6, !);
/* Check 17 */
mpfr_set_ui (x, 17, MPFR_RNDN);
CHECK_MAX (7, !);
/* Check hugest */
mpfr_set_ui_2exp (x, 42, sizeof (uintmax_t) * 32, MPFR_RNDN);
CHECK_MAX (8, !!);
/* Check a non-integer number just below a power of two. */
mpfr_set_ui_2exp (x, 255, -2, MPFR_RNDN);
CHECK_MAX (10, !);
/* Check the limits of the types (except 0 for uintmax_t) */
CHECK_LIM (20, MPFR_UINTMAX_MAX, mpfr_set_uj, mpfr_fits_uintmax_p);
CHECK_LIM (30, MPFR_INTMAX_MAX, mpfr_set_sj, mpfr_fits_intmax_p);
CHECK_LIM (35, MPFR_INTMAX_MIN, mpfr_set_sj, mpfr_fits_intmax_p);
/* Check negative op */
for (i = 1; i <= 4; i++)
{
int inv;
mpfr_set_si_2exp (x, -i, -2, MPFR_RNDN);
mpfr_rint (y, x, (mpfr_rnd_t) r);
inv = MPFR_NOTZERO (y);
if (r != MPFR_RNDF)
FTEST (80, inv ^ !, mpfr_fits_uintmax_p);
else
FTEST (80, !!, mpfr_fits_uintmax_p);
FTEST (81, !, mpfr_fits_intmax_p);
}
}
#endif /* _MPFR_H_HAVE_INTMAX_T */
mpfr_clear (x);
mpfr_clear (y);
tests_end_mpfr ();
return 0;
}
|