1 /* Support code for testing libm functions (compiled once per type).
2 Copyright (C) 1997-2017 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 /* Part of testsuite for libm.
21 libm-test-support.c contains functions shared by tests of different
22 libm functions and types; it is compiled once per type.
23 libm-test-driver.c defines the main function, and various variables
24 that are used to configure the code in libm-test-support.c for
25 different types and for variants such as testing inline functions.
27 The tests of individual functions are in .inc files processed by
28 gen-libm-test.pl, with the resulting files included together with
31 The per-type headers included both before libm-test-support.c and
32 for the tests of individual functions must define the following
35 FUNC(function): Convert general function name (like cos) to name
36 with correct suffix (e.g. cosl or cosf).
38 FLOAT: Floating-point type to test.
40 BUILD_COMPLEX(real, imag): Create a complex number by calling a
43 PREFIX: The prefix for <float.h> macros for the type (e.g. LDBL,
46 TYPE_STR: The name of the type as used in ulps files, as a string.
48 LIT: Append the correct suffix to a literal.
50 LITM: Append the correct suffix to an M_* macro name.
52 FTOSTR: A function similar in type to strfromf which converts a
55 snan_value_MACRO: The macro such as SNAN for a signaling NaN for
60 /* Parameter handling is primitive in the moment:
61 --verbose=[0..3] for different levels of output:
63 1: basic report on failed tests (default)
64 2: full report on all tests
65 -v for full output (equals --verbose=3)
66 -u for generation of an ULPs file
71 This suite tests some aspects of the correct implementation of
72 mathematical functions in libm. Some simple, specific parameters
73 are tested for correctness but there's no exhaustive
74 testing. Handling of specific inputs (e.g. infinity, not-a-number)
75 is also tested. Correct handling of exceptions is checked
76 against. These implemented tests should check all cases that are
79 NaN values: The payload of NaNs is set in inputs for functions
80 where it is significant, and is examined in the outputs of some
83 Inline functions: Inlining functions should give an improvement in
84 speed - but not in precission. The inlined functions return
85 reasonable values for a reasonable range of input values. The
86 result is not necessarily correct for all values and exceptions are
87 not correctly raised in all cases. Problematic input and return
88 values are infinity, not-a-number and minus zero. This suite
89 therefore does not check these specific inputs and the exception
90 handling for inlined mathematical functions - just the "reasonable"
93 Beware: The tests might fail for any of the following reasons:
96 - Floating Point Unit not working properly
99 With e.g. gcc 2.7.2.2 the test for cexp fails because of a compiler error.
102 To Do: All parameter should be numbers that can be represented as
103 exact floating point values. Currently some values cannot be
104 represented exactly and therefore the result is not the expected
105 result. For this we will use 36 digits so that numbers can be
106 represented exactly. */
108 #include "libm-test-support.h"
114 /* This header defines func_ulps, func_real_ulps and func_imag_ulps
116 #include "libm-test-ulps.h"
118 /* Maximum character buffer to store a stringitized FLOAT value. */
119 #define FSTR_MAX (128)
121 #define ulps_file_name "ULPs" /* Name of the ULPs file. */
122 static FILE *ulps_file
; /* File to document difference. */
123 static int output_ulps
; /* Should ulps printed? */
124 static char *output_dir
; /* Directory where generated files will be written. */
126 static int noErrors
; /* number of errors */
127 static int noTests
; /* number of tests (without testing exceptions) */
128 static int noExcTests
; /* number of tests for exception flags */
129 static int noErrnoTests
;/* number of tests for errno values */
132 static int output_max_error
; /* Should the maximal errors printed? */
133 static int output_points
; /* Should the single function results printed? */
134 static int ignore_max_ulp
; /* Should we ignore max_ulp? */
136 static FLOAT max_error
, real_max_error
, imag_max_error
;
138 static FLOAT prev_max_error
, prev_real_max_error
, prev_imag_max_error
;
140 static FLOAT max_valid_error
;
142 /* Sufficient numbers of digits to represent any floating-point value
143 unambiguously (for any choice of the number of bits in the first
144 hex digit, in the case of TYPE_HEX_DIG). When used with printf
145 formats where the precision counts only digits after the point, 1
146 is subtracted from these values. */
147 #define TYPE_DECIMAL_DIG __CONCATX (PREFIX, _DECIMAL_DIG)
148 #define TYPE_HEX_DIG ((MANT_DIG + 6) / 4)
150 /* Converts VALUE (a floating-point number) to string and writes it to DEST.
151 PRECISION specifies the number of fractional digits that should be printed.
152 CONVERSION is the conversion specifier, such as in printf, e.g. 'f' or 'a'.
153 The output is prepended with an empty space if VALUE is non-negative. */
155 fmt_ftostr (char *dest
, size_t size
, int precision
, const char *conversion
,
162 /* Generate the format string. */
163 ptr_format
= stpcpy (format
, "%.");
164 ret
= sprintf (ptr_format
, "%d", precision
);
166 ptr_format
= stpcpy (ptr_format
, conversion
);
168 /* Add a space to the beginning of the output string, if the floating-point
169 number is non-negative. This mimics the behavior of the space (' ') flag
170 in snprintf, which is not available on strfrom. */
171 if (! signbit (value
))
178 /* Call the float to string conversion function, e.g.: strfromd. */
179 FTOSTR (dest
, size
, format
, value
);
182 /* Compare KEY (a string, with the name of a function) with ULP (a
183 pointer to a struct ulp_data structure), returning a value less
184 than, equal to or greater than zero for use in bsearch. */
187 compare_ulp_data (const void *key
, const void *ulp
)
189 const char *keystr
= key
;
190 const struct ulp_data
*ulpdat
= ulp
;
191 return strcmp (keystr
, ulpdat
->name
);
194 static const int ulp_i_idx
= __CONCATX (ULP_I_
, PREFIX
);
195 static const int ulp_idx
= __CONCATX (ULP_
, PREFIX
);
197 /* Return the ulps for NAME in array DATA with NMEMB elements, or 0 if
201 find_ulps (const char *name
, const struct ulp_data
*data
, size_t nmemb
)
203 const struct ulp_data
*entry
= bsearch (name
, data
, nmemb
, sizeof (*data
),
208 return entry
->max_ulp
[(flag_test_inline
? ulp_i_idx
: ulp_idx
)];
212 init_max_error (const char *name
, int exact
)
217 prev_max_error
= find_ulps (name
, func_ulps
,
218 sizeof (func_ulps
) / sizeof (func_ulps
[0]));
219 prev_real_max_error
= find_ulps (name
, func_real_ulps
,
220 (sizeof (func_real_ulps
)
221 / sizeof (func_real_ulps
[0])));
222 prev_imag_max_error
= find_ulps (name
, func_imag_ulps
,
223 (sizeof (func_imag_ulps
)
224 / sizeof (func_imag_ulps
[0])));
226 /* The documented accuracy of IBM long double division is 3ulp (see
227 libgcc/config/rs6000/ibm-ldouble-format), so do not require
228 better accuracy for libm functions that are exactly defined for
230 max_valid_error
= exact
? 3 : 16;
232 max_valid_error
= exact
? 0 : 9;
234 prev_max_error
= (prev_max_error
<= max_valid_error
237 prev_real_max_error
= (prev_real_max_error
<= max_valid_error
238 ? prev_real_max_error
240 prev_imag_max_error
= (prev_imag_max_error
<= max_valid_error
241 ? prev_imag_max_error
243 feclearexcept (FE_ALL_EXCEPT
);
248 set_max_error (FLOAT current
, FLOAT
*curr_max_error
)
250 if (current
> *curr_max_error
&& current
<= max_valid_error
)
251 *curr_max_error
= current
;
257 print_float (FLOAT f
)
259 /* As printf doesn't differ between a sNaN and a qNaN, do this manually. */
266 char fstrn
[FSTR_MAX
], fstrx
[FSTR_MAX
];
267 fmt_ftostr (fstrn
, FSTR_MAX
, TYPE_DECIMAL_DIG
- 1, "e", f
);
268 fmt_ftostr (fstrx
, FSTR_MAX
, TYPE_HEX_DIG
- 1, "a", f
);
269 printf ("%s %s\n", fstrn
, fstrx
);
273 /* Should the message print to screen? This depends on the verbose flag,
274 and the test status. */
276 print_screen (int ok
)
280 || (verbose
== 1 && ok
== 0)))
286 /* Should the message print to screen? This depends on the verbose flag,
287 and the test status. */
289 print_screen_max_error (int ok
)
293 || ((verbose
== 1) && (ok
== 0))))
298 /* Update statistic counters. */
300 update_stats (int ok
)
308 print_function_ulps (const char *function_name
, FLOAT ulp
)
312 char ustrn
[FSTR_MAX
];
313 FTOSTR (ustrn
, FSTR_MAX
, "%.0f", FUNC (ceil
) (ulp
));
314 fprintf (ulps_file
, "Function: \"%s\":\n", function_name
);
315 fprintf (ulps_file
, "%s: %s\n", qtype_str
, ustrn
);
321 print_complex_function_ulps (const char *function_name
, FLOAT real_ulp
,
326 char fstrn
[FSTR_MAX
];
329 FTOSTR (fstrn
, FSTR_MAX
, "%.0f", FUNC (ceil
) (real_ulp
));
330 fprintf (ulps_file
, "Function: Real part of \"%s\":\n", function_name
);
331 fprintf (ulps_file
, "%s: %s\n", qtype_str
, fstrn
);
335 FTOSTR (fstrn
, FSTR_MAX
, "%.0f", FUNC (ceil
) (imag_ulp
));
336 fprintf (ulps_file
, "Function: Imaginary part of \"%s\":\n", function_name
);
337 fprintf (ulps_file
, "%s: %s\n", qtype_str
, fstrn
);
346 /* Test if Floating-Point stack hasn't changed */
348 fpstack_test (const char *test_name
)
350 #if defined (__i386__) || defined (__x86_64__)
351 static int old_stack
;
354 asm ("fnstsw" : "=a" (sw
));
360 printf ("FP-Stack wrong after test %s (%d, should be %d)\n",
361 test_name
, sw
, old_stack
);
370 print_max_error (const char *func_name
)
374 if (max_error
== 0.0 || (max_error
<= prev_max_error
&& !ignore_max_ulp
))
380 print_function_ulps (func_name
, max_error
);
383 if (print_screen_max_error (ok
))
385 char mestr
[FSTR_MAX
], pmestr
[FSTR_MAX
];
386 FTOSTR (mestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (max_error
));
387 FTOSTR (pmestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (prev_max_error
));
388 printf ("Maximal error of `%s'\n", func_name
);
389 printf (" is : %s ulp\n", mestr
);
390 printf (" accepted: %s ulp\n", pmestr
);
398 print_complex_max_error (const char *func_name
)
400 int real_ok
= 0, imag_ok
= 0, ok
;
402 if (real_max_error
== 0
403 || (real_max_error
<= prev_real_max_error
&& !ignore_max_ulp
))
408 if (imag_max_error
== 0
409 || (imag_max_error
<= prev_imag_max_error
&& !ignore_max_ulp
))
414 ok
= real_ok
&& imag_ok
;
417 print_complex_function_ulps (func_name
,
418 real_ok
? 0 : real_max_error
,
419 imag_ok
? 0 : imag_max_error
);
421 if (print_screen_max_error (ok
))
423 char rmestr
[FSTR_MAX
], prmestr
[FSTR_MAX
];
424 char imestr
[FSTR_MAX
], pimestr
[FSTR_MAX
];
425 FTOSTR (rmestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (real_max_error
));
426 FTOSTR (prmestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (prev_real_max_error
));
427 FTOSTR (imestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (imag_max_error
));
428 FTOSTR (pimestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (prev_imag_max_error
));
429 printf ("Maximal error of real part of: %s\n", func_name
);
430 printf (" is : %s ulp\n", rmestr
);
431 printf (" accepted: %s ulp\n", prmestr
);
432 printf ("Maximal error of imaginary part of: %s\n", func_name
);
433 printf (" is : %s ulp\n", imestr
);
434 printf (" accepted: %s ulp\n", pimestr
);
442 /* Test whether a given exception was raised. */
444 test_single_exception (const char *test_name
,
448 const char *flag_name
)
451 if (exception
& exc_flag
)
453 if (fetestexcept (fe_flag
))
455 if (print_screen (1))
456 printf ("Pass: %s: Exception \"%s\" set\n", test_name
, flag_name
);
461 if (print_screen (0))
462 printf ("Failure: %s: Exception \"%s\" not set\n",
463 test_name
, flag_name
);
468 if (fetestexcept (fe_flag
))
471 if (print_screen (0))
472 printf ("Failure: %s: Exception \"%s\" set\n",
473 test_name
, flag_name
);
477 if (print_screen (1))
478 printf ("%s: Exception \"%s\" not set\n", test_name
,
487 /* Test whether exceptions given by EXCEPTION are raised. Ignore thereby
488 allowed but not required exceptions.
491 test_exceptions (const char *test_name
, int exception
)
493 if (flag_test_exceptions
&& EXCEPTION_TESTS (FLOAT
))
497 if ((exception
& DIVIDE_BY_ZERO_EXCEPTION_OK
) == 0)
498 test_single_exception (test_name
, exception
,
499 DIVIDE_BY_ZERO_EXCEPTION
, FE_DIVBYZERO
,
503 if ((exception
& INVALID_EXCEPTION_OK
) == 0)
504 test_single_exception (test_name
, exception
,
505 INVALID_EXCEPTION
, FE_INVALID
,
506 "Invalid operation");
509 if ((exception
& OVERFLOW_EXCEPTION_OK
) == 0)
510 test_single_exception (test_name
, exception
, OVERFLOW_EXCEPTION
,
511 FE_OVERFLOW
, "Overflow");
513 /* Spurious "underflow" and "inexact" exceptions are always
514 allowed for IBM long double, in line with the underlying
517 if ((exception
& UNDERFLOW_EXCEPTION_OK
) == 0
518 && !(TEST_COND_ibm128
519 && (exception
& UNDERFLOW_EXCEPTION
) == 0))
520 test_single_exception (test_name
, exception
, UNDERFLOW_EXCEPTION
,
521 FE_UNDERFLOW
, "Underflow");
524 if ((exception
& (INEXACT_EXCEPTION
| NO_INEXACT_EXCEPTION
)) != 0
525 && !(TEST_COND_ibm128
526 && (exception
& NO_INEXACT_EXCEPTION
) != 0))
527 test_single_exception (test_name
, exception
, INEXACT_EXCEPTION
,
528 FE_INEXACT
, "Inexact");
531 feclearexcept (FE_ALL_EXCEPT
);
534 /* Test whether errno for TEST_NAME, set to ERRNO_VALUE, has value
535 EXPECTED_VALUE (description EXPECTED_NAME). */
537 test_single_errno (const char *test_name
, int errno_value
,
538 int expected_value
, const char *expected_name
)
540 if (errno_value
== expected_value
)
542 if (print_screen (1))
543 printf ("Pass: %s: errno set to %d (%s)\n", test_name
, errno_value
,
549 if (print_screen (0))
550 printf ("Failure: %s: errno set to %d, expected %d (%s)\n",
551 test_name
, errno_value
, expected_value
, expected_name
);
555 /* Test whether errno (value ERRNO_VALUE) has been for TEST_NAME set
556 as required by EXCEPTIONS. */
558 test_errno (const char *test_name
, int errno_value
, int exceptions
)
563 if (exceptions
& ERRNO_UNCHANGED
)
564 test_single_errno (test_name
, errno_value
, 0, "unchanged");
565 if (exceptions
& ERRNO_EDOM
)
566 test_single_errno (test_name
, errno_value
, EDOM
, "EDOM");
567 if (exceptions
& ERRNO_ERANGE
)
568 test_single_errno (test_name
, errno_value
, ERANGE
, "ERANGE");
572 /* Returns the number of ulps that GIVEN is away from EXPECTED. */
573 #define ULPDIFF(given, expected) \
574 (FUNC(fabs) ((given) - (expected)) / ulp (expected))
576 /* Returns the size of an ulp for VALUE. */
582 switch (fpclassify (value
))
585 /* We compute the distance to the next FP which is the same as the
586 value of the smallest subnormal number. Previously we used
587 2^-(MANT_DIG - 1) which is too large a value to be useful. Note that we
588 can't use ilogb(0), since that isn't a valid thing to do. As a point
589 of comparison Java's ulp returns the next normal value e.g.
590 2^(1 - MAX_EXP) for ulp(0), but that is not what we want for
592 /* Fall through... */
594 /* The next closest subnormal value is a constant distance away. */
595 ulp
= FUNC(ldexp
) (1.0, MIN_EXP
- MANT_DIG
);
599 ulp
= FUNC(ldexp
) (1.0, FUNC(ilogb
) (value
) - MANT_DIG
+ 1);
603 /* It should never happen. */
611 check_float_internal (const char *test_name
, FLOAT computed
, FLOAT expected
,
613 FLOAT
*curr_max_error
, FLOAT max_ulp
)
619 int errno_value
= errno
;
621 test_exceptions (test_name
, exceptions
);
622 test_errno (test_name
, errno_value
, exceptions
);
623 if (exceptions
& IGNORE_RESULT
)
625 if (issignaling (computed
) && issignaling (expected
))
627 if ((exceptions
& TEST_NAN_SIGN
) != 0
628 && signbit (computed
) != signbit (expected
))
631 printf ("signaling NaN has wrong sign.\n");
633 else if ((exceptions
& TEST_NAN_PAYLOAD
) != 0
634 && (FUNC (getpayload
) (&computed
)
635 != FUNC (getpayload
) (&expected
)))
638 printf ("signaling NaN has wrong payload.\n");
643 else if (issignaling (computed
) || issignaling (expected
))
645 else if (isnan (computed
) && isnan (expected
))
647 if ((exceptions
& TEST_NAN_SIGN
) != 0
648 && signbit (computed
) != signbit (expected
))
651 printf ("quiet NaN has wrong sign.\n");
653 else if ((exceptions
& TEST_NAN_PAYLOAD
) != 0
654 && (FUNC (getpayload
) (&computed
)
655 != FUNC (getpayload
) (&expected
)))
658 printf ("quiet NaN has wrong payload.\n");
663 else if (isinf (computed
) && isinf (expected
))
665 /* Test for sign of infinities. */
666 if ((exceptions
& IGNORE_ZERO_INF_SIGN
) == 0
667 && signbit (computed
) != signbit (expected
))
670 printf ("infinity has wrong sign.\n");
675 /* Don't calculate ULPs for infinities or any kind of NaNs. */
676 else if (isinf (computed
) || isnan (computed
)
677 || isinf (expected
) || isnan (expected
))
681 diff
= FUNC(fabs
) (computed
- expected
);
682 ulps
= ULPDIFF (computed
, expected
);
683 set_max_error (ulps
, curr_max_error
);
685 if ((exceptions
& IGNORE_ZERO_INF_SIGN
) == 0
686 && computed
== 0.0 && expected
== 0.0
687 && signbit(computed
) != signbit (expected
))
689 else if (ulps
<= max_ulp
&& !ignore_max_ulp
)
694 if (print_screen (ok
))
697 printf ("Failure: ");
698 printf ("Test: %s\n", test_name
);
699 printf ("Result:\n");
701 print_float (computed
);
702 printf (" should be: ");
703 print_float (expected
);
706 char dstrn
[FSTR_MAX
], dstrx
[FSTR_MAX
];
707 char ustrn
[FSTR_MAX
], mustrn
[FSTR_MAX
];
708 fmt_ftostr (dstrn
, FSTR_MAX
, TYPE_DECIMAL_DIG
- 1, "e", diff
);
709 fmt_ftostr (dstrx
, FSTR_MAX
, TYPE_HEX_DIG
- 1, "a", diff
);
710 fmt_ftostr (ustrn
, FSTR_MAX
, 4, "f", ulps
);
711 fmt_ftostr (mustrn
, FSTR_MAX
, 4, "f", max_ulp
);
712 printf (" difference: %s %s\n", dstrn
, dstrx
);
713 printf (" ulp : %s\n", ustrn
);
714 printf (" max.ulp : %s\n", mustrn
);
720 fpstack_test (test_name
);
726 check_float (const char *test_name
, FLOAT computed
, FLOAT expected
,
729 check_float_internal (test_name
, computed
, expected
,
730 exceptions
, &max_error
, prev_max_error
);
735 check_complex (const char *test_name
, __complex__ FLOAT computed
,
736 __complex__ FLOAT expected
,
739 FLOAT part_comp
, part_exp
;
742 if (asprintf (&str
, "Real part of: %s", test_name
) == -1)
745 part_comp
= __real__ computed
;
746 part_exp
= __real__ expected
;
748 check_float_internal (str
, part_comp
, part_exp
,
749 exception
, &real_max_error
, prev_real_max_error
);
752 if (asprintf (&str
, "Imaginary part of: %s", test_name
) == -1)
755 part_comp
= __imag__ computed
;
756 part_exp
= __imag__ expected
;
758 /* Don't check again for exceptions or errno, just pass through the
759 other relevant flags. */
760 check_float_internal (str
, part_comp
, part_exp
,
761 exception
& (IGNORE_ZERO_INF_SIGN
764 &imag_max_error
, prev_imag_max_error
);
769 /* Check that computed and expected values are equal (int values). */
771 check_int (const char *test_name
, int computed
, int expected
,
775 int errno_value
= errno
;
777 test_exceptions (test_name
, exceptions
);
778 test_errno (test_name
, errno_value
, exceptions
);
779 if (exceptions
& IGNORE_RESULT
)
782 if (computed
== expected
)
785 if (print_screen (ok
))
788 printf ("Failure: ");
789 printf ("Test: %s\n", test_name
);
790 printf ("Result:\n");
791 printf (" is: %d\n", computed
);
792 printf (" should be: %d\n", expected
);
797 fpstack_test (test_name
);
802 /* Check that computed and expected values are equal (long int values). */
804 check_long (const char *test_name
, long int computed
, long int expected
,
808 int errno_value
= errno
;
810 test_exceptions (test_name
, exceptions
);
811 test_errno (test_name
, errno_value
, exceptions
);
812 if (exceptions
& IGNORE_RESULT
)
815 if (computed
== expected
)
818 if (print_screen (ok
))
821 printf ("Failure: ");
822 printf ("Test: %s\n", test_name
);
823 printf ("Result:\n");
824 printf (" is: %ld\n", computed
);
825 printf (" should be: %ld\n", expected
);
830 fpstack_test (test_name
);
835 /* Check that computed value is true/false. */
837 check_bool (const char *test_name
, int computed
, int expected
,
841 int errno_value
= errno
;
843 test_exceptions (test_name
, exceptions
);
844 test_errno (test_name
, errno_value
, exceptions
);
845 if (exceptions
& IGNORE_RESULT
)
848 if ((computed
== 0) == (expected
== 0))
851 if (print_screen (ok
))
854 printf ("Failure: ");
855 printf ("Test: %s\n", test_name
);
856 printf ("Result:\n");
857 printf (" is: %d\n", computed
);
858 printf (" should be: %d\n", expected
);
863 fpstack_test (test_name
);
868 /* check that computed and expected values are equal (long int values) */
870 check_longlong (const char *test_name
, long long int computed
,
871 long long int expected
,
875 int errno_value
= errno
;
877 test_exceptions (test_name
, exceptions
);
878 test_errno (test_name
, errno_value
, exceptions
);
879 if (exceptions
& IGNORE_RESULT
)
882 if (computed
== expected
)
885 if (print_screen (ok
))
889 printf ("Test: %s\n", test_name
);
890 printf ("Result:\n");
891 printf (" is: %lld\n", computed
);
892 printf (" should be: %lld\n", expected
);
897 fpstack_test (test_name
);
902 /* Check that computed and expected values are equal (intmax_t values). */
904 check_intmax_t (const char *test_name
, intmax_t computed
,
905 intmax_t expected
, int exceptions
)
908 int errno_value
= errno
;
910 test_exceptions (test_name
, exceptions
);
911 test_errno (test_name
, errno_value
, exceptions
);
912 if (exceptions
& IGNORE_RESULT
)
915 if (computed
== expected
)
918 if (print_screen (ok
))
922 printf ("Test: %s\n", test_name
);
923 printf ("Result:\n");
924 printf (" is: %jd\n", computed
);
925 printf (" should be: %jd\n", expected
);
930 fpstack_test (test_name
);
935 /* Check that computed and expected values are equal (uintmax_t values). */
937 check_uintmax_t (const char *test_name
, uintmax_t computed
,
938 uintmax_t expected
, int exceptions
)
941 int errno_value
= errno
;
943 test_exceptions (test_name
, exceptions
);
944 test_errno (test_name
, errno_value
, exceptions
);
945 if (exceptions
& IGNORE_RESULT
)
948 if (computed
== expected
)
951 if (print_screen (ok
))
955 printf ("Test: %s\n", test_name
);
956 printf ("Result:\n");
957 printf (" is: %ju\n", computed
);
958 printf (" should be: %ju\n", expected
);
963 fpstack_test (test_name
);
967 /* Return whether a test with flags EXCEPTIONS should be run. */
969 enable_test (int exceptions
)
971 if (exceptions
& XFAIL_TEST
)
973 if (flag_test_inline
&& (exceptions
& NO_TEST_INLINE
))
975 if (flag_test_finite
&& (exceptions
& NON_FINITE
) != 0)
977 if (!SNAN_TESTS (FLOAT
) && (exceptions
& TEST_SNAN
) != 0)
979 if (flag_test_mathvec
&& (exceptions
& NO_TEST_MATHVEC
) != 0)
985 /* This is to prevent messages from the SVID libm emulation. */
987 matherr (struct exception
*x
__attribute__ ((unused
)))
995 fpstack_test ("start *init*");
997 /* Clear all exceptions. From now on we must not get random exceptions. */
998 feclearexcept (FE_ALL_EXCEPT
);
1001 /* Test to make sure we start correctly. */
1002 fpstack_test ("end *init*");
1005 /* Definitions of arguments for argp functions. */
1006 static const struct argp_option options
[] =
1008 { "verbose", 'v', "NUMBER", 0, "Level of verbosity (0..3)"},
1009 { "ulps-file", 'u', NULL
, 0, "Output ulps to file ULPs"},
1010 { "no-max-error", 'f', NULL
, 0,
1011 "Don't output maximal errors of functions"},
1012 { "no-points", 'p', NULL
, 0,
1013 "Don't output results of functions invocations"},
1014 { "ignore-max-ulp", 'i', "yes/no", 0,
1015 "Ignore given maximal errors"},
1016 { "output-dir", 'o', "DIR", 0,
1017 "Directory where generated files will be placed"},
1018 { NULL
, 0, NULL
, 0, NULL
}
1021 /* Prototype for option handler. */
1022 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
1024 /* Data structure to communicate with argp functions. */
1025 static struct argp argp
=
1027 options
, parse_opt
, NULL
, doc
,
1031 /* Handle program arguments. */
1033 parse_opt (int key
, char *arg
, struct argp_state
*state
)
1038 output_max_error
= 0;
1041 if (strcmp (arg
, "yes") == 0)
1043 else if (strcmp (arg
, "no") == 0)
1047 output_dir
= (char *) malloc (strlen (arg
) + 1);
1048 if (output_dir
!= NULL
)
1049 strcpy (output_dir
, arg
);
1061 verbose
= (unsigned int) strtoul (optarg
, NULL
, 0);
1066 return ARGP_ERR_UNKNOWN
;
1071 /* Verify that our ulp () implementation is behaving as expected
1076 FLOAT ulps
, ulpx
, value
;
1078 /* Check ulp of zero is a subnormal value... */
1079 ulps
= ulp (0x0.0p0
);
1080 if (fpclassify (ulps
) != FP_SUBNORMAL
)
1082 fprintf (stderr
, "ulp (0x0.0p0) is not FP_SUBNORMAL!\n");
1083 exit (EXIT_FAILURE
);
1085 /* Check that the ulp of one is a normal value... */
1087 if (fpclassify (ulps
) != FP_NORMAL
)
1089 fprintf (stderr
, "ulp (1.0L) is not FP_NORMAL\n");
1090 exit (EXIT_FAILURE
);
1093 /* Compute the next subnormal value using nextafter to validate ulp.
1094 We allow +/- 1 ulp around the represented value. */
1095 value
= FUNC(nextafter
) (0, 1);
1096 ulps
= ULPDIFF (value
, 0);
1098 if (ulps
< (1.0L - ulpx
) || ulps
> (1.0L + ulpx
))
1100 fprintf (stderr
, "Value outside of 1 +/- 1ulp.\n");
1101 exit (EXIT_FAILURE
);
1103 /* Compute the nearest representable number from 10 towards 20.
1104 The result is 10 + 1ulp. We use this to check the ulp function.
1105 We allow +/- 1 ulp around the represented value. */
1106 value
= FUNC(nextafter
) (10, 20);
1107 ulps
= ULPDIFF (value
, 10);
1109 if (ulps
< (1.0L - ulpx
) || ulps
> (1.0L + ulpx
))
1111 fprintf (stderr
, "Value outside of 1 +/- 1ulp.\n");
1112 exit (EXIT_FAILURE
);
1114 /* This gives one more ulp. */
1115 value
= FUNC(nextafter
) (value
, 20);
1116 ulps
= ULPDIFF (value
, 10);
1118 if (ulps
< (2.0L - ulpx
) || ulps
> (2.0L + ulpx
))
1120 fprintf (stderr
, "Value outside of 2 +/- 1ulp.\n");
1121 exit (EXIT_FAILURE
);
1123 /* And now calculate 100 ulp. */
1124 for (i
= 2; i
< 100; i
++)
1125 value
= FUNC(nextafter
) (value
, 20);
1126 ulps
= ULPDIFF (value
, 10);
1127 ulpx
= ulp (100.0L);
1128 if (ulps
< (100.0L - ulpx
) || ulps
> (100.0L + ulpx
))
1130 fprintf (stderr
, "Value outside of 100 +/- 1ulp.\n");
1131 exit (EXIT_FAILURE
);
1135 /* Do all initialization for a test run with arguments given by ARGC
1138 libm_test_init (int argc
, char **argv
)
1141 char *ulps_file_path
;
1146 output_max_error
= 1;
1149 /* XXX set to 0 for releases. */
1152 /* Parse and process arguments. */
1153 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
1155 if (remaining
!= argc
)
1157 fprintf (stderr
, "wrong number of arguments");
1158 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
1159 exit (EXIT_FAILURE
);
1164 if (output_dir
!= NULL
)
1165 dir_len
= strlen (output_dir
);
1166 ulps_file_path
= (char *) malloc (dir_len
+ strlen (ulps_file_name
) + 1);
1167 if (ulps_file_path
== NULL
)
1169 perror ("can't allocate path for `ULPs' file: ");
1172 sprintf (ulps_file_path
, "%s%s", output_dir
== NULL
? "" : output_dir
, ulps_file_name
);
1173 ulps_file
= fopen (ulps_file_path
, "a");
1174 if (ulps_file
== NULL
)
1176 perror ("can't open file `ULPs' for writing: ");
1183 fputs (test_msg
, stdout
);
1190 /* Process the test results, returning the exit status. */
1192 libm_test_finish (void)
1197 printf ("\nTest suite completed:\n");
1198 printf (" %d test cases plus %d tests for exception flags and\n"
1199 " %d tests for errno executed.\n",
1200 noTests
, noExcTests
, noErrnoTests
);
1203 printf (" %d errors occurred.\n", noErrors
);
1206 printf (" All tests passed successfully.\n");