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;
42 /* We cannot use the expression '-zerol' here, because on HP-UX/hppa it
43 evaluates to 0.0L, not -0.0L. */
44 long double minus_zerol = -0.0L;
50 ASSERT (!signbit (3.141f));
51 ASSERT (!signbit (3.141e30f));
52 ASSERT (!signbit (3.141e-30f));
53 ASSERT (signbit (-2.718f));
54 ASSERT (signbit (-2.718e30f));
55 ASSERT (signbit (-2.718e-30f));
57 ASSERT (!signbit (0.0f));
58 if (1.0f / -zerof < 0)
59 ASSERT (signbit (-0.0f));
61 ASSERT (!signbit (-0.0f));
62 /* Infinite values. */
63 ASSERT (!signbit (1.0f / 0.0f));
64 ASSERT (signbit (-1.0f / 0.0f));
66 (void) signbit (zerof / zerof);
67 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
71 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
72 typedef union { float value; unsigned int word[NWORDS]; } memory_float;
74 m.value = zerof / zerof;
75 # if FLT_EXPBIT0_BIT > 0
76 m.word[FLT_EXPBIT0_WORD] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT - 1);
78 m.word[FLT_EXPBIT0_WORD + (FLT_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
79 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
81 if (FLT_EXPBIT0_WORD < NWORDS / 2)
82 m.word[FLT_EXPBIT0_WORD + 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT;
84 m.word[0] |= (unsigned int) 1;
85 (void) signbit (m.value);
95 ASSERT (!signbit (3.141));
96 ASSERT (!signbit (3.141e30));
97 ASSERT (!signbit (3.141e-30));
98 ASSERT (signbit (-2.718));
99 ASSERT (signbit (-2.718e30));
100 ASSERT (signbit (-2.718e-30));
102 ASSERT (!signbit (0.0));
103 if (1.0 / -zerod < 0)
104 ASSERT (signbit (-0.0));
106 ASSERT (!signbit (-0.0));
107 /* Infinite values. */
108 ASSERT (!signbit (1.0 / 0.0));
109 ASSERT (signbit (-1.0 / 0.0));
111 (void) signbit (zerod / zerod);
112 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
113 /* Signalling NaN. */
116 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
117 typedef union { double value; unsigned int word[NWORDS]; } memory_double;
119 m.value = zerod / zerod;
120 # if DBL_EXPBIT0_BIT > 0
121 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1);
123 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
124 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
126 m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
127 |= (unsigned int) 1 << DBL_EXPBIT0_BIT;
128 (void) signbit (m.value);
138 ASSERT (!signbit (3.141L));
139 ASSERT (!signbit (3.141e30L));
140 ASSERT (!signbit (3.141e-30L));
141 ASSERT (signbit (-2.718L));
142 ASSERT (signbit (-2.718e30L));
143 ASSERT (signbit (-2.718e-30L));
145 ASSERT (!signbit (0.0L));
146 if (1.0L / minus_zerol < 0)
147 ASSERT (signbit (-0.0L));
149 ASSERT (!signbit (-0.0L));
150 /* Infinite values. */
151 ASSERT (!signbit (1.0L / 0.0L));
152 ASSERT (signbit (-1.0L / 0.0L));
154 (void) signbit (zerol / zerol);
155 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
156 /* Signalling NaN. */
159 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
160 typedef union { long double value; unsigned int word[NWORDS]; } memory_long_double;
161 memory_long_double m;
162 m.value = zerol / zerol;
163 # if LDBL_EXPBIT0_BIT > 0
164 m.word[LDBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT - 1);
166 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
167 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1);
169 m.word[LDBL_EXPBIT0_WORD + (LDBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)]
170 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT;
171 (void) signbit (m.value);