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 _fpequal_cs(atf_variant, x, y, checksign) do { \ 148 long double _x = x; \ 149 long double _y = y; \ 150 ATF_##atf_variant##_MSG(fpequal_cs(_x, _y, checksign), \ 151 "%s (%.25Lg) ~= %s (%.25Lg)", #x, _x, #y, _y); \ 152 } while (0) 153 154 #define _fpequal_tol(atf_variant, x, y, tol, flags) do { \ 155 long double _x = x; \ 156 long double _y = y; \ 157 bool eq = fpequal_tol(_x, _y, tol, flags); \ 158 long double _diff = eq ? 0.0L : fabsl(_x - _y); \ 159 ATF_##atf_variant##_MSG(eq, \ 160 "%s (%.25Lg) ~= %s (%.25Lg), diff=%Lg, maxdiff=%Lg,", \ 161 #x, _x, #y, _y, _diff, fabsl(_y * tol)); \ 162 } while (0) 163 164 static inline int 165 cfpequal(long double complex d1, long double complex d2) 166 { 167 168 return (fpequal_cs(creall(d1), creall(d2), true) && 169 fpequal_cs(cimagl(d1), cimagl(d2), true)); 170 } 171 172 #define _cfpequal_cs(atf_variant, x, y, checksign) do { \ 173 long double _x = x; \ 174 long double _y = y; \ 175 bool equal_cs = \ 176 fpequal_cs(creal(_x), creal(_y), (checksign & CS_REAL) != 0) && \ 177 fpequal_cs(cimag(_x), cimag(_y), (checksign & CS_IMAG) != 0); \ 178 ATF_##atf_variant##_MSG(equal_cs, \ 179 "%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 _cfpequal_tol(atf_variant, 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_##atf_variant##_MSG(equal_tol, \ 189 "%s (%Lg + %Lg I) ~= %s (%Lg + %Lg I)", \ 190 #x, creall(_x), cimagl(_x), #y, creall(_y), cimagl(_y)); \ 191 } while (0) 192 193 #define _fp_exceptions(atf_variant, excepts, exceptmask) \ 194 ATF_##atf_variant##_EQ_MSG((excepts), fetestexcept(exceptmask), \ 195 "unexpected exception flags: got %#x not %#x", \ 196 fetestexcept(exceptmask), (excepts)) 197 #define _fp_exceptions_msg(atf_variant, excepts, exceptmask, fmt, ...) \ 198 ATF_##atf_variant##_EQ_MSG((excepts), fetestexcept(exceptmask), \ 199 "unexpected exception flags: got %#x not %#x " fmt, \ 200 fetestexcept(exceptmask), (excepts), __VA_ARGS__) 201 202 #define CHECK_FP_EXCEPTIONS(excepts, exceptmask) _fp_exceptions(CHECK, excepts, exceptmask) 203 #define CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, fmt, ...) _fp_exceptions_msg(CHECK, excepts, exceptmask, fmt, __VA_ARGS__) 204 #define CHECK_CFPEQUAL_TOL(x, y, tol, flags) _cfpequal_tol(CHECK, x, y, tol, flags) 205 #define CHECK_CFPEQUAL_CS(x, y, checksign) _cfpequal_cs(CHECK, x, y, checksign) 206 #define CHECK_FPEQUAL(x, y) _fpequal_cs(CHECK, x, y, true) 207 #define CHECK_FPEQUAL_TOL(x, y, tol, flags) _fpequal_tol(CHECK, x, y, tol, flags) 208 209 #define REQUIRE_FP_EXCEPTIONS(excepts, exceptmask) _fp_exceptions(REQUIRE, excepts, exceptmask) 210 #define REQUIRE_FP_EXCEPTIONS_MSG(excepts, exceptmask, fmt, ...) _fp_exceptions_msg(REQUIRE, excepts, exceptmask, fmt, __VA_ARGS__) 211 #define REQUIRE_CFPEQUAL_TOL(x, y, tol, flags) _cfpequal_tol(REQUIRE, x, y, tol, flags) 212 #define REQUIRE_CFPEQUAL_CS(x, y, checksign) _cfpequal_cs(REQUIRE, x, y, checksign) 213 #define REQUIRE_FPEQUAL(x, y) _fpequal_cs(REQUIRE, x, y, true) 214 #define REQUIRE_FPEQUAL_TOL(x, y, tol, flags) _fpequal_tol(REQUIRE, x, y, tol, flags) 215 216 #endif /* _TEST_UTILS_H_ */ 217