1 /* Test of signbit() substitute.
2 Copyright (C) 2007, 2008, 2009, 2010 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
35 long double zerol = 0.0L;
37 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0f.
38 So we use -zerof instead. */
40 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
41 So we use -zerod instead. */
43 /* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
44 So we use minus_zerol instead.
45 IRIX cc can't put -0.0L into .data, but can compute at runtime.
46 Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
47 platforms, such as when cross-compiling to PowerPC on MacOS X 10.5. */
48 #if defined __hpux || defined __sgi
50 compute_minus_zerol (void)
52 return -LDBL_MIN * LDBL_MIN;
54 # define minus_zerol compute_minus_zerol ()
56 long double minus_zerol = -0.0L;
63 ASSERT (!signbit (3.141f));
64 ASSERT (!signbit (3.141e30f));
65 ASSERT (!signbit (3.141e-30f));
66 ASSERT (signbit (-2.718f));
67 ASSERT (signbit (-2.718e30f));
68 ASSERT (signbit (-2.718e-30f));
70 ASSERT (!signbit (0.0f));
71 if (1.0f / -zerof < 0)
72 ASSERT (signbit (-zerof));
74 ASSERT (!signbit (-zerof));
75 /* Infinite values. */
76 ASSERT (!signbit (1.0f / 0.0f));
77 ASSERT (signbit (-1.0f / 0.0f));
79 (void) signbit (zerof / zerof);
80 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
84 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
85 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
87 m.value = zerof / zerof;
88 # if FLT_EXPBIT0_BIT > 0
89 m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
91 m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
92 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
94 if (FLT_EXPBIT0_WORD < NWORDS / 2)
95 m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
97 m.word[0] |= (unsigned int) 1;
98 (void) signbit (m.value);
108 ASSERT (!signbit (3.141));
109 ASSERT (!signbit (3.141e30));
110 ASSERT (!signbit (3.141e-30));
111 ASSERT (signbit (-2.718));
112 ASSERT (signbit (-2.718e30));
113 ASSERT (signbit (-2.718e-30));
115 ASSERT (!signbit (0.0));
116 if (1.0 / -zerod < 0)
117 ASSERT (signbit (-zerod));
119 ASSERT (!signbit (-zerod));
120 /* Infinite values. */
121 ASSERT (!signbit (1.0 / 0.0));
122 ASSERT (signbit (-1.0 / 0.0));
124 (void) signbit (zerod / zerod);
125 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
126 /* Signalling NaN. */
129 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
130 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
132 m.value = zerod / zerod;
133 # if DBL_EXPBIT0_BIT > 0
134 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
136 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
137 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
139 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
140 |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
141 (void) signbit (m.value);
151 ASSERT (!signbit (3.141L));
152 ASSERT (!signbit (3.141e30L));
153 ASSERT (!signbit (3.141e-30L));
154 ASSERT (signbit (-2.718L));
155 ASSERT (signbit (-2.718e30L));
156 ASSERT (signbit (-2.718e-30L));
158 ASSERT (!signbit (0.0L));
159 if (1.0L / minus_zerol < 0)
160 ASSERT (signbit (minus_zerol));
162 ASSERT (!signbit (minus_zerol));
163 /* Infinite values. */
164 ASSERT (!signbit (1.0L / 0.0L));
165 ASSERT (signbit (-1.0L / 0.0L));
167 (void) signbit (zerol / zerol);
168 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
169 /* Signalling NaN. */
172 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
173 typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
174 memory_long_double m;
175 m.value = zerol / zerol;
176 # if LDBL_EXPBIT0_BIT > 0
177 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
179 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
180 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
182 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
183 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
184 (void) signbit (m.value);