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_
36 * Implementations are permitted to define additional exception flags
37 * not specified in the standard, so it is not necessarily true that
38 * FE_ALL_EXCEPT == ALL_STD_EXCEPT.
40 #define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
41 FE_OVERFLOW | FE_UNDERFLOW)
42 #define OPT_INVALID (ALL_STD_EXCEPT & ~FE_INVALID)
43 #define OPT_INEXACT (ALL_STD_EXCEPT & ~FE_INEXACT)
44 #define FLT_ULP() ldexpl(1.0, 1 - FLT_MANT_DIG)
45 #define DBL_ULP() ldexpl(1.0, 1 - DBL_MANT_DIG)
46 #define LDBL_ULP() ldexpl(1.0, 1 - LDBL_MANT_DIG)
49 * Flags that control the behavior of various fpequal* functions.
50 * XXX This is messy due to merging various notions of "close enough"
51 * that are best suited for different functions.
56 * (cfpequal_cs, fpequal_tol, cfpequal_tol) Whether to check the sign of
57 * the real part of the result, the imaginary part, or both.
60 * (fpequal_tol, cfpequal_tol) If set, treats the tolerance as an absolute
61 * tolerance when the expected value is 0. This is useful when there is
62 * round-off error in the input, e.g., cos(Pi/2) ~= 0.
66 #define CS_BOTH (CS_REAL | CS_IMAG)
67 #define FPE_ABS_ZERO 0x04
70 #define debug(...) printf(__VA_ARGS__)
72 #define debug(...) (void)0
76 * XXX The ancient version of gcc in the base system doesn't support CMPLXL,
77 * but we can fake it most of the time.
80 static inline long double complex
81 CMPLXL(long double x, long double y)
83 long double complex z;
91 static int fpequal(long double, long double) __used;
92 static int cfpequal(long double complex, long double complex) __used;
93 static int cfpequal_cs(long double complex, long double complex,
95 static int cfpequal_tol(long double complex, long double complex,
96 long double, unsigned int) __used;
99 * Compare d1 and d2 using special rules: NaN == NaN and +0 != -0.
100 * Fail an assertion if they differ.
103 fpequal(long double d1, long double d2)
107 return (isnan(d1) && isnan(d2));
108 return (copysignl(1.0, d1) == copysignl(1.0, d2));
112 * Determine whether x and y are equal, with two special rules:
115 * If checksign is 0, we compare the absolute values instead.
118 fpequal_cs(long double x, long double y, int checksign)
120 if (isnan(x) && isnan(y))
123 return (x == y && !signbit(x) == !signbit(y));
125 return (fabsl(x) == fabsl(y));
129 fpequal_tol(long double x, long double y, long double tol,
135 if (isnan(x) && isnan(y))
137 if (!signbit(x) != !signbit(y) && (flags & CS_BOTH))
144 /* Hard case: need to check the tolerance. */
147 * For our purposes here, if y=0, we interpret tol as an absolute
148 * tolerance. This is to account for roundoff in the input, e.g.,
151 if ((flags & FPE_ABS_ZERO) && y == 0.0)
152 ret = fabsl(x - y) <= fabsl(tol);
154 ret = fabsl(x - y) <= fabsl(y * tol);
160 cfpequal(long double complex d1, long double complex d2)
163 return (fpequal(creall(d1), creall(d2)) &&
164 fpequal(cimagl(d1), cimagl(d2)));
168 cfpequal_cs(long double complex x, long double complex y, int checksign)
170 return (fpequal_cs(creal(x), creal(y), checksign)
171 && fpequal_cs(cimag(x), cimag(y), checksign));
175 cfpequal_tol(long double complex x, long double complex y, long double tol,
178 return (fpequal_tol(creal(x), creal(y), tol, flags)
179 && fpequal_tol(cimag(x), cimag(y), tol, flags));
182 #endif /* _TEST_UTILS_H_ */