Merge branch 'upstream' into stable
[gnulib.git] / tests / test-printf-frexpl.c
1 /* Test of splitting a 'long double' into fraction and mantissa.
2    Copyright (C) 2007-2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include "printf-frexpl.h"
22
23 #include <float.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "fpucw.h"
28
29 #define ASSERT(expr) \
30   do                                                                         \
31     {                                                                        \
32       if (!(expr))                                                           \
33         {                                                                    \
34           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
35           fflush (stderr);                                                   \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
42    exponent for 'long double' is -964.  Similarly, on PowerPC machines,
43    LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
44    is -968.  For exponents below that, the precision may be truncated to the
45    precision used for 'double'.  */
46 #ifdef __sgi
47 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
48 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
49 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
50 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
51 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
52 #else
53 # define MIN_NORMAL_EXP LDBL_MIN_EXP
54 # define MIN_SUBNORMAL_EXP (LDBL_MIN_EXP - 100)
55 #endif
56
57 static long double
58 my_ldexp (long double x, int d)
59 {
60   for (; d > 0; d--)
61     x *= 2.0L;
62   for (; d < 0; d++)
63     x *= 0.5L;
64   return x;
65 }
66
67 int
68 main ()
69 {
70   int i;
71   long double x;
72   DECL_LONG_DOUBLE_ROUNDING
73
74   BEGIN_LONG_DOUBLE_ROUNDING ();
75
76   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
77     {
78       int exp = -9999;
79       long double mantissa = printf_frexpl (x, &exp);
80       ASSERT (exp == i - 1);
81       ASSERT (mantissa == 1.0L);
82     }
83   for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
84     {
85       int exp = -9999;
86       long double mantissa = printf_frexpl (x, &exp);
87       ASSERT (exp == i - 1);
88       ASSERT (mantissa == 1.0L);
89     }
90   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
91     {
92       int exp = -9999;
93       long double mantissa = printf_frexpl (x, &exp);
94       ASSERT (exp == LDBL_MIN_EXP - 1);
95       ASSERT (mantissa == my_ldexp (1.0L, i - LDBL_MIN_EXP));
96     }
97
98   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
99     {
100       int exp = -9999;
101       long double mantissa = printf_frexpl (x, &exp);
102       ASSERT (exp == i - 1);
103       ASSERT (mantissa == 1.01L);
104     }
105   for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
106     {
107       int exp = -9999;
108       long double mantissa = printf_frexpl (x, &exp);
109       ASSERT (exp == i - 1);
110       ASSERT (mantissa == 1.01L);
111     }
112   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
113     {
114       int exp = -9999;
115       long double mantissa = printf_frexpl (x, &exp);
116       ASSERT (exp == LDBL_MIN_EXP - 1);
117       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
118       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
119       ASSERT (mantissa == my_ldexp (x, - exp));
120     }
121
122   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
123     {
124       int exp = -9999;
125       long double mantissa = printf_frexpl (x, &exp);
126       ASSERT (exp == i - 1);
127       ASSERT (mantissa == 1.73205L);
128     }
129   for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
130     {
131       int exp = -9999;
132       long double mantissa = printf_frexpl (x, &exp);
133       ASSERT (exp == i - 1);
134       ASSERT (mantissa == 1.73205L);
135     }
136   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
137     {
138       int exp = -9999;
139       long double mantissa = printf_frexpl (x, &exp);
140       ASSERT (exp == LDBL_MIN_EXP - 1);
141       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
142       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
143       ASSERT (mantissa == my_ldexp (x, - exp));
144     }
145
146   return 0;
147 }