Replace MUTEX_INITIALIZER with _LIBC_LOCK_INITIALIZER in generic code
[glibc.git] / math / test-misc.c
blob20de84e57b1fb39e0b664593fa2c0f9170c81a5d
1 /* Miscellaneous tests which don't fit anywhere else.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <fenv.h>
20 #include <float.h>
21 #include <ieee754.h>
22 #include <math.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <math-tests.h>
28 static int
29 do_test (void)
31 int result = 0;
33 #ifndef NO_LONG_DOUBLE
35 long double x = 0x100000001ll + (long double) 0.5;
36 long double q;
37 long double r;
39 r = modfl (x, &q);
40 if (q != (long double) 0x100000001ll || r != 0.5)
42 printf ("modfl (%Lg, ...) failed\n", x);
43 result = 1;
48 long double x;
49 long double m;
50 long double r;
51 int e;
52 int i;
54 # if LDBL_MANT_DIG == 64
55 m = 0xf.fffffffffffffffp-4L;
56 # elif LDBL_MANT_DIG == 106
57 /* This has to match the mantissa of LDBL_MAX which actually does have a
58 missing bit in the middle. */
59 m = 0x1.fffffffffffff7ffffffffffff8p-1L;
60 # elif LDBL_MANT_DIG == 113
61 m = 0x1.ffffffffffffffffffffffffffffp-1L;
62 # else
63 # error "Please adjust"
64 # endif
66 for (i = LDBL_MAX_EXP, x = LDBL_MAX; i >= LDBL_MIN_EXP; --i, x /= 2.0L)
68 printf ("2^%d: ", i);
70 r = frexpl (x, &e);
71 if (r != m)
73 printf ("mantissa incorrect: %.20La\n", r);
74 result = 1;
75 continue;
77 if (e != i)
79 printf ("exponent wrong %d (%.20Lg)\n", e, x);
80 result = 1;
81 continue;
83 puts ("ok");
86 for (i = LDBL_MIN_EXP, x = LDBL_MIN; i >= LDBL_MIN_EXP - LDBL_MANT_DIG + 1;
87 --i, x /= 2.0L)
89 printf ("2^%d: ", i);
91 r = frexpl (x, &e);
92 if (r != 0.5L)
94 printf ("mantissa incorrect: %.20La\n", r);
95 result = 1;
96 continue;
98 if (e != i)
100 printf ("exponent wrong %d (%.20Lg)\n", e, x);
101 result = 1;
102 continue;
104 puts ("ok");
109 # if 0
111 int e;
112 long double r = frexpl (LDBL_MIN * LDBL_EPSILON, &e);
114 if (r != 0.5)
116 printf ("frexpl (LDBL_MIN * LDBL_EPSILON, ...): mantissa wrong: %Lg\n",
118 result = 1;
120 else if (e != -16444)
122 printf ("frexpl (LDBL_MIN * LDBL_EPSILON, ...): exponent wrong: %d\n",
124 result = 1;
127 # endif
128 #endif
131 double x = 0x100000001ll + (double) 0.5;
132 double q;
133 double r;
135 r = modf (x, &q);
136 if (q != (double) 0x100000001ll || r != 0.5)
138 printf ("modf (%g, ...) failed\n", x);
139 result = 1;
144 union ieee754_float v1;
145 union ieee754_float v2;
146 float f;
148 v1.f = f = FLT_MIN;
149 if (fpclassify (f) != FP_NORMAL)
151 printf ("fpclassify (FLT_MIN) failed: %d\n", fpclassify (f));
152 result = 1;
154 f = nextafterf (f, FLT_MIN / 2.0f);
155 if (fpclassify (f) != FP_SUBNORMAL)
157 printf ("fpclassify (FLT_MIN-epsilon) failed: %d\n", fpclassify (f));
158 result = 1;
160 v2.f = f = nextafterf (f, FLT_MIN);
161 if (fpclassify (f) != FP_NORMAL)
163 printf ("fpclassify (FLT_MIN-epsilon+epsilon) failed: %d\n",
164 fpclassify (f));
165 result = 1;
168 if (v1.ieee.mantissa != v2.ieee.mantissa)
170 printf ("FLT_MIN: mantissa differs: %8x vs %8x\n",
171 v1.ieee.mantissa, v2.ieee.mantissa);
172 result = 1;
174 if (v1.ieee.exponent != v2.ieee.exponent)
176 printf ("FLT_MIN: exponent differs: %4x vs %4x\n",
177 v1.ieee.exponent, v2.ieee.exponent);
178 result = 1;
180 if (v1.ieee.negative != v2.ieee.negative)
182 printf ("FLT_MIN: negative differs: %d vs %d\n",
183 v1.ieee.negative, v2.ieee.negative);
184 result = 1;
187 v1.f = f = -FLT_MIN;
188 if (fpclassify (f) != FP_NORMAL)
190 printf ("fpclassify (-FLT_MIN) failed: %d\n", fpclassify (f));
191 result = 1;
193 f = nextafterf (f, -FLT_MIN / 2.0f);
194 if (fpclassify (f) != FP_SUBNORMAL)
196 printf ("fpclassify (-FLT_MIN-epsilon) failed: %d\n", fpclassify (f));
197 result = 1;
199 v2.f = f = nextafterf (f, -FLT_MIN);
200 if (fpclassify (f) != FP_NORMAL)
202 printf ("fpclassify (-FLT_MIN-epsilon+epsilon) failed: %d\n",
203 fpclassify (f));
204 result = 1;
207 if (v1.ieee.mantissa != v2.ieee.mantissa)
209 printf ("-FLT_MIN: mantissa differs: %8x vs %8x\n",
210 v1.ieee.mantissa, v2.ieee.mantissa);
211 result = 1;
213 if (v1.ieee.exponent != v2.ieee.exponent)
215 printf ("-FLT_MIN: exponent differs: %4x vs %4x\n",
216 v1.ieee.exponent, v2.ieee.exponent);
217 result = 1;
219 if (v1.ieee.negative != v2.ieee.negative)
221 printf ("-FLT_MIN: negative differs: %d vs %d\n",
222 v1.ieee.negative, v2.ieee.negative);
223 result = 1;
226 f = FLT_MAX;
227 if (fpclassify (f) != FP_NORMAL)
229 printf ("fpclassify (FLT_MAX) failed: %d\n", fpclassify (f));
230 result = 1;
232 f = nextafterf (f, INFINITY);
233 if (fpclassify (f) != FP_INFINITE)
235 printf ("fpclassify (FLT_MAX+epsilon) failed: %d\n", fpclassify (f));
236 result = 1;
239 f = -FLT_MAX;
240 if (fpclassify (f) != FP_NORMAL)
242 printf ("fpclassify (-FLT_MAX) failed: %d\n", fpclassify (f));
243 result = 1;
245 f = nextafterf (f, -INFINITY);
246 if (fpclassify (f) != FP_INFINITE)
248 printf ("fpclassify (-FLT_MAX-epsilon) failed: %d\n", fpclassify (f));
249 result = 1;
252 v1.f = f = 0.0625;
253 f = nextafterf (f, 0.0);
254 v2.f = f = nextafterf (f, 1.0);
256 if (v1.ieee.mantissa != v2.ieee.mantissa)
258 printf ("0.0625f down: mantissa differs: %8x vs %8x\n",
259 v1.ieee.mantissa, v2.ieee.mantissa);
260 result = 1;
262 if (v1.ieee.exponent != v2.ieee.exponent)
264 printf ("0.0625f down: exponent differs: %4x vs %4x\n",
265 v1.ieee.exponent, v2.ieee.exponent);
266 result = 1;
268 if (v1.ieee.negative != v2.ieee.negative)
270 printf ("0.0625f down: negative differs: %d vs %d\n",
271 v1.ieee.negative, v2.ieee.negative);
272 result = 1;
275 v1.f = f = 0.0625;
276 f = nextafterf (f, 1.0);
277 v2.f = f = nextafterf (f, 0.0);
279 if (v1.ieee.mantissa != v2.ieee.mantissa)
281 printf ("0.0625f up: mantissa differs: %8x vs %8x\n",
282 v1.ieee.mantissa, v2.ieee.mantissa);
283 result = 1;
285 if (v1.ieee.exponent != v2.ieee.exponent)
287 printf ("0.0625f up: exponent differs: %4x vs %4x\n",
288 v1.ieee.exponent, v2.ieee.exponent);
289 result = 1;
291 if (v1.ieee.negative != v2.ieee.negative)
293 printf ("0.0625f up: negative differs: %d vs %d\n",
294 v1.ieee.negative, v2.ieee.negative);
295 result = 1;
298 v1.f = f = -0.0625;
299 f = nextafterf (f, 0.0);
300 v2.f = f = nextafterf (f, -1.0);
302 if (v1.ieee.mantissa != v2.ieee.mantissa)
304 printf ("-0.0625f up: mantissa differs: %8x vs %8x\n",
305 v1.ieee.mantissa, v2.ieee.mantissa);
306 result = 1;
308 if (v1.ieee.exponent != v2.ieee.exponent)
310 printf ("-0.0625f up: exponent differs: %4x vs %4x\n",
311 v1.ieee.exponent, v2.ieee.exponent);
312 result = 1;
314 if (v1.ieee.negative != v2.ieee.negative)
316 printf ("-0.0625f up: negative differs: %d vs %d\n",
317 v1.ieee.negative, v2.ieee.negative);
318 result = 1;
321 v1.f = f = -0.0625;
322 f = nextafterf (f, -1.0);
323 v2.f = f = nextafterf (f, 0.0);
325 if (v1.ieee.mantissa != v2.ieee.mantissa)
327 printf ("-0.0625f down: mantissa differs: %8x vs %8x\n",
328 v1.ieee.mantissa, v2.ieee.mantissa);
329 result = 1;
331 if (v1.ieee.exponent != v2.ieee.exponent)
333 printf ("-0.0625f down: exponent differs: %4x vs %4x\n",
334 v1.ieee.exponent, v2.ieee.exponent);
335 result = 1;
337 if (v1.ieee.negative != v2.ieee.negative)
339 printf ("-0.0625f down: negative differs: %d vs %d\n",
340 v1.ieee.negative, v2.ieee.negative);
341 result = 1;
344 v1.f = f = 0.0f;
345 f = nextafterf (f, 1.0);
346 v2.f = nextafterf (f, -1.0);
348 if (v1.ieee.mantissa != v2.ieee.mantissa)
350 printf ("0.0f up: mantissa differs: %8x vs %8x\n",
351 v1.ieee.mantissa, v2.ieee.mantissa);
352 result = 1;
354 if (v1.ieee.exponent != v2.ieee.exponent)
356 printf ("0.0f up: exponent differs: %4x vs %4x\n",
357 v1.ieee.exponent, v2.ieee.exponent);
358 result = 1;
360 if (0 != v2.ieee.negative)
362 printf ("0.0f up: negative differs: 0 vs %d\n",
363 v2.ieee.negative);
364 result = 1;
367 v1.f = f = 0.0f;
368 f = nextafterf (f, -1.0);
369 v2.f = nextafterf (f, 1.0);
371 if (v1.ieee.mantissa != v2.ieee.mantissa)
373 printf ("0.0f down: mantissa differs: %8x vs %8x\n",
374 v1.ieee.mantissa, v2.ieee.mantissa);
375 result = 1;
377 if (v1.ieee.exponent != v2.ieee.exponent)
379 printf ("0.0f down: exponent differs: %4x vs %4x\n",
380 v1.ieee.exponent, v2.ieee.exponent);
381 result = 1;
383 if (1 != v2.ieee.negative)
385 printf ("0.0f down: negative differs: 1 vs %d\n",
386 v2.ieee.negative);
387 result = 1;
390 if (nextafterf (0.0f, INFINITY) != nextafterf (0.0f, 1.0f)
391 || nextafterf (-0.0f, INFINITY) != nextafterf (-0.0f, 1.0f)
392 || nextafterf (0.0f, -INFINITY) != nextafterf (0.0f, -1.0f)
393 || nextafterf (-0.0f, -INFINITY) != nextafterf (-0.0f, -1.0f))
395 printf ("nextafterf (+-0, +-Inf) != nextafterf (+-0, +-1)\n");
396 result = 1;
399 if (nexttowardf (0.0f, INFINITY) != nexttowardf (0.0f, 1.0f)
400 || nexttowardf (-0.0f, INFINITY) != nexttowardf (-0.0f, 1.0f)
401 || nexttowardf (0.0f, -INFINITY) != nexttowardf (0.0f, -1.0f)
402 || nexttowardf (-0.0f, -INFINITY) != nexttowardf (-0.0f, -1.0f))
404 printf ("nexttowardf (+-0, +-Inf) != nexttowardf (+-0, +-1)\n");
405 result = 1;
410 union ieee754_double v1;
411 union ieee754_double v2;
412 double d;
414 v1.d = d = DBL_MIN;
415 if (fpclassify (d) != FP_NORMAL)
417 printf ("fpclassify (DBL_MIN) failed: %d\n", fpclassify (d));
418 result = 1;
420 d = nextafter (d, DBL_MIN / 2.0);
421 if (fpclassify (d) != FP_SUBNORMAL)
423 printf ("fpclassify (DBL_MIN-epsilon) failed: %d\n", fpclassify (d));
424 result = 1;
426 v2.d = d = nextafter (d, DBL_MIN);
427 if (fpclassify (d) != FP_NORMAL)
429 printf ("fpclassify (DBL_MIN-epsilon+epsilon) failed: %d\n",
430 fpclassify (d));
431 result = 1;
434 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
436 printf ("DBL_MIN: mantissa0 differs: %8x vs %8x\n",
437 v1.ieee.mantissa0, v2.ieee.mantissa0);
438 result = 1;
440 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
442 printf ("DBL_MIN: mantissa1 differs: %8x vs %8x\n",
443 v1.ieee.mantissa1, v2.ieee.mantissa1);
444 result = 1;
446 if (v1.ieee.exponent != v2.ieee.exponent)
448 printf ("DBL_MIN: exponent differs: %4x vs %4x\n",
449 v1.ieee.exponent, v2.ieee.exponent);
450 result = 1;
452 if (v1.ieee.negative != v2.ieee.negative)
454 printf ("DBL_MIN: negative differs: %d vs %d\n",
455 v1.ieee.negative, v2.ieee.negative);
456 result = 1;
459 v1.d = d = -DBL_MIN;
460 if (fpclassify (d) != FP_NORMAL)
462 printf ("fpclassify (-DBL_MIN) failed: %d\n", fpclassify (d));
463 result = 1;
465 d = nextafter (d, -DBL_MIN / 2.0);
466 if (fpclassify (d) != FP_SUBNORMAL)
468 printf ("fpclassify (-DBL_MIN-epsilon) failed: %d\n", fpclassify (d));
469 result = 1;
471 v2.d = d = nextafter (d, -DBL_MIN);
472 if (fpclassify (d) != FP_NORMAL)
474 printf ("fpclassify (-DBL_MIN-epsilon+epsilon) failed: %d\n",
475 fpclassify (d));
476 result = 1;
479 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
481 printf ("-DBL_MIN: mantissa0 differs: %8x vs %8x\n",
482 v1.ieee.mantissa0, v2.ieee.mantissa0);
483 result = 1;
485 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
487 printf ("-DBL_MIN: mantissa1 differs: %8x vs %8x\n",
488 v1.ieee.mantissa1, v2.ieee.mantissa1);
489 result = 1;
491 if (v1.ieee.exponent != v2.ieee.exponent)
493 printf ("-DBL_MIN: exponent differs: %4x vs %4x\n",
494 v1.ieee.exponent, v2.ieee.exponent);
495 result = 1;
497 if (v1.ieee.negative != v2.ieee.negative)
499 printf ("-DBL_MIN: negative differs: %d vs %d\n",
500 v1.ieee.negative, v2.ieee.negative);
501 result = 1;
504 d = DBL_MAX;
505 if (fpclassify (d) != FP_NORMAL)
507 printf ("fpclassify (DBL_MAX) failed: %d\n", fpclassify (d));
508 result = 1;
510 d = nextafter (d, INFINITY);
511 if (fpclassify (d) != FP_INFINITE)
513 printf ("fpclassify (DBL_MAX+epsilon) failed: %d\n", fpclassify (d));
514 result = 1;
517 d = -DBL_MAX;
518 if (fpclassify (d) != FP_NORMAL)
520 printf ("fpclassify (-DBL_MAX) failed: %d\n", fpclassify (d));
521 result = 1;
523 d = nextafter (d, -INFINITY);
524 if (fpclassify (d) != FP_INFINITE)
526 printf ("fpclassify (-DBL_MAX-epsilon) failed: %d\n", fpclassify (d));
527 result = 1;
530 v1.d = d = 0.0625;
531 d = nextafter (d, 0.0);
532 v2.d = d = nextafter (d, 1.0);
534 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
536 printf ("0.0625 down: mantissa0 differs: %8x vs %8x\n",
537 v1.ieee.mantissa0, v2.ieee.mantissa0);
538 result = 1;
540 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
542 printf ("0.0625 down: mantissa1 differs: %8x vs %8x\n",
543 v1.ieee.mantissa1, v2.ieee.mantissa1);
544 result = 1;
546 if (v1.ieee.exponent != v2.ieee.exponent)
548 printf ("0.0625 down: exponent differs: %4x vs %4x\n",
549 v1.ieee.exponent, v2.ieee.exponent);
550 result = 1;
552 if (v1.ieee.negative != v2.ieee.negative)
554 printf ("0.0625 down: negative differs: %d vs %d\n",
555 v1.ieee.negative, v2.ieee.negative);
556 result = 1;
559 v1.d = d = 0.0625;
560 d = nextafter (d, 1.0);
561 v2.d = d = nextafter (d, 0.0);
563 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
565 printf ("0.0625 up: mantissa0 differs: %8x vs %8x\n",
566 v1.ieee.mantissa0, v2.ieee.mantissa0);
567 result = 1;
569 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
571 printf ("0.0625 up: mantissa1 differs: %8x vs %8x\n",
572 v1.ieee.mantissa1, v2.ieee.mantissa1);
573 result = 1;
575 if (v1.ieee.exponent != v2.ieee.exponent)
577 printf ("0.0625 up: exponent differs: %4x vs %4x\n",
578 v1.ieee.exponent, v2.ieee.exponent);
579 result = 1;
581 if (v1.ieee.negative != v2.ieee.negative)
583 printf ("0.0625 up: negative differs: %d vs %d\n",
584 v1.ieee.negative, v2.ieee.negative);
585 result = 1;
588 v1.d = d = -0.0625;
589 d = nextafter (d, 0.0);
590 v2.d = d = nextafter (d, -1.0);
592 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
594 printf ("-0.0625 up: mantissa0 differs: %8x vs %8x\n",
595 v1.ieee.mantissa0, v2.ieee.mantissa0);
596 result = 1;
598 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
600 printf ("-0.0625 up: mantissa1 differs: %8x vs %8x\n",
601 v1.ieee.mantissa1, v2.ieee.mantissa1);
602 result = 1;
604 if (v1.ieee.exponent != v2.ieee.exponent)
606 printf ("-0.0625 up: exponent differs: %4x vs %4x\n",
607 v1.ieee.exponent, v2.ieee.exponent);
608 result = 1;
610 if (v1.ieee.negative != v2.ieee.negative)
612 printf ("-0.0625 up: negative differs: %d vs %d\n",
613 v1.ieee.negative, v2.ieee.negative);
614 result = 1;
617 v1.d = d = -0.0625;
618 d = nextafter (d, -1.0);
619 v2.d = d = nextafter (d, 0.0);
621 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
623 printf ("-0.0625 down: mantissa0 differs: %8x vs %8x\n",
624 v1.ieee.mantissa0, v2.ieee.mantissa0);
625 result = 1;
627 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
629 printf ("-0.0625 down: mantissa1 differs: %8x vs %8x\n",
630 v1.ieee.mantissa1, v2.ieee.mantissa1);
631 result = 1;
633 if (v1.ieee.exponent != v2.ieee.exponent)
635 printf ("-0.0625 down: exponent differs: %4x vs %4x\n",
636 v1.ieee.exponent, v2.ieee.exponent);
637 result = 1;
639 if (v1.ieee.negative != v2.ieee.negative)
641 printf ("-0.0625 down: negative differs: %d vs %d\n",
642 v1.ieee.negative, v2.ieee.negative);
643 result = 1;
646 v1.d = d = 0.0;
647 d = nextafter (d, 1.0);
648 v2.d = nextafter (d, -1.0);
650 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
652 printf ("0.0 up: mantissa0 differs: %8x vs %8x\n",
653 v1.ieee.mantissa0, v2.ieee.mantissa0);
654 result = 1;
656 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
658 printf ("0.0 up: mantissa1 differs: %8x vs %8x\n",
659 v1.ieee.mantissa1, v2.ieee.mantissa1);
660 result = 1;
662 if (v1.ieee.exponent != v2.ieee.exponent)
664 printf ("0.0 up: exponent differs: %4x vs %4x\n",
665 v1.ieee.exponent, v2.ieee.exponent);
666 result = 1;
668 if (0 != v2.ieee.negative)
670 printf ("0.0 up: negative differs: 0 vs %d\n",
671 v2.ieee.negative);
672 result = 1;
675 v1.d = d = 0.0;
676 d = nextafter (d, -1.0);
677 v2.d = nextafter (d, 1.0);
679 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
681 printf ("0.0 down: mantissa0 differs: %8x vs %8x\n",
682 v1.ieee.mantissa0, v2.ieee.mantissa0);
683 result = 1;
685 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
687 printf ("0.0 down: mantissa1 differs: %8x vs %8x\n",
688 v1.ieee.mantissa1, v2.ieee.mantissa1);
689 result = 1;
691 if (v1.ieee.exponent != v2.ieee.exponent)
693 printf ("0.0 down: exponent differs: %4x vs %4x\n",
694 v1.ieee.exponent, v2.ieee.exponent);
695 result = 1;
697 if (1 != v2.ieee.negative)
699 printf ("0.0 down: negative differs: 1 vs %d\n",
700 v2.ieee.negative);
701 result = 1;
704 if (nextafter (0.0, INFINITY) != nextafter (0.0, 1.0)
705 || nextafter (-0.0, INFINITY) != nextafter (-0.0, 1.0)
706 || nextafter (0.0, -INFINITY) != nextafter (0.0, -1.0)
707 || nextafter (-0.0, -INFINITY) != nextafter (-0.0, -1.0))
709 printf ("nextafter (+-0, +-Inf) != nextafter (+-0, +-1)\n");
710 result = 1;
713 if (nexttoward (0.0, INFINITY) != nexttoward (0.0, 1.0)
714 || nexttoward (-0.0, INFINITY) != nexttoward (-0.0, 1.0)
715 || nexttoward (0.0, -INFINITY) != nexttoward (0.0, -1.0)
716 || nexttoward (-0.0, -INFINITY) != nexttoward (-0.0, -1.0))
718 printf ("nexttoward (+-0, +-Inf) != nexttoward (+-0, +-1)\n");
719 result = 1;
723 #ifndef NO_LONG_DOUBLE
725 long double v1, v2;
727 v1 = LDBL_MIN;
728 if (fpclassify (v1) != FP_NORMAL)
730 printf ("fpclassify (LDBL_MIN) failed: %d (%La)\n",
731 fpclassify (v1), v1);
732 result = 1;
734 v2 = nextafterl (v1, LDBL_MIN / 2.0);
735 if (fpclassify (v2) != FP_SUBNORMAL)
737 printf ("fpclassify (LDBL_MIN-epsilon) failed: %d (%La)\n",
738 fpclassify (v2), v2);
739 result = 1;
741 v2 = nextafterl (v2, LDBL_MIN);
742 if (fpclassify (v2) != FP_NORMAL)
744 printf ("fpclassify (LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n",
745 fpclassify (v2), v2);
746 result = 1;
749 if (v1 != v2)
751 printf ("LDBL_MIN-epsilon+epsilon != LDBL_MIN: %La vs %La\n", v2, v1);
752 result = 1;
755 v1 = -LDBL_MIN;
756 if (fpclassify (v1) != FP_NORMAL)
758 printf ("fpclassify (-LDBL_MIN) failed: %d (%La)\n",
759 fpclassify (v1), v1);
760 result = 1;
762 v2 = nextafterl (v1, -LDBL_MIN / 2.0);
763 if (fpclassify (v2) != FP_SUBNORMAL)
765 printf ("fpclassify (-LDBL_MIN-epsilon) failed: %d (%La)\n",
766 fpclassify (v2), v2);
767 result = 1;
769 v2 = nextafterl (v2, -LDBL_MIN);
770 if (fpclassify (v2) != FP_NORMAL)
772 printf ("fpclassify (-LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n",
773 fpclassify (v2), v2);
774 result = 1;
777 if (v1 != v2)
779 printf ("-LDBL_MIN-epsilon+epsilon != -LDBL_MIN: %La vs %La\n", v2, v1);
780 result = 1;
783 v1 = LDBL_MAX;
784 if (fpclassify (v1) != FP_NORMAL)
786 printf ("fpclassify (LDBL_MAX) failed: %d (%La)\n",
787 fpclassify (v1), v1);
788 result = 1;
790 v2 = nextafterl (v1, INFINITY);
791 if (fpclassify (v2) != FP_INFINITE)
793 printf ("fpclassify (LDBL_MAX+epsilon) failed: %d (%La)\n",
794 fpclassify (v2), v2);
795 result = 1;
798 v1 = -LDBL_MAX;
799 if (fpclassify (v1) != FP_NORMAL)
801 printf ("fpclassify (-LDBL_MAX) failed: %d (%La)\n",
802 fpclassify (v1), v1);
803 result = 1;
805 v2 = nextafterl (v1, -INFINITY);
806 if (fpclassify (v2) != FP_INFINITE)
808 printf ("fpclassify (-LDBL_MAX-epsilon) failed: %d (%La)\n",
809 fpclassify (v2), v2);
810 result = 1;
813 v1 = 0.0625;
814 v2 = nextafterl (v1, 0.0);
815 v2 = nextafterl (v2, 1.0);
817 if (v1 != v2)
819 printf ("0.0625L-epsilon+epsilon != 0.0625L: %La vs %La\n", v2, v1);
820 result = 1;
823 v1 = 0.0625;
824 v2 = nextafterl (v1, 1.0);
825 v2 = nextafterl (v2, 0.0);
827 if (v1 != v2)
829 printf ("0.0625L+epsilon-epsilon != 0.0625L: %La vs %La\n", v2, v1);
830 result = 1;
833 v1 = -0.0625;
834 v2 = nextafterl (v1, 0.0);
835 v2 = nextafterl (v2, -1.0);
837 if (v1 != v2)
839 printf ("-0.0625L+epsilon-epsilon != -0.0625L: %La vs %La\n", v2, v1);
840 result = 1;
843 v1 = -0.0625;
844 v2 = nextafterl (v1, -1.0);
845 v2 = nextafterl (v2, 0.0);
847 if (v1 != v2)
849 printf ("-0.0625L-epsilon+epsilon != -0.0625L: %La vs %La\n", v2, v1);
850 result = 1;
853 v1 = 0.0;
854 v2 = nextafterl (v1, 1.0);
855 v2 = nextafterl (v2, -1.0);
857 if (v1 != v2)
859 printf ("0.0+epsilon-epsilon != 0.0L: %La vs %La\n", v2, v1);
860 result = 1;
862 if (signbit (v2))
864 printf ("0.0+epsilon-epsilon is negative\n");
865 result = 1;
868 v1 = 0.0;
869 v2 = nextafterl (v1, -1.0);
870 v2 = nextafterl (v2, 1.0);
872 if (v1 != v2)
874 printf ("0.0-epsilon+epsilon != 0.0L: %La vs %La\n", v2, v1);
875 result = 1;
877 if (!signbit (v2))
879 printf ("0.0-epsilon+epsilon is positive\n");
880 result = 1;
883 if (nextafterl (0.0, INFINITY) != nextafterl (0.0, 1.0)
884 || nextafterl (-0.0, INFINITY) != nextafterl (-0.0, 1.0)
885 || nextafterl (0.0, -INFINITY) != nextafterl (0.0, -1.0)
886 || nextafterl (-0.0, -INFINITY) != nextafterl (-0.0, -1.0))
888 printf ("nextafterl (+-0, +-Inf) != nextafterl (+-0, +-1)\n");
889 result = 1;
892 if (nexttowardl (0.0L, INFINITY) != nexttowardl (0.0L, 1.0L)
893 || nexttowardl (-0.0L, INFINITY) != nexttowardl (-0.0L, 1.0L)
894 || nexttowardl (0.0L, -INFINITY) != nexttowardl (0.0L, -1.0L)
895 || nexttowardl (-0.0L, -INFINITY) != nexttowardl (-0.0L, -1.0L))
897 printf ("nexttowardl (+-0, +-Inf) != nexttowardl (+-0, +-1)\n");
898 result = 1;
901 #endif
903 if (! isnormal (FLT_MIN))
905 puts ("isnormal (FLT_MIN) failed");
906 result = 1;
908 if (! isnormal (DBL_MIN))
910 puts ("isnormal (DBL_MIN) failed");
911 result = 1;
913 #ifndef NO_LONG_DOUBLE
914 if (! isnormal (LDBL_MIN))
916 puts ("isnormal (LDBL_MIN) failed");
917 result = 1;
919 #endif
921 #if defined (__i386__) || defined (__x86_64__)
922 /* This is a test for the strange long doubles in x86 FPUs. */
924 union
926 char b[10];
927 long double d;
928 } u =
929 { .b = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0 } };
931 if (fpclassify (u.d) != FP_NORMAL)
933 printf ("fpclassify (0x00008000000000000000) failed: %d (%Lg)\n",
934 fpclassify (u.d), u.d);
935 result = 1;
939 /* Special qNaNs in x86 long double. Test for scalbl. */
941 union
943 char b[10];
944 long double d;
945 } u =
946 { .b = { 0, 1, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f } };
947 long double r;
949 r = scalbl (u.d, 0.0);
950 if (!isnan (r))
952 puts ("scalbl (qNaN, 0) does not return NaN");
953 result = 1;
955 else if (memcmp (&r, &u.d, sizeof (double)) != 0)
957 puts ("scalbl (qNaN, 0) does not return the same NaN");
958 result = 1;
961 #endif
963 #ifndef NO_LONG_DOUBLE
965 long double r;
967 feclearexcept (FE_ALL_EXCEPT);
968 r = scalbl (LDBL_MIN, 2147483647);
969 if (! isinf (r))
971 puts ("scalbl (LDBL_MIN, 2147483647) does not return Inf");
972 result = 1;
974 else if (signbit (r) != 0)
976 puts ("scalbl (LDBL_MIN, 2147483647) returns -Inf");
977 result = 1;
979 else if (fetestexcept (FE_UNDERFLOW))
981 puts ("scalbl (LDBL_MIN, 2147483647) raises underflow exception");
982 result = 1;
985 feclearexcept (FE_ALL_EXCEPT);
986 r = scalbl (LDBL_MAX, -2147483647);
987 if (r != 0.0)
989 puts ("scalbl (LDBL_MAX, -2147483647) does not return 0");
990 result = 1;
992 else if (signbit (r) != 0)
994 puts ("scalbl (LDBL_MAX, -2147483647) returns -Inf");
995 result = 1;
997 else if (fetestexcept (FE_OVERFLOW))
999 puts ("scalbl (LDBL_MAX, -2147483647) raises overflow exception");
1000 result = 1;
1003 #endif
1005 /* The tests here are very similar to tests earlier in this file,
1006 the important difference is just that there are no intervening
1007 union variables that cause some GCC versions to hide possible
1008 bugs in nextafter* implementation. */
1009 if (nextafterf (nextafterf (FLT_MIN, FLT_MIN / 2.0), FLT_MIN) != FLT_MIN)
1011 puts ("nextafterf FLT_MIN test failed");
1012 result = 1;
1014 if (nextafterf (nextafterf (-FLT_MIN, -FLT_MIN / 2.0), -FLT_MIN)
1015 != -FLT_MIN)
1017 puts ("nextafterf -FLT_MIN test failed");
1018 result = 1;
1020 if (nextafter (nextafter (DBL_MIN, DBL_MIN / 2.0), DBL_MIN) != DBL_MIN)
1022 puts ("nextafter DBL_MIN test failed");
1023 result = 1;
1025 if (nextafter (nextafter (-DBL_MIN, -DBL_MIN / 2.0), -DBL_MIN) != -DBL_MIN)
1027 puts ("nextafter -DBL_MIN test failed");
1028 result = 1;
1030 #ifndef NO_LONG_DOUBLE
1031 if (nextafterl (nextafterl (LDBL_MIN, LDBL_MIN / 2.0), LDBL_MIN)
1032 != LDBL_MIN)
1034 puts ("nextafterl LDBL_MIN test failed");
1035 result = 1;
1037 if (nextafterl (nextafterl (-LDBL_MIN, -LDBL_MIN / 2.0), -LDBL_MIN)
1038 != -LDBL_MIN)
1040 puts ("nextafterl -LDBL_MIN test failed");
1041 result = 1;
1043 #endif
1045 volatile float f1 = FLT_MAX;
1046 volatile float f2 = FLT_MAX / 2;
1047 (void) &f1;
1048 (void) &f2;
1049 feclearexcept (FE_ALL_EXCEPT);
1050 f2 += f1;
1051 #if defined(FE_OVERFLOW) && defined(FE_INEXACT)
1052 int fe = fetestexcept (FE_ALL_EXCEPT);
1053 if (EXCEPTION_TESTS (float) && fe != (FE_OVERFLOW | FE_INEXACT))
1055 printf ("float overflow test failed: %x\n", fe);
1056 result = 1;
1058 #endif
1060 volatile double d1 = DBL_MAX;
1061 volatile double d2 = DBL_MAX / 2;
1062 (void) &d1;
1063 (void) &d2;
1064 feclearexcept (FE_ALL_EXCEPT);
1065 d2 += d1;
1066 #if defined(FE_OVERFLOW) && defined(FE_INEXACT)
1067 fe = fetestexcept (FE_ALL_EXCEPT);
1068 if (EXCEPTION_TESTS (double) && fe != (FE_OVERFLOW | FE_INEXACT))
1070 printf ("double overflow test failed: %x\n", fe);
1071 result = 1;
1073 #endif
1075 #ifndef NO_LONG_DOUBLE
1076 volatile long double ld1 = LDBL_MAX;
1077 volatile long double ld2 = LDBL_MAX / 2;
1078 (void) &ld1;
1079 (void) &ld2;
1080 feclearexcept (FE_ALL_EXCEPT);
1081 ld2 += ld1;
1082 # if defined(FE_OVERFLOW) && defined(FE_INEXACT)
1083 fe = fetestexcept (FE_ALL_EXCEPT);
1084 if (EXCEPTION_TESTS (long double) && fe != (FE_OVERFLOW | FE_INEXACT))
1086 printf ("long double overflow test failed: %x\n", fe);
1087 result = 1;
1089 # endif
1090 #endif
1092 #if !defined NO_LONG_DOUBLE && LDBL_MANT_DIG == 113
1093 volatile long double ld3 = 0x1.0000000000010000000100000001p+1;
1094 volatile long double ld4 = 0x1.0000000000000000000000000001p+1;
1095 (void) &ld3;
1096 (void) &ld4;
1097 ld3 -= ld4;
1098 if (ld3 != 0x1.0p-47)
1100 printf ("long double subtraction test failed %.28La\n", ld3);
1101 result = 1;
1103 #endif
1105 /* Skip testing IBM long double format, for 2 reasons:
1106 1) it only supports FE_TONEAREST
1107 2) nextafter (0.0, 1.0) == nextafterl (0.0L, 1.0L), so
1108 nextafter (0.0, 1.0) / 16.0L will be 0.0L. */
1109 #if !defined NO_LONG_DOUBLE && LDBL_MANT_DIG >= DBL_MANT_DIG + 4 \
1110 && LDBL_MANT_DIG != 106
1111 int oldmode = fegetround ();
1112 int j;
1113 for (j = 0; j < 4; j++)
1115 int mode;
1116 int i;
1117 int k = 0;
1118 const char *mstr;
1119 switch (j)
1121 #ifdef FE_TONEAREST
1122 case 0:
1123 mode = FE_TONEAREST;
1124 mstr = "nearest";
1125 k = 8;
1126 break;
1127 #endif
1128 #ifdef FE_DOWNWARD
1129 case 1:
1130 mode = FE_DOWNWARD;
1131 mstr = "-inf";
1132 break;
1133 #endif
1134 #ifdef FE_UPWARD
1135 case 2:
1136 mode = FE_UPWARD;
1137 mstr = "+inf";
1138 k = 15;
1139 break;
1140 #endif
1141 #ifdef FE_TOWARDZERO
1142 case 3:
1143 mode = FE_TOWARDZERO;
1144 mstr = "0";
1145 break;
1146 #endif
1147 default:
1148 continue;
1151 volatile long double ld5 = nextafter (0.0, 1.0) / 16.0L;
1152 volatile double d5;
1153 (void) &ld5;
1154 for (i = 0; i <= 32; i++)
1156 if (fesetround (mode))
1158 printf ("failed to set rounding mode to %s\n", mstr);
1159 if (ROUNDING_TESTS (long double, mode)
1160 && ROUNDING_TESTS (double, mode))
1161 result = 1;
1162 else
1163 puts ("ignoring this failure");
1164 break;
1166 d5 = ld5 * i;
1167 (void) &d5;
1168 fesetround (oldmode);
1169 if (d5 != ((j == 0 && i == 8) ? 0 : (i + k) / 16)
1170 * nextafter (0.0, 1.0))
1172 printf ("%La incorrectly rounded to %s as %a\n",
1173 ld5 * i, mstr, d5);
1174 if (ROUNDING_TESTS (long double, mode)
1175 && ROUNDING_TESTS (double, mode))
1176 result = 1;
1177 else
1178 puts ("ignoring this failure");
1183 volatile long double ld7 = nextafterl (0.0L, 1.0L);
1184 volatile double d7;
1185 (void) &ld7;
1186 fesetround (FE_UPWARD);
1187 d7 = ld7;
1188 (void) &d7;
1189 fesetround (oldmode);
1191 if (d7 != nextafter (0.0, 1.0))
1193 printf ("%La incorrectly rounded upward to %a\n", ld7, d7);
1194 if (ROUNDING_TESTS (long double, FE_UPWARD)
1195 && ROUNDING_TESTS (double, FE_UPWARD))
1196 result = 1;
1197 else
1198 puts ("ignoring this failure");
1200 #endif
1202 return result;
1205 #define TEST_FUNCTION do_test ()
1206 #include "../test-skeleton.c"