Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / tests / test-printf-frexpl.c
1 /* Test of splitting a 'long double' into fraction and mantissa.
2    Copyright (C) 2007 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           abort ();                                                          \
36         }                                                                    \
37     }                                                                        \
38   while (0)
39
40 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
41    exponent for 'long double' is -964.  Similarly, on PowerPC machines,
42    LDBL_MIN_EXP is -1021, but the smallest reliable exponent for 'long double'
43    is -968.  For exponents below that, the precision may be truncated to the
44    precision used for 'double'.  */
45 #ifdef __sgi
46 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
47 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
48 #elif defined __ppc || defined __ppc__ || defined __powerpc || defined __powerpc__
49 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 53)
50 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
51 #else
52 # define MIN_NORMAL_EXP LDBL_MIN_EXP
53 # define MIN_SUBNORMAL_EXP (LDBL_MIN_EXP - 100)
54 #endif
55
56 static long double
57 my_ldexp (long double x, int d)
58 {
59   for (; d > 0; d--)
60     x *= 2.0L;
61   for (; d < 0; d++)
62     x *= 0.5L;
63   return x;
64 }
65
66 int
67 main ()
68 {
69   int i;
70   long double x;
71   DECL_LONG_DOUBLE_ROUNDING
72
73   BEGIN_LONG_DOUBLE_ROUNDING ();
74
75   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
76     {
77       int exp = -9999;
78       long double mantissa = printf_frexpl (x, &exp);
79       ASSERT (exp == i - 1);
80       ASSERT (mantissa == 1.0L);
81     }
82   for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
83     {
84       int exp = -9999;
85       long double mantissa = printf_frexpl (x, &exp);
86       ASSERT (exp == i - 1);
87       ASSERT (mantissa == 1.0L);
88     }
89   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
90     {
91       int exp = -9999;
92       long double mantissa = printf_frexpl (x, &exp);
93       ASSERT (exp == LDBL_MIN_EXP - 1);
94       ASSERT (mantissa == my_ldexp (1.0L, i - LDBL_MIN_EXP));
95     }
96
97   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
98     {
99       int exp = -9999;
100       long double mantissa = printf_frexpl (x, &exp);
101       ASSERT (exp == i - 1);
102       ASSERT (mantissa == 1.01L);
103     }
104   for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
105     {
106       int exp = -9999;
107       long double mantissa = printf_frexpl (x, &exp);
108       ASSERT (exp == i - 1);
109       ASSERT (mantissa == 1.01L);
110     }
111   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
112     {
113       int exp = -9999;
114       long double mantissa = printf_frexpl (x, &exp);
115       ASSERT (exp == LDBL_MIN_EXP - 1);
116       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
117       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
118       ASSERT (mantissa == my_ldexp (x, - exp));
119     }
120
121   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
122     {
123       int exp = -9999;
124       long double mantissa = printf_frexpl (x, &exp);
125       ASSERT (exp == i - 1);
126       ASSERT (mantissa == 1.73205L);
127     }
128   for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
129     {
130       int exp = -9999;
131       long double mantissa = printf_frexpl (x, &exp);
132       ASSERT (exp == i - 1);
133       ASSERT (mantissa == 1.73205L);
134     }
135   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
136     {
137       int exp = -9999;
138       long double mantissa = printf_frexpl (x, &exp);
139       ASSERT (exp == LDBL_MIN_EXP - 1);
140       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
141       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
142       ASSERT (mantissa == my_ldexp (x, - exp));
143     }
144
145   return 0;
146 }