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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
/* Test file for mpfr_set_si and mpfr_set_ui.
Copyright 1999, 2001-2016 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. */
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "mpfr-test.h"
#define ERROR(str) do { printf ("Error for " str "\n"); exit (1); } while (0)
static void
test_2exp (void)
{
mpfr_t x;
int res;
mpfr_init2 (x, 32);
mpfr_set_ui_2exp (x, 1, 0, MPFR_RNDN);
if (mpfr_cmp_ui(x, 1))
ERROR("(1U,0)");
mpfr_set_ui_2exp (x, 1024, -10, MPFR_RNDN);
if (mpfr_cmp_ui(x, 1))
ERROR("(1024U,-10)");
mpfr_set_ui_2exp (x, 1024, 10, MPFR_RNDN);
if (mpfr_cmp_ui(x, 1024*1024))
ERROR("(1024U,+10)");
mpfr_set_si_2exp (x, -1024L * 1024L, -10, MPFR_RNDN);
if (mpfr_cmp_si(x, -1024))
ERROR("(1M,-10)");
mpfr_set_ui_2exp (x, 0x92345678, 16, MPFR_RNDN);
if (mpfr_cmp_str (x, "92345678@4", 16, MPFR_RNDN))
ERROR("(x92345678U,+16)");
mpfr_set_si_2exp (x, -0x1ABCDEF0, -256, MPFR_RNDN);
if (mpfr_cmp_str (x, "-1ABCDEF0@-64", 16, MPFR_RNDN))
ERROR("(-x1ABCDEF0,-256)");
mpfr_set_prec (x, 2);
res = mpfr_set_si_2exp (x, 7, 10, MPFR_RNDU);
if (mpfr_cmp_ui (x, 1<<13) || res <= 0)
ERROR ("Prec 2 + si_2exp");
res = mpfr_set_ui_2exp (x, 7, 10, MPFR_RNDU);
if (mpfr_cmp_ui (x, 1<<13) || res <= 0)
ERROR ("Prec 2 + ui_2exp");
mpfr_clear_flags ();
mpfr_set_ui_2exp (x, 17, MPFR_EMAX_MAX, MPFR_RNDN);
if (!mpfr_inf_p (x) || MPFR_IS_NEG (x))
ERROR ("mpfr_set_ui_2exp and overflow (bad result)");
if (!mpfr_overflow_p ())
ERROR ("mpfr_set_ui_2exp and overflow (overflow flag not set)");
mpfr_clear_flags ();
mpfr_set_si_2exp (x, 17, MPFR_EMAX_MAX, MPFR_RNDN);
if (!mpfr_inf_p (x) || MPFR_IS_NEG (x))
ERROR ("mpfr_set_si_2exp (pos) and overflow (bad result)");
if (!mpfr_overflow_p ())
ERROR ("mpfr_set_si_2exp (pos) and overflow (overflow flag not set)");
mpfr_clear_flags ();
mpfr_set_si_2exp (x, -17, MPFR_EMAX_MAX, MPFR_RNDN);
if (!mpfr_inf_p (x) || MPFR_IS_POS (x))
ERROR ("mpfr_set_si_2exp (neg) and overflow (bad result)");
if (!mpfr_overflow_p ())
ERROR ("mpfr_set_si_2exp (neg) and overflow (overflow flag not set)");
mpfr_clear (x);
}
static void
test_macros (void)
{
mpfr_t x[3];
mpfr_ptr p;
int r;
mpfr_inits (x[0], x[1], x[2], (mpfr_ptr) 0);
p = x[0];
r = 0;
mpfr_set_ui (p++, 0, (mpfr_rnd_t) r++);
if (p != x[1] || r != 1)
{
printf ("Error in mpfr_set_ui macro: p - x[0] = %d (expecting 1), "
"r = %d (expecting 1)\n", (int) (p - x[0]), r);
exit (1);
}
p = x[0];
r = 0;
mpfr_set_si (p++, 0, (mpfr_rnd_t) r++);
if (p != x[1] || r != 1)
{
printf ("Error in mpfr_set_si macro: p - x[0] = %d (expecting 1), "
"r = %d (expecting 1)\n", (int) (p - x[0]), r);
exit (1);
}
mpfr_clears (x[0], x[1], x[2], (mpfr_ptr) 0);
}
static void
test_macros_keyword (void)
{
mpfr_t x;
unsigned long i;
mpfr_init2 (x, 64);
#define MKN 0x1000000
#define long short
mpfr_set_ui (x, MKN, MPFR_RNDN);
#undef long
i = mpfr_get_ui (x, MPFR_RNDN);
if (i != MKN)
{
printf ("Error in test_macros_keyword: expected 0x%lx, got 0x%lx.\n",
(unsigned long) MKN, i);
exit (1);
}
mpfr_clear (x);
}
/* FIXME: Comparing against mpfr_get_si/ui is not ideal, it'd be better to
have all tests examine the bits in mpfr_t for what should come out. */
int
main (int argc, char *argv[])
{
mpfr_t x;
long k, z, d, N;
unsigned long zl, dl;
int inex;
int r;
mpfr_exp_t emin, emax;
int flag;
tests_start_mpfr ();
mpfr_init2 (x, 100);
N = (argc==1) ? 100000 : atol (argv[1]);
for (k = 1; k <= N; k++)
{
z = (long) (randlimb () & LONG_MAX) + LONG_MIN / 2;
inex = mpfr_set_si (x, z, MPFR_RNDZ);
d = mpfr_get_si (x, MPFR_RNDZ);
if (d != z)
{
printf ("Error in mpfr_set_si: expected %ld got %ld\n", z, d);
exit (1);
}
if (inex)
{
printf ("Error in mpfr_set_si: inex value incorrect for %ld: %d\n",
z, inex);
exit (1);
}
}
for (k = 1; k <= N; k++)
{
zl = randlimb ();
inex = mpfr_set_ui (x, zl, MPFR_RNDZ);
dl = mpfr_get_ui (x, MPFR_RNDZ);
if (dl != zl)
{
printf ("Error in mpfr_set_ui: expected %lu got %lu\n", zl, dl);
exit (1);
}
if (inex)
{
printf ("Error in mpfr_set_ui: inex value incorrect for %lu: %d\n",
zl, inex);
exit (1);
}
}
mpfr_set_prec (x, 2);
if (mpfr_set_si (x, 5, MPFR_RNDZ) >= 0)
{
printf ("Wrong inexact flag for x=5, rnd=MPFR_RNDZ\n");
exit (1);
}
mpfr_set_prec (x, 2);
if (mpfr_set_si (x, -5, MPFR_RNDZ) <= 0)
{
printf ("Wrong inexact flag for x=-5, rnd=MPFR_RNDZ\n");
exit (1);
}
mpfr_set_prec (x, 3);
inex = mpfr_set_si (x, 77617, MPFR_RNDD); /* should be 65536 */
if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1))
|| inex >= 0)
{
printf ("Error in mpfr_set_si(x:3, 77617, MPFR_RNDD)\n");
mpfr_print_binary (x);
puts ("");
exit (1);
}
inex = mpfr_set_ui (x, 77617, MPFR_RNDD); /* should be 65536 */
if (MPFR_MANT(x)[0] != ((mp_limb_t)1 << (mp_bits_per_limb-1))
|| inex >= 0)
{
printf ("Error in mpfr_set_ui(x:3, 77617, MPFR_RNDD)\n");
mpfr_print_binary (x);
puts ("");
exit (1);
}
mpfr_set_prec (x, 2);
inex = mpfr_set_si (x, 33096, MPFR_RNDU);
if (mpfr_get_si (x, MPFR_RNDZ) != 49152 || inex <= 0)
{
printf ("Error in mpfr_set_si, exp. 49152, got %ld, inex %d\n",
mpfr_get_si (x, MPFR_RNDZ), inex);
exit (1);
}
inex = mpfr_set_ui (x, 33096, MPFR_RNDU);
if (mpfr_get_si (x, MPFR_RNDZ) != 49152)
{
printf ("Error in mpfr_set_ui, exp. 49152, got %ld, inex %d\n",
mpfr_get_si (x, MPFR_RNDZ), inex);
exit (1);
}
/* Also test the mpfr_set_ui function (instead of macro). */
inex = (mpfr_set_ui) (x, 33096, MPFR_RNDU);
if (mpfr_get_si (x, MPFR_RNDZ) != 49152)
{
printf ("Error in mpfr_set_ui function, exp. 49152, got %ld, inex %d\n",
mpfr_get_si (x, MPFR_RNDZ), inex);
exit (1);
}
for (r = 0 ; r < MPFR_RND_MAX ; r++)
{
mpfr_set_si (x, -1, (mpfr_rnd_t) r);
mpfr_set_ui (x, 0, (mpfr_rnd_t) r);
if (MPFR_IS_NEG (x) || mpfr_get_ui (x, (mpfr_rnd_t) r) != 0)
{
printf ("mpfr_set_ui (x, 0) gives -0 for %s\n",
mpfr_print_rnd_mode ((mpfr_rnd_t) r));
exit (1);
}
mpfr_set_si (x, -1, (mpfr_rnd_t) r);
mpfr_set_si (x, 0, (mpfr_rnd_t) r);
if (MPFR_IS_NEG (x) || mpfr_get_si (x, (mpfr_rnd_t) r) != 0)
{
printf ("mpfr_set_si (x, 0) gives -0 for %s\n",
mpfr_print_rnd_mode ((mpfr_rnd_t) r));
exit (1);
}
}
/* check potential bug in case mp_limb_t is unsigned */
emax = mpfr_get_emax ();
set_emax (0);
mpfr_set_si (x, -1, MPFR_RNDN);
if (mpfr_sgn (x) >= 0)
{
printf ("mpfr_set_si (x, -1) fails\n");
exit (1);
}
set_emax (emax);
emax = mpfr_get_emax ();
set_emax (5);
mpfr_set_prec (x, 2);
mpfr_set_si (x, -31, MPFR_RNDN);
if (mpfr_sgn (x) >= 0)
{
printf ("mpfr_set_si (x, -31) fails\n");
exit (1);
}
set_emax (emax);
/* test for get_ui */
mpfr_set_ui (x, 0, MPFR_RNDN);
MPFR_ASSERTN(mpfr_get_ui (x, MPFR_RNDN) == 0);
mpfr_set_ui (x, ULONG_MAX, MPFR_RNDU);
mpfr_nextabove (x);
mpfr_get_ui (x, MPFR_RNDU);
/* another test for get_ui */
mpfr_set_prec (x, 10);
mpfr_set_str_binary (x, "10.101");
dl = mpfr_get_ui (x, MPFR_RNDN);
MPFR_ASSERTN (dl == 3);
mpfr_set_str_binary (x, "-1.0");
mpfr_get_ui (x, MPFR_RNDN);
mpfr_set_str_binary (x, "0.1");
dl = mpfr_get_ui (x, MPFR_RNDN);
MPFR_ASSERTN (dl == 0);
dl = mpfr_get_ui (x, MPFR_RNDZ);
MPFR_ASSERTN (dl == 0);
dl = mpfr_get_ui (x, MPFR_RNDD);
MPFR_ASSERTN (dl == 0);
dl = mpfr_get_ui (x, MPFR_RNDU);
MPFR_ASSERTN (dl == 1);
/* coverage tests */
mpfr_set_prec (x, 2);
mpfr_set_si (x, -7, MPFR_RNDD);
MPFR_ASSERTN(mpfr_cmp_si (x, -8) == 0);
mpfr_set_prec (x, 2);
mpfr_set_ui (x, 7, MPFR_RNDU);
MPFR_ASSERTN(mpfr_cmp_ui (x, 8) == 0);
emax = mpfr_get_emax ();
set_emax (3);
mpfr_set_ui (x, 7, MPFR_RNDU);
MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
set_emax (1);
MPFR_ASSERTN( mpfr_set_ui (x, 7, MPFR_RNDU) );
MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
set_emax (emax);
mpfr_set_ui_2exp (x, 17, -50, MPFR_RNDN);
MPFR_ASSERTN (mpfr_get_ui (x, MPFR_RNDD) == 0);
MPFR_ASSERTN (mpfr_get_si (x, MPFR_RNDD) == 0);
/* Test for ERANGE flag + correct behaviour if overflow */
mpfr_set_prec (x, 256);
mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN);
mpfr_clear_erangeflag ();
dl = mpfr_get_ui (x, MPFR_RNDN);
if (dl != ULONG_MAX || mpfr_erangeflag_p ())
{
printf ("ERROR for get_ui + ERANGE + ULONG_MAX (1)\n");
exit (1);
}
mpfr_add_ui (x, x, 1, MPFR_RNDN);
dl = mpfr_get_ui (x, MPFR_RNDN);
if (dl != ULONG_MAX || !mpfr_erangeflag_p ())
{
printf ("ERROR for get_ui + ERANGE + ULONG_MAX (2)\n");
exit (1);
}
mpfr_set_si (x, -1, MPFR_RNDN);
mpfr_clear_erangeflag ();
dl = mpfr_get_ui (x, MPFR_RNDN);
if (dl != 0 || !mpfr_erangeflag_p ())
{
printf ("ERROR for get_ui + ERANGE + -1 \n");
exit (1);
}
mpfr_set_si (x, LONG_MAX, MPFR_RNDN);
mpfr_clear_erangeflag ();
d = mpfr_get_si (x, MPFR_RNDN);
if (d != LONG_MAX || mpfr_erangeflag_p ())
{
printf ("ERROR for get_si + ERANGE + LONG_MAX (1): %ld\n", d);
exit (1);
}
mpfr_add_ui (x, x, 1, MPFR_RNDN);
d = mpfr_get_si (x, MPFR_RNDN);
if (d != LONG_MAX || !mpfr_erangeflag_p ())
{
printf ("ERROR for get_si + ERANGE + LONG_MAX (2)\n");
exit (1);
}
mpfr_set_si (x, LONG_MIN, MPFR_RNDN);
mpfr_clear_erangeflag ();
d = mpfr_get_si (x, MPFR_RNDN);
if (d != LONG_MIN || mpfr_erangeflag_p ())
{
printf ("ERROR for get_si + ERANGE + LONG_MIN (1)\n");
exit (1);
}
mpfr_sub_ui (x, x, 1, MPFR_RNDN);
d = mpfr_get_si (x, MPFR_RNDN);
if (d != LONG_MIN || !mpfr_erangeflag_p ())
{
printf ("ERROR for get_si + ERANGE + LONG_MIN (2)\n");
exit (1);
}
mpfr_set_nan (x);
mpfr_clear_erangeflag ();
d = mpfr_get_ui (x, MPFR_RNDN);
if (d != 0 || !mpfr_erangeflag_p ())
{
printf ("ERROR for get_ui + NaN\n");
exit (1);
}
mpfr_clear_erangeflag ();
d = mpfr_get_si (x, MPFR_RNDN);
if (d != 0 || !mpfr_erangeflag_p ())
{
printf ("ERROR for get_si + NaN\n");
exit (1);
}
emin = mpfr_get_emin ();
mpfr_set_prec (x, 2);
mpfr_set_emin (4);
mpfr_clear_flags ();
mpfr_set_ui (x, 7, MPFR_RNDU);
flag = mpfr_underflow_p ();
mpfr_set_emin (emin);
if (mpfr_cmp_ui (x, 8) != 0)
{
printf ("Error for mpfr_set_ui (x, 7, MPFR_RNDU), prec = 2, emin = 4\n");
exit (1);
}
if (flag)
{
printf ("mpfr_set_ui (x, 7, MPFR_RNDU) should not underflow "
"with prec = 2, emin = 4\n");
exit (1);
}
mpfr_set_emin (4);
mpfr_clear_flags ();
mpfr_set_si (x, -7, MPFR_RNDD);
flag = mpfr_underflow_p ();
mpfr_set_emin (emin);
if (mpfr_cmp_si (x, -8) != 0)
{
printf ("Error for mpfr_set_si (x, -7, MPFR_RNDD), prec = 2, emin = 4\n");
exit (1);
}
if (flag)
{
printf ("mpfr_set_si (x, -7, MPFR_RNDD) should not underflow "
"with prec = 2, emin = 4\n");
exit (1);
}
mpfr_clear (x);
test_2exp ();
test_macros ();
test_macros_keyword ();
tests_end_mpfr ();
return 0;
}
|