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
|
/* Test file for exceptions.
Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
Contributed by the Arenaire and Cacao projects, INRIA.
This file is part of the MPFR Library.
The 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 2.1 of the License, or (at your
option) any later version.
The 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 MPFR Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA. */
#include <stdio.h>
#include <stdlib.h>
#include "mpfr-test.h"
#define ERROR(s) do { printf(s"\n"); exit(1); } while(0)
/* Test powerof2 */
static void
check_powerof2 (void)
{
mpfr_t x;
mpfr_init (x);
mpfr_set_ui (x, 1, GMP_RNDN);
MPFR_ASSERTN (mpfr_powerof2_raw (x));
mpfr_set_ui (x, 3, GMP_RNDN);
MPFR_ASSERTN (!mpfr_powerof2_raw (x));
mpfr_clear (x);
}
/* Test default rounding mode */
static void
check_default_rnd (void)
{
int r;
mp_rnd_t t;
for(r = 0 ; r < GMP_RND_MAX ; r++)
{
mpfr_set_default_rounding_mode ((mp_rnd_t) r);
t = (mpfr_get_default_rounding_mode) ();
if ((mp_rnd_t) r != t)
ERROR("ERROR in setting / getting default rounding mode (1)");
}
mpfr_set_default_rounding_mode ((mp_rnd_t) 4);
if (mpfr_get_default_rounding_mode() != GMP_RNDD)
ERROR("ERROR in setting / getting default rounding mode (2)");
mpfr_set_default_rounding_mode((mp_rnd_t) -1);
if (mpfr_get_default_rounding_mode() != GMP_RNDD)
ERROR("ERROR in setting / getting default rounding mode (3)");
}
static void
check_emin_emax (void)
{
mp_exp_t old_emin, old_emax;
old_emin = mpfr_get_emin ();
old_emax = mpfr_get_emax ();
/* Check the functions not the macros ! */
if ((mpfr_set_emin)(MPFR_EMIN_MIN) != 0)
ERROR("set_emin failed!");
if ((mpfr_get_emin)() != MPFR_EMIN_MIN)
ERROR("get_emin FAILED!");
if ((mpfr_set_emin)(MPFR_EMIN_MIN-1) == 0)
ERROR("set_emin failed! (2)");
if ((mpfr_set_emax)(MPFR_EMAX_MAX) != 0)
ERROR("set_emax failed!");
if ((mpfr_get_emax)() != MPFR_EMAX_MAX)
ERROR("get_emax FAILED!");
if ((mpfr_set_emax)(MPFR_EMAX_MAX+1) == 0)
ERROR("set_emax failed! (2)");
if ((mpfr_get_emin_min) () != MPFR_EMIN_MIN)
ERROR ("get_emin_min");
if ((mpfr_get_emin_max) () != MPFR_EMIN_MAX)
ERROR ("get_emin_max");
if ((mpfr_get_emax_min) () != MPFR_EMAX_MIN)
ERROR ("get_emax_min");
if ((mpfr_get_emax_max) () != MPFR_EMAX_MAX)
ERROR ("get_emax_max");
set_emin (old_emin);
set_emax (old_emax);
}
static void
check_set_get_prec (void)
{
mpfr_t x;
mpfr_init2 (x, 17);
if (mpfr_get_prec (x) != 17 || (mpfr_get_prec)(x) != 17)
ERROR ("mpfr_get_prec");
mpfr_clear (x);
}
static void
mpfr_set_double_range (void)
{
mpfr_set_default_prec (54);
if (mpfr_get_default_prec () != 54)
ERROR ("get_default_prec failed (1)");
mpfr_set_default_prec (53);
if ((mpfr_get_default_prec) () != 53)
ERROR ("get_default_prec failed (2)");
/* in double precision format, the unbiased exponent is between 0 and
2047, where 0 is used for subnormal numbers, and 2047 for special
numbers (infinities, NaN), and the bias is 1023, thus "normal" numbers
have an exponent between -1022 and 1023, corresponding to numbers
between 2^(-1022) and previous(2^(1024)).
(The smallest subnormal number is 0.(0^51)1*2^(-1022)= 2^(-1074).)
The smallest normal power of two is 1.0*2^(-1022).
The largest normal power of two is 2^1023.
(We have to add one for mpfr since mantissa are between 1/2 and 1.)
*/
set_emin (-1021);
set_emax (1024);
}
static void
check_flags (void)
{
mpfr_t x;
mp_exp_t old_emin, old_emax;
old_emin = mpfr_get_emin ();
old_emax = mpfr_get_emax ();
mpfr_init (x);
/* Check the functions not the macros ! */
(mpfr_clear_flags)();
mpfr_set_double_range ();
mpfr_set_ui (x, 1, GMP_RNDN);
(mpfr_clear_overflow)();
mpfr_mul_2exp (x, x, 1024, GMP_RNDN);
if (!(mpfr_overflow_p)())
ERROR("ERROR: No overflow detected!\n");
(mpfr_clear_underflow)();
mpfr_set_ui (x, 1, GMP_RNDN);
mpfr_div_2exp (x, x, 1025, GMP_RNDN);
if (!(mpfr_underflow_p)())
ERROR("ERROR: No underflow detected!\n");
(mpfr_clear_nanflag)();
MPFR_SET_NAN(x);
mpfr_add (x, x, x, GMP_RNDN);
if (!(mpfr_nanflag_p)())
ERROR("ERROR: No NaN flag!\n");
(mpfr_clear_inexflag)();
mpfr_set_ui(x, 2, GMP_RNDN);
mpfr_cos(x, x, GMP_RNDN);
if (!(mpfr_inexflag_p)())
ERROR("ERROR: No inexact flag!\n");
(mpfr_clear_erangeflag) ();
mpfr_set_ui (x, 1, GMP_RNDN);
mpfr_mul_2exp (x, x, 1024, GMP_RNDN);
mpfr_get_ui (x, GMP_RNDN);
if (!(mpfr_erangeflag_p)())
ERROR ("ERROR: No erange flag!\n");
mpfr_clear (x);
set_emin (old_emin);
set_emax (old_emax);
}
static void
test_set_underflow (void)
{
/* static to allow non-constant initialiers in r */
mpfr_t x, zero, min;
mpfr_ptr r[4];
int t[4] = { 1, -1, 1, -1 };
int i;
int s;
mpfr_inits (x, zero, min, (mpfr_ptr) 0);
mpfr_set_ui (zero, 0, GMP_RNDN);
mpfr_set_ui (min, 0, GMP_RNDN);
mpfr_nextabove (min);
r[0] = r[2] = min;
r[1] = r[3] = zero;
for (s = 1; s > 0; s = -1)
{
for (i = 0; i < GMP_RND_MAX ; i++)
{
int j;
int inex;
j = s < 0 && i > 1 ? 5 - i : i;
inex = mpfr_underflow (x, (mp_rnd_t) i, s);
if (mpfr_cmp (x, r[j]) || inex * t[j] <= 0)
{
printf ("Error in test_set_underflow, sign = %d,"
" rnd_mode = %s\n", s, mpfr_print_rnd_mode ((mp_rnd_t) i));
printf ("Got\n");
mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
printf (", inex = %d\ninstead of\n", inex);
mpfr_out_str (stdout, 2, 0, r[j], GMP_RNDN);
printf (", inex = %d\n", t[j]);
exit (1);
}
}
mpfr_neg (zero, zero, GMP_RNDN);
mpfr_neg (min, min, GMP_RNDN);
}
mpfr_clears (x, zero, min, (mpfr_ptr) 0);
}
static void
test_set_overflow (void)
{
/* static to allow non-constant initialiers in r */
mpfr_t x, inf, max;
mpfr_ptr r[4];
int t[4] = { 1, -1, 1, -1 };
int i;
int s;
mpfr_inits2 (32, x, inf, max, (mpfr_ptr) 0);
mpfr_set_inf (inf, 1);
mpfr_set_inf (max, 1);
mpfr_nextbelow (max);
r[0] = r[2] = inf;
r[1] = r[3] = max;
for (s = 1; s > 0; s = -1)
{
for (i = 0; i < GMP_RND_MAX ; i++)
{
int j;
int inex;
j = s < 0 && i > 1 ? 5 - i : i;
inex = mpfr_overflow (x, (mp_rnd_t) i, s);
if (mpfr_cmp (x, r[j]) || inex * t[j] <= 0)
{
printf ("Error in test_set_overflow, sign = %d,"
" rnd_mode = %s\n", s, mpfr_print_rnd_mode ((mp_rnd_t) i));
printf ("Got\n");
mpfr_out_str (stdout, 2, 0, x, GMP_RNDN);
printf (", inex = %d\ninstead of\n", inex);
mpfr_out_str (stdout, 2, 0, r[j], GMP_RNDN);
printf (", inex = %d\n", t[j]);
exit (1);
}
}
mpfr_neg (inf, inf, GMP_RNDN);
mpfr_neg (max, max, GMP_RNDN);
}
mpfr_clears (x, inf, max, (mpfr_ptr) 0);
}
static void
check_set (void)
{
mpfr_clear_flags ();
mpfr_set_overflow ();
MPFR_ASSERTN ((mpfr_overflow_p) ());
mpfr_set_underflow ();
MPFR_ASSERTN ((mpfr_underflow_p) ());
mpfr_set_nanflag ();
MPFR_ASSERTN ((mpfr_nanflag_p) ());
mpfr_set_inexflag ();
MPFR_ASSERTN ((mpfr_inexflag_p) ());
mpfr_set_erangeflag ();
MPFR_ASSERTN ((mpfr_erangeflag_p) ());
mpfr_clear_flags ();
}
int
main (int argc, char *argv[])
{
mpfr_t x, y;
mp_exp_t emin, emax;
tests_start_mpfr ();
test_set_underflow ();
test_set_overflow ();
check_default_rnd();
mpfr_init (x);
mpfr_init (y);
emin = mpfr_get_emin ();
emax = mpfr_get_emax ();
if (emin >= emax)
{
printf ("Error: emin >= emax\n");
exit (1);
}
mpfr_set_ui (x, 1, GMP_RNDN);
mpfr_mul_2exp (x, x, 1024, GMP_RNDN);
mpfr_set_double_range ();
mpfr_check_range (x, 0, GMP_RNDN);
if (!mpfr_inf_p (x) || (mpfr_sgn(x) <= 0))
{
printf ("Error: 2^1024 rounded to nearest should give +Inf\n");
exit (1);
}
set_emax (1025);
mpfr_set_ui (x, 1, GMP_RNDN);
mpfr_mul_2exp (x, x, 1024, GMP_RNDN);
mpfr_set_double_range ();
mpfr_check_range (x, 0, GMP_RNDD);
if (!mpfr_number_p (x))
{
printf ("Error: 2^1024 rounded down should give a normal number\n");
exit (1);
}
mpfr_set_ui (x, 1, GMP_RNDN);
mpfr_mul_2exp (x, x, 1023, GMP_RNDN);
mpfr_add (x, x, x, GMP_RNDN);
if (!mpfr_inf_p (x) || (mpfr_sgn(x) <= 0))
{
printf ("Error: x+x rounded to nearest for x=2^1023 should give +Inf\n");
printf ("emax = %ld\n", mpfr_get_emax ());
printf ("got "); mpfr_print_binary (x); puts ("");
exit (1);
}
mpfr_set_ui (x, 1, GMP_RNDN);
mpfr_mul_2exp (x, x, 1023, GMP_RNDN);
mpfr_add (x, x, x, GMP_RNDD);
if (!mpfr_number_p (x))
{
printf ("Error: x+x rounded down for x=2^1023 should give"
" a normal number\n");
exit (1);
}
mpfr_set_ui (x, 1, GMP_RNDN);
mpfr_div_2exp (x, x, 1022, GMP_RNDN);
mpfr_set_str_binary (y, "1.1e-1022"); /* y = 3/2*x */
mpfr_sub (y, y, x, GMP_RNDZ);
if (mpfr_cmp_ui (y, 0))
{
printf ("Error: y-x rounded to zero should give 0"
" for y=3/2*2^(-1022), x=2^(-1022)\n");
printf ("y="); mpfr_print_binary (y); puts ("");
exit (1);
}
set_emin (-1026);
mpfr_set_ui (x, 1, GMP_RNDN);
mpfr_div_2exp (x, x, 1025, GMP_RNDN);
mpfr_set_double_range ();
mpfr_check_range (x, 0, GMP_RNDN);
if (!MPFR_IS_ZERO (x) )
{
printf ("Error: x rounded to nearest for x=2^-1024 should give Zero\n");
printf ("emin = %ld\n", mpfr_get_emin ());
printf ("got "); mpfr_dump (x);
exit (1);
}
mpfr_clear (x);
mpfr_clear (y);
set_emin (emin);
set_emax (emax);
check_emin_emax();
check_flags();
check_set_get_prec ();
check_powerof2 ();
check_set ();
tests_end_mpfr ();
return 0;
}
|