1 /* Support code for testing libm functions (compiled once per type).
2 Copyright (C) 1997-2025 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 <https://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.py, 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 ULP_IDX: The array indexes for ulps values for this function.
50 LIT: Append the correct suffix to a literal.
52 LITM: Append the correct suffix to an M_* macro name.
54 FTOSTR: A function similar in type to strfromf which converts a
57 snan_value_MACRO: The macro such as SNAN for a signaling NaN for
62 /* Parameter handling is primitive in the moment:
63 --verbose=[0..3] for different levels of output:
65 1: basic report on failed tests (default)
66 2: full report on all tests
67 -v for full output (equals --verbose=3)
68 -u for generation of an ULPs file
73 This suite tests some aspects of the correct implementation of
74 mathematical functions in libm. Some simple, specific parameters
75 are tested for correctness but there's no exhaustive
76 testing. Handling of specific inputs (e.g. infinity, not-a-number)
77 is also tested. Correct handling of exceptions is checked
78 against. These implemented tests should check all cases that are
81 NaN values: The payload of NaNs is set in inputs for functions
82 where it is significant, and is examined in the outputs of some
85 Inline functions: Inlining functions should give an improvement in
86 speed - but not in precision. The inlined functions return
87 reasonable values for a reasonable range of input values. The
88 result is not necessarily correct for all values and exceptions are
89 not correctly raised in all cases. Problematic input and return
90 values are infinity, not-a-number and minus zero. This suite
91 therefore does not check these specific inputs and the exception
92 handling for inlined mathematical functions - just the "reasonable"
95 Beware: The tests might fail for any of the following reasons:
98 - Floating Point Unit not working properly
101 With e.g. gcc 2.7.2.2 the test for cexp fails because of a compiler error.
104 To Do: All parameter should be numbers that can be represented as
105 exact floating point values. Currently some values cannot be
106 represented exactly and therefore the result is not the expected
107 result. For this we will use 36 digits so that numbers can be
108 represented exactly. */
110 #include "libm-test-support.h"
112 #include <array_length.h>
119 /* This header defines func_ulps, func_real_ulps and func_imag_ulps
121 #include "libm-test-ulps.h"
123 /* Maximum character buffer to store a stringitized FLOAT value. */
124 #define FSTR_MAX (128)
126 #define ulps_file_name "ULPs" /* Name of the ULPs file. */
127 static FILE *ulps_file
; /* File to document difference. */
128 static int output_ulps
; /* Should ulps printed? */
129 static char *output_dir
; /* Directory where generated files will be written. */
132 #define TEST_MAXERROR 2
133 static int noErrors
; /* number of errors */
134 static int noTests
; /* number of tests (without testing exceptions) */
135 static int noMaxErrorTests
; /* number of max error tests */
136 static int noExcTests
; /* number of tests for exception flags */
137 static int noErrnoTests
; /* number of tests for errno values */
139 static unsigned int verbose
;
140 static int output_max_error
; /* Should the maximal errors printed? */
141 static int output_points
; /* Should the single function results printed? */
142 static int ignore_max_ulp
; /* Should we ignore max_ulp? */
143 static int test_ibm128
; /* Is argument or result IBM long double? */
145 static FLOAT max_error
, real_max_error
, imag_max_error
;
147 static FLOAT prev_max_error
, prev_real_max_error
, prev_imag_max_error
;
149 static FLOAT max_valid_error
;
151 static bool is_complex
= false;
153 /* Sufficient numbers of digits to represent any floating-point value
154 unambiguously (for any choice of the number of bits in the first
155 hex digit, in the case of TYPE_HEX_DIG). When used with printf
156 formats where the precision counts only digits after the point, 1
157 is subtracted from these values. */
158 #define TYPE_DECIMAL_DIG __CONCATX (PREFIX, _DECIMAL_DIG)
159 #define TYPE_HEX_DIG ((MANT_DIG + 6) / 4)
161 /* Converts VALUE (a floating-point number) to string and writes it to DEST.
162 PRECISION specifies the number of fractional digits that should be printed.
163 CONVERSION is the conversion specifier, such as in printf, e.g. 'f' or 'a'.
164 The output is prepended with an empty space if VALUE is non-negative. */
166 fmt_ftostr (char *dest
, size_t size
, int precision
, const char *conversion
,
173 /* Generate the format string. */
174 ptr_format
= stpcpy (format
, "%.");
175 ret
= sprintf (ptr_format
, "%d", precision
);
177 ptr_format
= stpcpy (ptr_format
, conversion
);
179 /* Add a space to the beginning of the output string, if the floating-point
180 number is non-negative. This mimics the behavior of the space (' ') flag
181 in snprintf, which is not available on strfrom. */
182 if (! signbit (value
))
189 /* Call the float to string conversion function, e.g.: strfromd. */
190 FTOSTR (dest
, size
, format
, value
);
195 default_max_valid_error (int exact
, int testing_ibm128
)
198 /* The documented accuracy of IBM long double division is 3ulp
199 (see libgcc/config/rs6000/ibm-ldouble-format), so do not
200 require better accuracy for libm functions that are exactly
201 defined for other formats. */
202 return max_valid_error
= exact
? 3 : 16;
204 return max_valid_error
= exact
? 0 : 9;
207 /* Compare KEY (a string, with the name of a function) with ULP (a
208 pointer to a struct ulp_data structure), returning a value less
209 than, equal to or greater than zero for use in bsearch. */
212 compare_ulp_data (const void *key
, const void *ulp
)
214 const char *keystr
= key
;
215 const struct ulp_data
*ulpdat
= ulp
;
216 return strcmp (keystr
, ulpdat
->name
);
219 static const int ulp_idx
= ULP_IDX
;
221 /* Return the ulps for NAME in array DATA with NMEMB elements, or 0 if
225 find_ulps (const char *name
, const struct ulp_data
*data
, size_t nmemb
,
226 int exact
, int testing_ibm128
)
228 const struct ulp_data
*entry
= bsearch (name
, data
, nmemb
, sizeof (*data
),
231 return default_max_valid_error (exact
, testing_ibm128
);
233 return entry
->max_ulp
[ulp_idx
] == -1
234 ? default_max_valid_error (exact
, testing_ibm128
)
235 : entry
->max_ulp
[ulp_idx
];
239 init_max_error (const char *name
, int exact
, int testing_ibm128
)
244 test_ibm128
= testing_ibm128
;
245 prev_max_error
= find_ulps (name
, func_ulps
, array_length (func_ulps
),
246 exact
, testing_ibm128
);
247 prev_real_max_error
= find_ulps (name
, func_real_ulps
,
248 array_length (func_real_ulps
), exact
,
250 prev_imag_max_error
= find_ulps (name
, func_imag_ulps
,
251 array_length (func_imag_ulps
),
252 exact
, testing_ibm128
);
253 max_valid_error
= default_max_valid_error (exact
, testing_ibm128
);
254 prev_max_error
= (prev_max_error
<= max_valid_error
257 prev_real_max_error
= (prev_real_max_error
<= max_valid_error
258 ? prev_real_max_error
260 prev_imag_max_error
= (prev_imag_max_error
<= max_valid_error
261 ? prev_imag_max_error
263 feclearexcept (FE_ALL_EXCEPT
);
268 set_max_error (FLOAT current
, FLOAT
*curr_max_error
)
270 if (current
> *curr_max_error
&& current
<= max_valid_error
)
271 *curr_max_error
= current
;
277 print_float (FLOAT f
)
279 /* As printf doesn't differ between a sNaN and a qNaN, do this manually. */
286 char fstrn
[FSTR_MAX
], fstrx
[FSTR_MAX
];
287 fmt_ftostr (fstrn
, FSTR_MAX
, TYPE_DECIMAL_DIG
- 1, "e", f
);
288 fmt_ftostr (fstrx
, FSTR_MAX
, TYPE_HEX_DIG
- 1, "a", f
);
289 printf ("%s %s\n", fstrn
, fstrx
);
293 /* Should the message print to screen? This depends on the verbose flag,
294 and the test status. */
296 print_screen (int ok
)
300 || (verbose
== 1 && ok
== 0)))
306 /* Should the message print to screen? This depends on the verbose flag,
307 and the test status. */
309 print_screen_max_error (int ok
)
313 || ((verbose
== 1) && (ok
== 0))))
318 /* Update statistic counters. */
320 update_stats (int ok
, int testType
)
338 print_function_ulps (const char *function_name
, FLOAT ulp
)
342 char ustrn
[FSTR_MAX
];
343 FTOSTR (ustrn
, FSTR_MAX
, "%.0f", FUNC (ceil
) (ulp
));
344 fprintf (ulps_file
, "Function: \"%s\":\n", function_name
);
345 fprintf (ulps_file
, "%s: %s\n", qtype_str
, ustrn
);
351 print_complex_function_ulps (const char *function_name
, FLOAT real_ulp
,
356 char fstrn
[FSTR_MAX
];
359 FTOSTR (fstrn
, FSTR_MAX
, "%.0f", FUNC (ceil
) (real_ulp
));
360 fprintf (ulps_file
, "Function: Real part of \"%s\":\n", function_name
);
361 fprintf (ulps_file
, "%s: %s\n", qtype_str
, fstrn
);
365 FTOSTR (fstrn
, FSTR_MAX
, "%.0f", FUNC (ceil
) (imag_ulp
));
366 fprintf (ulps_file
, "Function: Imaginary part of \"%s\":\n", function_name
);
367 fprintf (ulps_file
, "%s: %s\n", qtype_str
, fstrn
);
376 /* Test if Floating-Point stack hasn't changed */
378 fpstack_test (const char *test_name
)
380 #if defined (__i386__) || defined (__x86_64__)
381 static int old_stack
;
384 asm ("fnstsw" : "=a" (sw
));
390 printf ("FP-Stack wrong after test %s (%d, should be %d)\n",
391 test_name
, sw
, old_stack
);
399 print_test_start (int test_num
, const char *test_name
, int test_type
)
401 if (print_screen (1))
402 printf ("--- Start of%s test # %d, named \"%s\" ---\n",
403 test_type
== TEST_MAXERROR
? " max error" : "", test_num
, test_name
);
407 print_test_end (int test_num
, const char *test_name
, int test_type
)
409 if (print_screen (1))
410 printf ("--- End of%s test # %d, named \"%s\" ---\n",
411 test_type
== TEST_MAXERROR
? " max error" : "", test_num
, test_name
);
414 /* This is a builtin test of overall max error. */
416 check_max_error (const char *func_name
)
419 int thisTest
= noMaxErrorTests
;
421 print_test_start (thisTest
, func_name
, TEST_MAXERROR
);
423 if (max_error
== 0.0 || (max_error
<= prev_max_error
&& !ignore_max_ulp
))
429 print_function_ulps (func_name
, max_error
);
432 if (print_screen_max_error (ok
))
434 char mestr
[FSTR_MAX
], pmestr
[FSTR_MAX
];
435 FTOSTR (mestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (max_error
));
436 FTOSTR (pmestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (prev_max_error
));
437 printf ("Maximal error of `%s'\n", func_name
);
438 printf (" is : %s ulp\n", mestr
);
439 printf (" accepted: %s ulp\n", pmestr
);
442 update_stats (ok
, TEST_MAXERROR
);
444 print_test_end (thisTest
, func_name
, TEST_MAXERROR
);
447 /* This is a builtin test of overall max error. */
449 check_complex_max_error (const char *func_name
)
451 int real_ok
= 0, imag_ok
= 0, ok
;
452 int thisTest
= noMaxErrorTests
;
454 print_test_start (thisTest
, func_name
, TEST_MAXERROR
);
456 if (real_max_error
== 0
457 || (real_max_error
<= prev_real_max_error
&& !ignore_max_ulp
))
462 if (imag_max_error
== 0
463 || (imag_max_error
<= prev_imag_max_error
&& !ignore_max_ulp
))
468 ok
= real_ok
&& imag_ok
;
471 print_complex_function_ulps (func_name
,
472 real_ok
? 0 : real_max_error
,
473 imag_ok
? 0 : imag_max_error
);
475 if (print_screen_max_error (ok
))
477 char rmestr
[FSTR_MAX
], prmestr
[FSTR_MAX
];
478 char imestr
[FSTR_MAX
], pimestr
[FSTR_MAX
];
479 FTOSTR (rmestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (real_max_error
));
480 FTOSTR (prmestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (prev_real_max_error
));
481 FTOSTR (imestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (imag_max_error
));
482 FTOSTR (pimestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (prev_imag_max_error
));
483 printf ("Maximal error of real part of: %s\n", func_name
);
484 printf (" is : %s ulp\n", rmestr
);
485 printf (" accepted: %s ulp\n", prmestr
);
486 printf ("Maximal error of imaginary part of: %s\n", func_name
);
487 printf (" is : %s ulp\n", imestr
);
488 printf (" accepted: %s ulp\n", pimestr
);
491 update_stats (ok
, TEST_MAXERROR
);
492 print_test_end (thisTest
, func_name
, TEST_MAXERROR
);
499 /* Test whether a given exception was raised. */
501 test_single_exception (const char *test_name
,
505 const char *flag_name
)
508 if (exception
& exc_flag
)
510 if (fetestexcept (fe_flag
))
512 if (print_screen (1))
513 printf ("Pass: %s: Exception \"%s\" set\n", test_name
, flag_name
);
518 if (print_screen (0))
519 printf ("Failure: %s: Exception \"%s\" not set\n",
520 test_name
, flag_name
);
525 if (fetestexcept (fe_flag
))
528 if (print_screen (0))
529 printf ("Failure: %s: Exception \"%s\" set\n",
530 test_name
, flag_name
);
534 if (print_screen (1))
535 printf ("Pass: %s: Exception \"%s\" not set\n", test_name
,
545 /* Test whether exceptions given by EXCEPTION are raised. Ignore thereby
546 allowed but not required exceptions.
549 test_exceptions (const char *test_name
, int exception
)
551 if (flag_test_exceptions
&& EXCEPTION_TESTS (FLOAT
))
555 if ((exception
& DIVIDE_BY_ZERO_EXCEPTION_OK
) == 0)
557 test_single_exception (test_name
, exception
,
558 DIVIDE_BY_ZERO_EXCEPTION
, FE_DIVBYZERO
,
564 if ((exception
& INVALID_EXCEPTION_OK
) == 0)
566 test_single_exception (test_name
, exception
,
567 INVALID_EXCEPTION
, FE_INVALID
,
568 "Invalid operation");
573 if ((exception
& OVERFLOW_EXCEPTION_OK
) == 0)
575 test_single_exception (test_name
, exception
, OVERFLOW_EXCEPTION
,
576 FE_OVERFLOW
, "Overflow");
580 /* Spurious "underflow" and "inexact" exceptions are always
581 allowed for IBM long double, in line with the underlying
584 if ((exception
& UNDERFLOW_EXCEPTION_OK
) == 0
586 && (exception
& UNDERFLOW_EXCEPTION
) == 0))
588 test_single_exception (test_name
, exception
, UNDERFLOW_EXCEPTION
,
589 FE_UNDERFLOW
, "Underflow");
595 if ((exception
& (INEXACT_EXCEPTION
| NO_INEXACT_EXCEPTION
)) != 0
597 && (exception
& NO_INEXACT_EXCEPTION
) != 0))
599 test_single_exception (test_name
, exception
, INEXACT_EXCEPTION
,
600 FE_INEXACT
, "Inexact");
608 if (print_screen (1))
609 printf ("Info: %s: No exceptions tested\n", test_name
);
611 feclearexcept (FE_ALL_EXCEPT
);
614 /* Test whether errno for TEST_NAME, set to ERRNO_VALUE, has value
615 EXPECTED_VALUE (description EXPECTED_NAME). */
617 test_single_errno (const char *test_name
, int errno_value
,
618 int expected_value
, const char *expected_name
)
620 if (errno_value
== expected_value
)
622 if (print_screen (1))
623 printf ("Pass: %s: errno set to %d (%s)\n", test_name
, errno_value
,
629 if (print_screen (0))
630 printf ("Failure: %s: errno set to %d, expected %d (%s)\n",
631 test_name
, errno_value
, expected_value
, expected_name
);
636 /* Test whether errno (value ERRNO_VALUE) has been for TEST_NAME set
637 as required by EXCEPTIONS. */
639 test_errno (const char *test_name
, int errno_value
, int exceptions
)
645 if ((exceptions
& (ERRNO_UNCHANGED
|ERRNO_EDOM
|ERRNO_ERANGE
)) == 0)
647 if (print_screen (1))
648 printf ("Info: %s: The value of errno was not tested\n",
654 if (exceptions
& ERRNO_UNCHANGED
)
656 test_single_errno (test_name
, errno_value
, 0, "unchanged");
659 if (exceptions
& ERRNO_EDOM
)
661 test_single_errno (test_name
, errno_value
, EDOM
, "EDOM");
664 if (exceptions
& ERRNO_ERANGE
)
666 test_single_errno (test_name
, errno_value
, ERANGE
, "ERANGE");
674 if (print_screen (1))
675 printf ("Info: %s: No errno tests\n", test_name
);
679 /* Returns the number of ulps that GIVEN is away from EXPECTED. */
680 #define ULPDIFF(given, expected) \
681 (FUNC(fabs) ((given) - (expected)) / ulp (expected))
683 /* Returns the size of an ulp for VALUE. */
689 switch (fpclassify (value
))
692 /* We compute the distance to the next FP which is the same as the
693 value of the smallest subnormal number. Previously we used
694 2^-(MANT_DIG - 1) which is too large a value to be useful. Note that we
695 can't use ilogb(0), since that isn't a valid thing to do. As a point
696 of comparison Java's ulp returns the next normal value e.g.
697 2^(1 - MAX_EXP) for ulp(0), but that is not what we want for
699 /* Fall through... */
701 /* The next closest subnormal value is a constant distance away. */
702 ulp
= FUNC(ldexp
) (1.0, MIN_EXP
- MANT_DIG
);
706 ulp
= FUNC(ldexp
) (1.0, FUNC(ilogb
) (value
) - MANT_DIG
+ 1);
710 /* It should never happen. */
718 check_float_internal (const char *test_name
, FLOAT computed
, FLOAT expected
,
720 FLOAT
*curr_max_error
, FLOAT max_ulp
)
726 int errno_value
= errno
;
727 int thisTest
= noTests
;
729 print_test_start (thisTest
, test_name
, TEST_INPUT
);
731 test_exceptions (test_name
, exceptions
);
732 test_errno (test_name
, errno_value
, exceptions
);
733 if (exceptions
& IGNORE_RESULT
)
735 if (issignaling (computed
) && issignaling (expected
))
737 if ((exceptions
& TEST_NAN_SIGN
) != 0
738 && signbit (computed
) != signbit (expected
))
741 printf ("signaling NaN has wrong sign.\n");
743 else if ((exceptions
& TEST_NAN_PAYLOAD
) != 0
744 && (FUNC (getpayload
) (&computed
)
745 != FUNC (getpayload
) (&expected
)))
748 printf ("signaling NaN has wrong payload.\n");
753 else if (issignaling (computed
) || issignaling (expected
))
755 else if (isnan (computed
) && isnan (expected
))
757 if ((exceptions
& TEST_NAN_SIGN
) != 0
758 && signbit (computed
) != signbit (expected
))
761 printf ("quiet NaN has wrong sign.\n");
763 else if ((exceptions
& TEST_NAN_PAYLOAD
) != 0
764 && (FUNC (getpayload
) (&computed
)
765 != FUNC (getpayload
) (&expected
)))
768 printf ("quiet NaN has wrong payload.\n");
773 else if (isinf (computed
) && isinf (expected
))
775 /* Test for sign of infinities. */
776 if ((exceptions
& IGNORE_ZERO_INF_SIGN
) == 0
777 && signbit (computed
) != signbit (expected
))
780 printf ("infinity has wrong sign.\n");
785 /* Don't calculate ULPs for infinities or any kind of NaNs. */
786 else if (isinf (computed
) || isnan (computed
)
787 || isinf (expected
) || isnan (expected
))
791 diff
= FUNC(fabs
) (computed
- expected
);
792 ulps
= ULPDIFF (computed
, expected
);
793 set_max_error (ulps
, curr_max_error
);
795 if (((exceptions
& IGNORE_ZERO_INF_SIGN
) == 0) && !flag_test_mathvec
796 && computed
== 0.0 && expected
== 0.0
797 && signbit(computed
) != signbit (expected
))
799 else if (ulps
<= max_ulp
&& !ignore_max_ulp
)
804 if (print_screen (ok
))
807 printf ("Failure: ");
808 printf ("Test: %s\n", test_name
);
809 printf ("Result:\n");
811 print_float (computed
);
812 printf (" should be: ");
813 print_float (expected
);
816 char dstrn
[FSTR_MAX
], dstrx
[FSTR_MAX
];
817 char ustrn
[FSTR_MAX
], mustrn
[FSTR_MAX
];
818 fmt_ftostr (dstrn
, FSTR_MAX
, TYPE_DECIMAL_DIG
- 1, "e", diff
);
819 fmt_ftostr (dstrx
, FSTR_MAX
, TYPE_HEX_DIG
- 1, "a", diff
);
820 fmt_ftostr (ustrn
, FSTR_MAX
, 4, "f", ulps
);
821 fmt_ftostr (mustrn
, FSTR_MAX
, 4, "f", max_ulp
);
822 printf (" difference: %s %s\n", dstrn
, dstrx
);
823 printf (" ulp : %s\n", ustrn
);
824 printf (" max.ulp : %s\n", mustrn
);
827 update_stats (ok
, TEST_INPUT
);
830 fpstack_test (test_name
);
831 feclearexcept (FE_ALL_EXCEPT
);
833 print_test_end (thisTest
, test_name
, TEST_INPUT
);
838 check_float (const char *test_name
, FLOAT computed
, FLOAT expected
,
841 check_float_internal (test_name
, computed
, expected
,
842 exceptions
, &max_error
, prev_max_error
);
847 check_complex (const char *test_name
, CFLOAT computed
,
851 FLOAT part_comp
, part_exp
;
854 if (asprintf (&str
, "Real part of: %s", test_name
) == -1)
857 part_comp
= __real__ computed
;
858 part_exp
= __real__ expected
;
860 check_float_internal (str
, part_comp
, part_exp
,
861 exception
, &real_max_error
, prev_real_max_error
);
864 if (asprintf (&str
, "Imaginary part of: %s", test_name
) == -1)
867 part_comp
= __imag__ computed
;
868 part_exp
= __imag__ expected
;
870 /* Don't check again for exceptions or errno, just pass through the
871 other relevant flags. */
872 check_float_internal (str
, part_comp
, part_exp
,
873 exception
& (IGNORE_ZERO_INF_SIGN
876 &imag_max_error
, prev_imag_max_error
);
881 /* Check that computed and expected values are equal (int values). */
883 check_int (const char *test_name
, int computed
, int expected
,
887 int errno_value
= errno
;
888 int thisTest
= noTests
;
890 print_test_start (thisTest
, test_name
, TEST_INPUT
);
892 test_exceptions (test_name
, exceptions
);
893 test_errno (test_name
, errno_value
, exceptions
);
894 if (exceptions
& IGNORE_RESULT
)
896 if (computed
== expected
)
899 if (print_screen (ok
))
902 printf ("Failure: ");
903 printf ("Test: %s\n", test_name
);
904 printf ("Result:\n");
905 printf (" is: %d\n", computed
);
906 printf (" should be: %d\n", expected
);
909 update_stats (ok
, TEST_INPUT
);
911 fpstack_test (test_name
);
912 feclearexcept (FE_ALL_EXCEPT
);
914 print_test_end (thisTest
, test_name
, TEST_INPUT
);
918 /* Check that computed and expected values are equal (long int values). */
920 check_long (const char *test_name
, long int computed
, long int expected
,
924 int errno_value
= errno
;
925 int thisTest
= noTests
;
927 print_test_start (thisTest
, test_name
, TEST_INPUT
);
929 test_exceptions (test_name
, exceptions
);
930 test_errno (test_name
, errno_value
, exceptions
);
931 if (exceptions
& IGNORE_RESULT
)
933 if (computed
== expected
)
936 if (print_screen (ok
))
939 printf ("Failure: ");
940 printf ("Test: %s\n", test_name
);
941 printf ("Result:\n");
942 printf (" is: %ld\n", computed
);
943 printf (" should be: %ld\n", expected
);
946 update_stats (ok
, TEST_INPUT
);
948 fpstack_test (test_name
);
949 feclearexcept (FE_ALL_EXCEPT
);
951 print_test_end (thisTest
, test_name
, TEST_INPUT
);
955 /* Check that computed value is true/false. */
957 check_bool (const char *test_name
, int computed
, int expected
,
961 int errno_value
= errno
;
962 int thisTest
= noTests
;
964 print_test_start (thisTest
, test_name
, TEST_INPUT
);
966 test_exceptions (test_name
, exceptions
);
967 test_errno (test_name
, errno_value
, exceptions
);
968 if (exceptions
& IGNORE_RESULT
)
970 if ((computed
== 0) == (expected
== 0))
973 if (print_screen (ok
))
976 printf ("Failure: ");
977 printf ("Test: %s\n", test_name
);
978 printf ("Result:\n");
979 printf (" is: %d\n", computed
);
980 printf (" should be: %d\n", expected
);
983 update_stats (ok
, TEST_INPUT
);
985 fpstack_test (test_name
);
986 feclearexcept (FE_ALL_EXCEPT
);
988 print_test_end (thisTest
, test_name
, TEST_INPUT
);
992 /* check that computed and expected values are equal (long int values) */
994 check_longlong (const char *test_name
, long long int computed
,
995 long long int expected
,
999 int errno_value
= errno
;
1000 int thisTest
= noTests
;
1002 print_test_start (thisTest
, test_name
, TEST_INPUT
);
1004 test_exceptions (test_name
, exceptions
);
1005 test_errno (test_name
, errno_value
, exceptions
);
1006 if (exceptions
& IGNORE_RESULT
)
1008 if (computed
== expected
)
1011 if (print_screen (ok
))
1014 printf ("Failure:");
1015 printf ("Test: %s\n", test_name
);
1016 printf ("Result:\n");
1017 printf (" is: %lld\n", computed
);
1018 printf (" should be: %lld\n", expected
);
1021 update_stats (ok
, TEST_INPUT
);
1023 fpstack_test (test_name
);
1024 feclearexcept (FE_ALL_EXCEPT
);
1026 print_test_end (thisTest
, test_name
, TEST_INPUT
);
1030 /* Check that computed and expected values are equal (intmax_t values). */
1032 check_intmax_t (const char *test_name
, intmax_t computed
,
1033 intmax_t expected
, int exceptions
)
1036 int errno_value
= errno
;
1037 int thisTest
= noTests
;
1039 print_test_start (thisTest
, test_name
, TEST_INPUT
);
1041 test_exceptions (test_name
, exceptions
);
1042 test_errno (test_name
, errno_value
, exceptions
);
1043 if (exceptions
& IGNORE_RESULT
)
1045 if (computed
== expected
)
1048 if (print_screen (ok
))
1051 printf ("Failure:");
1052 printf ("Test: %s\n", test_name
);
1053 printf ("Result:\n");
1054 printf (" is: %jd\n", computed
);
1055 printf (" should be: %jd\n", expected
);
1058 update_stats (ok
, TEST_INPUT
);
1060 fpstack_test (test_name
);
1061 feclearexcept (FE_ALL_EXCEPT
);
1063 print_test_end (thisTest
, test_name
, TEST_INPUT
);
1067 /* Check that computed and expected values are equal (uintmax_t values). */
1069 check_uintmax_t (const char *test_name
, uintmax_t computed
,
1070 uintmax_t expected
, int exceptions
)
1073 int errno_value
= errno
;
1074 int thisTest
= noTests
;
1076 print_test_start (thisTest
, test_name
, TEST_INPUT
);
1078 test_exceptions (test_name
, exceptions
);
1079 test_errno (test_name
, errno_value
, exceptions
);
1080 if (exceptions
& IGNORE_RESULT
)
1082 if (computed
== expected
)
1085 if (print_screen (ok
))
1088 printf ("Failure:");
1089 printf ("Test: %s\n", test_name
);
1090 printf ("Result:\n");
1091 printf (" is: %ju\n", computed
);
1092 printf (" should be: %ju\n", expected
);
1095 update_stats (ok
, TEST_INPUT
);
1097 fpstack_test (test_name
);
1098 feclearexcept (FE_ALL_EXCEPT
);
1100 print_test_end (thisTest
, test_name
, TEST_INPUT
);
1103 /* Return whether a test with flags EXCEPTIONS should be run. */
1105 enable_test (int exceptions
)
1107 if (exceptions
& XFAIL_TEST
)
1109 if ((!SNAN_TESTS (FLOAT
) || !snan_tests_arg
)
1110 && (exceptions
& TEST_SNAN
) != 0)
1112 if (flag_test_mathvec
&& (exceptions
& NO_TEST_MATHVEC
) != 0)
1121 fpstack_test ("start *init*");
1123 /* Clear all exceptions. From now on we must not get random exceptions. */
1124 feclearexcept (FE_ALL_EXCEPT
);
1127 /* Test to make sure we start correctly. */
1128 fpstack_test ("end *init*");
1131 /* Definitions of arguments for argp functions. */
1132 static const struct argp_option options
[] =
1134 { "verbose", 'v', "NUMBER", 0, "Level of verbosity (0..3)"},
1135 { "ulps-file", 'u', NULL
, 0, "Output ulps to file ULPs"},
1136 { "no-max-error", 'f', NULL
, 0,
1137 "Don't output maximal errors of functions"},
1138 { "no-points", 'p', NULL
, 0,
1139 "Don't output results of functions invocations"},
1140 { "ignore-max-ulp", 'i', "yes/no", 0,
1141 "Ignore given maximal errors"},
1142 { "output-dir", 'o', "DIR", 0,
1143 "Directory where generated files will be placed"},
1144 { NULL
, 0, NULL
, 0, NULL
}
1147 /* Prototype for option handler. */
1148 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
1150 /* Data structure to communicate with argp functions. */
1151 static struct argp argp
=
1153 options
, parse_opt
, NULL
, doc
,
1157 /* Handle program arguments. */
1159 parse_opt (int key
, char *arg
, struct argp_state
*state
)
1164 output_max_error
= 0;
1167 if (strcmp (arg
, "yes") == 0)
1169 else if (strcmp (arg
, "no") == 0)
1173 output_dir
= (char *) malloc (strlen (arg
) + 1);
1174 if (output_dir
!= NULL
)
1175 strcpy (output_dir
, arg
);
1188 char *optstr_conv
= optarg
;
1189 unsigned int opt_verbose
;
1191 opt_verbose
= (unsigned int) strtoul (optarg
, &optstr_conv
, 0);
1192 if (*optstr_conv
== '\0' && optstr_conv
!= optarg
)
1193 verbose
= opt_verbose
;
1199 return ARGP_ERR_UNKNOWN
;
1204 /* Verify that our ulp () implementation is behaving as expected
1209 FLOAT ulps
, ulpx
, value
;
1211 /* Check ulp of zero is a subnormal value... */
1212 ulps
= ulp (0x0.0p0
);
1213 if (fpclassify (ulps
) != FP_SUBNORMAL
)
1215 fprintf (stderr
, "ulp (0x0.0p0) is not FP_SUBNORMAL!\n");
1216 exit (EXIT_FAILURE
);
1218 /* Check that the ulp of one is a normal value... */
1219 ulps
= ulp (LIT(1.0));
1220 if (fpclassify (ulps
) != FP_NORMAL
)
1222 fprintf (stderr
, "ulp (1.0L) is not FP_NORMAL\n");
1223 exit (EXIT_FAILURE
);
1226 /* Compute the next subnormal value using nextafter to validate ulp.
1227 We allow +/- 1 ulp around the represented value. */
1228 value
= FUNC(nextafter
) (0, 1);
1229 ulps
= ULPDIFF (value
, 0);
1230 ulpx
= ulp (LIT(1.0));
1231 if (ulps
< (LIT(1.0) - ulpx
) || ulps
> (LIT(1.0) + ulpx
))
1233 fprintf (stderr
, "Value outside of 1 +/- 1ulp.\n");
1234 exit (EXIT_FAILURE
);
1236 /* Compute the nearest representable number from 10 towards 20.
1237 The result is 10 + 1ulp. We use this to check the ulp function.
1238 We allow +/- 1 ulp around the represented value. */
1239 value
= FUNC(nextafter
) (10, 20);
1240 ulps
= ULPDIFF (value
, 10);
1241 ulpx
= ulp (LIT(1.0));
1242 if (ulps
< (LIT(1.0) - ulpx
) || ulps
> (LIT(1.0) + ulpx
))
1244 fprintf (stderr
, "Value outside of 1 +/- 1ulp.\n");
1245 exit (EXIT_FAILURE
);
1247 /* This gives one more ulp. */
1248 value
= FUNC(nextafter
) (value
, 20);
1249 ulps
= ULPDIFF (value
, 10);
1250 ulpx
= ulp (LIT(2.0));
1251 if (ulps
< (LIT(2.0) - ulpx
) || ulps
> (LIT(2.0) + ulpx
))
1253 fprintf (stderr
, "Value outside of 2 +/- 1ulp.\n");
1254 exit (EXIT_FAILURE
);
1256 /* And now calculate 100 ulp. */
1257 for (i
= 2; i
< 100; i
++)
1258 value
= FUNC(nextafter
) (value
, 20);
1259 ulps
= ULPDIFF (value
, 10);
1260 ulpx
= ulp (LIT(100.0));
1261 if (ulps
< (LIT(100.0) - ulpx
) || ulps
> (LIT(100.0) + ulpx
))
1263 fprintf (stderr
, "Value outside of 100 +/- 1ulp.\n");
1264 exit (EXIT_FAILURE
);
1268 /* Do all initialization for a test run with arguments given by ARGC
1271 libm_test_init (int argc
, char **argv
)
1274 char *ulps_file_path
;
1276 char *envstr_verbose
;
1280 output_max_error
= 1;
1283 /* XXX set to 0 for releases. */
1286 envstr_verbose
= getenv("GLIBC_TEST_LIBM_VERBOSE");
1287 if (envstr_verbose
!= NULL
)
1289 char *envstr_conv
= envstr_verbose
;
1290 unsigned int env_verbose
;
1292 env_verbose
= (unsigned int) strtoul (envstr_verbose
, &envstr_conv
, 0);
1293 if (*envstr_conv
== '\0' && envstr_conv
!= envstr_verbose
)
1294 verbose
= env_verbose
;
1297 /* Parse and process arguments. */
1298 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
1300 if (remaining
!= argc
)
1302 fprintf (stderr
, "wrong number of arguments");
1303 argp_help (&argp
, stdout
, ARGP_HELP_SEE
, program_invocation_short_name
);
1304 exit (EXIT_FAILURE
);
1309 if (output_dir
!= NULL
)
1310 dir_len
= strlen (output_dir
);
1311 ulps_file_path
= (char *) malloc (dir_len
+ strlen (ulps_file_name
) + 1);
1312 if (ulps_file_path
== NULL
)
1314 perror ("can't allocate path for `ULPs' file: ");
1317 sprintf (ulps_file_path
, "%s%s", output_dir
== NULL
? "" : output_dir
, ulps_file_name
);
1318 ulps_file
= fopen (ulps_file_path
, "a");
1319 if (ulps_file
== NULL
)
1321 perror ("can't open file `ULPs' for writing: ");
1328 fputs (test_msg
, stdout
);
1333 /* Process the test results, returning the exit status. */
1335 libm_test_finish (void)
1340 printf ("\nTest suite completed:\n");
1341 printf (" Maximum error found of ");
1344 char rmestr
[FSTR_MAX
];
1345 char imestr
[FSTR_MAX
];
1346 FTOSTR (rmestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (real_max_error
));
1347 FTOSTR (imestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (imag_max_error
));
1348 printf ("`%s' ulp for real part and `%s' ulp for imaginary part\n",
1353 char mestr
[FSTR_MAX
];
1354 FTOSTR (mestr
, FSTR_MAX
, "%.0f", FUNC (ceil
) (max_error
));
1355 printf ("`%s' ulp\n", mestr
);
1357 printf (" %d max error test cases,\n", noMaxErrorTests
);
1358 printf (" %d input tests,\n", noTests
);
1359 printf (" - with %d tests for exception flags,\n", noExcTests
);
1360 printf (" - with %d tests for errno executed.\n", noErrnoTests
);
1364 printf (" %d errors occurred.\n", noErrors
);
1367 printf (" All tests passed successfully.\n");