1 /* Test of signbit() substitute.
2 Copyright (C) 2007, 2008 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 Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
53 platforms, such as when cross-compiling to PowerPC on MacOS X 10.5. */
54 #if defined __hpux || defined __sgi
55 long double minus_zerol = -LDBL_MIN * LDBL_MIN;
57 long double minus_zerol = -0.0L;
64 ASSERT (!signbit (3.141f));
65 ASSERT (!signbit (3.141e30f));
66 ASSERT (!signbit (3.141e-30f));
67 ASSERT (signbit (-2.718f));
68 ASSERT (signbit (-2.718e30f));
69 ASSERT (signbit (-2.718e-30f));
71 ASSERT (!signbit (0.0f));
72 if (1.0f / -zerof < 0)
73 ASSERT (signbit (-zerof));
75 ASSERT (!signbit (-zerof));
76 /* Infinite values. */
77 ASSERT (!signbit (1.0f / 0.0f));
78 ASSERT (signbit (-1.0f / 0.0f));
80 (void) signbit (zerof / zerof);
81 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
85 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
86 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
88 m.value = zerof / zerof;
89 # if FLT_EXPBIT0_BIT > 0
90 m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
92 m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
93 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
95 if (FLT_EXPBIT0_WORD < NWORDS / 2)
96 m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
98 m.word[0] |= (unsigned int) 1;
99 (void) signbit (m.value);
109 ASSERT (!signbit (3.141));
110 ASSERT (!signbit (3.141e30));
111 ASSERT (!signbit (3.141e-30));
112 ASSERT (signbit (-2.718));
113 ASSERT (signbit (-2.718e30));
114 ASSERT (signbit (-2.718e-30));
116 ASSERT (!signbit (0.0));
117 if (1.0 / -zerod < 0)
118 ASSERT (signbit (-zerod));
120 ASSERT (!signbit (-zerod));
121 /* Infinite values. */
122 ASSERT (!signbit (1.0 / 0.0));
123 ASSERT (signbit (-1.0 / 0.0));
125 (void) signbit (zerod / zerod);
126 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
127 /* Signalling NaN. */
130 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
131 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
133 m.value = zerod / zerod;
134 # if DBL_EXPBIT0_BIT > 0
135 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
137 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
138 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
140 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
141 |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
142 (void) signbit (m.value);
152 ASSERT (!signbit (3.141L));
153 ASSERT (!signbit (3.141e30L));
154 ASSERT (!signbit (3.141e-30L));
155 ASSERT (signbit (-2.718L));
156 ASSERT (signbit (-2.718e30L));
157 ASSERT (signbit (-2.718e-30L));
159 ASSERT (!signbit (0.0L));
160 if (1.0L / minus_zerol < 0)
161 ASSERT (signbit (minus_zerol));
163 ASSERT (!signbit (minus_zerol));
164 /* Infinite values. */
165 ASSERT (!signbit (1.0L / 0.0L));
166 ASSERT (signbit (-1.0L / 0.0L));
168 (void) signbit (zerol / zerol);
169 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
170 /* Signalling NaN. */
173 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
174 typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
175 memory_long_double m;
176 m.value = zerol / zerol;
177 # if LDBL_EXPBIT0_BIT > 0
178 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
180 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
181 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
183 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
184 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
185 (void) signbit (m.value);