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. */
28 #define ASSERT(expr) \
33 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
42 long double zerol = 0.0L;
44 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0f.
45 So we use -zerof instead. */
47 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
48 So we use -zerod instead. */
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
57 compute_minus_zerol (void)
59 return -LDBL_MIN * LDBL_MIN;
61 # define minus_zerol compute_minus_zerol ()
63 long double minus_zerol = -0.0L;
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));
77 ASSERT (!signbit (0.0f));
78 if (1.0f / -zerof < 0)
79 ASSERT (signbit (-zerof));
81 ASSERT (!signbit (-zerof));
82 /* Infinite values. */
83 ASSERT (!signbit (1.0f / 0.0f));
84 ASSERT (signbit (-1.0f / 0.0f));
86 (void) signbit (zerof / zerof);
87 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
91 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
92 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
94 m.value = zerof / zerof;
95 # if FLT_EXPBIT0_BIT > 0
96 m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
98 m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
99 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
101 if (FLT_EXPBIT0_WORD < NWORDS / 2)
102 m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
104 m.word[0] |= (unsigned int) 1;
105 (void) signbit (m.value);
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));
122 ASSERT (!signbit (0.0));
123 if (1.0 / -zerod < 0)
124 ASSERT (signbit (-zerod));
126 ASSERT (!signbit (-zerod));
127 /* Infinite values. */
128 ASSERT (!signbit (1.0 / 0.0));
129 ASSERT (signbit (-1.0 / 0.0));
131 (void) signbit (zerod / zerod);
132 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
133 /* Signalling NaN. */
136 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
137 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
139 m.value = zerod / zerod;
140 # if DBL_EXPBIT0_BIT > 0
141 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
143 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
144 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
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);
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));
165 ASSERT (!signbit (0.0L));
166 if (1.0L / minus_zerol < 0)
167 ASSERT (signbit (minus_zerol));
169 ASSERT (!signbit (minus_zerol));
170 /* Infinite values. */
171 ASSERT (!signbit (1.0L / 0.0L));
172 ASSERT (signbit (-1.0L / 0.0L));
174 (void) signbit (zerol / zerol);
175 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
176 /* Signalling NaN. */
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);
186 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
187 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
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);