Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
ieee.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright © 2017 Franklin "Snaipe" Mathieu <http://snai.pe/>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#ifndef CRITERION_INTERNAL_ASSERT_IEEE_H_
25#define CRITERION_INTERNAL_ASSERT_IEEE_H_
26
27/* IEEE specifiers */
28
29#ifdef __cplusplus
30
31# include <cmath>
32# include <limits>
33
34namespace criterion { namespace internal { namespace operators {
35
36template <typename Float,
37 typename = typename std::enable_if<std::numeric_limits<Float>::is_iec559>::type>
38bool ieee_eq(Float &a, Float &b, size_t ulp)
39{
40 if (ulp == 0)
41 return false;
42
43 /* Not the most efficient but the most portable */
44 a = std::nextafter(a, b);
45 return a == b ? true : ieee_eq(a, b, ulp - 1);
46}
47
48}}} /* criterion::internal::operators */
49
50# define CRI_IEEE_ULP_EQ(Lhs, Rhs, Ulp) \
51 ::criterion::internal::operators::ieee_eq((Lhs), (Rhs), (Ulp))
52
53# define CRI_IEEE_ULP_NE(Lhs, Rhs, Ulp) !CRI_IEEE_ULP_EQ(Lhs, Rhs, Ulp)
54
55# define CRI_IEEE_ULP_T_EQ(Tag, Lhs, Rhs, Ulp) CRI_IEEE_ULP_EQ(Lhs, Rhs, Ulp)
56# define CRI_IEEE_ULP_T_NE(Tag, Lhs, Rhs, Ulp) CRI_IEEE_ULP_NE(Lhs, Rhs, Ulp)
57#else
58# define CRI_IEEE_ULP_EQ(Lhs, Rhs, Ulp) \
59 CR_COMPILE_ERROR(ieee_ulp_eq without a tag parameter is unsupported on this compiler.)
60# define CRI_IEEE_ULP_NE(Lhs, Rhs, Ulp) \
61 CR_COMPILE_ERROR(ieee_ulp_ne without a tag parameter is unsupported on this compiler.)
62
63# define CRI_IEEE_ULP_T_EQ(Tag, Lhs, Rhs, Ulp) \
64 (CRI_USER_TAG_ID(ieee_ulp_eq, Tag)((Lhs), (Rhs), (Ulp)))
65
66# define CRI_IEEE_ULP_T_NE(Tag, Lhs, Rhs, Ulp) \
67 !(CRI_USER_TAG_ID(ieee_ulp_eq, Tag)((Lhs), (Rhs), (Ulp)))
68
69# include <math.h>
70
71# define CRI_DEFINE_IEEE_ULP_EQ(Tag, Suffix) \
72 static inline int CRI_USER_TAG_ID(ieee_ulp_eq, Tag)( \
73 CRI_ASSERT_TYPE_TAG(Tag) a, \
74 CRI_ASSERT_TYPE_TAG(Tag) b, size_t ulp) \
75 { \
76 if (ulp == 0) \
77 return a == b; \
78 a = nextafter ## Suffix(a, b); \
79 return a == b ? 1 : CRI_USER_TAG_ID(ieee_ulp_eq, Tag)(a, b, ulp - 1); \
80 }
81
84
85# if defined (CRI_CAPS_LDBL_IS_DBL)
87# else
89# endif
90#endif
91
92#define CRI_MKNODE_ULPS (CRI_MKNODE_ULP, CRI_MKNODE_ULP)
93#define CRI_MKNODE_ULP(_, Var, Name) CRI_MKNODE_STR(sz, Var, Name)
94
95#define CRI_ASSERT_TEST_SPECIFIER_ieee_ulp_eq(...) ,
96#define CRI_ASSERT_SPECIFIER_ieee_ulp_eq(...) \
97 CRI_ASSERT_SPECIFIER_OP(CRI_IEEE_ULP_T_EQ, CRI_IEEE_ULP_EQ, ieee_ulp_eq, ( \
98 (TAGGED, CRI_AS_GETTYPE(__VA_ARGS__), cri_actual, "actual", CRI_MKNODE_DEF), \
99 (TAGGED, CRI_AS_GETTYPE(__VA_ARGS__), cri_expected, "expected", CRI_MKNODE_DEF), \
100 (SINGLE, size_t, cri_ulp, "ulp", CRI_MKNODE_ULPS) \
101 ), __VA_ARGS__)
102
103#define CRI_ASSERT_TEST_SPECIFIER_ieee_ulp_ne(...) ,
104#define CRI_ASSERT_SPECIFIER_ieee_ulp_ne(...) \
105 CRI_ASSERT_SPECIFIER_OP(CRI_IEEE_ULP_T_NE, CRI_IEEE_ULP_NE, ieee_ulp_ne, ( \
106 (TAGGED, CRI_AS_GETTYPE(__VA_ARGS__), cri_actual, "actual", CRI_MKNODE_DEF), \
107 (TAGGED, CRI_AS_GETTYPE(__VA_ARGS__), cri_expected, "unexpected", CRI_MKNODE_DEF), \
108 (SINGLE, size_t, cri_ulp, "ulp", CRI_MKNODE_ULPS) \
109 ), __VA_ARGS__)
110
111/* Epsilon specifiers */
112
113#define CRI_EPSILON_EQ(Lhs, Rhs, Eps) \
114 (((Rhs) == (Lhs)) || ((Rhs) - (Lhs) <= (Eps) && (Lhs) - (Rhs) <= (Eps)))
115
116#define CRI_EPSILON_NE(Lhs, Rhs, Eps) !CRI_EPSILON_EQ(Lhs, Rhs, Eps)
117
118#define CRI_EPSILON_T_EQ(Tag, Lhs, Rhs, Eps) CRI_EPSILON_EQ(Lhs, Rhs, Eps)
119#define CRI_EPSILON_T_NE(Tag, Lhs, Rhs, Eps) CRI_EPSILON_NE(Lhs, Rhs, Eps)
120
121# define CRI_MKNODE_EPSS (CRI_MKNODE_EPS, CRI_MKNODE_EPS)
122
123#if defined (CRI_CAPS_LDBL_IS_DBL)
124# define CRI_MKNODE_EPS(_, Var, Name) CRI_MKNODE_STR(dbl, Var, Name)
125# define CRI_EPS_TYPE double
126#else
127# define CRI_MKNODE_EPS(_, Var, Name) CRI_MKNODE_STR(ldbl, Var, Name)
128# define CRI_EPS_TYPE long double
129#endif
130
131#define CRI_ASSERT_TEST_SPECIFIER_epsilon_eq(...) ,
132#define CRI_ASSERT_SPECIFIER_epsilon_eq(...) \
133 CRI_ASSERT_SPECIFIER_OP(CRI_EPSILON_T_EQ, CRI_EPSILON_EQ, epsilon_eq, ( \
134 (TAGGED, CRI_AS_GETTYPE(__VA_ARGS__), cri_actual, "actual", CRI_MKNODE_DEF), \
135 (TAGGED, CRI_AS_GETTYPE(__VA_ARGS__), cri_expected, "expected", CRI_MKNODE_DEF), \
136 (SINGLE, CRI_EPS_TYPE, cri_epsilon, "epsilon", CRI_MKNODE_EPSS) \
137 ), __VA_ARGS__)
138
139#define CRI_ASSERT_TEST_SPECIFIER_epsilon_ne(...) ,
140#define CRI_ASSERT_SPECIFIER_epsilon_ne(...) \
141 CRI_ASSERT_SPECIFIER_OP(CRI_EPSILON_T_NE, CRI_EPSILON_NE, epsilon_ne, ( \
142 (TAGGED, CRI_AS_GETTYPE(__VA_ARGS__), cri_actual, "actual", CRI_MKNODE_DEF), \
143 (TAGGED, CRI_AS_GETTYPE(__VA_ARGS__), cri_expected, "unexpected", CRI_MKNODE_DEF), \
144 (SINGLE, CRI_EPS_TYPE, cri_epsilon, "epsilon", CRI_MKNODE_EPSS) \
145 ), __VA_ARGS__)
146
147#endif /* !CRITERION_INTERNAL_ASSERT_IEEE_H_ */
#define CRI_DEFINE_IEEE_ULP_EQ(Tag, Suffix)
Definition ieee.h:71