xref: /freebsd-src/lib/msun/tests/test-utils.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
18a7d0e8cSEnji Cooper /*-
28a7d0e8cSEnji Cooper  * Copyright (c) 2005-2013 David Schultz <das@FreeBSD.org>
38a7d0e8cSEnji Cooper  * All rights reserved.
48a7d0e8cSEnji Cooper  *
58a7d0e8cSEnji Cooper  * Redistribution and use in source and binary forms, with or without
68a7d0e8cSEnji Cooper  * modification, are permitted provided that the following conditions
78a7d0e8cSEnji Cooper  * are met:
88a7d0e8cSEnji Cooper  * 1. Redistributions of source code must retain the above copyright
98a7d0e8cSEnji Cooper  *    notice, this list of conditions and the following disclaimer.
108a7d0e8cSEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
118a7d0e8cSEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
128a7d0e8cSEnji Cooper  *    documentation and/or other materials provided with the distribution.
138a7d0e8cSEnji Cooper  *
148a7d0e8cSEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158a7d0e8cSEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168a7d0e8cSEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178a7d0e8cSEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188a7d0e8cSEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198a7d0e8cSEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208a7d0e8cSEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218a7d0e8cSEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228a7d0e8cSEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238a7d0e8cSEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248a7d0e8cSEnji Cooper  * SUCH DAMAGE.
258a7d0e8cSEnji Cooper  */
268a7d0e8cSEnji Cooper 
278a7d0e8cSEnji Cooper #ifndef	_TEST_UTILS_H_
288a7d0e8cSEnji Cooper #define	_TEST_UTILS_H_
298a7d0e8cSEnji Cooper 
308a7d0e8cSEnji Cooper #include <complex.h>
318a7d0e8cSEnji Cooper #include <fenv.h>
32*b424e003SAlex Richardson #include <float.h>
338a7d0e8cSEnji Cooper 
34133bc645SAlex Richardson #include <atf-c.h>
35133bc645SAlex Richardson 
368a7d0e8cSEnji Cooper /*
378a7d0e8cSEnji Cooper  * Implementations are permitted to define additional exception flags
388a7d0e8cSEnji Cooper  * not specified in the standard, so it is not necessarily true that
398a7d0e8cSEnji Cooper  * FE_ALL_EXCEPT == ALL_STD_EXCEPT.
408a7d0e8cSEnji Cooper  */
418a7d0e8cSEnji Cooper #define	ALL_STD_EXCEPT	(FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
428a7d0e8cSEnji Cooper 			 FE_OVERFLOW | FE_UNDERFLOW)
438a7d0e8cSEnji Cooper #define	OPT_INVALID	(ALL_STD_EXCEPT & ~FE_INVALID)
448a7d0e8cSEnji Cooper #define	OPT_INEXACT	(ALL_STD_EXCEPT & ~FE_INEXACT)
458a7d0e8cSEnji Cooper #define	FLT_ULP()	ldexpl(1.0, 1 - FLT_MANT_DIG)
468a7d0e8cSEnji Cooper #define	DBL_ULP()	ldexpl(1.0, 1 - DBL_MANT_DIG)
478a7d0e8cSEnji Cooper #define	LDBL_ULP()	ldexpl(1.0, 1 - LDBL_MANT_DIG)
488a7d0e8cSEnji Cooper 
498a7d0e8cSEnji Cooper /*
508a7d0e8cSEnji Cooper  * Flags that control the behavior of various fpequal* functions.
518a7d0e8cSEnji Cooper  * XXX This is messy due to merging various notions of "close enough"
528a7d0e8cSEnji Cooper  * that are best suited for different functions.
538a7d0e8cSEnji Cooper  *
548a7d0e8cSEnji Cooper  * CS_REAL
558a7d0e8cSEnji Cooper  * CS_IMAG
568a7d0e8cSEnji Cooper  * CS_BOTH
578a7d0e8cSEnji Cooper  *   (cfpequal_cs, fpequal_tol, cfpequal_tol) Whether to check the sign of
588a7d0e8cSEnji Cooper  *   the real part of the result, the imaginary part, or both.
598a7d0e8cSEnji Cooper  *
608a7d0e8cSEnji Cooper  * FPE_ABS_ZERO
618a7d0e8cSEnji Cooper  *   (fpequal_tol, cfpequal_tol) If set, treats the tolerance as an absolute
628a7d0e8cSEnji Cooper  *   tolerance when the expected value is 0.  This is useful when there is
638a7d0e8cSEnji Cooper  *   round-off error in the input, e.g., cos(Pi/2) ~= 0.
648a7d0e8cSEnji Cooper  */
658a7d0e8cSEnji Cooper #define	CS_REAL		0x01
668a7d0e8cSEnji Cooper #define	CS_IMAG		0x02
678a7d0e8cSEnji Cooper #define	CS_BOTH		(CS_REAL | CS_IMAG)
688a7d0e8cSEnji Cooper #define	FPE_ABS_ZERO	0x04
698a7d0e8cSEnji Cooper 
708a7d0e8cSEnji Cooper #ifdef	DEBUG
718a7d0e8cSEnji Cooper #define	debug(...)	printf(__VA_ARGS__)
728a7d0e8cSEnji Cooper #else
738a7d0e8cSEnji Cooper #define	debug(...)	(void)0
748a7d0e8cSEnji Cooper #endif
758a7d0e8cSEnji Cooper 
768a7d0e8cSEnji Cooper /*
778a7d0e8cSEnji Cooper  * XXX The ancient version of gcc in the base system doesn't support CMPLXL,
788a7d0e8cSEnji Cooper  * but we can fake it most of the time.
798a7d0e8cSEnji Cooper  */
808a7d0e8cSEnji Cooper #ifndef CMPLXL
818a7d0e8cSEnji Cooper static inline long double complex
CMPLXL(long double x,long double y)828a7d0e8cSEnji Cooper CMPLXL(long double x, long double y)
838a7d0e8cSEnji Cooper {
848a7d0e8cSEnji Cooper 	long double complex z;
858a7d0e8cSEnji Cooper 
868a7d0e8cSEnji Cooper 	__real__ z = x;
878a7d0e8cSEnji Cooper 	__imag__ z = y;
888a7d0e8cSEnji Cooper 	return (z);
898a7d0e8cSEnji Cooper }
908a7d0e8cSEnji Cooper #endif
918a7d0e8cSEnji Cooper 
928a7d0e8cSEnji Cooper /*
93*b424e003SAlex Richardson  * The compiler-rt fp128 builtins do not update FP exceptions.
94*b424e003SAlex Richardson  * See https://llvm.org/PR34126
958a7d0e8cSEnji Cooper  */
968a7d0e8cSEnji Cooper 
97*b424e003SAlex Richardson static int	cfpequal(long double complex, long double complex) __used;
988a7d0e8cSEnji Cooper 
998a7d0e8cSEnji Cooper /*
1008a7d0e8cSEnji Cooper  * Determine whether x and y are equal, with two special rules:
1018a7d0e8cSEnji Cooper  *	+0.0 != -0.0
1028a7d0e8cSEnji Cooper  *	 NaN == NaN
103*b424e003SAlex Richardson  * If checksign is false, we compare the absolute values instead.
1048a7d0e8cSEnji Cooper  */
105*b424e003SAlex Richardson static inline int
fpequal_cs(long double x,long double y,bool checksign)106*b424e003SAlex Richardson fpequal_cs(long double x, long double y, bool checksign)
1078a7d0e8cSEnji Cooper {
1088a7d0e8cSEnji Cooper 	if (isnan(x) && isnan(y))
1098a7d0e8cSEnji Cooper 		return (1);
1108a7d0e8cSEnji Cooper 	if (checksign)
1118a7d0e8cSEnji Cooper 		return (x == y && !signbit(x) == !signbit(y));
1128a7d0e8cSEnji Cooper 	else
1138a7d0e8cSEnji Cooper 		return (fabsl(x) == fabsl(y));
1148a7d0e8cSEnji Cooper }
1158a7d0e8cSEnji Cooper 
116*b424e003SAlex Richardson static inline int
fpequal_tol(long double x,long double y,long double tol,unsigned int flags)117abe427afSEnji Cooper fpequal_tol(long double x, long double y, long double tol,
118abe427afSEnji Cooper     unsigned int flags)
1198a7d0e8cSEnji Cooper {
1208a7d0e8cSEnji Cooper 	fenv_t env;
1218a7d0e8cSEnji Cooper 	int ret;
1228a7d0e8cSEnji Cooper 
1238a7d0e8cSEnji Cooper 	if (isnan(x) && isnan(y))
1248a7d0e8cSEnji Cooper 		return (1);
1258a7d0e8cSEnji Cooper 	if (!signbit(x) != !signbit(y) && (flags & CS_BOTH))
1268a7d0e8cSEnji Cooper 		return (0);
1278a7d0e8cSEnji Cooper 	if (x == y)
1288a7d0e8cSEnji Cooper 		return (1);
1298a7d0e8cSEnji Cooper 	if (tol == 0)
1308a7d0e8cSEnji Cooper 		return (0);
1318a7d0e8cSEnji Cooper 
1328a7d0e8cSEnji Cooper 	/* Hard case: need to check the tolerance. */
1338a7d0e8cSEnji Cooper 	feholdexcept(&env);
1348a7d0e8cSEnji Cooper 	/*
1358a7d0e8cSEnji Cooper 	 * For our purposes here, if y=0, we interpret tol as an absolute
1368a7d0e8cSEnji Cooper 	 * tolerance. This is to account for roundoff in the input, e.g.,
1378a7d0e8cSEnji Cooper 	 * cos(Pi/2) ~= 0.
1388a7d0e8cSEnji Cooper 	 */
1398a7d0e8cSEnji Cooper 	if ((flags & FPE_ABS_ZERO) && y == 0.0)
1408a7d0e8cSEnji Cooper 		ret = fabsl(x - y) <= fabsl(tol);
1418a7d0e8cSEnji Cooper 	else
1428a7d0e8cSEnji Cooper 		ret = fabsl(x - y) <= fabsl(y * tol);
1438a7d0e8cSEnji Cooper 	fesetenv(&env);
1448a7d0e8cSEnji Cooper 	return (ret);
1458a7d0e8cSEnji Cooper }
1468a7d0e8cSEnji Cooper 
147*b424e003SAlex Richardson #define CHECK_FPEQUAL(x, y) CHECK_FPEQUAL_CS(x, y, true)
148*b424e003SAlex Richardson 
149*b424e003SAlex Richardson #define CHECK_FPEQUAL_CS(x, y, checksign) do {					\
150*b424e003SAlex Richardson 	long double _x = x;							\
151*b424e003SAlex Richardson 	long double _y = y;							\
152*b424e003SAlex Richardson 	ATF_CHECK_MSG(fpequal_cs(_x, _y, checksign),				\
153*b424e003SAlex Richardson 	    "%s (%.25Lg) ~= %s (%.25Lg)", #x, _x, #y, _y);			\
154*b424e003SAlex Richardson } while (0)
155*b424e003SAlex Richardson 
156*b424e003SAlex Richardson #define CHECK_FPEQUAL_TOL(x, y, tol, flags) do {				\
157*b424e003SAlex Richardson 	long double _x = x;							\
158*b424e003SAlex Richardson 	long double _y = y;							\
159*b424e003SAlex Richardson 	bool eq = fpequal_tol(_x, _y, tol, flags);				\
160*b424e003SAlex Richardson 	long double _diff = eq ? 0.0L : fabsl(_x - _y);				\
161*b424e003SAlex Richardson 	ATF_CHECK_MSG(eq, "%s (%.25Lg) ~= %s (%.25Lg), diff=%Lg, maxdiff=%Lg,",	\
162*b424e003SAlex Richardson 	    #x, _x, #y, _y, _diff, fabsl(_y * tol));				\
163*b424e003SAlex Richardson } while (0)
164*b424e003SAlex Richardson 
165*b424e003SAlex Richardson static inline int
cfpequal(long double complex d1,long double complex d2)1668a7d0e8cSEnji Cooper cfpequal(long double complex d1, long double complex d2)
1678a7d0e8cSEnji Cooper {
1688a7d0e8cSEnji Cooper 
169*b424e003SAlex Richardson 	return (fpequal_cs(creall(d1), creall(d2), true) &&
170*b424e003SAlex Richardson 	    fpequal_cs(cimagl(d1), cimagl(d2), true));
1718a7d0e8cSEnji Cooper }
1728a7d0e8cSEnji Cooper 
173*b424e003SAlex Richardson #define CHECK_CFPEQUAL_CS(x, y, checksign) do {					\
174*b424e003SAlex Richardson 	long double _x = x;							\
175*b424e003SAlex Richardson 	long double _y = y;							\
176*b424e003SAlex Richardson 	bool equal_cs =								\
177*b424e003SAlex Richardson 	    fpequal_cs(creal(_x), creal(_y), (checksign & CS_REAL) != 0) &&	\
178*b424e003SAlex Richardson 	    fpequal_cs(cimag(_x), cimag(_y), (checksign & CS_IMAG) != 0);	\
179*b424e003SAlex Richardson 	ATF_CHECK_MSG(equal_cs, "%s (%Lg + %Lg I) ~=  %s (%Lg + %Lg I)",	\
180*b424e003SAlex Richardson 	    #x, creall(_x), cimagl(_x), #y, creall(_y), cimagl(_y));		\
181*b424e003SAlex Richardson } while (0)
1828a7d0e8cSEnji Cooper 
183*b424e003SAlex Richardson #define CHECK_CFPEQUAL_TOL(x, y, tol, flags) do {				\
184*b424e003SAlex Richardson 	long double _x = x;							\
185*b424e003SAlex Richardson 	long double _y = y;							\
186*b424e003SAlex Richardson 	bool equal_tol = (fpequal_tol(creal(_x), creal(_y), tol, flags) &&	\
187*b424e003SAlex Richardson 	    fpequal_tol(cimag(_x), cimag(_y), tol, flags));			\
188*b424e003SAlex Richardson 	ATF_CHECK_MSG(equal_tol, "%s (%Lg + %Lg I) ~=  %s (%Lg + %Lg I)",	\
189*b424e003SAlex Richardson 	    #x, creall(_x), cimagl(_x), #y, creall(_y), cimagl(_y));		\
190*b424e003SAlex Richardson } while (0)
1918a7d0e8cSEnji Cooper 
192133bc645SAlex Richardson #define CHECK_FP_EXCEPTIONS(excepts, exceptmask)		\
193133bc645SAlex Richardson 	ATF_CHECK_EQ_MSG((excepts), fetestexcept(exceptmask),	\
194*b424e003SAlex Richardson 	    "unexpected exception flags: got %#x not %#x",	\
195133bc645SAlex Richardson 	    fetestexcept(exceptmask), (excepts))
196133bc645SAlex Richardson #define CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, fmt, ...)	\
197133bc645SAlex Richardson 	ATF_CHECK_EQ_MSG((excepts), fetestexcept(exceptmask),	\
198133bc645SAlex Richardson 	    "unexpected exception flags: got %#x not %#x " fmt,	\
199133bc645SAlex Richardson 	    fetestexcept(exceptmask), (excepts), __VA_ARGS__)
200133bc645SAlex Richardson 
2018a7d0e8cSEnji Cooper #endif /* _TEST_UTILS_H_ */
202