xref: /freebsd/lib/msun/tests/test-utils.h (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1 /*-
2  * Copyright (c) 2005-2013 David Schultz <das@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef	_TEST_UTILS_H_
30 #define	_TEST_UTILS_H_
31 
32 #include <complex.h>
33 #include <fenv.h>
34 #include <float.h>
35 
36 #include <atf-c.h>
37 
38 /*
39  * Implementations are permitted to define additional exception flags
40  * not specified in the standard, so it is not necessarily true that
41  * FE_ALL_EXCEPT == ALL_STD_EXCEPT.
42  */
43 #define	ALL_STD_EXCEPT	(FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
44 			 FE_OVERFLOW | FE_UNDERFLOW)
45 #define	OPT_INVALID	(ALL_STD_EXCEPT & ~FE_INVALID)
46 #define	OPT_INEXACT	(ALL_STD_EXCEPT & ~FE_INEXACT)
47 #define	FLT_ULP()	ldexpl(1.0, 1 - FLT_MANT_DIG)
48 #define	DBL_ULP()	ldexpl(1.0, 1 - DBL_MANT_DIG)
49 #define	LDBL_ULP()	ldexpl(1.0, 1 - LDBL_MANT_DIG)
50 
51 /*
52  * Flags that control the behavior of various fpequal* functions.
53  * XXX This is messy due to merging various notions of "close enough"
54  * that are best suited for different functions.
55  *
56  * CS_REAL
57  * CS_IMAG
58  * CS_BOTH
59  *   (cfpequal_cs, fpequal_tol, cfpequal_tol) Whether to check the sign of
60  *   the real part of the result, the imaginary part, or both.
61  *
62  * FPE_ABS_ZERO
63  *   (fpequal_tol, cfpequal_tol) If set, treats the tolerance as an absolute
64  *   tolerance when the expected value is 0.  This is useful when there is
65  *   round-off error in the input, e.g., cos(Pi/2) ~= 0.
66  */
67 #define	CS_REAL		0x01
68 #define	CS_IMAG		0x02
69 #define	CS_BOTH		(CS_REAL | CS_IMAG)
70 #define	FPE_ABS_ZERO	0x04
71 
72 #ifdef	DEBUG
73 #define	debug(...)	printf(__VA_ARGS__)
74 #else
75 #define	debug(...)	(void)0
76 #endif
77 
78 /*
79  * XXX The ancient version of gcc in the base system doesn't support CMPLXL,
80  * but we can fake it most of the time.
81  */
82 #ifndef CMPLXL
83 static inline long double complex
84 CMPLXL(long double x, long double y)
85 {
86 	long double complex z;
87 
88 	__real__ z = x;
89 	__imag__ z = y;
90 	return (z);
91 }
92 #endif
93 
94 /*
95  * The compiler-rt fp128 builtins do not update FP exceptions.
96  * See https://llvm.org/PR34126
97  */
98 
99 static int	cfpequal(long double complex, long double complex) __used;
100 
101 /*
102  * Determine whether x and y are equal, with two special rules:
103  *	+0.0 != -0.0
104  *	 NaN == NaN
105  * If checksign is false, we compare the absolute values instead.
106  */
107 static inline int
108 fpequal_cs(long double x, long double y, bool checksign)
109 {
110 	if (isnan(x) && isnan(y))
111 		return (1);
112 	if (checksign)
113 		return (x == y && !signbit(x) == !signbit(y));
114 	else
115 		return (fabsl(x) == fabsl(y));
116 }
117 
118 static inline int
119 fpequal_tol(long double x, long double y, long double tol,
120     unsigned int flags)
121 {
122 	fenv_t env;
123 	int ret;
124 
125 	if (isnan(x) && isnan(y))
126 		return (1);
127 	if (!signbit(x) != !signbit(y) && (flags & CS_BOTH))
128 		return (0);
129 	if (x == y)
130 		return (1);
131 	if (tol == 0)
132 		return (0);
133 
134 	/* Hard case: need to check the tolerance. */
135 	feholdexcept(&env);
136 	/*
137 	 * For our purposes here, if y=0, we interpret tol as an absolute
138 	 * tolerance. This is to account for roundoff in the input, e.g.,
139 	 * cos(Pi/2) ~= 0.
140 	 */
141 	if ((flags & FPE_ABS_ZERO) && y == 0.0)
142 		ret = fabsl(x - y) <= fabsl(tol);
143 	else
144 		ret = fabsl(x - y) <= fabsl(y * tol);
145 	fesetenv(&env);
146 	return (ret);
147 }
148 
149 #define CHECK_FPEQUAL(x, y) CHECK_FPEQUAL_CS(x, y, true)
150 
151 #define CHECK_FPEQUAL_CS(x, y, checksign) do {					\
152 	long double _x = x;							\
153 	long double _y = y;							\
154 	ATF_CHECK_MSG(fpequal_cs(_x, _y, checksign),				\
155 	    "%s (%.25Lg) ~= %s (%.25Lg)", #x, _x, #y, _y);			\
156 } while (0)
157 
158 #define CHECK_FPEQUAL_TOL(x, y, tol, flags) do {				\
159 	long double _x = x;							\
160 	long double _y = y;							\
161 	bool eq = fpequal_tol(_x, _y, tol, flags);				\
162 	long double _diff = eq ? 0.0L : fabsl(_x - _y);				\
163 	ATF_CHECK_MSG(eq, "%s (%.25Lg) ~= %s (%.25Lg), diff=%Lg, maxdiff=%Lg,",	\
164 	    #x, _x, #y, _y, _diff, fabsl(_y * tol));				\
165 } while (0)
166 
167 static inline int
168 cfpequal(long double complex d1, long double complex d2)
169 {
170 
171 	return (fpequal_cs(creall(d1), creall(d2), true) &&
172 	    fpequal_cs(cimagl(d1), cimagl(d2), true));
173 }
174 
175 #define CHECK_CFPEQUAL_CS(x, y, checksign) do {					\
176 	long double _x = x;							\
177 	long double _y = y;							\
178 	bool equal_cs =								\
179 	    fpequal_cs(creal(_x), creal(_y), (checksign & CS_REAL) != 0) &&	\
180 	    fpequal_cs(cimag(_x), cimag(_y), (checksign & CS_IMAG) != 0);	\
181 	ATF_CHECK_MSG(equal_cs, "%s (%Lg + %Lg I) ~=  %s (%Lg + %Lg I)",	\
182 	    #x, creall(_x), cimagl(_x), #y, creall(_y), cimagl(_y));		\
183 } while (0)
184 
185 #define CHECK_CFPEQUAL_TOL(x, y, tol, flags) do {				\
186 	long double _x = x;							\
187 	long double _y = y;							\
188 	bool equal_tol = (fpequal_tol(creal(_x), creal(_y), tol, flags) &&	\
189 	    fpequal_tol(cimag(_x), cimag(_y), tol, flags));			\
190 	ATF_CHECK_MSG(equal_tol, "%s (%Lg + %Lg I) ~=  %s (%Lg + %Lg I)",	\
191 	    #x, creall(_x), cimagl(_x), #y, creall(_y), cimagl(_y));		\
192 } while (0)
193 
194 #define CHECK_FP_EXCEPTIONS(excepts, exceptmask)		\
195 	ATF_CHECK_EQ_MSG((excepts), fetestexcept(exceptmask),	\
196 	    "unexpected exception flags: got %#x not %#x",	\
197 	    fetestexcept(exceptmask), (excepts))
198 #define CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, fmt, ...)	\
199 	ATF_CHECK_EQ_MSG((excepts), fetestexcept(exceptmask),	\
200 	    "unexpected exception flags: got %#x not %#x " fmt,	\
201 	    fetestexcept(exceptmask), (excepts), __VA_ARGS__)
202 
203 #endif /* _TEST_UTILS_H_ */
204