xref: /freebsd/contrib/netbsd-tests/lib/libm/t_libm.h (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1 /* $NetBSD: t_libm.h,v 1.7 2018/11/07 03:59:36 riastradh Exp $ */
2 
3 /*
4  * Check result of fn(arg) is correct within the bounds.
5  * Should be ok to do the checks using 'double' for 'float' functions.
6  * On i386 float and double values are returned on the x87 stack and might
7  * be out of range for the function - so save and print as 'long double'.
8  * (otherwise you can get 'inf != inf' reported!)
9  */
10 #define T_LIBM_CHECK(subtest, fn, arg, expect_, epsilon_) do { \
11 	long double epsilon = epsilon_; \
12 	long double expect = expect_; \
13 	long double r = fn(arg); \
14 	long double e = fabsl((r - expect)/expect); \
15 	if (!(r == expect || e <= epsilon)) \
16 		atf_tc_fail_nonfatal( \
17 		    "subtest %u: " #fn "(%g) is %Lg (%.14La) " \
18 		    "not %Lg (%.13La), error %Lg (%.6La) > %Lg", \
19 		    subtest, arg, r, r, expect, expect, e, e, epsilon); \
20     } while (0)
21 
22 /* Check that the result of fn(arg) is NaN */
23 #ifndef __vax__
24 #define T_LIBM_CHECK_NAN(subtest, fn, arg) do { \
25 	double r = fn(arg); \
26 	if (!isnan(r)) \
27 		atf_tc_fail_nonfatal("subtest %u: " #fn "(%g) is %g not NaN", \
28 		    subtest, arg, r); \
29     } while (0)
30 #else
31 /* vax doesn't support NaN */
32 #define T_LIBM_CHECK_NAN(subtest, fn, arg) (void)(arg)
33 #endif
34 
35 /* Check that the result of fn(arg) is +0.0 */
36 #define T_LIBM_CHECK_PLUS_ZERO(subtest, fn, arg) do { \
37 	double r = fn(arg); \
38 	if (fabs(r) > 0.0 || signbit(r) != 0) \
39 		atf_tc_fail_nonfatal("subtest %u: " #fn "(%g) is %g not +0.0", \
40 		    subtest, arg, r); \
41     } while (0)
42 
43 /* Check that the result of fn(arg) is -0.0 */
44 #define T_LIBM_CHECK_MINUS_ZERO(subtest, fn, arg) do { \
45 	double r = fn(arg); \
46 	if (fabs(r) > 0.0 || signbit(r) == 0) \
47 		atf_tc_fail_nonfatal("subtest %u: " #fn "(%g) is %g not -0.0", \
48 		    subtest, arg, r); \
49     } while (0)
50 
51 /* Some useful constants (for test vectors) */
52 #ifndef __vax__	/* no NAN nor +/- INF on vax */
53 #define T_LIBM_NAN	(0.0 / 0.0)
54 #define T_LIBM_PLUS_INF	(+1.0 / 0.0)
55 #define T_LIBM_MINUS_INF (-1.0 / 0.0)
56 #endif
57 
58 /* One line definition of a simple test */
59 #define ATF_LIBM_TEST(name, description) \
60 ATF_TC(name); \
61 ATF_TC_HEAD(name, tc) { atf_tc_set_md_var(tc, "descr", description); } \
62 ATF_TC_BODY(name, tc)
63