xref: /freebsd/lib/msun/tests/test-utils.h (revision e0c4386e)
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 
27 #ifndef	_TEST_UTILS_H_
28 #define	_TEST_UTILS_H_
29 
30 #include <complex.h>
31 #include <fenv.h>
32 #include <float.h>
33 
34 #include <atf-c.h>
35 
36 /*
37  * Implementations are permitted to define additional exception flags
38  * not specified in the standard, so it is not necessarily true that
39  * FE_ALL_EXCEPT == ALL_STD_EXCEPT.
40  */
41 #define	ALL_STD_EXCEPT	(FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
42 			 FE_OVERFLOW | FE_UNDERFLOW)
43 #define	OPT_INVALID	(ALL_STD_EXCEPT & ~FE_INVALID)
44 #define	OPT_INEXACT	(ALL_STD_EXCEPT & ~FE_INEXACT)
45 #define	FLT_ULP()	ldexpl(1.0, 1 - FLT_MANT_DIG)
46 #define	DBL_ULP()	ldexpl(1.0, 1 - DBL_MANT_DIG)
47 #define	LDBL_ULP()	ldexpl(1.0, 1 - LDBL_MANT_DIG)
48 
49 /*
50  * Flags that control the behavior of various fpequal* functions.
51  * XXX This is messy due to merging various notions of "close enough"
52  * that are best suited for different functions.
53  *
54  * CS_REAL
55  * CS_IMAG
56  * CS_BOTH
57  *   (cfpequal_cs, fpequal_tol, cfpequal_tol) Whether to check the sign of
58  *   the real part of the result, the imaginary part, or both.
59  *
60  * FPE_ABS_ZERO
61  *   (fpequal_tol, cfpequal_tol) If set, treats the tolerance as an absolute
62  *   tolerance when the expected value is 0.  This is useful when there is
63  *   round-off error in the input, e.g., cos(Pi/2) ~= 0.
64  */
65 #define	CS_REAL		0x01
66 #define	CS_IMAG		0x02
67 #define	CS_BOTH		(CS_REAL | CS_IMAG)
68 #define	FPE_ABS_ZERO	0x04
69 
70 #ifdef	DEBUG
71 #define	debug(...)	printf(__VA_ARGS__)
72 #else
73 #define	debug(...)	(void)0
74 #endif
75 
76 /*
77  * XXX The ancient version of gcc in the base system doesn't support CMPLXL,
78  * but we can fake it most of the time.
79  */
80 #ifndef CMPLXL
81 static inline long double complex
82 CMPLXL(long double x, long double y)
83 {
84 	long double complex z;
85 
86 	__real__ z = x;
87 	__imag__ z = y;
88 	return (z);
89 }
90 #endif
91 
92 /*
93  * The compiler-rt fp128 builtins do not update FP exceptions.
94  * See https://llvm.org/PR34126
95  */
96 
97 static int	cfpequal(long double complex, long double complex) __used;
98 
99 /*
100  * Determine whether x and y are equal, with two special rules:
101  *	+0.0 != -0.0
102  *	 NaN == NaN
103  * If checksign is false, we compare the absolute values instead.
104  */
105 static inline int
106 fpequal_cs(long double x, long double y, bool checksign)
107 {
108 	if (isnan(x) && isnan(y))
109 		return (1);
110 	if (checksign)
111 		return (x == y && !signbit(x) == !signbit(y));
112 	else
113 		return (fabsl(x) == fabsl(y));
114 }
115 
116 static inline int
117 fpequal_tol(long double x, long double y, long double tol,
118     unsigned int flags)
119 {
120 	fenv_t env;
121 	int ret;
122 
123 	if (isnan(x) && isnan(y))
124 		return (1);
125 	if (!signbit(x) != !signbit(y) && (flags & CS_BOTH))
126 		return (0);
127 	if (x == y)
128 		return (1);
129 	if (tol == 0)
130 		return (0);
131 
132 	/* Hard case: need to check the tolerance. */
133 	feholdexcept(&env);
134 	/*
135 	 * For our purposes here, if y=0, we interpret tol as an absolute
136 	 * tolerance. This is to account for roundoff in the input, e.g.,
137 	 * cos(Pi/2) ~= 0.
138 	 */
139 	if ((flags & FPE_ABS_ZERO) && y == 0.0)
140 		ret = fabsl(x - y) <= fabsl(tol);
141 	else
142 		ret = fabsl(x - y) <= fabsl(y * tol);
143 	fesetenv(&env);
144 	return (ret);
145 }
146 
147 #define CHECK_FPEQUAL(x, y) CHECK_FPEQUAL_CS(x, y, true)
148 
149 #define CHECK_FPEQUAL_CS(x, y, checksign) do {					\
150 	long double _x = x;							\
151 	long double _y = y;							\
152 	ATF_CHECK_MSG(fpequal_cs(_x, _y, checksign),				\
153 	    "%s (%.25Lg) ~= %s (%.25Lg)", #x, _x, #y, _y);			\
154 } while (0)
155 
156 #define CHECK_FPEQUAL_TOL(x, y, tol, flags) do {				\
157 	long double _x = x;							\
158 	long double _y = y;							\
159 	bool eq = fpequal_tol(_x, _y, tol, flags);				\
160 	long double _diff = eq ? 0.0L : fabsl(_x - _y);				\
161 	ATF_CHECK_MSG(eq, "%s (%.25Lg) ~= %s (%.25Lg), diff=%Lg, maxdiff=%Lg,",	\
162 	    #x, _x, #y, _y, _diff, fabsl(_y * tol));				\
163 } while (0)
164 
165 static inline int
166 cfpequal(long double complex d1, long double complex d2)
167 {
168 
169 	return (fpequal_cs(creall(d1), creall(d2), true) &&
170 	    fpequal_cs(cimagl(d1), cimagl(d2), true));
171 }
172 
173 #define CHECK_CFPEQUAL_CS(x, y, checksign) do {					\
174 	long double _x = x;							\
175 	long double _y = y;							\
176 	bool equal_cs =								\
177 	    fpequal_cs(creal(_x), creal(_y), (checksign & CS_REAL) != 0) &&	\
178 	    fpequal_cs(cimag(_x), cimag(_y), (checksign & CS_IMAG) != 0);	\
179 	ATF_CHECK_MSG(equal_cs, "%s (%Lg + %Lg I) ~=  %s (%Lg + %Lg I)",	\
180 	    #x, creall(_x), cimagl(_x), #y, creall(_y), cimagl(_y));		\
181 } while (0)
182 
183 #define CHECK_CFPEQUAL_TOL(x, y, tol, flags) do {				\
184 	long double _x = x;							\
185 	long double _y = y;							\
186 	bool equal_tol = (fpequal_tol(creal(_x), creal(_y), tol, flags) &&	\
187 	    fpequal_tol(cimag(_x), cimag(_y), tol, flags));			\
188 	ATF_CHECK_MSG(equal_tol, "%s (%Lg + %Lg I) ~=  %s (%Lg + %Lg I)",	\
189 	    #x, creall(_x), cimagl(_x), #y, creall(_y), cimagl(_y));		\
190 } while (0)
191 
192 #define CHECK_FP_EXCEPTIONS(excepts, exceptmask)		\
193 	ATF_CHECK_EQ_MSG((excepts), fetestexcept(exceptmask),	\
194 	    "unexpected exception flags: got %#x not %#x",	\
195 	    fetestexcept(exceptmask), (excepts))
196 #define CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, fmt, ...)	\
197 	ATF_CHECK_EQ_MSG((excepts), fetestexcept(exceptmask),	\
198 	    "unexpected exception flags: got %#x not %#x " fmt,	\
199 	    fetestexcept(exceptmask), (excepts), __VA_ARGS__)
200 
201 #endif /* _TEST_UTILS_H_ */
202