diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2025-09-11 17:19:29 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2025-09-11 17:19:29 +0200 |
commit | 50d223b12c1319b4b9c4a5b8e34866c46996cb36 (patch) | |
tree | 1d8d7297afaa443f7945c9c3fe6668cc816c8ffe /include | |
parent | cec79a3f5578da4a9f9085282389482edf45c81b (diff) |
New upstream version 4.27upstream/4.27upstream
Diffstat (limited to 'include')
-rw-r--r-- | include/Makefile.am | 3 | ||||
-rw-r--r-- | include/Makefile.in | 3 | ||||
-rw-r--r-- | include/libHX/endian_float.h | 107 |
3 files changed, 111 insertions, 2 deletions
diff --git a/include/Makefile.am b/include/Makefile.am index 82b8289..e3d3f86 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,7 +1,8 @@ # -*- Makefile -*- nobase_include_HEADERS = libHX.h libHX/cast.h \ - libHX/ctype_helper.h libHX/defs.h libHX/deque.h libHX/endian.h libHX/init.h \ + libHX/ctype_helper.h libHX/defs.h libHX/deque.h \ + libHX/endian.h libHX/endian_float.h libHX/init.h \ libHX/intdiff.hpp libHX/io.h libHX/list.h \ libHX/map.h libHX/misc.h libHX/option.h libHX/proc.h \ libHX/scope.hpp libHX/socket.h libHX/string.h \ diff --git a/include/Makefile.in b/include/Makefile.in index 0bfc881..7c0e74c 100644 --- a/include/Makefile.in +++ b/include/Makefile.in @@ -307,7 +307,8 @@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ nobase_include_HEADERS = libHX.h libHX/cast.h \ - libHX/ctype_helper.h libHX/defs.h libHX/deque.h libHX/endian.h libHX/init.h \ + libHX/ctype_helper.h libHX/defs.h libHX/deque.h \ + libHX/endian.h libHX/endian_float.h libHX/init.h \ libHX/intdiff.hpp libHX/io.h libHX/list.h \ libHX/map.h libHX/misc.h libHX/option.h libHX/proc.h \ libHX/scope.hpp libHX/socket.h libHX/string.h \ diff --git a/include/libHX/endian_float.h b/include/libHX/endian_float.h new file mode 100644 index 0000000..f36e110 --- /dev/null +++ b/include/libHX/endian_float.h @@ -0,0 +1,107 @@ +#ifndef _LIBHX_ENDIAN_FLOAT_H +#define _LIBHX_ENDIAN_FLOAT_H 1 +#include <string.h> +#include <libHX/endian.h> + +/* + * While construction of integers from bytes was easy, it would be more work + * for floats — and compilers probably won't be able to optimize it. + * + * So then, we make some shortcuts/assumptions here in endian_float.h: + * - that the host platform uses the same byte order for integers as for floats + * - that the host platform is using IEEE754/IEC559 + * + * This holds for the typical Linux on {arm gnueabi LE, arm gnueabi + * BE, aarch64 LE, aarch64 BE, i386, amd64, hppa, loongarch64, m68k, + * mips, ppc64, ppc64le, sparc, sparc64, riscv64, s390x}. + */ + +/* + * Unlike cpu_to_be32, we will offer no float_cpu_to_be32. Values comprised of + * inverted bytes should probably not be passed around in memory. + */ +LIBHX_DBG_INLINE float LIBHX_OPT_O2 float_be32p_to_cpu(const void *p) +{ + uint32_t v = be32p_to_cpu(p); + float w; +#ifdef __cplusplus + static_assert(sizeof(v) == sizeof(w)); +#endif + memcpy(&w, &v, sizeof(w)); + return w; +} + +LIBHX_DBG_INLINE double LIBHX_OPT_O2 float_le32p_to_cpu(const void *p) +{ + uint32_t v = le32p_to_cpu(p); + float w; +#ifdef __cplusplus + static_assert(sizeof(v) == sizeof(w)); +#endif + memcpy(&w, &v, sizeof(w)); + return w; +} + +LIBHX_DBG_INLINE float LIBHX_OPT_O2 float_be64p_to_cpu(const void *p) +{ + uint64_t v = be64p_to_cpu(p); + double w; +#ifdef __cplusplus + static_assert(sizeof(v) == sizeof(w)); +#endif + memcpy(&w, &v, sizeof(w)); + return w; +} + +LIBHX_DBG_INLINE double LIBHX_OPT_O2 float_le64p_to_cpu(const void *p) +{ + uint64_t v = le64p_to_cpu(p); + double w; +#ifdef __cplusplus + static_assert(sizeof(v) == sizeof(w)); +#endif + memcpy(&w, &v, sizeof(w)); + return w; +} + +LIBHX_DBG_INLINE void LIBHX_OPT_O2 float_cpu_to_be32p(void *p, float v) +{ + uint32_t w; +#ifdef __cplusplus + static_assert(sizeof(v) == sizeof(w)); +#endif + memcpy(&w, &v, sizeof(w)); + cpu_to_be32p(p, w); +} + +LIBHX_DBG_INLINE void LIBHX_OPT_O2 float_cpu_to_le32p(void *p, float v) +{ + uint32_t w; +#ifdef __cplusplus + static_assert(sizeof(v) == sizeof(w)); +#endif + memcpy(&w, &v, sizeof(w)); + cpu_to_le32p(p, w); +} + +LIBHX_DBG_INLINE void LIBHX_OPT_O2 float_cpu_to_be64p(void *p, double v) +{ + uint64_t w; +#ifdef __cplusplus + static_assert(sizeof(v) == sizeof(w)); +#endif + memcpy(&w, &v, sizeof(w)); + cpu_to_be64p(p, w); +} + +LIBHX_DBG_INLINE void LIBHX_OPT_O2 float_cpu_to_le64p(void *p, double v) +{ + uint64_t w; +#ifdef __cplusplus + static_assert(sizeof(v) == sizeof(w)); +#endif + memcpy(&w, &v, sizeof(w)); + cpu_to_le64p(p, w); +} + +#endif /* _LIBHX_ENDIAN_FLOAT_H */ |