Use a more portable replacement expression for -0.0L.
[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 long double minus_zerol = -LDBL_MIN * LDBL_MIN;
53
54 static void
55 test_signbitf ()
56 {
57   /* Finite values.  */
58   ASSERT (!signbit (3.141f));
59   ASSERT (!signbit (3.141e30f));
60   ASSERT (!signbit (3.141e-30f));
61   ASSERT (signbit (-2.718f));
62   ASSERT (signbit (-2.718e30f));
63   ASSERT (signbit (-2.718e-30f));
64   /* Zeros.  */
65   ASSERT (!signbit (0.0f));
66   if (1.0f / -zerof < 0)
67     ASSERT (signbit (-zerof));
68   else
69     ASSERT (!signbit (-zerof));
70   /* Infinite values.  */
71   ASSERT (!signbit (1.0f / 0.0f));
72   ASSERT (signbit (-1.0f / 0.0f));
73   /* Quiet NaN.  */
74   (void) signbit (zerof / zerof);
75 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
76   /* Signalling NaN.  */
77   {
78     #define NWORDS \
79       ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
80     typedef union { float value; unsigned int word[NWORDS]; } memory_float;
81     memory_float m;
82     m.value = zerof / zerof;
83 # if FLT_EXPBIT0_BIT > 0
84     m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
85 # else
86     m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
87       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
88 # endif
89     if (FLT_EXPBIT0_WORD < NWORDS / 2)
90       m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
91     else
92       m.word[0] |= (unsigned int) 1;
93     (void) signbit (m.value);
94     #undef NWORDS
95   }
96 #endif
97 }
98
99 static void
100 test_signbitd ()
101 {
102   /* Finite values.  */
103   ASSERT (!signbit (3.141));
104   ASSERT (!signbit (3.141e30));
105   ASSERT (!signbit (3.141e-30));
106   ASSERT (signbit (-2.718));
107   ASSERT (signbit (-2.718e30));
108   ASSERT (signbit (-2.718e-30));
109   /* Zeros.  */
110   ASSERT (!signbit (0.0));
111   if (1.0 / -zerod < 0)
112     ASSERT (signbit (-zerod));
113   else
114     ASSERT (!signbit (-zerod));
115   /* Infinite values.  */
116   ASSERT (!signbit (1.0 / 0.0));
117   ASSERT (signbit (-1.0 / 0.0));
118   /* Quiet NaN.  */
119   (void) signbit (zerod / zerod);
120 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
121   /* Signalling NaN.  */
122   {
123     #define NWORDS \
124       ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
125     typedef union { double value; unsigned int word[NWORDS]; } memory_double;
126     memory_double m;
127     m.value = zerod / zerod;
128 # if DBL_EXPBIT0_BIT > 0
129     m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
130 # else
131     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
132       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
133 # endif
134     m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
135       |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
136     (void) signbit (m.value);
137     #undef NWORDS
138   }
139 #endif
140 }
141
142 static void
143 test_signbitl ()
144 {
145   /* Finite values.  */
146   ASSERT (!signbit (3.141L));
147   ASSERT (!signbit (3.141e30L));
148   ASSERT (!signbit (3.141e-30L));
149   ASSERT (signbit (-2.718L));
150   ASSERT (signbit (-2.718e30L));
151   ASSERT (signbit (-2.718e-30L));
152   /* Zeros.  */
153   ASSERT (!signbit (0.0L));
154   if (1.0L / minus_zerol < 0)
155     ASSERT (signbit (minus_zerol));
156   else
157     ASSERT (!signbit (minus_zerol));
158   /* Infinite values.  */
159   ASSERT (!signbit (1.0L / 0.0L));
160   ASSERT (signbit (-1.0L / 0.0L));
161   /* Quiet NaN.  */
162   (void) signbit (zerol / zerol);
163 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
164   /* Signalling NaN.  */
165   {
166     #define NWORDS \
167       ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
168     typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
169     memory_long_double m;
170     m.value = zerol / zerol;
171 # if LDBL_EXPBIT0_BIT > 0
172     m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
173 # else
174     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
175       ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
176 # endif
177     m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
178       |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
179     (void) signbit (m.value);
180     #undef NWORDS
181   }
182 #endif
183 }
184
185 int
186 main ()
187 {
188   test_signbitf ();
189   test_signbitd ();
190   test_signbitl ();
191   return 0;
192 }