1 /* Test of signbit() substitute.
2 Copyright (C) 2007-2020 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
23 /* signbit must be a macro. */
25 # error missing declaration
31 #include "minus-zero.h"
37 long double zerol
= 0.0L;
43 ASSERT (!signbit (3.141f
));
44 ASSERT (!signbit (3.141e30f
));
45 ASSERT (!signbit (3.141e-30f
));
46 ASSERT (signbit (-2.718f
));
47 ASSERT (signbit (-2.718e30f
));
48 ASSERT (signbit (-2.718e-30f
));
50 ASSERT (!signbit (0.0f
));
51 if (1.0f
/ minus_zerof
< 0)
52 ASSERT (signbit (minus_zerof
));
54 ASSERT (!signbit (minus_zerof
));
55 /* Infinite values. */
56 ASSERT (!signbit (Infinityf ()));
57 ASSERT (signbit (- Infinityf ()));
59 (void) signbit (zerof
/ zerof
);
60 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
64 ((sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
65 typedef union { float value
; unsigned int word
[NWORDS
]; } memory_float
;
67 m
.value
= zerof
/ zerof
;
68 # if FLT_EXPBIT0_BIT > 0
69 m
.word
[FLT_EXPBIT0_WORD
] ^= (unsigned int) 1 << (FLT_EXPBIT0_BIT
- 1);
71 m
.word
[FLT_EXPBIT0_WORD
+ (FLT_EXPBIT0_WORD
< NWORDS
/ 2 ? 1 : - 1)]
72 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT
- 1);
74 if (FLT_EXPBIT0_WORD
< NWORDS
/ 2)
75 m
.word
[FLT_EXPBIT0_WORD
+ 1] |= (unsigned int) 1 << FLT_EXPBIT0_BIT
;
77 m
.word
[0] |= (unsigned int) 1;
78 (void) signbit (m
.value
);
88 ASSERT (!signbit (3.141));
89 ASSERT (!signbit (3.141e30
));
90 ASSERT (!signbit (3.141e-30));
91 ASSERT (signbit (-2.718));
92 ASSERT (signbit (-2.718e30
));
93 ASSERT (signbit (-2.718e-30));
95 ASSERT (!signbit (0.0));
96 if (1.0 / minus_zerod
< 0)
97 ASSERT (signbit (minus_zerod
));
99 ASSERT (!signbit (minus_zerod
));
100 /* Infinite values. */
101 ASSERT (!signbit (Infinityd ()));
102 ASSERT (signbit (- Infinityd ()));
104 (void) signbit (zerod
/ zerod
);
105 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
106 /* Signalling NaN. */
109 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
110 typedef union { double value
; unsigned int word
[NWORDS
]; } memory_double
;
112 m
.value
= zerod
/ zerod
;
113 # if DBL_EXPBIT0_BIT > 0
114 m
.word
[DBL_EXPBIT0_WORD
] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT
- 1);
116 m
.word
[DBL_EXPBIT0_WORD
+ (DBL_EXPBIT0_WORD
< NWORDS
/ 2 ? 1 : - 1)]
117 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT
- 1);
119 m
.word
[DBL_EXPBIT0_WORD
+ (DBL_EXPBIT0_WORD
< NWORDS
/ 2 ? 1 : - 1)]
120 |= (unsigned int) 1 << DBL_EXPBIT0_BIT
;
121 (void) signbit (m
.value
);
131 ASSERT (!signbit (3.141L));
132 ASSERT (!signbit (3.141e30L
));
133 ASSERT (!signbit (3.141e-30L));
134 ASSERT (signbit (-2.718L));
135 ASSERT (signbit (-2.718e30L
));
136 ASSERT (signbit (-2.718e-30L));
138 ASSERT (!signbit (0.0L));
139 if (1.0L / minus_zerol
< 0)
140 ASSERT (signbit (minus_zerol
));
142 ASSERT (!signbit (minus_zerol
));
143 /* Infinite values. */
144 ASSERT (!signbit (Infinityl ()));
145 ASSERT (signbit (- Infinityl ()));
147 (void) signbit (zerol
/ zerol
);
148 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
149 /* Signalling NaN. */
152 ((sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
153 typedef union { long double value
; unsigned int word
[NWORDS
]; } memory_long_double
;
155 #if defined __powerpc__ && LDBL_MANT_DIG == 106
156 /* This is PowerPC "double double", a pair of two doubles. Inf and Nan are
157 represented as the corresponding 64-bit IEEE values in the first double;
158 the second is ignored. Manipulate only the first double. */
161 ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
164 memory_long_double m
;
165 m
.value
= zerol
/ zerol
;
166 # if LDBL_EXPBIT0_BIT > 0
167 m
.word
[LDBL_EXPBIT0_WORD
] ^= (unsigned int) 1 << (LDBL_EXPBIT0_BIT
- 1);
169 m
.word
[LDBL_EXPBIT0_WORD
+ (LDBL_EXPBIT0_WORD
< NWORDS
/ 2 ? 1 : - 1)]
170 ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT
- 1);
172 m
.word
[LDBL_EXPBIT0_WORD
+ (LDBL_EXPBIT0_WORD
< NWORDS
/ 2 ? 1 : - 1)]
173 |= (unsigned int) 1 << LDBL_EXPBIT0_BIT
;
174 (void) signbit (m
.value
);