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. */
27 #define ASSERT(expr) \
32 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41 long double zerol = 0.0L;
47 ASSERT (!signbit (3.141f));
48 ASSERT (!signbit (3.141e30f));
49 ASSERT (!signbit (3.141e-30f));
50 ASSERT (signbit (-2.718f));
51 ASSERT (signbit (-2.718e30f));
52 ASSERT (signbit (-2.718e-30f));
54 ASSERT (!signbit (0.0f));
55 if (1.0f / -zerof < 0)
56 ASSERT (signbit (-0.0f));
58 ASSERT (!signbit (-0.0f));
59 /* Infinite values. */
60 ASSERT (!signbit (1.0f / 0.0f));
61 ASSERT (signbit (-1.0f / 0.0f));
63 (void) signbit (zerof / zerof);
64 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
68 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
69 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
71 m.value = zerof / zerof;
72 # if FLT_EXPBIT0_BIT > 0
73 m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
75 m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
76 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
78 if (FLT_EXPBIT0_WORD < NWORDS / 2)
79 m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
81 m.word[0] |= (unsigned int) 1;
82 (void) signbit (m.value);
92 ASSERT (!signbit (3.141));
93 ASSERT (!signbit (3.141e30));
94 ASSERT (!signbit (3.141e-30));
95 ASSERT (signbit (-2.718));
96 ASSERT (signbit (-2.718e30));
97 ASSERT (signbit (-2.718e-30));
99 ASSERT (!signbit (0.0));
100 if (1.0 / -zerod < 0)
101 ASSERT (signbit (-0.0));
103 ASSERT (!signbit (-0.0));
104 /* Infinite values. */
105 ASSERT (!signbit (1.0 / 0.0));
106 ASSERT (signbit (-1.0 / 0.0));
108 (void) signbit (zerod / zerod);
109 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
110 /* Signalling NaN. */
113 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
114 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
116 m.value = zerod / zerod;
117 # if DBL_EXPBIT0_BIT > 0
118 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
120 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
121 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
123 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
124 |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
125 (void) signbit (m.value);
135 ASSERT (!signbit (3.141L));
136 ASSERT (!signbit (3.141e30L));
137 ASSERT (!signbit (3.141e-30L));
138 ASSERT (signbit (-2.718L));
139 ASSERT (signbit (-2.718e30L));
140 ASSERT (signbit (-2.718e-30L));
142 ASSERT (!signbit (0.0L));
143 if (1.0L / -zerol < 0)
144 ASSERT (signbit (-0.0L));
146 ASSERT (!signbit (-0.0L));
147 /* Infinite values. */
148 ASSERT (!signbit (1.0L / 0.0L));
149 ASSERT (signbit (-1.0L / 0.0L));
151 (void) signbit (zerol / zerol);
152 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
153 /* Signalling NaN. */
156 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
157 typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
158 memory_long_double m;
159 m.value = zerol / zerol;
160 # if LDBL_EXPBIT0_BIT > 0
161 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
163 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
164 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
166 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
167 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
168 (void) signbit (m.value);