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