Avoid compilation error due to MacOS X 10.5 gcc cross-compilation bug.
[gnulib.git] / tests / test-ldexpl.c
1 /* Test of multiplying a 'long double' by a power of 2.
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 <math.h>
22
23 #include <float.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "fpucw.h"
28 #include "isnanl-nolibm.h"
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
43    So we use minus_zero instead.
44    Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
45    platforms, such as when cross-compiling to PowerPC on MacOS X 10.5.  */
46 #if defined __hpux || defined __sgi
47 long double minus_zero = -LDBL_MIN * LDBL_MIN;
48 #else
49 long double minus_zero = -0.0L;
50 #endif
51
52 int
53 main ()
54 {
55   int i;
56   long double x;
57   long double y;
58   DECL_LONG_DOUBLE_ROUNDING
59
60   BEGIN_LONG_DOUBLE_ROUNDING ();
61
62   { /* NaN.  */
63     x = 0.0L / 0.0L;
64     y = ldexpl (x, 0); ASSERT (isnanl (y));
65     y = ldexpl (x, 5); ASSERT (isnanl (y));
66     y = ldexpl (x, -5); ASSERT (isnanl (y));
67   }
68
69   { /* Positive infinity.  */
70     x = 1.0L / 0.0L;
71     y = ldexpl (x, 0); ASSERT (y == x);
72     y = ldexpl (x, 5); ASSERT (y == x);
73     y = ldexpl (x, -5); ASSERT (y == x);
74   }
75
76   { /* Negative infinity.  */
77     x = -1.0L / 0.0L;
78     y = ldexpl (x, 0); ASSERT (y == x);
79     y = ldexpl (x, 5); ASSERT (y == x);
80     y = ldexpl (x, -5); ASSERT (y == x);
81   }
82
83   { /* Positive zero.  */
84     x = 0.0L;
85     y = ldexpl (x, 0); ASSERT (y == x); ASSERT (!signbit (x));
86     y = ldexpl (x, 5); ASSERT (y == x); ASSERT (!signbit (x));
87     y = ldexpl (x, -5); ASSERT (y == x); ASSERT (!signbit (x));
88   }
89
90   { /* Negative zero.  */
91     x = minus_zero;
92     y = ldexpl (x, 0); ASSERT (y == x); ASSERT (signbit (x));
93     y = ldexpl (x, 5); ASSERT (y == x); ASSERT (signbit (x));
94     y = ldexpl (x, -5); ASSERT (y == x); ASSERT (signbit (x));
95   }
96
97   { /* Positive finite number.  */
98     x = 1.73205L;
99     y = ldexpl (x, 0); ASSERT (y == x);
100     y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
101     y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
102   }
103
104   { /* Negative finite number.  */
105     x = -20.085536923187667742L;
106     y = ldexpl (x, 0); ASSERT (y == x);
107     y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
108     y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
109   }
110
111   for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L)
112     {
113       y = ldexpl (x, 0); ASSERT (y == x);
114       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
115       y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
116     }
117   for (i = 1, x = 1.73205L; i >= LDBL_MIN_EXP; i--, x *= 0.5L)
118     {
119       y = ldexpl (x, 0); ASSERT (y == x);
120       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
121       if (i - 5 >= LDBL_MIN_EXP)
122         {
123           y = ldexpl (x, -5); ASSERT (y == x * 0.03125L);
124         }
125     }
126   for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L)
127     {
128       y = ldexpl (x, 0); ASSERT (y == x);
129       y = ldexpl (x, 5); ASSERT (y == x * 32.0L);
130     }
131
132   return 0;
133 }