1 /* $OpenBSD: test-utils.h,v 1.2 2019/02/21 17:36:41 bluhm Exp $ */ 2 /*- 3 * Copyright (c) 2005-2013 David Schultz <das@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/lib/msun/tests/test-utils.h 314650 2017-03-04 10:07:46Z ngie $ 28 */ 29 30 #ifndef _TEST_UTILS_H_ 31 #define _TEST_UTILS_H_ 32 33 #include <complex.h> 34 #include <fenv.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 static int fpequal(long double, long double) __used; 93 static int cfpequal(long double complex, long double complex) __used; 94 static int cfpequal_cs(long double complex, long double complex, 95 int) __used; 96 static int cfpequal_tol(long double complex, long double complex, 97 long double, unsigned int) __used; 98 99 /* 100 * Compare d1 and d2 using special rules: NaN == NaN and +0 != -0. 101 * Fail an assertion if they differ. 102 */ 103 static int 104 fpequal(long double d1, long double d2) 105 { 106 107 if (d1 != d2) 108 return (isnan(d1) && isnan(d2)); 109 return (copysignl(1.0, d1) == copysignl(1.0, d2)); 110 } 111 112 /* 113 * Determine whether x and y are equal, with two special rules: 114 * +0.0 != -0.0 115 * NaN == NaN 116 * If checksign is 0, we compare the absolute values instead. 117 */ 118 static int 119 fpequal_cs(long double x, long double y, int checksign) 120 { 121 if (isnan(x) && isnan(y)) 122 return (1); 123 if (checksign) 124 return (x == y && !signbit(x) == !signbit(y)); 125 else 126 return (fabsl(x) == fabsl(y)); 127 } 128 129 static int 130 fpequal_tol(long double x, long double y, long double tol, 131 unsigned int flags) 132 { 133 fenv_t env; 134 int ret; 135 136 if (isnan(x) && isnan(y)) 137 return (1); 138 if (!signbit(x) != !signbit(y) && (flags & CS_BOTH)) 139 return (0); 140 if (x == y) 141 return (1); 142 if (tol == 0) 143 return (0); 144 145 /* Hard case: need to check the tolerance. */ 146 feholdexcept(&env); 147 /* 148 * For our purposes here, if y=0, we interpret tol as an absolute 149 * tolerance. This is to account for roundoff in the input, e.g., 150 * cos(Pi/2) ~= 0. 151 */ 152 if ((flags & FPE_ABS_ZERO) && y == 0.0) 153 ret = fabsl(x - y) <= fabsl(tol); 154 else 155 ret = fabsl(x - y) <= fabsl(y * tol); 156 fesetenv(&env); 157 return (ret); 158 } 159 160 static int 161 cfpequal(long double complex d1, long double complex d2) 162 { 163 164 return (fpequal(creall(d1), creall(d2)) && 165 fpequal(cimagl(d1), cimagl(d2))); 166 } 167 168 static int 169 cfpequal_cs(long double complex x, long double complex y, int checksign) 170 { 171 return (fpequal_cs(creal(x), creal(y), checksign) 172 && fpequal_cs(cimag(x), cimag(y), checksign)); 173 } 174 175 static int 176 cfpequal_tol(long double complex x, long double complex y, long double tol, 177 unsigned int flags) 178 { 179 return (fpequal_tol(creal(x), creal(y), tol, flags) 180 && fpequal_tol(cimag(x), cimag(y), tol, flags)); 181 } 182 183 #endif /* _TEST_UTILS_H_ */ 184