diff options
Diffstat (limited to 'tests/test_data.h')
-rw-r--r-- | tests/test_data.h | 26 |
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; +} |