From 03e3b6c156f2b5809bfe0d65c52a1ad1df1da4fd Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 6 Oct 2019 23:34:36 +0200 Subject: Add precise floating point parsing --- tests/test_data.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'tests/test_data.h') diff --git a/tests/test_data.h b/tests/test_data.h index fa32f529..6ff7b644 100644 --- a/tests/test_data.h +++ b/tests/test_data.h @@ -14,6 +14,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "../src/ieee_float.h" +#include "../src/soft_float.h" + +#include +#include #include #include @@ -54,3 +59,24 @@ double_from_rep(const uint64_t rep) memcpy(&d, &rep, sizeof(d)); return d; } + +/// Return the distance between two doubles in ULPs +static uint64_t +ulp_distance(const double a, const double b) +{ + assert(a >= 0.0); + assert(b >= 0.0); + + if (a == b) { + return 0; + } else if (isnan(a) || isnan(b)) { + return UINT64_MAX; + } else if (isinf(a) || isinf(b)) { + return UINT64_MAX; + } + + const uint64_t ia = double_to_rep(a); + const uint64_t ib = double_to_rep(b); + + return ia > ib ? ia - ib : ib - ia; +} -- cgit v1.2.1