1 /* Test of signbit() substitute.
2 Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
23 /* signbit must be a macro. */
25 # error missing declaration
33 #define ASSERT(expr) \
38 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
47 long double zerol = 0.0L;
49 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0f.
50 So we use -zerof instead. */
52 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
53 So we use -zerod instead. */
55 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
56 So we use minus_zerol instead.
57 IRIX cc can't put -0.0L into .data, but can compute at runtime.
58 Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
59 platforms, such as when cross-compiling to PowerPC on MacOS X 10.5. */
60 #if defined __hpux || defined __sgi
62 compute_minus_zerol (void)
64 return -LDBL_MIN * LDBL_MIN;
66 # define minus_zerol compute_minus_zerol ()
68 long double minus_zerol = -0.0L;
75 ASSERT (!signbit (3.141f));
76 ASSERT (!signbit (3.141e30f));
77 ASSERT (!signbit (3.141e-30f));
78 ASSERT (signbit (-2.718f));
79 ASSERT (signbit (-2.718e30f));
80 ASSERT (signbit (-2.718e-30f));
82 ASSERT (!signbit (0.0f));
83 if (1.0f / -zerof < 0)
84 ASSERT (signbit (-zerof));
86 ASSERT (!signbit (-zerof));
87 /* Infinite values. */
88 ASSERT (!signbit (1.0f / 0.0f));
89 ASSERT (signbit (-1.0f / 0.0f));
91 (void) signbit (zerof / zerof);
92 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
96 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
97 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
99 m.value = zerof / zerof;
100 # if FLT_EXPBIT0_BIT > 0
101 m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
103 m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
104 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
106 if (FLT_EXPBIT0_WORD < NWORDS / 2)
107 m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
109 m.word[0] |= (unsigned int) 1;
110 (void) signbit (m.value);
120 ASSERT (!signbit (3.141));
121 ASSERT (!signbit (3.141e30));
122 ASSERT (!signbit (3.141e-30));
123 ASSERT (signbit (-2.718));
124 ASSERT (signbit (-2.718e30));
125 ASSERT (signbit (-2.718e-30));
127 ASSERT (!signbit (0.0));
128 if (1.0 / -zerod < 0)
129 ASSERT (signbit (-zerod));
131 ASSERT (!signbit (-zerod));
132 /* Infinite values. */
133 ASSERT (!signbit (1.0 / 0.0));
134 ASSERT (signbit (-1.0 / 0.0));
136 (void) signbit (zerod / zerod);
137 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
138 /* Signalling NaN. */
141 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
142 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
144 m.value = zerod / zerod;
145 # if DBL_EXPBIT0_BIT > 0
146 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
148 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
149 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
151 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
152 |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
153 (void) signbit (m.value);
163 ASSERT (!signbit (3.141L));
164 ASSERT (!signbit (3.141e30L));
165 ASSERT (!signbit (3.141e-30L));
166 ASSERT (signbit (-2.718L));
167 ASSERT (signbit (-2.718e30L));
168 ASSERT (signbit (-2.718e-30L));
170 ASSERT (!signbit (0.0L));
171 if (1.0L / minus_zerol < 0)
172 ASSERT (signbit (minus_zerol));
174 ASSERT (!signbit (minus_zerol));
175 /* Infinite values. */
176 ASSERT (!signbit (1.0L / 0.0L));
177 ASSERT (signbit (-1.0L / 0.0L));
179 (void) signbit (zerol / zerol);
180 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
181 /* Signalling NaN. */
184 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
185 typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
186 memory_long_double m;
187 m.value = zerol / zerol;
188 # if LDBL_EXPBIT0_BIT > 0
189 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
191 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
192 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
194 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
195 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
196 (void) signbit (m.value);