Avoid test failures on IRIX MIPS.
[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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
19
20 #include <config.h>
21
22 #include "printf-frexpl.h"
23
24 #include <float.h>
25 #include <stdlib.h>
26
27 #include "fpucw.h"
28
29 #define ASSERT(expr) if (!(expr)) abort ();
30
31 /* On MIPS IRIX machines, LDBL_MIN_EXP is -1021, but the smallest reliable
32    exponent for 'long double' is -964.  For exponents below that, the
33    precision may be truncated to the precision used for 'double'.  */
34 #ifdef __sgi
35 # define MIN_NORMAL_EXP (LDBL_MIN_EXP + 57)
36 # define MIN_SUBNORMAL_EXP MIN_NORMAL_EXP
37 #else
38 # define MIN_NORMAL_EXP LDBL_MIN_EXP
39 # define MIN_SUBNORMAL_EXP (LDBL_MIN_EXP - 100)
40 #endif
41
42 static long double
43 my_ldexp (long double x, int d)
44 {
45   for (; d > 0; d--)
46     x *= 2.0L;
47   for (; d < 0; d++)
48     x *= 0.5L;
49   return x;
50 }
51
52 int
53 main ()
54 {
55   int i;
56   long double x;
57   DECL_LONG_DOUBLE_ROUNDING
58
59   BEGIN_LONG_DOUBLE_ROUNDING ();
60
61   for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
62     {
63       int exp = -9999;
64       long double mantissa = printf_frexpl (x, &exp);
65       ASSERT (exp == i - 1);
66       ASSERT (mantissa == 1.0L);
67     }
68   for (i = 1, x = 1.0L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
69     {
70       int exp = -9999;
71       long double mantissa = printf_frexpl (x, &exp);
72       ASSERT (exp == i - 1);
73       ASSERT (mantissa == 1.0L);
74     }
75   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
76     {
77       int exp = -9999;
78       long double mantissa = printf_frexpl (x, &exp);
79       ASSERT (exp == LDBL_MIN_EXP - 1);
80       ASSERT (mantissa == my_ldexp (1.0L, i - LDBL_MIN_EXP));
81     }
82
83   for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
84     {
85       int exp = -9999;
86       long double mantissa = printf_frexpl (x, &exp);
87       ASSERT (exp == i - 1);
88       ASSERT (mantissa == 1.01L);
89     }
90   for (i = 1, x = 1.01L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
91     {
92       int exp = -9999;
93       long double mantissa = printf_frexpl (x, &exp);
94       ASSERT (exp == i - 1);
95       ASSERT (mantissa == 1.01L);
96     }
97   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
98     {
99       int exp = -9999;
100       long double mantissa = printf_frexpl (x, &exp);
101       ASSERT (exp == LDBL_MIN_EXP - 1);
102       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
103       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
104       ASSERT (mantissa == my_ldexp (x, - exp));
105     }
106
107   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
108     {
109       int exp = -9999;
110       long double mantissa = printf_frexpl (x, &exp);
111       ASSERT (exp == i - 1);
112       ASSERT (mantissa == 1.73205L);
113     }
114   for (i = 1, x = 1.73205L; i >= MIN_NORMAL_EXP; i--, x *= 0.5L)
115     {
116       int exp = -9999;
117       long double mantissa = printf_frexpl (x, &exp);
118       ASSERT (exp == i - 1);
119       ASSERT (mantissa == 1.73205L);
120     }
121   for (; i >= MIN_SUBNORMAL_EXP && x > 0.0L; i--, x *= 0.5L)
122     {
123       int exp = -9999;
124       long double mantissa = printf_frexpl (x, &exp);
125       ASSERT (exp == LDBL_MIN_EXP - 1);
126       ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP));
127       ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP));
128       ASSERT (mantissa == my_ldexp (x, - exp));
129     }
130
131   return 0;
132 }