lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <fenv.h> |
| 2 | #include <math.h> |
| 3 | #include <float.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <math-tests.h> |
| 7 | |
| 8 | #if !defined(FE_OVERFLOW) && !defined(FE_UNDERFLOW) |
| 9 | /* If there's no support for the exceptions this test is checking, |
| 10 | then just return success and allow the test to be compiled. */ |
| 11 | # define fetestexcept(e) 1 |
| 12 | #endif |
| 13 | |
| 14 | float zero = 0.0; |
| 15 | float inf = INFINITY; |
| 16 | |
| 17 | int |
| 18 | main (void) |
| 19 | { |
| 20 | int result = 0; |
| 21 | |
| 22 | float i = INFINITY; |
| 23 | float m = FLT_MAX; |
| 24 | feclearexcept (FE_ALL_EXCEPT); |
| 25 | if (nextafterf (m, i) != i) |
| 26 | { |
| 27 | puts ("nextafterf+ failed"); |
| 28 | ++result; |
| 29 | } |
| 30 | if (EXCEPTION_TESTS (float) && fetestexcept (FE_OVERFLOW) == 0) |
| 31 | { |
| 32 | puts ("nextafterf+ did not overflow"); |
| 33 | ++result; |
| 34 | } |
| 35 | feclearexcept (FE_ALL_EXCEPT); |
| 36 | if (nextafterf (-m, -i) != -i) |
| 37 | { |
| 38 | puts ("nextafterf- failed"); |
| 39 | ++result; |
| 40 | } |
| 41 | if (EXCEPTION_TESTS (float) && fetestexcept (FE_OVERFLOW) == 0) |
| 42 | { |
| 43 | puts ("nextafterf- did not overflow"); |
| 44 | ++result; |
| 45 | } |
| 46 | |
| 47 | i = 0; |
| 48 | m = FLT_MIN; |
| 49 | feclearexcept (FE_ALL_EXCEPT); |
| 50 | i = nextafterf (m, i); |
| 51 | if (i < 0 || i >= FLT_MIN) |
| 52 | { |
| 53 | puts ("nextafterf+ failed"); |
| 54 | ++result; |
| 55 | } |
| 56 | if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0) |
| 57 | { |
| 58 | puts ("nextafterf+ did not underflow"); |
| 59 | ++result; |
| 60 | } |
| 61 | i = 0; |
| 62 | feclearexcept (FE_ALL_EXCEPT); |
| 63 | i = nextafterf (-m, -i); |
| 64 | if (i > 0 || i <= -FLT_MIN) |
| 65 | { |
| 66 | puts ("nextafterf- failed"); |
| 67 | ++result; |
| 68 | } |
| 69 | if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0) |
| 70 | { |
| 71 | puts ("nextafterf- did not underflow"); |
| 72 | ++result; |
| 73 | } |
| 74 | i = -INFINITY; |
| 75 | feclearexcept (FE_ALL_EXCEPT); |
| 76 | m = nextafterf (zero, inf); |
| 77 | if (m < 0.0 || m >= FLT_MIN) |
| 78 | { |
| 79 | puts ("nextafterf+ failed"); |
| 80 | ++result; |
| 81 | } |
| 82 | if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0) |
| 83 | { |
| 84 | puts ("nextafterf+ did not underflow"); |
| 85 | ++result; |
| 86 | } |
| 87 | feclearexcept (FE_ALL_EXCEPT); |
| 88 | if (nextafterf (m, i) != 0.0) |
| 89 | { |
| 90 | puts ("nextafterf+ failed"); |
| 91 | ++result; |
| 92 | } |
| 93 | if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0) |
| 94 | { |
| 95 | puts ("nextafterf+ did not underflow"); |
| 96 | ++result; |
| 97 | } |
| 98 | feclearexcept (FE_ALL_EXCEPT); |
| 99 | m = nextafterf (copysignf (zero, -1.0), -inf); |
| 100 | if (m > 0.0 || m <= -FLT_MIN) |
| 101 | { |
| 102 | puts ("nextafterf- failed"); |
| 103 | ++result; |
| 104 | } |
| 105 | if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0) |
| 106 | { |
| 107 | puts ("nextafterf- did not underflow"); |
| 108 | ++result; |
| 109 | } |
| 110 | feclearexcept (FE_ALL_EXCEPT); |
| 111 | if (nextafterf (m, -i) != 0.0) |
| 112 | { |
| 113 | puts ("nextafterf- failed"); |
| 114 | ++result; |
| 115 | } |
| 116 | if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0) |
| 117 | { |
| 118 | puts ("nextafterf- did not underflow"); |
| 119 | ++result; |
| 120 | } |
| 121 | |
| 122 | double di = INFINITY; |
| 123 | double dm = DBL_MAX; |
| 124 | feclearexcept (FE_ALL_EXCEPT); |
| 125 | if (nextafter (dm, di) != di) |
| 126 | { |
| 127 | puts ("nextafter+ failed"); |
| 128 | ++result; |
| 129 | } |
| 130 | if (EXCEPTION_TESTS (double) && fetestexcept (FE_OVERFLOW) == 0) |
| 131 | { |
| 132 | puts ("nextafter+ did not overflow"); |
| 133 | ++result; |
| 134 | } |
| 135 | feclearexcept (FE_ALL_EXCEPT); |
| 136 | if (nextafter (-dm, -di) != -di) |
| 137 | { |
| 138 | puts ("nextafter failed"); |
| 139 | ++result; |
| 140 | } |
| 141 | if (EXCEPTION_TESTS (double) && fetestexcept (FE_OVERFLOW) == 0) |
| 142 | { |
| 143 | puts ("nextafter- did not overflow"); |
| 144 | ++result; |
| 145 | } |
| 146 | |
| 147 | di = 0; |
| 148 | dm = DBL_MIN; |
| 149 | feclearexcept (FE_ALL_EXCEPT); |
| 150 | di = nextafter (dm, di); |
| 151 | if (di < 0 || di >= DBL_MIN) |
| 152 | { |
| 153 | puts ("nextafter+ failed"); |
| 154 | ++result; |
| 155 | } |
| 156 | if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 157 | { |
| 158 | puts ("nextafter+ did not underflow"); |
| 159 | ++result; |
| 160 | } |
| 161 | di = 0; |
| 162 | feclearexcept (FE_ALL_EXCEPT); |
| 163 | di = nextafter (-dm, -di); |
| 164 | if (di > 0 || di <= -DBL_MIN) |
| 165 | { |
| 166 | puts ("nextafter- failed"); |
| 167 | ++result; |
| 168 | } |
| 169 | if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 170 | { |
| 171 | puts ("nextafter- did not underflow"); |
| 172 | ++result; |
| 173 | } |
| 174 | di = -INFINITY; |
| 175 | feclearexcept (FE_ALL_EXCEPT); |
| 176 | dm = nextafter (zero, inf); |
| 177 | if (dm < 0.0 || dm >= DBL_MIN) |
| 178 | { |
| 179 | puts ("nextafter+ failed"); |
| 180 | ++result; |
| 181 | } |
| 182 | if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 183 | { |
| 184 | puts ("nextafter+ did not underflow"); |
| 185 | ++result; |
| 186 | } |
| 187 | feclearexcept (FE_ALL_EXCEPT); |
| 188 | if (nextafter (dm, di) != 0.0) |
| 189 | { |
| 190 | puts ("nextafter+ failed"); |
| 191 | ++result; |
| 192 | } |
| 193 | if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 194 | { |
| 195 | puts ("nextafter+ did not underflow"); |
| 196 | ++result; |
| 197 | } |
| 198 | feclearexcept (FE_ALL_EXCEPT); |
| 199 | dm = nextafter (copysign (zero, -1.0), -inf); |
| 200 | if (dm > 0.0 || dm <= -DBL_MIN) |
| 201 | { |
| 202 | puts ("nextafter- failed"); |
| 203 | ++result; |
| 204 | } |
| 205 | if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 206 | { |
| 207 | puts ("nextafter- did not underflow"); |
| 208 | ++result; |
| 209 | } |
| 210 | feclearexcept (FE_ALL_EXCEPT); |
| 211 | if (nextafter (dm, -di) != 0.0) |
| 212 | { |
| 213 | puts ("nextafter- failed"); |
| 214 | ++result; |
| 215 | } |
| 216 | if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 217 | { |
| 218 | puts ("nextafter- did not underflow"); |
| 219 | ++result; |
| 220 | } |
| 221 | |
| 222 | #ifndef NO_LONG_DOUBLE |
| 223 | long double li = INFINITY; |
| 224 | long double lm = LDBL_MAX; |
| 225 | feclearexcept (FE_ALL_EXCEPT); |
| 226 | if (nextafterl (lm, li) != li) |
| 227 | { |
| 228 | puts ("nextafterl+ failed"); |
| 229 | ++result; |
| 230 | } |
| 231 | if (EXCEPTION_TESTS (long double) && fetestexcept (FE_OVERFLOW) == 0) |
| 232 | { |
| 233 | puts ("nextafterl+ did not overflow"); |
| 234 | ++result; |
| 235 | } |
| 236 | feclearexcept (FE_ALL_EXCEPT); |
| 237 | if (nextafterl (-lm, -li) != -li) |
| 238 | { |
| 239 | puts ("nextafterl failed"); |
| 240 | ++result; |
| 241 | } |
| 242 | if (EXCEPTION_TESTS (long double) && fetestexcept (FE_OVERFLOW) == 0) |
| 243 | { |
| 244 | puts ("nextafterl- did not overflow"); |
| 245 | ++result; |
| 246 | } |
| 247 | |
| 248 | li = 0; |
| 249 | lm = LDBL_MIN; |
| 250 | feclearexcept (FE_ALL_EXCEPT); |
| 251 | li = nextafterl (lm, li); |
| 252 | if (li < 0 || li >= LDBL_MIN) |
| 253 | { |
| 254 | puts ("nextafterl+ failed"); |
| 255 | ++result; |
| 256 | } |
| 257 | if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 258 | { |
| 259 | puts ("nextafterl+ did not underflow"); |
| 260 | ++result; |
| 261 | } |
| 262 | li = 0; |
| 263 | feclearexcept (FE_ALL_EXCEPT); |
| 264 | li = nextafterl (-lm, -li); |
| 265 | if (li > 0 || li <= -LDBL_MIN) |
| 266 | { |
| 267 | puts ("nextafterl- failed"); |
| 268 | ++result; |
| 269 | } |
| 270 | if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 271 | { |
| 272 | puts ("nextafterl- did not underflow"); |
| 273 | ++result; |
| 274 | } |
| 275 | li = -INFINITY; |
| 276 | feclearexcept (FE_ALL_EXCEPT); |
| 277 | lm = nextafterl (zero, inf); |
| 278 | if (lm < 0.0 || lm >= LDBL_MIN) |
| 279 | { |
| 280 | puts ("nextafterl+ failed"); |
| 281 | ++result; |
| 282 | } |
| 283 | if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 284 | { |
| 285 | puts ("nextafterl+ did not underflow"); |
| 286 | ++result; |
| 287 | } |
| 288 | feclearexcept (FE_ALL_EXCEPT); |
| 289 | if (nextafterl (lm, li) != 0.0) |
| 290 | { |
| 291 | puts ("nextafterl+ failed"); |
| 292 | ++result; |
| 293 | } |
| 294 | if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 295 | { |
| 296 | puts ("nextafterl+ did not underflow"); |
| 297 | ++result; |
| 298 | } |
| 299 | feclearexcept (FE_ALL_EXCEPT); |
| 300 | lm = nextafterl (copysign (zero, -1.0), -inf); |
| 301 | if (lm > 0.0 || lm <= -LDBL_MIN) |
| 302 | { |
| 303 | puts ("nextafterl- failed"); |
| 304 | ++result; |
| 305 | } |
| 306 | if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 307 | { |
| 308 | puts ("nextafterl- did not underflow"); |
| 309 | ++result; |
| 310 | } |
| 311 | feclearexcept (FE_ALL_EXCEPT); |
| 312 | if (nextafterl (lm, -li) != 0.0) |
| 313 | { |
| 314 | puts ("nextafterl- failed"); |
| 315 | ++result; |
| 316 | } |
| 317 | if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0) |
| 318 | { |
| 319 | puts ("nextafterl- did not underflow"); |
| 320 | ++result; |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | return result; |
| 325 | } |