From 153fac480480d2eb6001f3180622853bbd2d71a7 Mon Sep 17 00:00:00 2001 From: Frederic Lecaille Date: Mon, 15 Apr 2024 09:52:25 +0200 Subject: [PATCH] MINOR: net_helper: Add support for floats/doubles. Implement (read|write)_flt() (resp. (read|write)_dbl()) to read/write floats (resp. read/write doubles) from/to an unaligned buffer. --- include/haproxy/net_helper.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/include/haproxy/net_helper.h b/include/haproxy/net_helper.h index f019d30ff..ee27ed483 100644 --- a/include/haproxy/net_helper.h +++ b/include/haproxy/net_helper.h @@ -91,6 +91,34 @@ static inline void write_ptr(void *p, const void *ptr) return write_u64(p, (uintptr_t)ptr); } +/* Read a float in native host order */ +static inline float read_flt(const void *p) +{ + const union { float flt; } __attribute__((packed))*u = p; + return u->flt; +} + +/* Write a float in native host order */ +static inline void write_flt(void *p, const float flt) +{ + union { float flt; } __attribute__((packed))*u = p; + u->flt = flt; +} + +/* Read a double in native host order */ +static inline double read_dbl(const void *p) +{ + const union { double dbl; } __attribute__((packed))*u = p; + return u->dbl; +} + +/* Write a double in native host order */ +static inline void write_dbl(void *p, const double dbl) +{ + union { double dbl; } __attribute__((packed))*u = p; + u->dbl = dbl; +} + /* Read a possibly wrapping number of bytes into destination . The * first segment is composed of bytes at p1. The remaining byte(s), if any, * are read from . may be zero and may also be larger than . The