Handle invalid buffer pointers when count is zero.
[glibc/history.git] / math / test-misc.c
blob14fe38b90a6dc9d5f48a2dba80254a0ba6938c70
1 /* Miscellaneous tests which don't fit anywhere else.
2 Copyright (C) 2000, 2001, 2004, 2005, 2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <fenv.h>
21 #include <float.h>
22 #include <ieee754.h>
23 #include <math.h>
24 #include <stdio.h>
25 #include <string.h>
28 int
29 main (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 union ieee854_long_double v1;
726 union ieee854_long_double v2;
727 long double ld;
729 v1.d = ld = LDBL_MIN;
730 if (fpclassify (ld) != FP_NORMAL)
732 printf ("fpclassify (LDBL_MIN) failed: %d\n", fpclassify (ld));
733 result = 1;
735 ld = nextafterl (ld, LDBL_MIN / 2.0);
736 if (fpclassify (ld) != FP_SUBNORMAL)
738 printf ("fpclassify (LDBL_MIN-epsilon) failed: %d (%La)\n",
739 fpclassify (ld), ld);
740 result = 1;
742 v2.d = ld = nextafterl (ld, LDBL_MIN);
743 if (fpclassify (ld) != FP_NORMAL)
745 printf ("fpclassify (LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n",
746 fpclassify (ld), ld);
747 result = 1;
750 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
752 printf ("LDBL_MIN: mantissa0 differs: %8x vs %8x\n",
753 v1.ieee.mantissa0, v2.ieee.mantissa0);
754 result = 1;
756 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
758 printf ("LDBL_MIN: mantissa1 differs: %8x vs %8x\n",
759 v1.ieee.mantissa1, v2.ieee.mantissa1);
760 result = 1;
762 if (v1.ieee.exponent != v2.ieee.exponent)
764 printf ("LDBL_MIN: exponent differs: %4x vs %4x\n",
765 v1.ieee.exponent, v2.ieee.exponent);
766 result = 1;
768 if (v1.ieee.negative != v2.ieee.negative)
770 printf ("LDBL_MIN: negative differs: %d vs %d\n",
771 v1.ieee.negative, v2.ieee.negative);
772 result = 1;
775 v1.d = ld = -LDBL_MIN;
776 if (fpclassify (ld) != FP_NORMAL)
778 printf ("fpclassify (-LDBL_MIN) failed: %d\n", fpclassify (ld));
779 result = 1;
781 ld = nextafterl (ld, -LDBL_MIN / 2.0);
782 if (fpclassify (ld) != FP_SUBNORMAL)
784 printf ("fpclassify (-LDBL_MIN-epsilon) failed: %d (%La)\n",
785 fpclassify (ld), ld);
786 result = 1;
788 v2.d = ld = nextafterl (ld, -LDBL_MIN);
789 if (fpclassify (ld) != FP_NORMAL)
791 printf ("fpclassify (-LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n",
792 fpclassify (ld), ld);
793 result = 1;
796 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
798 printf ("-LDBL_MIN: mantissa0 differs: %8x vs %8x\n",
799 v1.ieee.mantissa0, v2.ieee.mantissa0);
800 result = 1;
802 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
804 printf ("-LDBL_MIN: mantissa1 differs: %8x vs %8x\n",
805 v1.ieee.mantissa1, v2.ieee.mantissa1);
806 result = 1;
808 if (v1.ieee.exponent != v2.ieee.exponent)
810 printf ("-LDBL_MIN: exponent differs: %4x vs %4x\n",
811 v1.ieee.exponent, v2.ieee.exponent);
812 result = 1;
814 if (v1.ieee.negative != v2.ieee.negative)
816 printf ("-LDBL_MIN: negative differs: %d vs %d\n",
817 v1.ieee.negative, v2.ieee.negative);
818 result = 1;
821 ld = LDBL_MAX;
822 if (fpclassify (ld) != FP_NORMAL)
824 printf ("fpclassify (LDBL_MAX) failed: %d\n", fpclassify (ld));
825 result = 1;
827 ld = nextafterl (ld, INFINITY);
828 if (fpclassify (ld) != FP_INFINITE)
830 printf ("fpclassify (LDBL_MAX+epsilon) failed: %d\n", fpclassify (ld));
831 result = 1;
834 ld = -LDBL_MAX;
835 if (fpclassify (ld) != FP_NORMAL)
837 printf ("fpclassify (-LDBL_MAX) failed: %d\n", fpclassify (ld));
838 result = 1;
840 ld = nextafterl (ld, -INFINITY);
841 if (fpclassify (ld) != FP_INFINITE)
843 printf ("fpclassify (-LDBL_MAX-epsilon) failed: %d\n",
844 fpclassify (ld));
845 result = 1;
848 v1.d = ld = 0.0625;
849 ld = nextafterl (ld, 0.0);
850 v2.d = ld = nextafterl (ld, 1.0);
852 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
854 printf ("0.0625L down: mantissa0 differs: %8x vs %8x\n",
855 v1.ieee.mantissa0, v2.ieee.mantissa0);
856 result = 1;
858 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
860 printf ("0.0625L down: mantissa1 differs: %8x vs %8x\n",
861 v1.ieee.mantissa1, v2.ieee.mantissa1);
862 result = 1;
864 if (v1.ieee.exponent != v2.ieee.exponent)
866 printf ("0.0625L down: exponent differs: %4x vs %4x\n",
867 v1.ieee.exponent, v2.ieee.exponent);
868 result = 1;
870 if (v1.ieee.negative != v2.ieee.negative)
872 printf ("0.0625L down: negative differs: %d vs %d\n",
873 v1.ieee.negative, v2.ieee.negative);
874 result = 1;
877 v1.d = ld = 0.0625;
878 ld = nextafterl (ld, 1.0);
879 v2.d = ld = nextafterl (ld, 0.0);
881 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
883 printf ("0.0625L up: mantissa0 differs: %8x vs %8x\n",
884 v1.ieee.mantissa0, v2.ieee.mantissa0);
885 result = 1;
887 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
889 printf ("0.0625L up: mantissa1 differs: %8x vs %8x\n",
890 v1.ieee.mantissa1, v2.ieee.mantissa1);
891 result = 1;
893 if (v1.ieee.exponent != v2.ieee.exponent)
895 printf ("0.0625L up: exponent differs: %4x vs %4x\n",
896 v1.ieee.exponent, v2.ieee.exponent);
897 result = 1;
899 if (v1.ieee.negative != v2.ieee.negative)
901 printf ("0.0625L up: negative differs: %d vs %d\n",
902 v1.ieee.negative, v2.ieee.negative);
903 result = 1;
906 v1.d = ld = -0.0625;
907 ld = nextafterl (ld, 0.0);
908 v2.d = ld = nextafterl (ld, -1.0);
910 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
912 printf ("-0.0625L up: mantissa0 differs: %8x vs %8x\n",
913 v1.ieee.mantissa0, v2.ieee.mantissa0);
914 result = 1;
916 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
918 printf ("-0.0625L up: mantissa1 differs: %8x vs %8x\n",
919 v1.ieee.mantissa1, v2.ieee.mantissa1);
920 result = 1;
922 if (v1.ieee.exponent != v2.ieee.exponent)
924 printf ("-0.0625L up: exponent differs: %4x vs %4x\n",
925 v1.ieee.exponent, v2.ieee.exponent);
926 result = 1;
928 if (v1.ieee.negative != v2.ieee.negative)
930 printf ("-0.0625L up: negative differs: %d vs %d\n",
931 v1.ieee.negative, v2.ieee.negative);
932 result = 1;
935 v1.d = ld = -0.0625;
936 ld = nextafterl (ld, -1.0);
937 v2.d = ld = nextafterl (ld, 0.0);
939 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
941 printf ("-0.0625L down: mantissa0 differs: %8x vs %8x\n",
942 v1.ieee.mantissa0, v2.ieee.mantissa0);
943 result = 1;
945 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
947 printf ("-0.0625L down: mantissa1 differs: %8x vs %8x\n",
948 v1.ieee.mantissa1, v2.ieee.mantissa1);
949 result = 1;
951 if (v1.ieee.exponent != v2.ieee.exponent)
953 printf ("-0.0625L down: exponent differs: %4x vs %4x\n",
954 v1.ieee.exponent, v2.ieee.exponent);
955 result = 1;
957 if (v1.ieee.negative != v2.ieee.negative)
959 printf ("-0.0625L down: negative differs: %d vs %d\n",
960 v1.ieee.negative, v2.ieee.negative);
961 result = 1;
964 v1.d = ld = 0.0;
965 ld = nextafterl (ld, 1.0);
966 v2.d = nextafterl (ld, -1.0);
968 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
970 printf ("0.0L up: mantissa0 differs: %8x vs %8x\n",
971 v1.ieee.mantissa0, v2.ieee.mantissa0);
972 result = 1;
974 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
976 printf ("0.0L up: mantissa1 differs: %8x vs %8x\n",
977 v1.ieee.mantissa1, v2.ieee.mantissa1);
978 result = 1;
980 if (v1.ieee.exponent != v2.ieee.exponent)
982 printf ("0.0L up: exponent differs: %4x vs %4x\n",
983 v1.ieee.exponent, v2.ieee.exponent);
984 result = 1;
986 if (0 != v2.ieee.negative)
988 printf ("0.0L up: negative differs: 0 vs %d\n",
989 v2.ieee.negative);
990 result = 1;
993 v1.d = ld = 0.0;
994 ld = nextafterl (ld, -1.0);
995 v2.d = nextafterl (ld, 1.0);
997 if (v1.ieee.mantissa0 != v2.ieee.mantissa0)
999 printf ("0.0L down: mantissa0 differs: %8x vs %8x\n",
1000 v1.ieee.mantissa0, v2.ieee.mantissa0);
1001 result = 1;
1003 if (v1.ieee.mantissa1 != v2.ieee.mantissa1)
1005 printf ("0.0L down: mantissa1 differs: %8x vs %8x\n",
1006 v1.ieee.mantissa1, v2.ieee.mantissa1);
1007 result = 1;
1009 if (v1.ieee.exponent != v2.ieee.exponent)
1011 printf ("0.0L down: exponent differs: %4x vs %4x\n",
1012 v1.ieee.exponent, v2.ieee.exponent);
1013 result = 1;
1015 if (1 != v2.ieee.negative)
1017 printf ("0.0L down: negative differs: 1 vs %d\n",
1018 v2.ieee.negative);
1019 result = 1;
1022 if (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)
1025 || nextafterl (-0.0, -INFINITY) != nextafterl (-0.0, -1.0))
1027 printf ("nextafterl (+-0, +-Inf) != nextafterl (+-0, +-1)\n");
1028 result = 1;
1031 if (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)
1034 || nexttowardl (-0.0L, -INFINITY) != nexttowardl (-0.0L, -1.0L))
1036 printf ("nexttowardl (+-0, +-Inf) != nexttowardl (+-0, +-1)\n");
1037 result = 1;
1040 #endif
1042 if (! isnormal (FLT_MIN))
1044 puts ("isnormal (FLT_MIN) failed");
1045 result = 1;
1047 if (! isnormal (DBL_MIN))
1049 puts ("isnormal (DBL_MIN) failed");
1050 result = 1;
1052 #ifndef NO_LONG_DOUBLE
1053 if (! isnormal (LDBL_MIN))
1055 puts ("isnormal (LDBL_MIN) failed");
1056 result = 1;
1058 #endif
1060 #ifdef __i386__
1061 /* This is a test for the strange long doubles in x86 FPUs. */
1063 union
1065 char b[10];
1066 long double d;
1067 } u =
1068 { .b = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0 } };
1070 if (fpclassify (u.d) != FP_NORMAL)
1072 printf ("fpclassify (0x00008000000000000000) failed: %d (%Lg)\n",
1073 fpclassify (u.d), u.d);
1074 result = 1;
1078 /* Special NaNs in x86 long double. Test for scalbl. */
1080 union
1082 char b[10];
1083 long double d;
1084 } u =
1085 { .b = { 0, 1, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f } };
1086 long double r;
1088 r = scalbl (u.d, 0.0);
1089 if (!isnan (r))
1091 puts ("scalbl(NaN, 0) does not return NaN");
1092 result = 1;
1094 else if (memcmp (&r, &u.d, sizeof (double)) != 0)
1096 puts ("scalbl(NaN, 0) does not return the same NaN");
1097 result = 1;
1100 #endif
1102 #ifndef NO_LONG_DOUBLE
1104 long double r;
1106 feclearexcept (FE_ALL_EXCEPT);
1107 r = scalbl (LDBL_MIN, 2147483647);
1108 if (! isinf (r))
1110 puts ("scalbl (LDBL_MIN, 2147483647) does not return Inf");
1111 result = 1;
1113 else if (signbit (r) != 0)
1115 puts ("scalbl (LDBL_MIN, 2147483647) returns -Inf");
1116 result = 1;
1118 else if (fetestexcept (FE_UNDERFLOW))
1120 puts ("scalbl(NaN, 0) raises underflow exception");
1121 result = 1;
1124 feclearexcept (FE_ALL_EXCEPT);
1125 r = scalbl (LDBL_MAX, -2147483647);
1126 if (r != 0.0)
1128 puts ("scalbl (LDBL_MAX, -2147483647) does not return 0");
1129 result = 1;
1131 else if (signbit (r) != 0)
1133 puts ("scalbl (LDBL_MAX, -2147483647) returns -Inf");
1134 result = 1;
1136 else if (fetestexcept (FE_OVERFLOW))
1138 puts ("scalbl(NaN, 0) raises overflow exception");
1139 result = 1;
1142 #endif
1144 /* The tests here are very similar to tests earlier in this file,
1145 the important difference is just that there are no intervening
1146 union variables that cause some GCC versions to hide possible
1147 bugs in nextafter* implementation. */
1148 if (nextafterf (nextafterf (FLT_MIN, FLT_MIN / 2.0), FLT_MIN) != FLT_MIN)
1150 puts ("nextafterf FLT_MIN test failed");
1151 result = 1;
1153 if (nextafterf (nextafterf (-FLT_MIN, -FLT_MIN / 2.0), -FLT_MIN)
1154 != -FLT_MIN)
1156 puts ("nextafterf -FLT_MIN test failed");
1157 result = 1;
1159 if (nextafter (nextafter (DBL_MIN, DBL_MIN / 2.0), DBL_MIN) != DBL_MIN)
1161 puts ("nextafter DBL_MIN test failed");
1162 result = 1;
1164 if (nextafter (nextafter (-DBL_MIN, -DBL_MIN / 2.0), -DBL_MIN) != -DBL_MIN)
1166 puts ("nextafter -DBL_MIN test failed");
1167 result = 1;
1169 #ifndef NO_LONG_DOUBLE
1170 if (nextafterl (nextafterl (LDBL_MIN, LDBL_MIN / 2.0), LDBL_MIN)
1171 != LDBL_MIN)
1173 puts ("nextafterl LDBL_MIN test failed");
1174 result = 1;
1176 if (nextafterl (nextafterl (-LDBL_MIN, -LDBL_MIN / 2.0), -LDBL_MIN)
1177 != -LDBL_MIN)
1179 puts ("nextafterl -LDBL_MIN test failed");
1180 result = 1;
1182 #endif
1184 volatile float f1 = FLT_MAX;
1185 volatile float f2 = FLT_MAX / 2;
1186 (void) &f1;
1187 (void) &f2;
1188 feclearexcept (FE_ALL_EXCEPT);
1189 f2 += f1;
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;
1197 volatile double d1 = DBL_MAX;
1198 volatile double d2 = DBL_MAX / 2;
1199 (void) &d1;
1200 (void) &d2;
1201 feclearexcept (FE_ALL_EXCEPT);
1202 d2 += d1;
1203 fe = fetestexcept (FE_ALL_EXCEPT);
1204 if (fe != (FE_OVERFLOW | FE_INEXACT))
1206 printf ("double overflow test failed: %x\n", fe);
1207 result = 1;
1210 #ifndef NO_LONG_DOUBLE
1211 volatile long double ld1 = LDBL_MAX;
1212 volatile long double ld2 = LDBL_MAX / 2;
1213 (void) &ld1;
1214 (void) &ld2;
1215 feclearexcept (FE_ALL_EXCEPT);
1216 ld2 += ld1;
1217 fe = fetestexcept (FE_ALL_EXCEPT);
1218 if (fe != (FE_OVERFLOW | FE_INEXACT))
1220 printf ("long double overflow test failed: %x\n", fe);
1221 result = 1;
1223 #endif
1225 #if !defined NO_LONG_DOUBLE && LDBL_MANT_DIG == 113
1226 volatile long double ld3 = 0x1.0000000000010000000100000001p+1;
1227 volatile long double ld4 = 0x1.0000000000000000000000000001p+1;
1228 (void) &ld3;
1229 (void) &ld4;
1230 ld3 -= ld4;
1231 if (ld3 != 0x1.0p-47)
1233 printf ("long double subtraction test failed %.28La\n", ld3);
1234 result = 1;
1236 #endif
1238 /* Skip testing IBM long double format, for 2 reasons:
1239 1) it only supports FE_TONEAREST
1240 2) nextafter (0.0, 1.0) == nextafterl (0.0L, 1.0L), so
1241 nextafter (0.0, 1.0) / 16.0L will be 0.0L. */
1242 #if !defined NO_LONG_DOUBLE && LDBL_MANT_DIG >= DBL_MANT_DIG + 4 \
1243 && LDBL_MANT_DIG != 106
1244 int oldmode = fegetround ();
1245 int j;
1246 for (j = 0; j < 4; j++)
1248 int mode;
1249 int i;
1250 int k = 0;
1251 const char *mstr;
1252 switch (j)
1254 #ifdef FE_TONEAREST
1255 case 0:
1256 mode = FE_TONEAREST;
1257 mstr = "nearest";
1258 k = 8;
1259 break;
1260 #endif
1261 #ifdef FE_DOWNWARD
1262 case 1:
1263 mode = FE_DOWNWARD;
1264 mstr = "-inf";
1265 break;
1266 #endif
1267 #ifdef FE_UPWARD
1268 case 2:
1269 mode = FE_UPWARD;
1270 mstr = "+inf";
1271 k = 15;
1272 break;
1273 #endif
1274 #ifdef FE_TOWARDZERO
1275 case 3:
1276 mode = FE_TOWARDZERO;
1277 mstr = "0";
1278 break;
1279 #endif
1280 default:
1281 continue;
1284 volatile long double ld5 = nextafter (0.0, 1.0) / 16.0L;
1285 volatile double d5;
1286 (void) &ld5;
1287 for (i = 0; i <= 32; i++)
1289 if (fesetround (mode))
1291 printf ("failed to set rounding mode to %s\n", mstr);
1292 result = 1;
1293 break;
1295 d5 = ld5 * i;
1296 (void) &d5;
1297 fesetround (oldmode);
1298 if (d5 != ((j == 0 && i == 8) ? 0 : (i + k) / 16)
1299 * nextafter (0.0, 1.0))
1301 printf ("%La incorrectly rounded to %s as %a\n",
1302 ld5 * i, mstr, d5);
1303 result = 1;
1308 volatile long double ld7 = nextafterl (0.0L, 1.0L);
1309 volatile double d7;
1310 (void) &ld7;
1311 fesetround (FE_UPWARD);
1312 d7 = ld7;
1313 (void) &d7;
1314 fesetround (oldmode);
1316 if (d7 != nextafter (0.0, 1.0))
1318 printf ("%La incorrectly rounded upward to %a\n", ld7, d7);
1319 result = 1;
1321 #endif
1323 return result;