1*c36e572eSmbuhl /* $OpenBSD: test-utils.h,v 1.3 2021/10/22 18:00:23 mbuhl Exp $ */
24c142d94Sbluhm /*-
34c142d94Sbluhm * Copyright (c) 2005-2013 David Schultz <das@FreeBSD.org>
44c142d94Sbluhm * All rights reserved.
54c142d94Sbluhm *
64c142d94Sbluhm * Redistribution and use in source and binary forms, with or without
74c142d94Sbluhm * modification, are permitted provided that the following conditions
84c142d94Sbluhm * are met:
94c142d94Sbluhm * 1. Redistributions of source code must retain the above copyright
104c142d94Sbluhm * notice, this list of conditions and the following disclaimer.
114c142d94Sbluhm * 2. Redistributions in binary form must reproduce the above copyright
124c142d94Sbluhm * notice, this list of conditions and the following disclaimer in the
134c142d94Sbluhm * documentation and/or other materials provided with the distribution.
144c142d94Sbluhm *
154c142d94Sbluhm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
164c142d94Sbluhm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174c142d94Sbluhm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184c142d94Sbluhm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
194c142d94Sbluhm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
204c142d94Sbluhm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
214c142d94Sbluhm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
224c142d94Sbluhm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
234c142d94Sbluhm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
244c142d94Sbluhm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
254c142d94Sbluhm * SUCH DAMAGE.
264c142d94Sbluhm *
274c142d94Sbluhm * $FreeBSD: head/lib/msun/tests/test-utils.h 314650 2017-03-04 10:07:46Z ngie $
284c142d94Sbluhm */
294c142d94Sbluhm
304c142d94Sbluhm #ifndef _TEST_UTILS_H_
314c142d94Sbluhm #define _TEST_UTILS_H_
324c142d94Sbluhm
334c142d94Sbluhm #include <complex.h>
344c142d94Sbluhm #include <fenv.h>
35*c36e572eSmbuhl #include <float.h>
36*c36e572eSmbuhl
37*c36e572eSmbuhl #include "atf-c.h"
384c142d94Sbluhm
394c142d94Sbluhm /*
404c142d94Sbluhm * Implementations are permitted to define additional exception flags
414c142d94Sbluhm * not specified in the standard, so it is not necessarily true that
424c142d94Sbluhm * FE_ALL_EXCEPT == ALL_STD_EXCEPT.
434c142d94Sbluhm */
444c142d94Sbluhm #define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
454c142d94Sbluhm FE_OVERFLOW | FE_UNDERFLOW)
464c142d94Sbluhm #define OPT_INVALID (ALL_STD_EXCEPT & ~FE_INVALID)
474c142d94Sbluhm #define OPT_INEXACT (ALL_STD_EXCEPT & ~FE_INEXACT)
484c142d94Sbluhm #define FLT_ULP() ldexpl(1.0, 1 - FLT_MANT_DIG)
494c142d94Sbluhm #define DBL_ULP() ldexpl(1.0, 1 - DBL_MANT_DIG)
504c142d94Sbluhm #define LDBL_ULP() ldexpl(1.0, 1 - LDBL_MANT_DIG)
514c142d94Sbluhm
524c142d94Sbluhm /*
534c142d94Sbluhm * Flags that control the behavior of various fpequal* functions.
544c142d94Sbluhm * XXX This is messy due to merging various notions of "close enough"
554c142d94Sbluhm * that are best suited for different functions.
564c142d94Sbluhm *
574c142d94Sbluhm * CS_REAL
584c142d94Sbluhm * CS_IMAG
594c142d94Sbluhm * CS_BOTH
604c142d94Sbluhm * (cfpequal_cs, fpequal_tol, cfpequal_tol) Whether to check the sign of
614c142d94Sbluhm * the real part of the result, the imaginary part, or both.
624c142d94Sbluhm *
634c142d94Sbluhm * FPE_ABS_ZERO
644c142d94Sbluhm * (fpequal_tol, cfpequal_tol) If set, treats the tolerance as an absolute
654c142d94Sbluhm * tolerance when the expected value is 0. This is useful when there is
664c142d94Sbluhm * round-off error in the input, e.g., cos(Pi/2) ~= 0.
674c142d94Sbluhm */
684c142d94Sbluhm #define CS_REAL 0x01
694c142d94Sbluhm #define CS_IMAG 0x02
704c142d94Sbluhm #define CS_BOTH (CS_REAL | CS_IMAG)
714c142d94Sbluhm #define FPE_ABS_ZERO 0x04
724c142d94Sbluhm
734c142d94Sbluhm #ifdef DEBUG
744c142d94Sbluhm #define debug(...) printf(__VA_ARGS__)
754c142d94Sbluhm #else
764c142d94Sbluhm #define debug(...) (void)0
774c142d94Sbluhm #endif
784c142d94Sbluhm
794c142d94Sbluhm /*
804c142d94Sbluhm * XXX The ancient version of gcc in the base system doesn't support CMPLXL,
814c142d94Sbluhm * but we can fake it most of the time.
824c142d94Sbluhm */
834c142d94Sbluhm #ifndef CMPLXL
844c142d94Sbluhm static inline long double complex
CMPLXL(long double x,long double y)854c142d94Sbluhm CMPLXL(long double x, long double y)
864c142d94Sbluhm {
874c142d94Sbluhm long double complex z;
884c142d94Sbluhm
894c142d94Sbluhm __real__ z = x;
904c142d94Sbluhm __imag__ z = y;
914c142d94Sbluhm return (z);
924c142d94Sbluhm }
934c142d94Sbluhm #endif
944c142d94Sbluhm
954c142d94Sbluhm /*
96*c36e572eSmbuhl * The compiler-rt fp128 builtins do not update FP exceptions.
97*c36e572eSmbuhl * See https://llvm.org/PR34126
984c142d94Sbluhm */
994c142d94Sbluhm
100*c36e572eSmbuhl static int cfpequal(long double complex, long double complex) __used;
1014c142d94Sbluhm
1024c142d94Sbluhm /*
1034c142d94Sbluhm * Determine whether x and y are equal, with two special rules:
1044c142d94Sbluhm * +0.0 != -0.0
1054c142d94Sbluhm * NaN == NaN
106*c36e572eSmbuhl * If checksign is false, we compare the absolute values instead.
1074c142d94Sbluhm */
108*c36e572eSmbuhl static inline int
fpequal_cs(long double x,long double y,bool checksign)109*c36e572eSmbuhl fpequal_cs(long double x, long double y, bool checksign)
1104c142d94Sbluhm {
1114c142d94Sbluhm if (isnan(x) && isnan(y))
1124c142d94Sbluhm return (1);
1134c142d94Sbluhm if (checksign)
1144c142d94Sbluhm return (x == y && !signbit(x) == !signbit(y));
1154c142d94Sbluhm else
1164c142d94Sbluhm return (fabsl(x) == fabsl(y));
1174c142d94Sbluhm }
1184c142d94Sbluhm
119*c36e572eSmbuhl static inline int
fpequal_tol(long double x,long double y,long double tol,unsigned int flags)1204c142d94Sbluhm fpequal_tol(long double x, long double y, long double tol,
1214c142d94Sbluhm unsigned int flags)
1224c142d94Sbluhm {
1234c142d94Sbluhm fenv_t env;
1244c142d94Sbluhm int ret;
1254c142d94Sbluhm
1264c142d94Sbluhm if (isnan(x) && isnan(y))
1274c142d94Sbluhm return (1);
1284c142d94Sbluhm if (!signbit(x) != !signbit(y) && (flags & CS_BOTH))
1294c142d94Sbluhm return (0);
1304c142d94Sbluhm if (x == y)
1314c142d94Sbluhm return (1);
1324c142d94Sbluhm if (tol == 0)
1334c142d94Sbluhm return (0);
1344c142d94Sbluhm
1354c142d94Sbluhm /* Hard case: need to check the tolerance. */
1364c142d94Sbluhm feholdexcept(&env);
1374c142d94Sbluhm /*
1384c142d94Sbluhm * For our purposes here, if y=0, we interpret tol as an absolute
1394c142d94Sbluhm * tolerance. This is to account for roundoff in the input, e.g.,
1404c142d94Sbluhm * cos(Pi/2) ~= 0.
1414c142d94Sbluhm */
1424c142d94Sbluhm if ((flags & FPE_ABS_ZERO) && y == 0.0)
1434c142d94Sbluhm ret = fabsl(x - y) <= fabsl(tol);
1444c142d94Sbluhm else
1454c142d94Sbluhm ret = fabsl(x - y) <= fabsl(y * tol);
1464c142d94Sbluhm fesetenv(&env);
1474c142d94Sbluhm return (ret);
1484c142d94Sbluhm }
1494c142d94Sbluhm
150*c36e572eSmbuhl #define CHECK_FPEQUAL(x, y) CHECK_FPEQUAL_CS(x, y, true)
151*c36e572eSmbuhl
152*c36e572eSmbuhl #define CHECK_FPEQUAL_CS(x, y, checksign) do { \
153*c36e572eSmbuhl long double _x = x; \
154*c36e572eSmbuhl long double _y = y; \
155*c36e572eSmbuhl ATF_CHECK_MSG(fpequal_cs(_x, _y, checksign), \
156*c36e572eSmbuhl "%s (%.25Lg) ~= %s (%.25Lg)", #x, _x, #y, _y); \
157*c36e572eSmbuhl } while (0)
158*c36e572eSmbuhl
159*c36e572eSmbuhl #define CHECK_FPEQUAL_TOL(x, y, tol, flags) do { \
160*c36e572eSmbuhl long double _x = x; \
161*c36e572eSmbuhl long double _y = y; \
162*c36e572eSmbuhl bool eq = fpequal_tol(_x, _y, tol, flags); \
163*c36e572eSmbuhl long double _diff = eq ? 0.0L : fabsl(_x - _y); \
164*c36e572eSmbuhl ATF_CHECK_MSG(eq, "%s (%.25Lg) ~= %s (%.25Lg), diff=%Lg, maxdiff=%Lg,", \
165*c36e572eSmbuhl #x, _x, #y, _y, _diff, fabsl(_y * tol)); \
166*c36e572eSmbuhl } while (0)
167*c36e572eSmbuhl
168*c36e572eSmbuhl static inline int
cfpequal(long double complex d1,long double complex d2)1694c142d94Sbluhm cfpequal(long double complex d1, long double complex d2)
1704c142d94Sbluhm {
1714c142d94Sbluhm
172*c36e572eSmbuhl return (fpequal_cs(creall(d1), creall(d2), true) &&
173*c36e572eSmbuhl fpequal_cs(cimagl(d1), cimagl(d2), true));
1744c142d94Sbluhm }
1754c142d94Sbluhm
176*c36e572eSmbuhl #ifdef __OpenBSD__
1774c142d94Sbluhm static int
cfpequal_cs(x,y,checksign)178*c36e572eSmbuhl cfpequal_cs(x, y, checksign)
1794c142d94Sbluhm {
180*c36e572eSmbuhl long double _x = x;
181*c36e572eSmbuhl long double _y = y;
182*c36e572eSmbuhl return
183*c36e572eSmbuhl fpequal_cs(creal(_x), creal(_y), (checksign & CS_REAL) != 0) &&
184*c36e572eSmbuhl fpequal_cs(cimag(_x), cimag(_y), (checksign & CS_IMAG) != 0);
1854c142d94Sbluhm }
186*c36e572eSmbuhl #endif
1874c142d94Sbluhm
188*c36e572eSmbuhl #define CHECK_CFPEQUAL_CS(x, y, checksign) do { \
189*c36e572eSmbuhl long double _x = x; \
190*c36e572eSmbuhl long double _y = y; \
191*c36e572eSmbuhl bool equal_cs = \
192*c36e572eSmbuhl fpequal_cs(creal(_x), creal(_y), (checksign & CS_REAL) != 0) && \
193*c36e572eSmbuhl fpequal_cs(cimag(_x), cimag(_y), (checksign & CS_IMAG) != 0); \
194*c36e572eSmbuhl ATF_CHECK_MSG(equal_cs, "%s (%Lg + %Lg I) ~= %s (%Lg + %Lg I)", \
195*c36e572eSmbuhl #x, creall(_x), cimagl(_x), #y, creall(_y), cimagl(_y)); \
196*c36e572eSmbuhl } while (0)
197*c36e572eSmbuhl
198*c36e572eSmbuhl #define CHECK_CFPEQUAL_TOL(x, y, tol, flags) do { \
199*c36e572eSmbuhl long double _x = x; \
200*c36e572eSmbuhl long double _y = y; \
201*c36e572eSmbuhl bool equal_tol = (fpequal_tol(creal(_x), creal(_y), tol, flags) && \
202*c36e572eSmbuhl fpequal_tol(cimag(_x), cimag(_y), tol, flags)); \
203*c36e572eSmbuhl ATF_CHECK_MSG(equal_tol, "%s (%Lg + %Lg I) ~= %s (%Lg + %Lg I)", \
204*c36e572eSmbuhl #x, creall(_x), cimagl(_x), #y, creall(_y), cimagl(_y)); \
205*c36e572eSmbuhl } while (0)
206*c36e572eSmbuhl
207*c36e572eSmbuhl #define CHECK_FP_EXCEPTIONS(excepts, exceptmask) \
208*c36e572eSmbuhl ATF_CHECK_EQ_MSG((excepts), fetestexcept(exceptmask), \
209*c36e572eSmbuhl "unexpected exception flags: got %#x not %#x", \
210*c36e572eSmbuhl fetestexcept(exceptmask), (excepts))
211*c36e572eSmbuhl #define CHECK_FP_EXCEPTIONS_MSG(excepts, exceptmask, fmt, ...) \
212*c36e572eSmbuhl ATF_CHECK_EQ_MSG((excepts), fetestexcept(exceptmask), \
213*c36e572eSmbuhl "unexpected exception flags: got %#x not %#x " fmt, \
214*c36e572eSmbuhl fetestexcept(exceptmask), (excepts), __VA_ARGS__)
2154c142d94Sbluhm
2164c142d94Sbluhm #endif /* _TEST_UTILS_H_ */
217