1 /* Test file for mpfr_subnormalize.
3 Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
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.
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.
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. */
27 #include "mpfr-test.h"
35 } tab[] = { /* 4th field: use the mpfr_dump format, in case of error. */
36 {"1E1", 0, MPFR_RNDN, "0.100000000E2", 0},
37 {"1E1", -1, MPFR_RNDZ, "0.100000000E2", -1},
38 {"1E1", -1, MPFR_RNDD, "0.100000000E2", -1},
39 {"1E1", 1, MPFR_RNDU, "0.100000000E2", 1},
40 {"0.10000E-10", 0, MPFR_RNDN, "0.100000000E-10", 0},
41 {"0.10001E-10", 0, MPFR_RNDN, "0.100000000E-10", -1},
42 {"0.11001E-10", 0, MPFR_RNDN, "0.100000000E-9", 1},
43 {"0.11001E-10", 0, MPFR_RNDZ, "0.100000000E-10", -1},
44 {"0.11001E-10", 0, MPFR_RNDU, "0.100000000E-9", 1},
45 {"0.11000E-10", 0, MPFR_RNDN, "0.100000000E-9", 1},
46 {"0.11000E-10", -1, MPFR_RNDN, "0.100000000E-9", 1},
47 {"0.11000E-10", 1, MPFR_RNDN, "0.100000000E-10", -1},
48 {"0.11111E-8", 0, MPFR_RNDN, "0.100000000E-7", 1},
49 {"0.10111E-8", 0, MPFR_RNDN, "0.110000000E-8", 1},
50 {"0.11110E-8", -1, MPFR_RNDN, "0.100000000E-7", 1},
51 {"0.10110E-8", 1, MPFR_RNDN, "0.101000000E-8", -1}
58 int i, j, k, s, old_inex, tiny, expj;
59 mpfr_exp_t emin, emax;
60 unsigned int expflags, flags;
62 emin = mpfr_get_emin ();
63 emax = mpfr_get_emax ();
65 mpfr_set_default_prec (9);
70 for (i = 0; i < (sizeof (tab) / sizeof (tab[0])); i++)
71 for (s = 0; s <= (tab[i].rnd == MPFR_RNDN); s++)
72 for (k = 0; k <= 1; k++)
74 mpfr_set_str (x, tab[i].in, 2, MPFR_RNDN);
79 mpfr_neg (x, x, MPFR_RNDN);
80 old_inex = - old_inex;
84 old_inex = old_inex < 0 ? INT_MIN : INT_MAX;
85 tiny = MPFR_GET_EXP (x) <= -3;
87 j = mpfr_subnormalize (x, old_inex, tab[i].rnd);
89 (tiny ? MPFR_FLAGS_UNDERFLOW : 0) |
90 (expj ? MPFR_FLAGS_INEXACT : 0);
91 flags = __gmpfr_flags;
93 mpfr_neg (x, x, MPFR_RNDN);
94 if (mpfr_cmp_str (x, tab[i].out, 2, MPFR_RNDN) != 0 ||
95 flags != expflags || ! SAME_SIGN (j, expj))
97 const char *sgn = s ? "-" : "";
98 printf ("Error for i = %d (old_inex = %d), k = %d, x = %s%s\n"
99 "Expected: %s%s\nGot: ", i, old_inex, k,
100 sgn, tab[i].in, sgn, tab[i].out);
102 mpfr_neg (x, x, MPFR_RNDN);
104 printf ("Expected flags = %u, got %u\n", expflags, flags);
105 printf ("Expected ternary value = %d, got %d\n", expj, j);
111 MPFR_ASSERTN (mpfr_get_emin () == -10);
112 MPFR_ASSERTN (mpfr_get_emax () == 10);
118 /* bug found by Kevin P. Rauch on 22 Oct 2007 */
126 emin = mpfr_get_emin ();
132 mpfr_set_ui (x, 0xC0000000U, MPFR_RNDN);
133 mpfr_neg (x, x, MPFR_RNDN);
134 mpfr_set_ui (y, 0xFFFFFFFEU, MPFR_RNDN);
139 tern = mpfr_mul (z, x, y, MPFR_RNDN);
140 /* z = -0.BFFFFFFE, tern > 0 */
142 tern = mpfr_subnormalize (z, tern, MPFR_RNDN);
143 /* z should be -0.75 */
144 MPFR_ASSERTN (tern < 0 && mpfr_cmp_si_2exp (z, -3, -2) == 0);
150 MPFR_ASSERTN (mpfr_get_emin () == -29);
155 /* bug found by Kevin P. Rauch on 22 Oct 2007 */
163 emin = mpfr_get_emin ();
169 mpfr_set_ui (x, 0xBFFFFFFFU, MPFR_RNDN); /* 3221225471/2^32 */
170 mpfr_set_ui (y, 0x80000001U, MPFR_RNDN); /* 2147483649/2^32 */
175 /* the exact product is 6917529028714823679/2^64, which is rounded to
176 3/8 = 0.375, which is smaller, thus tern < 0 */
177 tern = mpfr_mul (z, x, y, MPFR_RNDN);
178 MPFR_ASSERTN (tern < 0 && mpfr_cmp_ui_2exp (z, 3, -3) == 0);
180 tern = mpfr_subnormalize (z, tern, MPFR_RNDN);
181 /* since emin = -1, and EXP(z)=-1, z should be rounded to precision
182 EXP(z)-emin+1 = 1, i.e., z should be a multiple of the smallest possible
183 positive representable value with emin=-1, which is 1/4. The two
184 possible values are 1/4 and 2/4, which are at equal distance of z.
185 But since tern < 0, we should choose the largest value, i.e., 2/4. */
186 MPFR_ASSERTN (tern > 0 && mpfr_cmp_ui_2exp (z, 1, -1) == 0);
188 /* here is another test for the alternate case, where z was rounded up
189 first, thus we have to round down */
190 mpfr_set_str_binary (x, "0.11111111111010110101011011011011");
191 mpfr_set_str_binary (y, "0.01100000000001111100000000001110");
192 tern = mpfr_mul (z, x, y, MPFR_RNDN);
193 MPFR_ASSERTN (tern > 0 && mpfr_cmp_ui_2exp (z, 3, -3) == 0);
194 tern = mpfr_subnormalize (z, tern, MPFR_RNDN);
195 MPFR_ASSERTN (tern < 0 && mpfr_cmp_ui_2exp (z, 1, -2) == 0);
197 /* finally the case where z was exact, which we simulate here */
198 mpfr_set_ui_2exp (z, 3, -3, MPFR_RNDN);
199 tern = mpfr_subnormalize (z, 0, MPFR_RNDN);
200 MPFR_ASSERTN (tern > 0 && mpfr_cmp_ui_2exp (z, 1, -1) == 0);
206 MPFR_ASSERTN (mpfr_get_emin () == -1);
212 main (int argc, char *argv[])