xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tget_set_d64.c (revision d536862b7d93d77932ef5de7eebdc48d76921b77)
1 /* Test file for mpfr_get_decimal64 and mpfr_set_decimal64.
2 
3 Copyright 2006-2020 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5 
6 This file is part of the GNU MPFR Library.
7 
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 /* Needed due to the test on MPFR_WANT_DECIMAL_FLOATS */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 
28 #ifdef MPFR_WANT_DECIMAL_FLOATS
29 
30 #include "mpfr-test.h"
31 
32 #ifndef DEC64_MAX
33 # define DEC64_MAX 9.999999999999999E384dd
34 #endif
35 
36 #if _MPFR_IEEE_FLOATS
37 static void
38 print_decimal64 (_Decimal64 d)
39 {
40   union mpfr_ieee_double_extract x;
41   union ieee_double_decimal64 y;
42   unsigned int Gh, i;
43 
44   y.d64 = d;
45   x.d = y.d;
46   Gh = x.s.exp >> 6;
47   printf ("|%d%d%d%d%d%d", x.s.sig, Gh >> 4, (Gh >> 3) & 1,
48           (Gh >> 2) & 1, (Gh >> 1) & 1, Gh & 1);
49   printf ("%d%d%d%d%d%d", (x.s.exp >> 5) & 1, (x.s.exp >> 4) & 1,
50           (x.s.exp >> 3) & 1, (x.s.exp >> 2) & 1, (x.s.exp >> 1) & 1,
51           x.s.exp & 1);
52   for (i = 20; i > 0; i--)
53     printf ("%d", (x.s.manh >> (i - 1)) & 1);
54   for (i = 32; i > 0; i--)
55     printf ("%d", (x.s.manl >> (i - 1)) & 1);
56   printf ("|\n");
57 }
58 #else
59 /* Portable version, assuming long double has at least 55 bits.
60    Note: __STDC_WANT_IEC_60559_DFP_EXT__ or __STDC_WANT_DEC_FP__
61    might allow to use printf("%.15De\n", d) */
62 static void
63 print_decimal64 (_Decimal64 d)
64 {
65   printf ("%.15Le\n", (long double) d);
66 }
67 #endif /* _MPFR_IEEE_FLOATS */
68 
69 #define PRINT_ERR_MISC(V)                                   \
70   do                                                        \
71     {                                                       \
72       printf ("Error in check_misc for %s.\n", V);          \
73       printf ("  mpfr_get_decimal64() returned: ");         \
74       print_decimal64 (d);                                  \
75       printf ("  mpfr_set_decimal64() set x to: ");         \
76       mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);           \
77       printf (" approx.\n    = ");                          \
78       mpfr_dump (x);                                        \
79       exit (1);                                             \
80     }                                                       \
81  while (0)
82 
83 static void
84 check_misc (void)
85 {
86   mpfr_t  x, y;
87   _Decimal64 d;
88 
89   mpfr_init2 (x, 123);
90   mpfr_init2 (y, 123);
91 
92 #if !defined(MPFR_ERRDIVZERO)
93   mpfr_set_nan (x);
94   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
95   mpfr_set_ui (x, 1, MPFR_RNDZ);
96   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
97   MPFR_ASSERTN (mpfr_nan_p (x));
98 
99   mpfr_set_inf (x, 1);
100   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
101   mpfr_set_ui (x, 1, MPFR_RNDZ);
102   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
103   if (! mpfr_inf_p (x) || MPFR_IS_NEG (x))
104     PRINT_ERR_MISC ("+Inf");
105 
106   mpfr_set_inf (x, -1);
107   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
108   mpfr_set_ui (x, 1, MPFR_RNDZ);
109   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
110   if (! mpfr_inf_p (x) || MPFR_IS_POS (x))
111     PRINT_ERR_MISC ("-Inf");
112 #endif
113 
114   mpfr_set_ui (x, 0, MPFR_RNDZ);
115   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
116   mpfr_set_ui (x, 1, MPFR_RNDZ);
117   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
118   if (MPFR_NOTZERO (x) || MPFR_IS_NEG (x))
119     PRINT_ERR_MISC ("+0");
120 
121   mpfr_set_ui (x, 0, MPFR_RNDZ);
122   mpfr_neg (x, x, MPFR_RNDZ);
123   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
124   mpfr_set_ui (x, 1, MPFR_RNDZ);
125   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
126   if (MPFR_NOTZERO (x) || MPFR_IS_POS (x))
127     PRINT_ERR_MISC ("-0");
128 
129   mpfr_set_ui (x, 1, MPFR_RNDZ);
130   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
131   mpfr_set_ui (x, 0, MPFR_RNDZ);
132   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
133   if (mpfr_cmp_ui (x, 1) != 0)
134     PRINT_ERR_MISC ("+1");
135 
136   mpfr_set_si (x, -1, MPFR_RNDZ);
137   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
138   mpfr_set_ui (x, 0, MPFR_RNDZ);
139   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
140   if (mpfr_cmp_si (x, -1) != 0)
141     PRINT_ERR_MISC ("-1");
142 
143   mpfr_set_ui (x, 2, MPFR_RNDZ);
144   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
145   mpfr_set_ui (x, 0, MPFR_RNDZ);
146   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
147   if (mpfr_cmp_ui (x, 2) != 0)
148     PRINT_ERR_MISC ("2");
149 
150   mpfr_set_ui (x, 99, MPFR_RNDZ);
151   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
152   mpfr_set_ui (x, 0, MPFR_RNDZ);
153   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
154   if (mpfr_cmp_ui (x, 99) != 0)
155     PRINT_ERR_MISC ("99");
156 
157   mpfr_set_str (x, "9999999999999999", 10, MPFR_RNDZ);
158   mpfr_set (y, x, MPFR_RNDZ);
159   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
160   mpfr_set_ui (x, 0, MPFR_RNDZ);
161   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
162   if (! mpfr_equal_p (x, y))
163     PRINT_ERR_MISC ("9999999999999999");
164 
165   /* smallest normal number */
166   mpfr_set_str (x, "1E-383", 10, MPFR_RNDU);
167   mpfr_set (y, x, MPFR_RNDZ);
168   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
169   mpfr_set_ui (x, 0, MPFR_RNDZ);
170   mpfr_set_decimal64 (x, d, MPFR_RNDU);
171   if (! mpfr_equal_p (x, y))
172     PRINT_ERR_MISC ("1E-383");
173 
174   /* smallest subnormal number */
175   mpfr_set_str (x, "1E-398", 10, MPFR_RNDU);
176   mpfr_set (y, x, MPFR_RNDZ);
177   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
178   mpfr_set_ui (x, 0, MPFR_RNDZ);
179   mpfr_set_decimal64 (x, d, MPFR_RNDU);
180   if (! mpfr_equal_p (x, y))
181     PRINT_ERR_MISC ("1E-398");
182 
183   /* exercise case e < -1323, i.e., x < 0.5*2^(-1323) */
184   mpfr_set_ui_2exp (x, 1, -1324, MPFR_RNDN);
185   mpfr_nextbelow (x);
186   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
187   /* d should equal +0 */
188   mpfr_set_decimal64 (x, d, MPFR_RNDN);
189   MPFR_ASSERTN(mpfr_zero_p (x) && mpfr_signbit (x) == 0);
190   /* check RNDA */
191   mpfr_set_ui_2exp (x, 1, -1324, MPFR_RNDN);
192   mpfr_nextbelow (x);
193   d = mpfr_get_decimal64 (x, MPFR_RNDA);
194   /* d should equal 1E-398 */
195   mpfr_set_decimal64 (x, d, MPFR_RNDN);
196   mpfr_set_str (y, "1E-398", 10, MPFR_RNDN);
197   MPFR_ASSERTN(mpfr_equal_p (x, y));
198   /* check negative number */
199   mpfr_set_ui_2exp (x, 1, -1324, MPFR_RNDN);
200   mpfr_nextbelow (x);
201   mpfr_neg (x, x, MPFR_RNDN);
202   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
203   /* d should equal -0 */
204   mpfr_set_decimal64 (x, d, MPFR_RNDN);
205   MPFR_ASSERTN(mpfr_zero_p (x) && mpfr_signbit (x) == 1);
206 
207   /* exercise case e10 < -397 */
208   mpfr_set_ui_2exp (x, 1, -1323, MPFR_RNDN);
209   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
210   mpfr_set_decimal64 (x, d, MPFR_RNDN);
211   MPFR_ASSERTN(mpfr_zero_p (x) && mpfr_signbit (x) == 0);
212   mpfr_set_ui_2exp (x, 1, -1323, MPFR_RNDN);
213   d = mpfr_get_decimal64 (x, MPFR_RNDU);
214   mpfr_set_str (y, "1E-398", 10, MPFR_RNDN);
215   mpfr_set_decimal64 (x, d, MPFR_RNDN);
216   MPFR_ASSERTN(mpfr_equal_p (x, y));
217   mpfr_set_ui_2exp (x, 1, -1323, MPFR_RNDN);
218   /* 2^(-1323) = 5.46154776930125e-399 thus should be rounded to 1E-398 */
219   d = mpfr_get_decimal64 (x, MPFR_RNDN);
220   mpfr_set_str (y, "1E-398", 10, MPFR_RNDN);
221   mpfr_set_decimal64 (x, d, MPFR_RNDN);
222   MPFR_ASSERTN(mpfr_equal_p (x, y));
223 
224   /* subnormal number with exponent change when we round back
225      from 16 digits to 1 digit */
226   mpfr_set_str (x, "9.9E-398", 10, MPFR_RNDN);
227   d = mpfr_get_decimal64 (x, MPFR_RNDU); /* should be 1E-397 */
228   mpfr_set_ui (x, 0, MPFR_RNDZ);
229   mpfr_set_decimal64 (x, d, MPFR_RNDD);
230   mpfr_set_str (y, "1E-397", 10, MPFR_RNDN);
231   if (! mpfr_equal_p (x, y))
232     PRINT_ERR_MISC ("9.9E-398");
233 
234   /* largest number */
235   mpfr_set_str (x, "9.999999999999999E384", 10, MPFR_RNDZ);
236   mpfr_set (y, x, MPFR_RNDZ);
237   d = mpfr_get_decimal64 (x, MPFR_RNDU);
238   if (d == DEC64_MAX)
239     {
240       mpfr_set_ui (x, 0, MPFR_RNDZ);
241       mpfr_set_decimal64 (x, d, MPFR_RNDZ);
242       if (! mpfr_equal_p (x, y))
243         PRINT_ERR_MISC ("DEC64_MAX");
244     }
245   else
246     {
247       printf ("Error in check_misc for DEC64_MAX.\n");
248       printf ("  mpfr_get_decimal64() returned: ");
249       print_decimal64 (d);
250       exit (1);
251     }
252 
253   mpfr_set_str (x, "-9.999999999999999E384", 10, MPFR_RNDZ);
254   mpfr_set (y, x, MPFR_RNDZ);
255   d = mpfr_get_decimal64 (x, MPFR_RNDA);
256   if (d == -DEC64_MAX)
257     {
258       mpfr_set_ui (x, 0, MPFR_RNDZ);
259       mpfr_set_decimal64 (x, d, MPFR_RNDZ);
260       if (! mpfr_equal_p (x, y))
261         PRINT_ERR_MISC ("-DEC64_MAX");
262     }
263   else
264     {
265       printf ("Error in check_misc for -DEC64_MAX.\n");
266       printf ("  mpfr_get_decimal64() returned: ");
267       print_decimal64 (d);
268       exit (1);
269     }
270 
271   mpfr_set_prec (x, 53);
272   mpfr_set_prec (y, 53);
273 
274   /* largest number */
275   mpfr_set_str (x, "9.999999999999999E384", 10, MPFR_RNDZ);
276   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
277   mpfr_set_decimal64 (y, d, MPFR_RNDU);
278   if (! mpfr_equal_p (x, y))
279     PRINT_ERR_MISC ("DEC64_MAX (2)");
280 
281   mpfr_clear (x);
282   mpfr_clear (y);
283 }
284 
285 static void
286 check_random (void)
287 {
288   mpfr_t  x, y;
289   _Decimal64 d;
290   int i;
291 
292   mpfr_init2 (x, 49);
293   mpfr_init2 (y, 49);
294 
295   for (i = 0; i < 100000; i++)
296     {
297       mpfr_urandomb (x, RANDS); /* 0 <= x < 1 */
298       /* the normal decimal64 range contains [2^(-1272), 2^1278] */
299       mpfr_mul_2si (x, x, (i % 2550) - 1272, MPFR_RNDN);
300       if (mpfr_get_exp (x) <= -1272)
301         mpfr_mul_2ui (x, x, -1271 - mpfr_get_exp (x), MPFR_RNDN);
302       d = mpfr_get_decimal64 (x, MPFR_RNDN);
303       mpfr_set_decimal64 (y, d, MPFR_RNDN);
304       if (! mpfr_equal_p (x, y))
305         {
306           printf ("Error:\n");
307           printf ("x="); mpfr_dump (x);
308           printf ("d="); print_decimal64 (d);
309           printf ("y="); mpfr_dump (y);
310           exit (1);
311         }
312     }
313 
314   mpfr_clear (x);
315   mpfr_clear (y);
316 }
317 
318 /* check with native decimal formats */
319 static void
320 check_native (void)
321 {
322   mpfr_t x;
323   _Decimal64 d;
324 
325   mpfr_init2 (x, 53);
326 
327   /* check important constants are correctly converted */
328   mpfr_set_ui (x, 17, MPFR_RNDN);
329   d = mpfr_get_decimal64 (x, MPFR_RNDN);
330   MPFR_ASSERTN(d == 17.0dd);
331 
332   mpfr_set_ui (x, 42, MPFR_RNDN);
333   d = mpfr_get_decimal64 (x, MPFR_RNDN);
334   MPFR_ASSERTN(d == 42.0dd);
335 
336   mpfr_set_decimal64 (x, 17.0dd, MPFR_RNDN);
337   MPFR_ASSERTN(mpfr_cmp_ui (x, 17) == 0);
338 
339   mpfr_set_decimal64 (x, 42.0dd, MPFR_RNDN);
340   MPFR_ASSERTN(mpfr_cmp_ui (x, 42) == 0);
341 
342   mpfr_clear (x);
343 }
344 
345 static void
346 check_overflow (void)
347 {
348   mpfr_t x;
349   int err = 0, neg, rnd;
350 
351   mpfr_init2 (x, 96);
352   for (neg = 0; neg < 2; neg++)
353     RND_LOOP (rnd)
354       {
355         _Decimal64 d, e;
356         mpfr_rnd_t r = (mpfr_rnd_t) rnd;
357         int sign = neg ? -1 : 1;
358 
359         e = sign * (MPFR_IS_LIKE_RNDZ (r, neg) ? 1 : 2) * DEC64_MAX;
360         /* This tests the binary exponent e > 1279 case of get_d64.c */
361         mpfr_set_si_2exp (x, sign, 9999, MPFR_RNDN);
362         d = mpfr_get_decimal64 (x, r);
363         if (d != e)
364           {
365             printf ("Error 1 in check_overflow for %s, %s\n",
366                     neg ? "negative" : "positive",
367                     mpfr_print_rnd_mode (r));
368             err = 1;
369           }
370         /* This tests the decimal exponent e > 385 case of get_d64.c */
371         mpfr_set_si_2exp (x, sign * 31, 1274, MPFR_RNDN);
372         d = mpfr_get_decimal64 (x, r);
373         if (d != e)
374           {
375             printf ("Error 2 in check_overflow for %s, %s\n",
376                     neg ? "negative" : "positive",
377                     mpfr_print_rnd_mode (r));
378             err = 1;
379           }
380         /* This tests the last else (-382 <= e <= 385) of get_d64.c */
381         mpfr_set_decimal64 (x, e, MPFR_RNDA);
382         d = mpfr_get_decimal64 (x, r);
383         if (d != e)
384           {
385             printf ("Error 3 in check_overflow for %s, %s\n",
386                     neg ? "negative" : "positive",
387                     mpfr_print_rnd_mode (r));
388             err = 1;
389           }
390       }
391   mpfr_clear (x);
392   if (err)
393     exit (1);
394 }
395 
396 static void
397 check_tiny (void)
398 {
399   mpfr_t x;
400   _Decimal64 d;
401 
402   /* If 0.5E-398 < |x| < 1E-398 (smallest subnormal), x should round
403      to +/- 1E-398 in MPFR_RNDN. Note: the midpoint 0.5E-398 between
404      0 and 1E-398 is not a representable binary number, so that there
405      are no tests for it. */
406   mpfr_init2 (x, 128);
407   mpfr_set_str (x, "1E-398", 10, MPFR_RNDZ);
408   d = mpfr_get_decimal64 (x, MPFR_RNDN);
409   MPFR_ASSERTN (d == 1.0E-398dd);
410   mpfr_neg (x, x, MPFR_RNDN);
411   d = mpfr_get_decimal64 (x, MPFR_RNDN);
412   MPFR_ASSERTN (d == -1.0E-398dd);
413   mpfr_set_str (x, "0.5E-398", 10, MPFR_RNDU);
414   d = mpfr_get_decimal64 (x, MPFR_RNDN);
415   MPFR_ASSERTN (d == 1.0E-398dd);
416   mpfr_neg (x, x, MPFR_RNDN);
417   d = mpfr_get_decimal64 (x, MPFR_RNDN);
418   MPFR_ASSERTN (d == -1.0E-398dd);
419   mpfr_clear (x);
420 }
421 
422 static void
423 powers_of_10 (void)
424 {
425   mpfr_t x1, x2;
426   _Decimal64 d[2];
427   int i, rnd;
428   unsigned int neg;
429 
430   mpfr_inits2 (200, x1, x2, (mpfr_ptr) 0);
431   for (i = 0, d[0] = 1, d[1] = 1; i < 150; i++, d[0] *= 10, d[1] /= 10)
432     for (neg = 0; neg <= 3; neg++)
433       RND_LOOP_NO_RNDF (rnd)
434         {
435           int inex1, inex2;
436           mpfr_flags_t flags1, flags2;
437           mpfr_rnd_t rx1;
438           _Decimal64 dd;
439 
440           inex1 = mpfr_set_si (x1, (neg >> 1) ? -i : i, MPFR_RNDN);
441           MPFR_ASSERTN (inex1 == 0);
442 
443           rx1 = (neg & 1) ?
444             MPFR_INVERT_RND ((mpfr_rnd_t) rnd) : (mpfr_rnd_t) rnd;
445           mpfr_clear_flags ();
446           inex1 = mpfr_exp10 (x1, x1, rx1);
447           flags1 = __gmpfr_flags;
448 
449           dd = d[neg >> 1];
450 
451           if (neg & 1)
452             {
453               MPFR_SET_NEG (x1);
454               inex1 = -inex1;
455               dd = -dd;
456             }
457 
458           mpfr_clear_flags ();
459           inex2 = mpfr_set_decimal64 (x2, dd, (mpfr_rnd_t) rnd);
460           flags2 = __gmpfr_flags;
461 
462           if (!(mpfr_equal_p (x1, x2) &&
463                 SAME_SIGN (inex1, inex2) &&
464                 flags1 == flags2))
465             {
466               printf ("Error in powers_of_10 for i=%d, neg=%d, %s\n",
467                       i, neg, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
468               printf ("Expected ");
469               mpfr_dump (x1);
470               printf ("with inex = %d and flags =", inex1);
471               flags_out (flags1);
472               printf ("Got      ");
473               mpfr_dump (x2);
474               printf ("with inex = %d and flags =", inex2);
475               flags_out (flags2);
476               exit (1);
477             }
478         }
479   mpfr_clears (x1, x2, (mpfr_ptr) 0);
480 }
481 
482 static void
483 noncanonical (void)
484 {
485   /* The code below assumes BID. It also needs _MPFR_IEEE_FLOATS
486      due to the use of union mpfr_ieee_double_extract. */
487 #if _MPFR_IEEE_FLOATS && defined(DECIMAL_BID_FORMAT)
488   /* The volatile below avoids _Decimal64 constant propagation, which is
489      buggy for non-canonical encoding in various GCC versions on the x86 and
490      x86_64 targets: failure with gcc (Debian 20190719-1) 10.0.0 20190718
491      (experimental) [trunk revision 273586]; the MPFR test was not failing
492      with previous GCC versions, but GCC versions 5 to 9 are also affected
493      on the simple testcase at:
494      https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91226
495   */
496   volatile _Decimal64 d = 9999999999999999.0dd;
497   union mpfr_ieee_double_extract x;
498   union ieee_double_decimal64 y;
499 
500   MPFR_ASSERTN (sizeof (x) == 8);
501   MPFR_ASSERTN (sizeof (y) == 8);
502   /* test for non-canonical encoding */
503   y.d64 = d;
504   memcpy (&x, &y, 8);
505   /* if BID, we have sig=0, exp=1735, manh=231154, manl=1874919423 */
506   if (x.s.sig == 0 && x.s.exp == 1735 && x.s.manh == 231154 &&
507       x.s.manl == 1874919423)
508     {
509       mpfr_t z;
510       mpfr_init2 (z, 54); /* 54 bits ensure z is exact, since 10^16 < 2^54 */
511       x.s.manl += 1; /* then the significand equals 10^16 */
512       memcpy (&y, &x, 8);
513       mpfr_set_decimal64 (z, y.d64, MPFR_RNDN);
514       if (MPFR_NOTZERO (z) || MPFR_IS_NEG (z))
515         {
516           int i;
517           printf ("Error in noncanonical on");
518           for (i = 0; i < 8; i++)
519             printf (" %02X", ((unsigned char *)&y)[i]);
520           printf ("\nExpected +0, got:\n");
521           mpfr_dump (z);
522           exit (1);
523         }
524       mpfr_clear (z);
525     }
526   else
527     printf ("Warning! Unexpected value of x in noncanonical.\n");
528 #endif
529 }
530 
531 /* generate random sequences of 8 bytes and interpret them as _Decimal64 */
532 static void
533 check_random_bytes (void)
534 {
535   union {
536     _Decimal64 d;
537     unsigned char c[8];
538   } x;
539   int i;
540   mpfr_t y;
541   _Decimal64 e;
542 
543   mpfr_init2 (y, 55); /* 55 = 1 + ceil(16*log(10)/log(2)), thus ensures
544                          that if a decimal64 number is converted to a 55-bit
545                          value and back, we should get the same value */
546   for (i = 0; i < 100000; i++)
547     {
548       int j;
549       for (j = 0; j < 8; j++)
550         x.c[j] = randlimb () & 255;
551       mpfr_set_decimal64 (y, x.d, MPFR_RNDN);
552       e = mpfr_get_decimal64 (y, MPFR_RNDN);
553       if (!mpfr_nan_p (y))
554         if (x.d != e)
555           {
556             printf ("check_random_bytes failed\n");
557             printf ("x.d="); print_decimal64 (x.d);
558             printf ("y="); mpfr_dump (y);
559             printf ("e  ="); print_decimal64 (e);
560             exit (1);
561           }
562     }
563   mpfr_clear (y);
564 }
565 
566 int
567 main (int argc, char *argv[])
568 {
569   int verbose = argc > 1;
570 
571   tests_start_mpfr ();
572   mpfr_test_init ();
573 
574   if (verbose)
575     {
576 #ifdef DECIMAL_DPD_FORMAT
577       printf ("Using DPD encoding\n");
578 #endif
579 #ifdef DECIMAL_BID_FORMAT
580       printf ("Using BID encoding\n");
581 #endif
582     }
583 
584 #if !defined(MPFR_ERRDIVZERO)
585   check_random_bytes ();
586 #endif
587   noncanonical ();
588   check_misc ();
589   check_random ();
590   check_native ();
591 #if !defined(MPFR_ERRDIVZERO)
592   check_overflow ();
593 #endif
594   check_tiny ();
595   powers_of_10 ();
596 
597   tests_end_mpfr ();
598   return 0;
599 }
600 
601 #else /* MPFR_WANT_DECIMAL_FLOATS */
602 
603 int
604 main (void)
605 {
606   return 77;
607 }
608 
609 #endif /* MPFR_WANT_DECIMAL_FLOATS */
610