Debian Codegraph @

packages/seacas/applications/exodiff/Tolerance.C

raw
1 // Copyright(C) 1999-2020, 2023 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6 #include "Tolerance.h"
7 #include <cstdlib> // for abs
8 #include <sys/types.h> // for int32_t, int64_t
9
10 namespace {
11 /* See
12 http://randomascii.wordpress.com/2012/01/11/tricks-with-the-floating-point-format
13 for the potential portability problems with the union and bit-fields below.
14 */
15 union Float_t {
16 explicit Float_t(float num = 0.0F) : f(num) {}
17 // Portable extraction of components.
18 bool Negative() const { return (static_cast<uint32_t>(i) >> 31) != 0; }
19
20 int32_t i;
21 float f;
22 };
23
24 union Double_t {
25 explicit Double_t(double num = 0.0) : f(num) {}
26 // Portable extraction of components.
27 bool Negative() const { return (static_cast<uint64_t>(i) >> 63) != 0; }
28
29 int64_t i;
30 double f;
31 };
32
33 bool AlmostEqualUlpsFloat(float A, float B, int maxUlpsDiff)
34 {
35 Float_t uA(A);
36 Float_t uB(B);
37
38 // Different signs means they do not match.
39 if (uA.Negative() != uB.Negative()) {
40 // Check for equality to make sure +0==-0
41 return (A == B);
42 }
43
44 // Find the difference in ULPs.
45 int ulpsDiff = std::abs(uA.i - uB.i);
46 return (ulpsDiff <= maxUlpsDiff);
47 }
48
49 bool AlmostEqualUlpsDouble(double A, double B, int maxUlpsDiff)
50 {
51 Double_t uA(A);
52 Double_t uB(B);
53
54 // Different signs means they do not match.
55 if (uA.Negative() != uB.Negative()) {
56 // Check for equality to make sure +0==-0
57 return (A == B);
58 }
59
60 // Find the difference in ULPs.
61 int ulpsDiff = std::abs(uA.i - uB.i);
62 return (ulpsDiff <= maxUlpsDiff);
63 }
64 } // namespace
65
66 bool Tolerance::use_old_floor = false;
67
68 bool Tolerance::Diff(double v1, double v2) const
69 {
70 if (type == ToleranceMode::IGNORE_) {
71 return false;
72 }
73
74 if (use_old_floor) {
75 if (fabs(v1 - v2) < floor) {
76 return false;
77 }
78 }
79 else {
80 if (fabs(v1) <= floor && fabs(v2) <= floor) {
81 return false;
82 }
83 }
84
85 if (type == ToleranceMode::RELATIVE_) {
86 if (v1 == 0.0 && v2 == 0.0) {
87 return false;
88 }
89 double max = fabs(v1) < fabs(v2) ? fabs(v2) : fabs(v1);
90 return fabs(v1 - v2) > value * max;
91 }
92 if (type == ToleranceMode::ABSOLUTE_) {
93 return fabs(v1 - v2) > value;
94 }
95 if (type == ToleranceMode::COMBINED_) {
96 // if (Abs(x - y) <= Max(absTol, relTol * Max(Abs(x), Abs(y))))
97 // In the current implementation, absTol == relTol;
98 // At some point, store both values...
99 // In summary, use abs tolerance if both values are less than 1.0;
100 // else use relative tolerance.
101
102 double max = fabs(v1) < fabs(v2) ? fabs(v2) : fabs(v1);
103 double tol = 1.0 < max ? max : 1.0;
104 return fabs(v1 - v2) >= tol * value;
105
106 // EIGEN type diffs work on the absolute values. They are intended
107 // for diffing eigenvectors where one shape may be the inverse of
108 // the other and they should compare equal. Eventually would like
109 // to do a better check to ensure that ratio of one shape to other
110 // is 1 or -1...
111 }
112 if (type == ToleranceMode::ULPS_FLOAT_) {
113 return !AlmostEqualUlpsFloat(v1, v2, static_cast<int>(value));
114 }
115 if (type == ToleranceMode::ULPS_DOUBLE_) {
116 return !AlmostEqualUlpsDouble(v1, v2, static_cast<int>(value));
117 }
118 if (type == ToleranceMode::EIGEN_REL_) {
119 if (v1 == 0.0 && v2 == 0.0) {
120 return false;
121 }
122 double max = fabs(v1) < fabs(v2) ? fabs(v2) : fabs(v1);
123 return fabs(fabs(v1) - fabs(v2)) > value * max;
124 }
125 if (type == ToleranceMode::EIGEN_ABS_) {
126 return fabs(fabs(v1) - fabs(v2)) > value;
127 }
128 if (type == ToleranceMode::EIGEN_COM_) {
129 // if (Abs(x - y) <= Max(absTol, relTol * Max(Abs(x), Abs(y))))
130 // In the current implementation, absTol == relTol;
131 // At some point, store both values...
132 // In summary, use abs tolerance if both values are less than 1.0;
133 // else use relative tolerance.
134
135 double max = fabs(v1) < fabs(v2) ? fabs(v2) : fabs(v1);
136 double tol = 1.0 < max ? max : 1.0;
137 return fabs(fabs(v1) - fabs(v2)) >= tol * value;
138 }
139 return true;
140 }
141
142 const char *Tolerance::typestr() const
143 {
144 if (type == ToleranceMode::RELATIVE_) {
145 return "relative";
146 }
147 if (type == ToleranceMode::ABSOLUTE_) {
148 return "absolute";
149 }
150 if (type == ToleranceMode::COMBINED_) {
151 return "combined";
152 }
153 if (type == ToleranceMode::ULPS_FLOAT_) {
154 return "ulps_float";
155 }
156 if (type == ToleranceMode::ULPS_DOUBLE_) {
157 return "ulps_double";
158 }
159 if (type == ToleranceMode::EIGEN_REL_) {
160 return "eigenrel";
161 }
162 if (type == ToleranceMode::EIGEN_ABS_) {
163 return "eigenabs";
164 }
165 if (type == ToleranceMode::EIGEN_COM_) {
166 return "eigencom";
167 }
168 return "ignore";
169 }
170
171 const char *Tolerance::abrstr() const
172 {
173 if (type == ToleranceMode::RELATIVE_) {
174 return "rel";
175 }
176 if (type == ToleranceMode::ABSOLUTE_) {
177 return "abs";
178 }
179 if (type == ToleranceMode::COMBINED_) {
180 return "com";
181 }
182 if (type == ToleranceMode::ULPS_FLOAT_) {
183 return "upf";
184 }
185 if (type == ToleranceMode::ULPS_DOUBLE_) {
186 return "upd";
187 }
188 if (type == ToleranceMode::EIGEN_REL_) {
189 return "ere";
190 }
191 if (type == ToleranceMode::EIGEN_ABS_) {
192 return "eab";
193 }
194 if (type == ToleranceMode::EIGEN_COM_) {
195 return "eco";
196 }
197 return "ign";
198 }
199
200 double Tolerance::UlpsDiffFloat(double A, double B) const
201 {
202 Float_t uA(A);
203 Float_t uB(B);
204
205 // Different signs means they do not match.
206 if (uA.Negative() != uB.Negative()) {
207 // Check for equality to make sure +0==-0
208 if (A == B) {
209 return 0.0;
210 }
211 return 2 << 28;
212 }
213
214 // Find the difference in ULPs.
215 return abs(uA.i - uB.i);
216 }
217
218 double Tolerance::UlpsDiffDouble(double A, double B) const
219 {
220 Double_t uA(A);
221 Double_t uB(B);
222
223 // Different signs means they do not match.
224 if (uA.Negative() != uB.Negative()) {
225 // Check for equality to make sure +0==-0
226 if (A == B) {
227 return 0.0;
228 }
229 return 2 << 28;
230 }
231
232 // Find the difference in ULPs.
233 return std::abs(uA.i - uB.i);
234 }