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