2 * Copyright (c) 2005-2013 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #ifndef _TEST_UTILS_H_
30 #define _TEST_UTILS_H_
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.
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)
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.
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.
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.
69 #define CS_BOTH (CS_REAL | CS_IMAG)
70 #define FPE_ABS_ZERO 0x04
73 #define debug(...) printf(__VA_ARGS__)
75 #define debug(...) (void)0
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.
83 static inline long double complex
84 CMPLXL(long double x, long double y)
86 long double complex z;
95 * The compiler-rt fp128 builtins do not update FP exceptions.
96 * See https://llvm.org/PR34126
99 static int cfpequal(long double complex, long double complex) __used;
102 * Determine whether x and y are equal, with two special rules:
105 * If checksign is false, we compare the absolute values instead.
108 fpequal_cs(long double x, long double y, bool checksign)
110 if (isnan(x) && isnan(y))
113 return (x == y && !signbit(x) == !signbit(y));
115 return (fabsl(x) == fabsl(y));
119 fpequal_tol(long double x, long double y, long double tol,
125 if (isnan(x) && isnan(y))
127 if (!signbit(x) != !signbit(y) && (flags & CS_BOTH))
134 /* Hard case: need to check the tolerance. */
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.,
141 if ((flags & FPE_ABS_ZERO) && y == 0.0)
142 ret = fabsl(x - y) <= fabsl(tol);
144 ret = fabsl(x - y) <= fabsl(y * tol);
149 #define CHECK_FPEQUAL(x, y) CHECK_FPEQUAL_CS(x, y, true)
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); \
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)); \
168 cfpequal(long double complex d1, long double complex d2)
171 return (fpequal_cs(creall(d1), creall(d2), true) &&
172 fpequal_cs(cimagl(d1), cimagl(d2), true));
175 #define CHECK_CFPEQUAL_CS(x, y, checksign) do { \
176 long double _x = x; \
177 long double _y = y; \
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)); \
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)); \
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__)
203 #endif /* _TEST_UTILS_H_ */