summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff-webhosting.net>2025-09-12 21:04:27 +0200
committerJörg Frings-Fürst <debian@jff-webhosting.net>2025-09-12 21:04:27 +0200
commitafd606e6806b6c34b816893f6d04789d86a83fbc (patch)
treedfee168c5a4b33fb318e474b1c9ab4903d94c87d /include
parenta81a93beca9a077b4254488ba5617b44fca8d0c1 (diff)
parente47b9f3664c0a80dfade9306441594ad81f2232f (diff)
Merge branch 'release/debian/4.27-1'HEADdebian/4.27-1master
Diffstat (limited to 'include')
-rw-r--r--include/Makefile.am3
-rw-r--r--include/Makefile.in3
-rw-r--r--include/libHX/endian_float.h107
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 */