aboutsummaryrefslogtreecommitdiffstats
path: root/unpack.c
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-07-23 16:03:33 +0100
committerTomasz Kramkowski <tk@the-tk.com>2021-07-23 16:03:33 +0100
commit5a904fbf12c2d4a270ac76ace4cc70a87869c74c (patch)
treeb800d937d927c66874c8098fad8d39baa2774b84 /unpack.c
parent42cc7b4840818fc0d989202d44315271f968e74d (diff)
downloadpack-5a904fbf12c2d4a270ac76ace4cc70a87869c74c.tar.gz
pack-5a904fbf12c2d4a270ac76ace4cc70a87869c74c.tar.xz
pack-5a904fbf12c2d4a270ac76ace4cc70a87869c74c.zip
Move ieee754b functions to their own file
Diffstat (limited to 'unpack.c')
-rw-r--r--unpack.c40
1 files changed, 3 insertions, 37 deletions
diff --git a/unpack.c b/unpack.c
index 011aebf..732685d 100644
--- a/unpack.c
+++ b/unpack.c
@@ -5,50 +5,16 @@
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
-#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "common.h"
+#include "ieee754b.h"
#include "pack.h"
#include "trace.h"
-#define GEN_CONV_IEEE754B(type, total, nexp, nfrac) \
-static type convert_ieee754b##total(uintmax_t b) \
-{ \
- bool isneg; \
- int exp; \
- type n; \
-\
- isneg = (b >> (total - 1)) & 0x1; \
- exp = (b >> nfrac) & BITMASK(nexp); \
- n = b & BITMASK(nfrac); \
-\
- if (exp == BITMASK(nexp)) { \
- if (n) { \
- return NAN; \
- } else { \
- return isneg ? -INFINITY : INFINITY; \
- } \
- } else if (exp == 0) { \
- if (n == 0) \
- return isneg ? -0.0 : 0.0; \
- exp = -(int)(BITMASK(nexp) / 2 - 1); \
- } else { \
- n += 0x1p##nfrac##f; \
- exp -= BITMASK(nexp) / 2; \
- } \
-\
- n = ldexpf(n, exp - nfrac); \
-\
- return isneg ? -n : n; \
-}
-
-GEN_CONV_IEEE754B(float, 32, 8, 23)
-GEN_CONV_IEEE754B(double, 64, 11, 52)
-
static uintmax_t read_val(const unsigned char *buf, size_t size, enum endian e)
{
uintmax_t val = 0;
@@ -149,11 +115,11 @@ enum pack_status unpack(const void *buf_, size_t size, const char *fmt, ...)
tr_debug("val.u: %" PRIuMAX ", at: %zu", val.u, offset + s * j);
if (fmt[i] == 'f') {
- float f = convert_ieee754b32(val.u);
+ float f = ieee754b32_deserialise(val.u);
val.f = f;
tr_debug("val.f: %f", val.f);
} else if (fmt[i] == 'd') {
- double d = convert_ieee754b64(val.u);
+ double d = ieee754b64_deserialise(val.u);
val.d = d;
tr_debug("val.d: %f", val.d);
} else if (sign) {