From 58e637574feb38b975f486e7424a2b0ac8b21e0c Mon Sep 17 00:00:00 2001 From: Morris Jette <jette@schedmd.com> Date: Wed, 22 Jul 2015 08:22:10 -0700 Subject: [PATCH] Add functions to un/pack long double --- src/common/pack.c | 36 ++++++++++++++++++++++++++++++++++++ src/common/pack.h | 16 ++++++++++++++++ src/common/slurm_xlator.h | 2 ++ 3 files changed, 54 insertions(+) diff --git a/src/common/pack.c b/src/common/pack.c index b925fe71dc1..5cf79bb6bb3 100644 --- a/src/common/pack.c +++ b/src/common/pack.c @@ -70,6 +70,8 @@ strong_alias(pack_time, slurm_pack_time); strong_alias(unpack_time, slurm_unpack_time); strong_alias(packdouble, slurm_packdouble); strong_alias(unpackdouble, slurm_unpackdouble); +strong_alias(packlongdouble, slurm_packlongdouble); +strong_alias(unpacklongdouble, slurm_unpacklongdouble); strong_alias(pack64, slurm_pack64); strong_alias(unpack64, slurm_unpack64); strong_alias(pack32, slurm_pack32); @@ -208,6 +210,7 @@ int unpack_time(time_t * valp, Buf buffer) * Given a double, multiple by FLOAT_MULT and then * typecast to a uint64_t in host byte order, convert to network byte order * store in buffer, and adjust buffer counters. + * NOTE: There is an IEEE standard format for double. */ void packdouble(double val, Buf buffer) { @@ -240,6 +243,7 @@ void packdouble(double val, Buf buffer) * Given a buffer containing a network byte order 64-bit integer, * typecast as double, and divide by FLOAT_MULT * store a host double at 'valp', and adjust buffer counters. + * NOTE: There is an IEEE standard format for double. */ int unpackdouble(double *valp, Buf buffer) { @@ -261,6 +265,38 @@ int unpackdouble(double *valp, Buf buffer) return SLURM_SUCCESS; } +/* + * long double has no standard format, so pass the data as a string + */ +void packlongdouble(long double val, Buf buffer) +{ + char val_str[256]; + + snprintf(val_str, sizeof(val_str), "%Lf", val); + packstr(val_str, buffer); +} + +/* + * long double has no standard format, so pass the data as a string + */ +int unpacklongdouble(long double *valp, Buf buffer) +{ + long double nl; + char *val_str = NULL; + uint32_t size_val_str = 0; + int rc; + + rc = unpackmem_ptr(&val_str, &size_val_str, buffer); + if (rc != SLURM_SUCCESS) + return rc; + + if (sscanf(val_str, "%Lf", &nl) != 1) + return SLURM_ERROR; + + *valp = nl; + return SLURM_SUCCESS; +} + /* * Given a 64-bit integer in host byte order, convert to network byte order * store in buffer, and adjust buffer counters. diff --git a/src/common/pack.h b/src/common/pack.h index a8d310c1427..a5ae62c986d 100644 --- a/src/common/pack.h +++ b/src/common/pack.h @@ -96,6 +96,9 @@ int unpack_time(time_t *valp, Buf buffer); void packdouble(double val, Buf buffer); int unpackdouble(double *valp, Buf buffer); +void packlongdouble(long double val, Buf buffer); +int unpacklongdouble(long double *valp, Buf buffer); + void pack64(uint64_t val, Buf buffer); int unpack64(uint64_t *valp, Buf buffer); @@ -155,6 +158,19 @@ int unpackmem_array(char *valp, uint32_t size_valp, Buf buffer); goto unpack_error; \ } while (0) +#define safe_packlongdouble(val,buf) do { \ + assert(sizeof(val) == sizeof(long double)); \ + assert(buf->magic == BUF_MAGIC); \ + packlongdouble(val,buf); \ +} while (0) + +#define safe_unpacklongdouble(valp,buf) do { \ + assert(sizeof(*valp) == sizeof(long double)); \ + assert(buf->magic == BUF_MAGIC); \ + if (unpacklongdouble(valp,buf)) \ + goto unpack_error; \ +} while (0) + #define safe_pack64(val,buf) do { \ assert(sizeof(val) == sizeof(uint64_t)); \ assert(buf->magic == BUF_MAGIC); \ diff --git a/src/common/slurm_xlator.h b/src/common/slurm_xlator.h index 7087b25a55c..339b3e65196 100644 --- a/src/common/slurm_xlator.h +++ b/src/common/slurm_xlator.h @@ -240,6 +240,8 @@ #define unpack_time slurm_unpack_time #define packdouble slurm_packdouble #define unpackdouble slurm_unpackdouble +#define packlongdouble slurm_packlongdouble +#define unpacklongdouble slurm_unpacklongdouble #define pack64 slurm_pack64 #define unpack64 slurm_unpack64 #define pack32 slurm_pack32 -- GitLab