tests: IRIX 6.2 cc can't compile -0.0 into .data
[gnulib.git] / tests / test-signbit.c
1 /* Test of signbit() substitute.
2    Copyright (C) 2007, 2008, 2009 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    IRIX cc can't put -0.0L into .data, but can compute at runtime.
53    Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
54    platforms, such as when cross-compiling to PowerPC on MacOS X 10.5.  */
55 #if defined __hpux || defined __sgi
56 static long double
57 compute_minus_zerol (void)
58 {
59   return -LDBL_MIN * LDBL_MIN;
60 }
61 # define minus_zerol compute_minus_zerol ()
62 #else
63 long double minus_zerol = -0.0L;
64 #endif
65
66 static void
67 test_signbitf ()
68 {
69   /* Finite values.  */
70   ASSERT (!signbit (3.141f));
71   ASSERT (!signbit (3.141e30f));
72   ASSERT (!signbit (3.141e-30f));
73   ASSERT (signbit (-2.718f));
74   ASSERT (signbit (-2.718e30f));
75   ASSERT (signbit (-2.718e-30f));
76   /* Zeros.  */
77   ASSERT (!signbit (0.0f));
78   if (1.0f / -zerof < 0)
79     ASSERT (signbit (-zerof));
80   else
81     ASSERT (!signbit (-zerof));
82   /* Infinite values.  */
83   ASSERT (!signbit (1.0f / 0.0f));
84   ASSERT (signbit (-1.0f / 0.0f));
85   /* Quiet NaN.  */
86   (void) signbit (zerof / zerof);
87 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
88   /* Signalling NaN.  */
89   {
90     #define NWORDS \
91       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
92     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
93     memory_float m;
94     m.value = zerof / zerof;
95 # if FLT_EXPBIT0_BIT > 0
96     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
97 # else
98     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
99       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
100 # endif
101     if (FLT_EXPBIT0_WORD < NWORDS / 2)
102       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
103     else
104       m.word[0] |= (unsigned int) 1;
105     (void) signbit (m.value);
106     #undef NWORDS
107   }
108 #endif
109 }
110
111 static void
112 test_signbitd ()
113 {
114   /* Finite values.  */
115   ASSERT (!signbit (3.141));
116   ASSERT (!signbit (3.141e30));
117   ASSERT (!signbit (3.141e-30));
118   ASSERT (signbit (-2.718));
119   ASSERT (signbit (-2.718e30));
120   ASSERT (signbit (-2.718e-30));
121   /* Zeros.  */
122   ASSERT (!signbit (0.0));
123   if (1.0 / -zerod < 0)
124     ASSERT (signbit (-zerod));
125   else
126     ASSERT (!signbit (-zerod));
127   /* Infinite values.  */
128   ASSERT (!signbit (1.0 / 0.0));
129   ASSERT (signbit (-1.0 / 0.0));
130   /* Quiet NaN.  */
131   (void) signbit (zerod / zerod);
132 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
133   /* Signalling NaN.  */
134   {
135     #define NWORDS \
136       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
137     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
138     memory_double m;
139     m.value = zerod / zerod;
140 # if DBL_EXPBIT0_BIT > 0
141     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
142 # else
143     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
144       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
145 # endif
146     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
147       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
148     (void) signbit (m.value);
149     #undef NWORDS
150   }
151 #endif
152 }
153
154 static void
155 test_signbitl ()
156 {
157   /* Finite values.  */
158   ASSERT (!signbit (3.141L));
159   ASSERT (!signbit (3.141e30L));
160   ASSERT (!signbit (3.141e-30L));
161   ASSERT (signbit (-2.718L));
162   ASSERT (signbit (-2.718e30L));
163   ASSERT (signbit (-2.718e-30L));
164   /* Zeros.  */
165   ASSERT (!signbit (0.0L));
166   if (1.0L / minus_zerol < 0)
167     ASSERT (signbit (minus_zerol));
168   else
169     ASSERT (!signbit (minus_zerol));
170   /* Infinite values.  */
171   ASSERT (!signbit (1.0L / 0.0L));
172   ASSERT (signbit (-1.0L / 0.0L));
173   /* Quiet NaN.  */
174   (void) signbit (zerol / zerol);
175 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
176   /* Signalling NaN.  */
177   {
178     #define NWORDS \
179       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
180     typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
181     memory_long_double m;
182     m.value = zerol / zerol;
183 # if LDBL_EXPBIT0_BIT > 0
184     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
185 # else
186     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
187       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
188 # endif
189     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
190       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
191     (void) signbit (m.value);
192     #undef NWORDS
193   }
194 #endif
195 }
196
197 int
198 main ()
199 {
200   test_signbitf ();
201   test_signbitd ();
202   test_signbitl ();
203   return 0;
204 }