aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_data.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-10-06 23:34:36 +0200
committerDavid Robillard <d@drobilla.net>2020-10-27 13:13:59 +0100
commit03e3b6c156f2b5809bfe0d65c52a1ad1df1da4fd (patch)
tree4c94c182d026bc91ba9d0ef4001d59df66c56591 /tests/test_data.h
parent78c87ccdda887a4b49373cd2dfb20936e704a44a (diff)
downloadserd-03e3b6c156f2b5809bfe0d65c52a1ad1df1da4fd.tar.gz
serd-03e3b6c156f2b5809bfe0d65c52a1ad1df1da4fd.tar.bz2
serd-03e3b6c156f2b5809bfe0d65c52a1ad1df1da4fd.zip
Add precise floating point parsing
Diffstat (limited to 'tests/test_data.h')
-rw-r--r--tests/test_data.h26
1 files changed, 26 insertions, 0 deletions
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 <assert.h>
+#include <math.h>
#include <stdint.h>
#include <string.h>
@@ -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;
+}