ARM: Make armv6t2 memchr implementation usable without Thumb.
[glibc.git] / math / test-misc.c
blobd64e8e2fcca9cc815f4fa14aa88674a5dbfcb910
1 /* Miscellaneous tests which don't fit anywhere else.
2 Copyright (C) 2000-2013 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>
27 int
28 main (void)
30 int result = 0;
32 #ifndef NO_LONG_DOUBLE
34 long double x = 0x100000001ll + (long double) 0.5;
35 long double q;
36 long double r;
38 r = modfl (x, &q);
39 if (q != (long double) 0x100000001ll || r != 0.5)
41 printf ("modfl (%Lg, ...) failed\n", x);
42 result = 1;
47 long double x;
48 long double m;
49 long double r;
50 int e;
51 int i;
53 # if LDBL_MANT_DIG == 64
54 m = 0xf.fffffffffffffffp-4L;
55 # elif LDBL_MANT_DIG == 106
56 /* This has to match the mantissa of LDBL_MAX which actually does have a
57 missing bit in the middle. */
58 m = 0x1.fffffffffffff7ffffffffffff8p-1L;
59 # elif LDBL_MANT_DIG == 113
60 m = 0x1.ffffffffffffffffffffffffffffp-1L;
61 # else
62 # error "Please adjust"
63 # endif
65 for (i = LDBL_MAX_EXP, x = LDBL_MAX; i >= LDBL_MIN_EXP; --i, x /= 2.0L)
67 printf ("2^%d: ", i);
69 r = frexpl (x, &e);
70 if (r != m)
72 printf ("mantissa incorrect: %.20La\n", r);
73 result = 1;
74 continue;
76 if (e != i)
78 printf ("exponent wrong %d (%.20Lg)\n", e, x);
79 result = 1;
80 continue;
82 puts ("ok");
85 for (i = LDBL_MIN_EXP, x = LDBL_MIN; i >= LDBL_MIN_EXP - LDBL_MANT_DIG + 1;
86 --i, x /= 2.0L)
88 printf ("2^%d: ", i);
90 r = frexpl (x, &e);
91 if (r != 0.5L)
93 printf ("mantissa incorrect: %.20La\n", r);
94 result = 1;
95 continue;
97 if (e != i)
99 printf ("exponent wrong %d (%.20Lg)\n", e, x);
100 result = 1;
101 continue;
103 puts ("ok");
108 # if 0
110 int e;
111 long double r = frexpl (LDBL_MIN * LDBL_EPSILON, &e);
113 if (r != 0.5)
115 printf ("frexpl (LDBL_MIN * LDBL_EPSILON, ...): mantissa wrong: %Lg\n",
117 result = 1;
119 else if (e != -16444)
121 printf ("frexpl (LDBL_MIN * LDBL_EPSILON, ...): exponent wrong: %d\n",
123 result = 1;
126 # endif
127 #endif
130 double x = 0x100000001ll + (double) 0.5;
131 double q;
132 double r;
134 r = modf (x, &q);
135 if (q != (double) 0x100000001ll || r != 0.5)
137 printf ("modf (%g, ...) failed\n", x);
138 result = 1;
143 union ieee754_float v1;
144 union ieee754_float v2;
145 float f;
147 v1.f = f = FLT_MIN;
148 if (fpclassify (f) != FP_NORMAL)
150 printf ("fpclassify (FLT_MIN) failed: %d\n", fpclassify (f));
151 result = 1;
153 f = nextafterf (f, FLT_MIN / 2.0f);
154 if (fpclassify (f) != FP_SUBNORMAL)
156 printf ("fpclassify (FLT_MIN-epsilon) failed: %d\n", fpclassify (f));
157 result = 1;
159 v2.f = f = nextafterf (f, FLT_MIN);
160 if (fpclassify (f) != FP_NORMAL)
162 printf ("fpclassify (FLT_MIN-epsilon+epsilon) failed: %d\n",
163 fpclassify (f));
164 result = 1;
167 if (v1.ieee.mantissa != v2.ieee.mantissa)
169 printf ("FLT_MIN: mantissa differs: %8x vs %8x\n",
170 v1.ieee.mantissa, v2.ieee.mantissa);
171 result = 1;
173 if (v1.ieee.exponent != v2.ieee.exponent)
175 printf ("FLT_MIN: exponent differs: %4x vs %4x\n",
176 v1.ieee.exponent, v2.ieee.exponent);
177 result = 1;
179 if (v1.ieee.negative != v2.ieee.negative)
181 printf ("FLT_MIN: negative differs: %d vs %d\n",
182 v1.ieee.negative, v2.ieee.negative);
183 result = 1;
186 v1.f = f = -FLT_MIN;
187 if (fpclassify (f) != FP_NORMAL)
189 printf ("fpclassify (-FLT_MIN) failed: %d\n", fpclassify (f));
190 result = 1;
192 f = nextafterf (f, -FLT_MIN / 2.0f);
193 if (fpclassify (f) != FP_SUBNORMAL)
195 printf ("fpclassify (-FLT_MIN-epsilon) failed: %d\n", fpclassify (f));
196 result = 1;
198 v2.f = f = nextafterf (f, -FLT_MIN);
199 if (fpclassify (f) != FP_NORMAL)
201 printf ("fpclassify (-FLT_MIN-epsilon+epsilon) failed: %d\n",
202 fpclassify (f));
203 result = 1;
206 if (v1.ieee.mantissa != v2.ieee.mantissa)
208 printf ("-FLT_MIN: mantissa differs: %8x vs %8x\n",
209 v1.ieee.mantissa, v2.ieee.mantissa);
210 result = 1;
212 if (v1.ieee.exponent != v2.ieee.exponent)
214 printf ("-FLT_MIN: exponent differs: %4x vs %4x\n",
215 v1.ieee.exponent, v2.ieee.exponent);
216 result = 1;
218 if (v1.ieee.negative != v2.ieee.negative)
220 printf ("-FLT_MIN: negative differs: %d vs %d\n",
221 v1.ieee.negative, v2.ieee.negative);
222 result = 1;
225 f = FLT_MAX;
226 if (fpclassify (f) != FP_NORMAL)
228 printf ("fpclassify (FLT_MAX) failed: %d\n", fpclassify (f));
229 result = 1;
231 f = nextafterf (f, INFINITY);
232 if (fpclassify (f) != FP_INFINITE)
234 printf ("fpclassify (FLT_MAX+epsilon) failed: %d\n", fpclassify (f));
235 result = 1;
238 f = -FLT_MAX;
239 if (fpclassify (f) != FP_NORMAL)
241 printf ("fpclassify (-FLT_MAX) failed: %d\n", fpclassify (f));
242 result = 1;
244 f = nextafterf (f, -INFINITY);
245 if (fpclassify (f) != FP_INFINITE)
247 printf ("fpclassify (-FLT_MAX-epsilon) failed: %d\n", fpclassify (f));
248 result = 1;
251 v1.f = f = 0.0625;
252 f = nextafterf (f, 0.0);
253 v2.f = f = nextafterf (f, 1.0);
255 if (v1.ieee.mantissa != v2.ieee.mantissa)
257 printf ("0.0625f down: mantissa differs: %8x vs %8x\n",
258 v1.ieee.mantissa, v2.ieee.mantissa);
259 result = 1;
261 if (v1.ieee.exponent != v2.ieee.exponent)
263 printf ("0.0625f down: exponent differs: %4x vs %4x\n",
264 v1.ieee.exponent, v2.ieee.exponent);
265 result = 1;
267 if (v1.ieee.negative != v2.ieee.negative)
269 printf ("0.0625f down: negative differs: %d vs %d\n",
270 v1.ieee.negative, v2.ieee.negative);
271 result = 1;
274 v1.f = f = 0.0625;
275 f = nextafterf (f, 1.0);
276 v2.f = f = nextafterf (f, 0.0);
278 if (v1.ieee.mantissa != v2.ieee.mantissa)
280 printf ("0.0625f up: mantissa differs: %8x vs %8x\n",
281 v1.ieee.mantissa, v2.ieee.mantissa);
282 result = 1;
284 if (v1.ieee.exponent != v2.ieee.exponent)
286 printf ("0.0625f up: exponent differs: %4x vs %4x\n",
287 v1.ieee.exponent, v2.ieee.exponent);
288 result = 1;
290 if (v1.ieee.negative != v2.ieee.negative)
292 printf ("0.0625f up: negative differs: %d vs %d\n",
293 v1.ieee.negative, v2.ieee.negative);
294 result = 1;
297 v1.f = f = -0.0625;
298 f = nextafterf (f, 0.0);
299 v2.f = f = nextafterf (f, -1.0);
301 if (v1.ieee.mantissa != v2.ieee.mantissa)
303 printf ("-0.0625f up: mantissa differs: %8x vs %8x\n",
304 v1.ieee.mantissa, v2.ieee.mantissa);
305 result = 1;
307 if (v1.ieee.exponent != v2.ieee.exponent)
309 printf ("-0.0625f up: exponent differs: %4x vs %4x\n",
310 v1.ieee.exponent, v2.ieee.exponent);
311 result = 1;
313 if (v1.ieee.negative != v2.ieee.negative)
315 printf ("-0.0625f up: negative differs: %d vs %d\n",
316 v1.ieee.negative, v2.ieee.negative);
317 result = 1;
320 v1.f = f = -0.0625;
321 f = nextafterf (f, -1.0);
322 v2.f = f = nextafterf (f, 0.0);
324 if (v1.ieee.mantissa != v2.ieee.mantissa)
326 printf ("-0.0625f down: mantissa differs: %8x vs %8x\n",
327 v1.ieee.mantissa, v2.ieee.mantissa);
328 result = 1;
330 if (v1.ieee.exponent != v2.ieee.exponent)
332 printf ("-0.0625f down: exponent differs: %4x vs %4x\n",
333 v1.ieee.exponent, v2.ieee.exponent);
334 result = 1;
336 if (v1.ieee.negative != v2.ieee.negative)
338 printf ("-0.0625f down: negative differs: %d vs %d\n",
339 v1.ieee.negative, v2.ieee.negative);
340 result = 1;
343 v1.f = f = 0.0f;
344 f = nextafterf (f, 1.0);
345 v2.f = nextafterf (f, -1.0);
347 if (v1.ieee.mantissa != v2.ieee.mantissa)
349 printf ("0.0f up: mantissa differs: %8x vs %8x\n",
350 v1.ieee.mantissa, v2.ieee.mantissa);
351 result = 1;
353 if (v1.ieee.exponent != v2.ieee.exponent)
355 printf ("0.0f up: exponent differs: %4x vs %4x\n",
356 v1.ieee.exponent, v2.ieee.exponent);
357 result = 1;
359 if (0 != v2.ieee.negative)
361 printf ("0.0f up: negative differs: 0 vs %d\n",
362 v2.ieee.negative);
363 result = 1;
366 v1.f = f = 0.0f;
367 f = nextafterf (f, -1.0);
368 v2.f = nextafterf (f, 1.0);
370 if (v1.ieee.mantissa != v2.ieee.mantissa)
372 printf ("0.0f down: mantissa differs: %8x vs %8x\n",
373 v1.ieee.mantissa, v2.ieee.mantissa);
374 result = 1;
376 if (v1.ieee.exponent != v2.ieee.exponent)
378 printf ("0.0f down: exponent differs: %4x vs %4x\n",
379 v1.ieee.exponent, v2.ieee.exponent);
380 result = 1;
382 if (1 != v2.ieee.negative)
384 printf ("0.0f down: negative differs: 1 vs %d\n",
385 v2.ieee.negative);
386 result = 1;
389 if (nextafterf (0.0f, INFINITY) != nextafterf (0.0f, 1.0f)
390 || 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))
394 printf ("nextafterf (+-0, +-Inf) != nextafterf (+-0, +-1)\n");
395 result = 1;
398 if (nexttowardf (0.0f, INFINITY) != nexttowardf (0.0f, 1.0f)
399 || 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))
403 printf ("nexttowardf (+-0, +-Inf) != nexttowardf (+-0, +-1)\n");
404 result = 1;
409 union ieee754_double v1;
410 union ieee754_double v2;
411 double d;
413 v1.d = d = DBL_MIN;
414 if (fpclassify (d) != FP_NORMAL)
416 printf ("fpclassify (DBL_MIN) failed: %d\n", fpclassify (d));
417 result = 1;
419 d = nextafter (d, DBL_MIN / 2.0);
420 if (fpclassify (d) != FP_SUBNORMAL)
422 printf ("fpclassify (DBL_MIN-epsilon) failed: %d\n", fpclassify (d));
423 result = 1;
425 v2.d = d = nextafter (d, DBL_MIN);
426 if (fpclassify (d) != FP_NORMAL)
428 printf ("fpclassify (DBL_MIN-epsilon+epsilon) failed: %d\n",
429 fpclassify (d));
430 result = 1;
433 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
435 printf ("DBL_MIN: mantissa0 differs: %8x vs %8x\n",
436 v1.ieee.mantissa0, v2.ieee.mantissa0);
437 result = 1;
439 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
441 printf ("DBL_MIN: mantissa1 differs: %8x vs %8x\n",
442 v1.ieee.mantissa1, v2.ieee.mantissa1);
443 result = 1;
445 if (v1.ieee.exponent != v2.ieee.exponent)
447 printf ("DBL_MIN: exponent differs: %4x vs %4x\n",
448 v1.ieee.exponent, v2.ieee.exponent);
449 result = 1;
451 if (v1.ieee.negative != v2.ieee.negative)
453 printf ("DBL_MIN: negative differs: %d vs %d\n",
454 v1.ieee.negative, v2.ieee.negative);
455 result = 1;
458 v1.d = d = -DBL_MIN;
459 if (fpclassify (d) != FP_NORMAL)
461 printf ("fpclassify (-DBL_MIN) failed: %d\n", fpclassify (d));
462 result = 1;
464 d = nextafter (d, -DBL_MIN / 2.0);
465 if (fpclassify (d) != FP_SUBNORMAL)
467 printf ("fpclassify (-DBL_MIN-epsilon) failed: %d\n", fpclassify (d));
468 result = 1;
470 v2.d = d = nextafter (d, -DBL_MIN);
471 if (fpclassify (d) != FP_NORMAL)
473 printf ("fpclassify (-DBL_MIN-epsilon+epsilon) failed: %d\n",
474 fpclassify (d));
475 result = 1;
478 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
480 printf ("-DBL_MIN: mantissa0 differs: %8x vs %8x\n",
481 v1.ieee.mantissa0, v2.ieee.mantissa0);
482 result = 1;
484 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
486 printf ("-DBL_MIN: mantissa1 differs: %8x vs %8x\n",
487 v1.ieee.mantissa1, v2.ieee.mantissa1);
488 result = 1;
490 if (v1.ieee.exponent != v2.ieee.exponent)
492 printf ("-DBL_MIN: exponent differs: %4x vs %4x\n",
493 v1.ieee.exponent, v2.ieee.exponent);
494 result = 1;
496 if (v1.ieee.negative != v2.ieee.negative)
498 printf ("-DBL_MIN: negative differs: %d vs %d\n",
499 v1.ieee.negative, v2.ieee.negative);
500 result = 1;
503 d = DBL_MAX;
504 if (fpclassify (d) != FP_NORMAL)
506 printf ("fpclassify (DBL_MAX) failed: %d\n", fpclassify (d));
507 result = 1;
509 d = nextafter (d, INFINITY);
510 if (fpclassify (d) != FP_INFINITE)
512 printf ("fpclassify (DBL_MAX+epsilon) failed: %d\n", fpclassify (d));
513 result = 1;
516 d = -DBL_MAX;
517 if (fpclassify (d) != FP_NORMAL)
519 printf ("fpclassify (-DBL_MAX) failed: %d\n", fpclassify (d));
520 result = 1;
522 d = nextafter (d, -INFINITY);
523 if (fpclassify (d) != FP_INFINITE)
525 printf ("fpclassify (-DBL_MAX-epsilon) failed: %d\n", fpclassify (d));
526 result = 1;
529 v1.d = d = 0.0625;
530 d = nextafter (d, 0.0);
531 v2.d = d = nextafter (d, 1.0);
533 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
535 printf ("0.0625 down: mantissa0 differs: %8x vs %8x\n",
536 v1.ieee.mantissa0, v2.ieee.mantissa0);
537 result = 1;
539 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
541 printf ("0.0625 down: mantissa1 differs: %8x vs %8x\n",
542 v1.ieee.mantissa1, v2.ieee.mantissa1);
543 result = 1;
545 if (v1.ieee.exponent != v2.ieee.exponent)
547 printf ("0.0625 down: exponent differs: %4x vs %4x\n",
548 v1.ieee.exponent, v2.ieee.exponent);
549 result = 1;
551 if (v1.ieee.negative != v2.ieee.negative)
553 printf ("0.0625 down: negative differs: %d vs %d\n",
554 v1.ieee.negative, v2.ieee.negative);
555 result = 1;
558 v1.d = d = 0.0625;
559 d = nextafter (d, 1.0);
560 v2.d = d = nextafter (d, 0.0);
562 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
564 printf ("0.0625 up: mantissa0 differs: %8x vs %8x\n",
565 v1.ieee.mantissa0, v2.ieee.mantissa0);
566 result = 1;
568 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
570 printf ("0.0625 up: mantissa1 differs: %8x vs %8x\n",
571 v1.ieee.mantissa1, v2.ieee.mantissa1);
572 result = 1;
574 if (v1.ieee.exponent != v2.ieee.exponent)
576 printf ("0.0625 up: exponent differs: %4x vs %4x\n",
577 v1.ieee.exponent, v2.ieee.exponent);
578 result = 1;
580 if (v1.ieee.negative != v2.ieee.negative)
582 printf ("0.0625 up: negative differs: %d vs %d\n",
583 v1.ieee.negative, v2.ieee.negative);
584 result = 1;
587 v1.d = d = -0.0625;
588 d = nextafter (d, 0.0);
589 v2.d = d = nextafter (d, -1.0);
591 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
593 printf ("-0.0625 up: mantissa0 differs: %8x vs %8x\n",
594 v1.ieee.mantissa0, v2.ieee.mantissa0);
595 result = 1;
597 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
599 printf ("-0.0625 up: mantissa1 differs: %8x vs %8x\n",
600 v1.ieee.mantissa1, v2.ieee.mantissa1);
601 result = 1;
603 if (v1.ieee.exponent != v2.ieee.exponent)
605 printf ("-0.0625 up: exponent differs: %4x vs %4x\n",
606 v1.ieee.exponent, v2.ieee.exponent);
607 result = 1;
609 if (v1.ieee.negative != v2.ieee.negative)
611 printf ("-0.0625 up: negative differs: %d vs %d\n",
612 v1.ieee.negative, v2.ieee.negative);
613 result = 1;
616 v1.d = d = -0.0625;
617 d = nextafter (d, -1.0);
618 v2.d = d = nextafter (d, 0.0);
620 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
622 printf ("-0.0625 down: mantissa0 differs: %8x vs %8x\n",
623 v1.ieee.mantissa0, v2.ieee.mantissa0);
624 result = 1;
626 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
628 printf ("-0.0625 down: mantissa1 differs: %8x vs %8x\n",
629 v1.ieee.mantissa1, v2.ieee.mantissa1);
630 result = 1;
632 if (v1.ieee.exponent != v2.ieee.exponent)
634 printf ("-0.0625 down: exponent differs: %4x vs %4x\n",
635 v1.ieee.exponent, v2.ieee.exponent);
636 result = 1;
638 if (v1.ieee.negative != v2.ieee.negative)
640 printf ("-0.0625 down: negative differs: %d vs %d\n",
641 v1.ieee.negative, v2.ieee.negative);
642 result = 1;
645 v1.d = d = 0.0;
646 d = nextafter (d, 1.0);
647 v2.d = nextafter (d, -1.0);
649 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
651 printf ("0.0 up: mantissa0 differs: %8x vs %8x\n",
652 v1.ieee.mantissa0, v2.ieee.mantissa0);
653 result = 1;
655 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
657 printf ("0.0 up: mantissa1 differs: %8x vs %8x\n",
658 v1.ieee.mantissa1, v2.ieee.mantissa1);
659 result = 1;
661 if (v1.ieee.exponent != v2.ieee.exponent)
663 printf ("0.0 up: exponent differs: %4x vs %4x\n",
664 v1.ieee.exponent, v2.ieee.exponent);
665 result = 1;
667 if (0 != v2.ieee.negative)
669 printf ("0.0 up: negative differs: 0 vs %d\n",
670 v2.ieee.negative);
671 result = 1;
674 v1.d = d = 0.0;
675 d = nextafter (d, -1.0);
676 v2.d = nextafter (d, 1.0);
678 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
680 printf ("0.0 down: mantissa0 differs: %8x vs %8x\n",
681 v1.ieee.mantissa0, v2.ieee.mantissa0);
682 result = 1;
684 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
686 printf ("0.0 down: mantissa1 differs: %8x vs %8x\n",
687 v1.ieee.mantissa1, v2.ieee.mantissa1);
688 result = 1;
690 if (v1.ieee.exponent != v2.ieee.exponent)
692 printf ("0.0 down: exponent differs: %4x vs %4x\n",
693 v1.ieee.exponent, v2.ieee.exponent);
694 result = 1;
696 if (1 != v2.ieee.negative)
698 printf ("0.0 down: negative differs: 1 vs %d\n",
699 v2.ieee.negative);
700 result = 1;
703 if (nextafter (0.0, INFINITY) != nextafter (0.0, 1.0)
704 || 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))
708 printf ("nextafter (+-0, +-Inf) != nextafter (+-0, +-1)\n");
709 result = 1;
712 if (nexttoward (0.0, INFINITY) != nexttoward (0.0, 1.0)
713 || 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))
717 printf ("nexttoward (+-0, +-Inf) != nexttoward (+-0, +-1)\n");
718 result = 1;
722 #ifndef NO_LONG_DOUBLE
724 union ieee854_long_double v1;
725 union ieee854_long_double v2;
726 long double ld;
728 v1.d = ld = LDBL_MIN;
729 if (fpclassify (ld) != FP_NORMAL)
731 printf ("fpclassify (LDBL_MIN) failed: %d\n", fpclassify (ld));
732 result = 1;
734 ld = nextafterl (ld, LDBL_MIN / 2.0);
735 if (fpclassify (ld) != FP_SUBNORMAL)
737 printf ("fpclassify (LDBL_MIN-epsilon) failed: %d (%La)\n",
738 fpclassify (ld), ld);
739 result = 1;
741 v2.d = ld = nextafterl (ld, LDBL_MIN);
742 if (fpclassify (ld) != FP_NORMAL)
744 printf ("fpclassify (LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n",
745 fpclassify (ld), ld);
746 result = 1;
749 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
751 printf ("LDBL_MIN: mantissa0 differs: %8x vs %8x\n",
752 v1.ieee.mantissa0, v2.ieee.mantissa0);
753 result = 1;
755 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
757 printf ("LDBL_MIN: mantissa1 differs: %8x vs %8x\n",
758 v1.ieee.mantissa1, v2.ieee.mantissa1);
759 result = 1;
761 if (v1.ieee.exponent != v2.ieee.exponent)
763 printf ("LDBL_MIN: exponent differs: %4x vs %4x\n",
764 v1.ieee.exponent, v2.ieee.exponent);
765 result = 1;
767 if (v1.ieee.negative != v2.ieee.negative)
769 printf ("LDBL_MIN: negative differs: %d vs %d\n",
770 v1.ieee.negative, v2.ieee.negative);
771 result = 1;
774 v1.d = ld = -LDBL_MIN;
775 if (fpclassify (ld) != FP_NORMAL)
777 printf ("fpclassify (-LDBL_MIN) failed: %d\n", fpclassify (ld));
778 result = 1;
780 ld = nextafterl (ld, -LDBL_MIN / 2.0);
781 if (fpclassify (ld) != FP_SUBNORMAL)
783 printf ("fpclassify (-LDBL_MIN-epsilon) failed: %d (%La)\n",
784 fpclassify (ld), ld);
785 result = 1;
787 v2.d = ld = nextafterl (ld, -LDBL_MIN);
788 if (fpclassify (ld) != FP_NORMAL)
790 printf ("fpclassify (-LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n",
791 fpclassify (ld), ld);
792 result = 1;
795 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
797 printf ("-LDBL_MIN: mantissa0 differs: %8x vs %8x\n",
798 v1.ieee.mantissa0, v2.ieee.mantissa0);
799 result = 1;
801 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
803 printf ("-LDBL_MIN: mantissa1 differs: %8x vs %8x\n",
804 v1.ieee.mantissa1, v2.ieee.mantissa1);
805 result = 1;
807 if (v1.ieee.exponent != v2.ieee.exponent)
809 printf ("-LDBL_MIN: exponent differs: %4x vs %4x\n",
810 v1.ieee.exponent, v2.ieee.exponent);
811 result = 1;
813 if (v1.ieee.negative != v2.ieee.negative)
815 printf ("-LDBL_MIN: negative differs: %d vs %d\n",
816 v1.ieee.negative, v2.ieee.negative);
817 result = 1;
820 ld = LDBL_MAX;
821 if (fpclassify (ld) != FP_NORMAL)
823 printf ("fpclassify (LDBL_MAX) failed: %d\n", fpclassify (ld));
824 result = 1;
826 ld = nextafterl (ld, INFINITY);
827 if (fpclassify (ld) != FP_INFINITE)
829 printf ("fpclassify (LDBL_MAX+epsilon) failed: %d\n", fpclassify (ld));
830 result = 1;
833 ld = -LDBL_MAX;
834 if (fpclassify (ld) != FP_NORMAL)
836 printf ("fpclassify (-LDBL_MAX) failed: %d\n", fpclassify (ld));
837 result = 1;
839 ld = nextafterl (ld, -INFINITY);
840 if (fpclassify (ld) != FP_INFINITE)
842 printf ("fpclassify (-LDBL_MAX-epsilon) failed: %d\n",
843 fpclassify (ld));
844 result = 1;
847 v1.d = ld = 0.0625;
848 ld = nextafterl (ld, 0.0);
849 v2.d = ld = nextafterl (ld, 1.0);
851 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
853 printf ("0.0625L down: mantissa0 differs: %8x vs %8x\n",
854 v1.ieee.mantissa0, v2.ieee.mantissa0);
855 result = 1;
857 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
859 printf ("0.0625L down: mantissa1 differs: %8x vs %8x\n",
860 v1.ieee.mantissa1, v2.ieee.mantissa1);
861 result = 1;
863 if (v1.ieee.exponent != v2.ieee.exponent)
865 printf ("0.0625L down: exponent differs: %4x vs %4x\n",
866 v1.ieee.exponent, v2.ieee.exponent);
867 result = 1;
869 if (v1.ieee.negative != v2.ieee.negative)
871 printf ("0.0625L down: negative differs: %d vs %d\n",
872 v1.ieee.negative, v2.ieee.negative);
873 result = 1;
876 v1.d = ld = 0.0625;
877 ld = nextafterl (ld, 1.0);
878 v2.d = ld = nextafterl (ld, 0.0);
880 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
882 printf ("0.0625L up: mantissa0 differs: %8x vs %8x\n",
883 v1.ieee.mantissa0, v2.ieee.mantissa0);
884 result = 1;
886 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
888 printf ("0.0625L up: mantissa1 differs: %8x vs %8x\n",
889 v1.ieee.mantissa1, v2.ieee.mantissa1);
890 result = 1;
892 if (v1.ieee.exponent != v2.ieee.exponent)
894 printf ("0.0625L up: exponent differs: %4x vs %4x\n",
895 v1.ieee.exponent, v2.ieee.exponent);
896 result = 1;
898 if (v1.ieee.negative != v2.ieee.negative)
900 printf ("0.0625L up: negative differs: %d vs %d\n",
901 v1.ieee.negative, v2.ieee.negative);
902 result = 1;
905 v1.d = ld = -0.0625;
906 ld = nextafterl (ld, 0.0);
907 v2.d = ld = nextafterl (ld, -1.0);
909 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
911 printf ("-0.0625L up: mantissa0 differs: %8x vs %8x\n",
912 v1.ieee.mantissa0, v2.ieee.mantissa0);
913 result = 1;
915 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
917 printf ("-0.0625L up: mantissa1 differs: %8x vs %8x\n",
918 v1.ieee.mantissa1, v2.ieee.mantissa1);
919 result = 1;
921 if (v1.ieee.exponent != v2.ieee.exponent)
923 printf ("-0.0625L up: exponent differs: %4x vs %4x\n",
924 v1.ieee.exponent, v2.ieee.exponent);
925 result = 1;
927 if (v1.ieee.negative != v2.ieee.negative)
929 printf ("-0.0625L up: negative differs: %d vs %d\n",
930 v1.ieee.negative, v2.ieee.negative);
931 result = 1;
934 v1.d = ld = -0.0625;
935 ld = nextafterl (ld, -1.0);
936 v2.d = ld = nextafterl (ld, 0.0);
938 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
940 printf ("-0.0625L down: mantissa0 differs: %8x vs %8x\n",
941 v1.ieee.mantissa0, v2.ieee.mantissa0);
942 result = 1;
944 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
946 printf ("-0.0625L down: mantissa1 differs: %8x vs %8x\n",
947 v1.ieee.mantissa1, v2.ieee.mantissa1);
948 result = 1;
950 if (v1.ieee.exponent != v2.ieee.exponent)
952 printf ("-0.0625L down: exponent differs: %4x vs %4x\n",
953 v1.ieee.exponent, v2.ieee.exponent);
954 result = 1;
956 if (v1.ieee.negative != v2.ieee.negative)
958 printf ("-0.0625L down: negative differs: %d vs %d\n",
959 v1.ieee.negative, v2.ieee.negative);
960 result = 1;
963 v1.d = ld = 0.0;
964 ld = nextafterl (ld, 1.0);
965 v2.d = nextafterl (ld, -1.0);
967 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
969 printf ("0.0L up: mantissa0 differs: %8x vs %8x\n",
970 v1.ieee.mantissa0, v2.ieee.mantissa0);
971 result = 1;
973 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
975 printf ("0.0L up: mantissa1 differs: %8x vs %8x\n",
976 v1.ieee.mantissa1, v2.ieee.mantissa1);
977 result = 1;
979 if (v1.ieee.exponent != v2.ieee.exponent)
981 printf ("0.0L up: exponent differs: %4x vs %4x\n",
982 v1.ieee.exponent, v2.ieee.exponent);
983 result = 1;
985 if (0 != v2.ieee.negative)
987 printf ("0.0L up: negative differs: 0 vs %d\n",
988 v2.ieee.negative);
989 result = 1;
992 v1.d = ld = 0.0;
993 ld = nextafterl (ld, -1.0);
994 v2.d = nextafterl (ld, 1.0);
996 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
998 printf ("0.0L down: mantissa0 differs: %8x vs %8x\n",
999 v1.ieee.mantissa0, v2.ieee.mantissa0);
1000 result = 1;
1002 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
1004 printf ("0.0L down: mantissa1 differs: %8x vs %8x\n",
1005 v1.ieee.mantissa1, v2.ieee.mantissa1);
1006 result = 1;
1008 if (v1.ieee.exponent != v2.ieee.exponent)
1010 printf ("0.0L down: exponent differs: %4x vs %4x\n",
1011 v1.ieee.exponent, v2.ieee.exponent);
1012 result = 1;
1014 if (1 != v2.ieee.negative)
1016 printf ("0.0L down: negative differs: 1 vs %d\n",
1017 v2.ieee.negative);
1018 result = 1;
1021 if (nextafterl (0.0, INFINITY) != nextafterl (0.0, 1.0)
1022 || nextafterl (-0.0, INFINITY) != nextafterl (-0.0, 1.0)
1023 || nextafterl (0.0, -INFINITY) != nextafterl (0.0, -1.0)
1024 || nextafterl (-0.0, -INFINITY) != nextafterl (-0.0, -1.0))
1026 printf ("nextafterl (+-0, +-Inf) != nextafterl (+-0, +-1)\n");
1027 result = 1;
1030 if (nexttowardl (0.0L, INFINITY) != nexttowardl (0.0L, 1.0L)
1031 || nexttowardl (-0.0L, INFINITY) != nexttowardl (-0.0L, 1.0L)
1032 || nexttowardl (0.0L, -INFINITY) != nexttowardl (0.0L, -1.0L)
1033 || nexttowardl (-0.0L, -INFINITY) != nexttowardl (-0.0L, -1.0L))
1035 printf ("nexttowardl (+-0, +-Inf) != nexttowardl (+-0, +-1)\n");
1036 result = 1;
1039 #endif
1041 if (! isnormal (FLT_MIN))
1043 puts ("isnormal (FLT_MIN) failed");
1044 result = 1;
1046 if (! isnormal (DBL_MIN))
1048 puts ("isnormal (DBL_MIN) failed");
1049 result = 1;
1051 #ifndef NO_LONG_DOUBLE
1052 if (! isnormal (LDBL_MIN))
1054 puts ("isnormal (LDBL_MIN) failed");
1055 result = 1;
1057 #endif
1059 #ifdef __i386__
1060 /* This is a test for the strange long doubles in x86 FPUs. */
1062 union
1064 char b[10];
1065 long double d;
1066 } u =
1067 { .b = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0 } };
1069 if (fpclassify (u.d) != FP_NORMAL)
1071 printf ("fpclassify (0x00008000000000000000) failed: %d (%Lg)\n",
1072 fpclassify (u.d), u.d);
1073 result = 1;
1077 /* Special NaNs in x86 long double. Test for scalbl. */
1079 union
1081 char b[10];
1082 long double d;
1083 } u =
1084 { .b = { 0, 1, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f } };
1085 long double r;
1087 r = scalbl (u.d, 0.0);
1088 if (!isnan (r))
1090 puts ("scalbl(NaN, 0) does not return NaN");
1091 result = 1;
1093 else if (memcmp (&r, &u.d, sizeof (double)) != 0)
1095 puts ("scalbl(NaN, 0) does not return the same NaN");
1096 result = 1;
1099 #endif
1101 #ifndef NO_LONG_DOUBLE
1103 long double r;
1105 feclearexcept (FE_ALL_EXCEPT);
1106 r = scalbl (LDBL_MIN, 2147483647);
1107 if (! isinf (r))
1109 puts ("scalbl (LDBL_MIN, 2147483647) does not return Inf");
1110 result = 1;
1112 else if (signbit (r) != 0)
1114 puts ("scalbl (LDBL_MIN, 2147483647) returns -Inf");
1115 result = 1;
1117 else if (fetestexcept (FE_UNDERFLOW))
1119 puts ("scalbl(NaN, 0) raises underflow exception");
1120 result = 1;
1123 feclearexcept (FE_ALL_EXCEPT);
1124 r = scalbl (LDBL_MAX, -2147483647);
1125 if (r != 0.0)
1127 puts ("scalbl (LDBL_MAX, -2147483647) does not return 0");
1128 result = 1;
1130 else if (signbit (r) != 0)
1132 puts ("scalbl (LDBL_MAX, -2147483647) returns -Inf");
1133 result = 1;
1135 else if (fetestexcept (FE_OVERFLOW))
1137 puts ("scalbl(NaN, 0) raises overflow exception");
1138 result = 1;
1141 #endif
1143 /* The tests here are very similar to tests earlier in this file,
1144 the important difference is just that there are no intervening
1145 union variables that cause some GCC versions to hide possible
1146 bugs in nextafter* implementation. */
1147 if (nextafterf (nextafterf (FLT_MIN, FLT_MIN / 2.0), FLT_MIN) != FLT_MIN)
1149 puts ("nextafterf FLT_MIN test failed");
1150 result = 1;
1152 if (nextafterf (nextafterf (-FLT_MIN, -FLT_MIN / 2.0), -FLT_MIN)
1153 != -FLT_MIN)
1155 puts ("nextafterf -FLT_MIN test failed");
1156 result = 1;
1158 if (nextafter (nextafter (DBL_MIN, DBL_MIN / 2.0), DBL_MIN) != DBL_MIN)
1160 puts ("nextafter DBL_MIN test failed");
1161 result = 1;
1163 if (nextafter (nextafter (-DBL_MIN, -DBL_MIN / 2.0), -DBL_MIN) != -DBL_MIN)
1165 puts ("nextafter -DBL_MIN test failed");
1166 result = 1;
1168 #ifndef NO_LONG_DOUBLE
1169 if (nextafterl (nextafterl (LDBL_MIN, LDBL_MIN / 2.0), LDBL_MIN)
1170 != LDBL_MIN)
1172 puts ("nextafterl LDBL_MIN test failed");
1173 result = 1;
1175 if (nextafterl (nextafterl (-LDBL_MIN, -LDBL_MIN / 2.0), -LDBL_MIN)
1176 != -LDBL_MIN)
1178 puts ("nextafterl -LDBL_MIN test failed");
1179 result = 1;
1181 #endif
1183 volatile float f1 = FLT_MAX;
1184 volatile float f2 = FLT_MAX / 2;
1185 (void) &f1;
1186 (void) &f2;
1187 feclearexcept (FE_ALL_EXCEPT);
1188 f2 += f1;
1189 #if defined(FE_OVERFLOW) && defined(FE_INEXACT)
1190 int fe = fetestexcept (FE_ALL_EXCEPT);
1191 if (fe != (FE_OVERFLOW | FE_INEXACT))
1193 printf ("float overflow test failed: %x\n", fe);
1194 result = 1;
1196 #endif
1198 volatile double d1 = DBL_MAX;
1199 volatile double d2 = DBL_MAX / 2;
1200 (void) &d1;
1201 (void) &d2;
1202 feclearexcept (FE_ALL_EXCEPT);
1203 d2 += d1;
1204 #if defined(FE_OVERFLOW) && defined(FE_INEXACT)
1205 fe = fetestexcept (FE_ALL_EXCEPT);
1206 if (fe != (FE_OVERFLOW | FE_INEXACT))
1208 printf ("double overflow test failed: %x\n", fe);
1209 result = 1;
1211 #endif
1213 #ifndef NO_LONG_DOUBLE
1214 volatile long double ld1 = LDBL_MAX;
1215 volatile long double ld2 = LDBL_MAX / 2;
1216 (void) &ld1;
1217 (void) &ld2;
1218 feclearexcept (FE_ALL_EXCEPT);
1219 ld2 += ld1;
1220 # if defined(FE_OVERFLOW) && defined(FE_INEXACT)
1221 fe = fetestexcept (FE_ALL_EXCEPT);
1222 if (fe != (FE_OVERFLOW | FE_INEXACT))
1224 printf ("long double overflow test failed: %x\n", fe);
1225 result = 1;
1227 # endif
1228 #endif
1230 #if !defined NO_LONG_DOUBLE && LDBL_MANT_DIG == 113
1231 volatile long double ld3 = 0x1.0000000000010000000100000001p+1;
1232 volatile long double ld4 = 0x1.0000000000000000000000000001p+1;
1233 (void) &ld3;
1234 (void) &ld4;
1235 ld3 -= ld4;
1236 if (ld3 != 0x1.0p-47)
1238 printf ("long double subtraction test failed %.28La\n", ld3);
1239 result = 1;
1241 #endif
1243 /* Skip testing IBM long double format, for 2 reasons:
1244 1) it only supports FE_TONEAREST
1245 2) nextafter (0.0, 1.0) == nextafterl (0.0L, 1.0L), so
1246 nextafter (0.0, 1.0) / 16.0L will be 0.0L. */
1247 #if !defined NO_LONG_DOUBLE && LDBL_MANT_DIG >= DBL_MANT_DIG + 4 \
1248 && LDBL_MANT_DIG != 106
1249 int oldmode = fegetround ();
1250 int j;
1251 for (j = 0; j < 4; j++)
1253 int mode;
1254 int i;
1255 int k = 0;
1256 const char *mstr;
1257 switch (j)
1259 #ifdef FE_TONEAREST
1260 case 0:
1261 mode = FE_TONEAREST;
1262 mstr = "nearest";
1263 k = 8;
1264 break;
1265 #endif
1266 #ifdef FE_DOWNWARD
1267 case 1:
1268 mode = FE_DOWNWARD;
1269 mstr = "-inf";
1270 break;
1271 #endif
1272 #ifdef FE_UPWARD
1273 case 2:
1274 mode = FE_UPWARD;
1275 mstr = "+inf";
1276 k = 15;
1277 break;
1278 #endif
1279 #ifdef FE_TOWARDZERO
1280 case 3:
1281 mode = FE_TOWARDZERO;
1282 mstr = "0";
1283 break;
1284 #endif
1285 default:
1286 continue;
1289 volatile long double ld5 = nextafter (0.0, 1.0) / 16.0L;
1290 volatile double d5;
1291 (void) &ld5;
1292 for (i = 0; i <= 32; i++)
1294 if (fesetround (mode))
1296 printf ("failed to set rounding mode to %s\n", mstr);
1297 result = 1;
1298 break;
1300 d5 = ld5 * i;
1301 (void) &d5;
1302 fesetround (oldmode);
1303 if (d5 != ((j == 0 && i == 8) ? 0 : (i + k) / 16)
1304 * nextafter (0.0, 1.0))
1306 printf ("%La incorrectly rounded to %s as %a\n",
1307 ld5 * i, mstr, d5);
1308 result = 1;
1313 volatile long double ld7 = nextafterl (0.0L, 1.0L);
1314 volatile double d7;
1315 (void) &ld7;
1316 fesetround (FE_UPWARD);
1317 d7 = ld7;
1318 (void) &d7;
1319 fesetround (oldmode);
1321 if (d7 != nextafter (0.0, 1.0))
1323 printf ("%La incorrectly rounded upward to %a\n", ld7, d7);
1324 result = 1;
1326 #endif
1328 return result;