Imported Upstream version 3.1.1
[platform/upstream/mpfr.git] / tests / tget_set_d64.c
1 /* Test file for mpfr_get_decimal64 and mpfr_set_decimal64.
2
3 Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel 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 http://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 #ifdef MPFR_WANT_DECIMAL_FLOATS
24
25 #include <stdlib.h> /* for exit */
26 #include "mpfr-test.h"
27
28 /* #define DEBUG */
29
30 static void
31 print_decimal64 (_Decimal64 d)
32 {
33   union ieee_double_extract x;
34   union ieee_double_decimal64 y;
35   unsigned int Gh, i;
36
37   y.d64 = d;
38   x.d = y.d;
39   Gh = x.s.exp >> 6;
40   printf ("|%d%d%d%d%d%d", x.s.sig, Gh >> 4, (Gh >> 3) & 1,
41           (Gh >> 2) & 1, (Gh >> 1) & 1, Gh & 1);
42   printf ("%d%d%d%d%d%d", (x.s.exp >> 5) & 1, (x.s.exp >> 4) & 1,
43           (x.s.exp >> 3) & 1, (x.s.exp >> 2) & 1, (x.s.exp >> 1) & 1,
44           x.s.exp & 1);
45   for (i = 20; i > 0; i--)
46     printf ("%d", (x.s.manh >> (i - 1)) & 1);
47   for (i = 32; i > 0; i--)
48     printf ("%d", (x.s.manl >> (i - 1)) & 1);
49   printf ("|\n");
50 }
51
52 static void
53 check_inf_nan (void)
54 {
55   mpfr_t  x, y;
56   _Decimal64 d;
57
58   mpfr_init2 (x, 123);
59   mpfr_init2 (y, 123);
60
61   mpfr_set_nan (x);
62   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
63   mpfr_set_ui (x, 1, MPFR_RNDZ);
64   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
65   ASSERT_ALWAYS (mpfr_nan_p (x));
66
67   mpfr_set_inf (x, 1);
68   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
69   mpfr_set_ui (x, 1, MPFR_RNDZ);
70   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
71   ASSERT_ALWAYS (mpfr_inf_p (x) && mpfr_sgn (x) > 0);
72
73   mpfr_set_inf (x, -1);
74   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
75   mpfr_set_ui (x, 1, MPFR_RNDZ);
76   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
77   ASSERT_ALWAYS (mpfr_inf_p (x) && mpfr_sgn (x) < 0);
78
79   mpfr_set_ui (x, 0, MPFR_RNDZ);
80   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
81   mpfr_set_ui (x, 1, MPFR_RNDZ);
82   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
83   ASSERT_ALWAYS (mpfr_cmp_ui (x, 0) == 0 && MPFR_SIGN (x) > 0);
84
85   mpfr_set_ui (x, 0, MPFR_RNDZ);
86   mpfr_neg (x, x, MPFR_RNDZ);
87   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
88   mpfr_set_ui (x, 1, MPFR_RNDZ);
89   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
90   ASSERT_ALWAYS (mpfr_cmp_ui (x, 0) == 0 && MPFR_SIGN (x) < 0);
91
92   mpfr_set_ui (x, 1, MPFR_RNDZ);
93   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
94   mpfr_set_ui (x, 0, MPFR_RNDZ);
95   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
96   ASSERT_ALWAYS (mpfr_cmp_ui (x, 1) == 0);
97
98   mpfr_set_si (x, -1, MPFR_RNDZ);
99   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
100   mpfr_set_ui (x, 0, MPFR_RNDZ);
101   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
102   ASSERT_ALWAYS (mpfr_cmp_si (x, -1) == 0);
103
104   mpfr_set_ui (x, 2, MPFR_RNDZ);
105   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
106   mpfr_set_ui (x, 0, MPFR_RNDZ);
107   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
108   ASSERT_ALWAYS (mpfr_cmp_ui (x, 2) == 0);
109
110   mpfr_set_ui (x, 99, MPFR_RNDZ);
111   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
112   mpfr_set_ui (x, 0, MPFR_RNDZ);
113   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
114   ASSERT_ALWAYS (mpfr_cmp_ui (x, 99) == 0);
115
116   mpfr_set_str (x, "9999999999999999", 10, MPFR_RNDZ);
117   mpfr_set (y, x, MPFR_RNDZ);
118   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
119   mpfr_set_ui (x, 0, MPFR_RNDZ);
120   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
121   ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
122
123   /* smallest normal number */
124   mpfr_set_str (x, "1E-383", 10, MPFR_RNDU);
125   mpfr_set (y, x, MPFR_RNDZ);
126   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
127   mpfr_set_ui (x, 0, MPFR_RNDZ);
128   mpfr_set_decimal64 (x, d, MPFR_RNDU);
129   ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
130
131   /* smallest subnormal number */
132   mpfr_set_str (x, "1E-398", 10, MPFR_RNDU);
133   mpfr_set (y, x, MPFR_RNDZ);
134   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
135   mpfr_set_ui (x, 0, MPFR_RNDZ);
136   mpfr_set_decimal64 (x, d, MPFR_RNDU);
137   ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
138
139   /* subnormal number with exponent change when we round back
140      from 16 digits to 1 digit */
141   mpfr_set_str (x, "9.9E-398", 10, MPFR_RNDN);
142   d = mpfr_get_decimal64 (x, MPFR_RNDU); /* should be 1E-397 */
143   mpfr_set_ui (x, 0, MPFR_RNDZ);
144   mpfr_set_decimal64 (x, d, MPFR_RNDD);
145   mpfr_set_str (y, "1E-397", 10, MPFR_RNDN);
146   ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
147
148   /* largest number */
149   mpfr_set_str (x, "9.999999999999999E384", 10, MPFR_RNDZ);
150   mpfr_set (y, x, MPFR_RNDZ);
151   d = mpfr_get_decimal64 (x, MPFR_RNDU);
152   mpfr_set_ui (x, 0, MPFR_RNDZ);
153   mpfr_set_decimal64 (x, d, MPFR_RNDZ);
154   ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
155
156   mpfr_set_prec (x, 53);
157   mpfr_set_prec (y, 53);
158
159   /* largest number */
160   mpfr_set_str (x, "9.999999999999999E384", 10, MPFR_RNDZ);
161   d = mpfr_get_decimal64 (x, MPFR_RNDZ);
162   mpfr_set_decimal64 (y, d, MPFR_RNDU);
163   ASSERT_ALWAYS (mpfr_cmp (x, y) == 0);
164
165   mpfr_clear (x);
166   mpfr_clear (y);
167 }
168
169 static void
170 check_random (void)
171 {
172   mpfr_t  x, y;
173   _Decimal64 d;
174   int i;
175
176   mpfr_init2 (x, 49);
177   mpfr_init2 (y, 49);
178
179   for (i = 0; i < 100000; i++)
180     {
181       mpfr_urandomb (x, RANDS); /* 0 <= x < 1 */
182       /* the normal decimal64 range contains [2^(-1272), 2^1278] */
183       mpfr_mul_2si (x, x, (i % 2550) - 1272, MPFR_RNDN);
184       if (mpfr_get_exp (x) <= -1272)
185         mpfr_mul_2exp (x, x, -1271 - mpfr_get_exp (x), MPFR_RNDN);
186       d = mpfr_get_decimal64 (x, MPFR_RNDN);
187       mpfr_set_decimal64 (y, d, MPFR_RNDN);
188       if (mpfr_cmp (x, y) != 0)
189         {
190           printf ("x="); mpfr_dump (x);
191           printf ("d="); print_decimal64 (d);
192           printf ("y="); mpfr_dump (y);
193           exit (1);
194         }
195     }
196
197   mpfr_clear (x);
198   mpfr_clear (y);
199 }
200
201 /* check with native decimal formats */
202 static void
203 check_native (void)
204 {
205   mpfr_t x;
206   _Decimal64 d;
207
208   mpfr_init2 (x, 53);
209
210   /* check important constants are correctly converted */
211   mpfr_set_ui (x, 17, MPFR_RNDN);
212   d = mpfr_get_decimal64 (x, MPFR_RNDN);
213   MPFR_ASSERTN(d == 17.0dd);
214
215   mpfr_set_ui (x, 42, MPFR_RNDN);
216   d = mpfr_get_decimal64 (x, MPFR_RNDN);
217   MPFR_ASSERTN(d == 42.0dd);
218
219   mpfr_set_decimal64 (x, 17.0dd, MPFR_RNDN);
220   MPFR_ASSERTN(mpfr_cmp_ui (x, 17) == 0);
221
222   mpfr_set_decimal64 (x, 42.0dd, MPFR_RNDN);
223   MPFR_ASSERTN(mpfr_cmp_ui (x, 42) == 0);
224
225   mpfr_clear (x);
226 }
227
228 int
229 main (void)
230 {
231   tests_start_mpfr ();
232   mpfr_test_init ();
233
234 #ifdef DEBUG
235 #ifdef DPD_FORMAT
236   printf ("Using DPD format\n");
237 #else
238   printf ("Using BID format\n");
239 #endif
240 #endif
241   check_inf_nan ();
242   check_random ();
243   check_native ();
244
245   tests_end_mpfr ();
246   return 0;
247 }
248
249 #else /* MPFR_WANT_DECIMAL_FLOATS */
250
251 int
252 main (void)
253 {
254   return 77;
255 }
256
257 #endif /* MPFR_WANT_DECIMAL_FLOATS */