Avoid compilation error due to MacOS X 10.5 gcc cross-compilation bug.
[gnulib.git] / tests / test-signbit.c
1 /* Test of signbit() substitute.
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 <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #define ASSERT(expr) \
29   do                                                                         \
30     {                                                                        \
31       if (!(expr))                                                           \
32         {                                                                    \
33           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
34           fflush (stderr);                                                   \
35           abort ();                                                          \
36         }                                                                    \
37     }                                                                        \
38   while (0)
39
40 float zerof = 0.0f;
41 double zerod = 0.0;
42 long double zerol = 0.0L;
43
44 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0f.
45    So we use -zerof instead.  */
46
47 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
48    So we use -zerod instead.  */
49
50 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
51    So we use minus_zerol instead.
52    Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
53    platforms, such as when cross-compiling to PowerPC on MacOS X 10.5.  */
54 #if defined __hpux || defined __sgi
55 long double minus_zerol = -LDBL_MIN * LDBL_MIN;
56 #else
57 long double minus_zerol = -0.0L;
58 #endif
59
60 static void
61 test_signbitf ()
62 {
63   /* Finite values.  */
64   ASSERT (!signbit (3.141f));
65   ASSERT (!signbit (3.141e30f));
66   ASSERT (!signbit (3.141e-30f));
67   ASSERT (signbit (-2.718f));
68   ASSERT (signbit (-2.718e30f));
69   ASSERT (signbit (-2.718e-30f));
70   /* Zeros.  */
71   ASSERT (!signbit (0.0f));
72   if (1.0f / -zerof < 0)
73     ASSERT (signbit (-zerof));
74   else
75     ASSERT (!signbit (-zerof));
76   /* Infinite values.  */
77   ASSERT (!signbit (1.0f / 0.0f));
78   ASSERT (signbit (-1.0f / 0.0f));
79   /* Quiet NaN.  */
80   (void) signbit (zerof / zerof);
81 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
82   /* Signalling NaN.  */
83   {
84     #define NWORDS \
85       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
86     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
87     memory_float m;
88     m.value = zerof / zerof;
89 # if FLT_EXPBIT0_BIT > 0
90     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
91 # else
92     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
93       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
94 # endif
95     if (FLT_EXPBIT0_WORD < NWORDS / 2)
96       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
97     else
98       m.word[0] |= (unsigned int) 1;
99     (void) signbit (m.value);
100     #undef NWORDS
101   }
102 #endif
103 }
104
105 static void
106 test_signbitd ()
107 {
108   /* Finite values.  */
109   ASSERT (!signbit (3.141));
110   ASSERT (!signbit (3.141e30));
111   ASSERT (!signbit (3.141e-30));
112   ASSERT (signbit (-2.718));
113   ASSERT (signbit (-2.718e30));
114   ASSERT (signbit (-2.718e-30));
115   /* Zeros.  */
116   ASSERT (!signbit (0.0));
117   if (1.0 / -zerod < 0)
118     ASSERT (signbit (-zerod));
119   else
120     ASSERT (!signbit (-zerod));
121   /* Infinite values.  */
122   ASSERT (!signbit (1.0 / 0.0));
123   ASSERT (signbit (-1.0 / 0.0));
124   /* Quiet NaN.  */
125   (void) signbit (zerod / zerod);
126 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
127   /* Signalling NaN.  */
128   {
129     #define NWORDS \
130       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
131     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
132     memory_double m;
133     m.value = zerod / zerod;
134 # if DBL_EXPBIT0_BIT > 0
135     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
136 # else
137     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
138       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
139 # endif
140     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
141       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
142     (void) signbit (m.value);
143     #undef NWORDS
144   }
145 #endif
146 }
147
148 static void
149 test_signbitl ()
150 {
151   /* Finite values.  */
152   ASSERT (!signbit (3.141L));
153   ASSERT (!signbit (3.141e30L));
154   ASSERT (!signbit (3.141e-30L));
155   ASSERT (signbit (-2.718L));
156   ASSERT (signbit (-2.718e30L));
157   ASSERT (signbit (-2.718e-30L));
158   /* Zeros.  */
159   ASSERT (!signbit (0.0L));
160   if (1.0L / minus_zerol < 0)
161     ASSERT (signbit (minus_zerol));
162   else
163     ASSERT (!signbit (minus_zerol));
164   /* Infinite values.  */
165   ASSERT (!signbit (1.0L / 0.0L));
166   ASSERT (signbit (-1.0L / 0.0L));
167   /* Quiet NaN.  */
168   (void) signbit (zerol / zerol);
169 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
170   /* Signalling NaN.  */
171   {
172     #define NWORDS \
173       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
174     typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
175     memory_long_double m;
176     m.value = zerol / zerol;
177 # if LDBL_EXPBIT0_BIT > 0
178     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
179 # else
180     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
181       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
182 # endif
183     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
184       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
185     (void) signbit (m.value);
186     #undef NWORDS
187   }
188 #endif
189 }
190
191 int
192 main ()
193 {
194   test_signbitf ();
195   test_signbitd ();
196   test_signbitl ();
197   return 0;
198 }