Unaligned memcpy API is buried within 'qemu/bswap.h',
supposed to be related to endianness swapping. Extract
to a new header to clarify.
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
MAINTAINERS | 1 +
include/qemu/bswap.h | 62 +-------------------------------
include/qemu/ldst_unaligned.h | 67 +++++++++++++++++++++++++++++++++++
accel/tcg/translator.c | 1 +
hw/display/ati_2d.c | 1 +
hw/display/sm501.c | 2 +-
hw/remote/vfio-user-obj.c | 1 +
hw/vmapple/virtio-blk.c | 1 +
net/checksum.c | 1 +
ui/vnc-enc-tight.c | 1 +
util/bufferiszero.c | 2 +-
11 files changed, 77 insertions(+), 63 deletions(-)
create mode 100644 include/qemu/ldst_unaligned.h
diff --git a/MAINTAINERS b/MAINTAINERS
index 9a55b649e8b..6602c9891db 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3257,6 +3257,7 @@ R: Philippe Mathieu-Daudé <philmd@linaro.org>
S: Supported
F: include/system/ioport.h
F: include/exec/memop.h
+F: include/qemu/ldst_unaligned.h
F: include/system/ram_addr.h
F: include/system/memory.h
F: include/system/physmem.h
diff --git a/include/qemu/bswap.h b/include/qemu/bswap.h
index 39ba64046a6..e70452b425a 100644
--- a/include/qemu/bswap.h
+++ b/include/qemu/bswap.h
@@ -1,6 +1,7 @@
#ifndef BSWAP_H
#define BSWAP_H
+#include "qemu/ldst_unaligned.h"
#include "qemu/target-info.h"
#undef bswap16
@@ -173,8 +174,6 @@ CPU_CONVERT(le, 64, uint64_t)
# define const_le16(_x) (_x)
#endif
-/* unaligned/endian-independent pointer access */
-
/*
* the generic syntax is:
*
@@ -201,7 +200,6 @@ CPU_CONVERT(le, 64, uint64_t)
* q: 64 bits
*
* endian is:
- * he : host endian
* be : big endian
* le : little endian
* te : target endian
@@ -237,64 +235,6 @@ static inline void stb_p(void *ptr, uint8_t v)
*(uint8_t *)ptr = v;
}
-/*
- * Any compiler worth its salt will turn these memcpy into native unaligned
- * operations. Thus we don't need to play games with packed attributes, or
- * inline byte-by-byte stores.
- * Some compilation environments (eg some fortify-source implementations)
- * may intercept memcpy() in a way that defeats the compiler optimization,
- * though, so we use __builtin_memcpy() to give ourselves the best chance
- * of good performance.
- */
-
-static inline int lduw_he_p(const void *ptr)
-{
- uint16_t r;
- __builtin_memcpy(&r, ptr, sizeof(r));
- return r;
-}
-
-static inline int ldsw_he_p(const void *ptr)
-{
- int16_t r;
- __builtin_memcpy(&r, ptr, sizeof(r));
- return r;
-}
-
-static inline void stw_he_p(void *ptr, uint16_t v)
-{
- __builtin_memcpy(ptr, &v, sizeof(v));
-}
-
-static inline void st24_he_p(void *ptr, uint32_t v)
-{
- __builtin_memcpy(ptr, &v, 3);
-}
-
-static inline int ldl_he_p(const void *ptr)
-{
- int32_t r;
- __builtin_memcpy(&r, ptr, sizeof(r));
- return r;
-}
-
-static inline void stl_he_p(void *ptr, uint32_t v)
-{
- __builtin_memcpy(ptr, &v, sizeof(v));
-}
-
-static inline uint64_t ldq_he_p(const void *ptr)
-{
- uint64_t r;
- __builtin_memcpy(&r, ptr, sizeof(r));
- return r;
-}
-
-static inline void stq_he_p(void *ptr, uint64_t v)
-{
- __builtin_memcpy(ptr, &v, sizeof(v));
-}
-
static inline int lduw_le_p(const void *ptr)
{
return (uint16_t)le_bswap(lduw_he_p(ptr), 16);
diff --git a/include/qemu/ldst_unaligned.h b/include/qemu/ldst_unaligned.h
new file mode 100644
index 00000000000..201e32d0734
--- /dev/null
+++ b/include/qemu/ldst_unaligned.h
@@ -0,0 +1,67 @@
+/*
+ * QEMU unaligned/endian-independent host pointer access
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef QEMU_LDST_UNALIGNED_H
+#define QEMU_LDST_UNALIGNED_H
+
+/*
+ * Any compiler worth its salt will turn these memcpy into native unaligned
+ * operations. Thus we don't need to play games with packed attributes, or
+ * inline byte-by-byte stores.
+ * Some compilation environments (eg some fortify-source implementations)
+ * may intercept memcpy() in a way that defeats the compiler optimization,
+ * though, so we use __builtin_memcpy() to give ourselves the best chance
+ * of good performance.
+ */
+
+static inline int lduw_he_p(const void *ptr)
+{
+ uint16_t r;
+ __builtin_memcpy(&r, ptr, sizeof(r));
+ return r;
+}
+
+static inline int ldsw_he_p(const void *ptr)
+{
+ int16_t r;
+ __builtin_memcpy(&r, ptr, sizeof(r));
+ return r;
+}
+
+static inline void stw_he_p(void *ptr, uint16_t v)
+{
+ __builtin_memcpy(ptr, &v, sizeof(v));
+}
+
+static inline void st24_he_p(void *ptr, uint32_t v)
+{
+ __builtin_memcpy(ptr, &v, 3);
+}
+
+static inline int ldl_he_p(const void *ptr)
+{
+ int32_t r;
+ __builtin_memcpy(&r, ptr, sizeof(r));
+ return r;
+}
+
+static inline void stl_he_p(void *ptr, uint32_t v)
+{
+ __builtin_memcpy(ptr, &v, sizeof(v));
+}
+
+static inline uint64_t ldq_he_p(const void *ptr)
+{
+ uint64_t r;
+ __builtin_memcpy(&r, ptr, sizeof(r));
+ return r;
+}
+
+static inline void stq_he_p(void *ptr, uint64_t v)
+{
+ __builtin_memcpy(ptr, &v, sizeof(v));
+}
+
+#endif
diff --git a/accel/tcg/translator.c b/accel/tcg/translator.c
index 034f2f359ef..d767e5dff24 100644
--- a/accel/tcg/translator.c
+++ b/accel/tcg/translator.c
@@ -9,6 +9,7 @@
#include "qemu/osdep.h"
#include "qemu/bswap.h"
+#include "qemu/ldst_unaligned.h"
#include "qemu/log.h"
#include "qemu/error-report.h"
#include "accel/tcg/cpu-ldst-common.h"
diff --git a/hw/display/ati_2d.c b/hw/display/ati_2d.c
index 309bb5ccb6c..08f722cd63e 100644
--- a/hw/display/ati_2d.c
+++ b/hw/display/ati_2d.c
@@ -10,6 +10,7 @@
#include "qemu/osdep.h"
#include "ati_int.h"
#include "ati_regs.h"
+#include "qemu/ldst_unaligned.h"
#include "qemu/log.h"
#include "ui/pixel_ops.h"
#include "ui/console.h"
diff --git a/hw/display/sm501.c b/hw/display/sm501.c
index a07aa9886f9..1c38b17b04c 100644
--- a/hw/display/sm501.c
+++ b/hw/display/sm501.c
@@ -28,6 +28,7 @@
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "qemu/log.h"
+#include "qemu/ldst_unaligned.h"
#include "qemu/module.h"
#include "hw/usb/hcd-ohci.h"
#include "hw/char/serial-mm.h"
@@ -40,7 +41,6 @@
#include "hw/display/i2c-ddc.h"
#include "qemu/range.h"
#include "ui/pixel_ops.h"
-#include "qemu/bswap.h"
#include "trace.h"
#include "qom/object.h"
diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c
index 4eb036a5469..8d99b78245d 100644
--- a/hw/remote/vfio-user-obj.c
+++ b/hw/remote/vfio-user-obj.c
@@ -49,6 +49,7 @@
#include "qapi/error.h"
#include "qapi/qapi-visit-sockets.h"
#include "qapi/qapi-events-misc.h"
+#include "qemu/ldst_unaligned.h"
#include "qemu/notify.h"
#include "qemu/thread.h"
#include "qemu/main-loop.h"
diff --git a/hw/vmapple/virtio-blk.c b/hw/vmapple/virtio-blk.c
index 9de9aaae0bf..3acb29eea88 100644
--- a/hw/vmapple/virtio-blk.c
+++ b/hw/vmapple/virtio-blk.c
@@ -19,6 +19,7 @@
#include "hw/vmapple/vmapple.h"
#include "hw/virtio/virtio-blk.h"
#include "hw/virtio/virtio-pci.h"
+#include "qemu/ldst_unaligned.h"
#include "qemu/log.h"
#include "qemu/module.h"
#include "qapi/error.h"
diff --git a/net/checksum.c b/net/checksum.c
index 537457d89d0..ea55b468060 100644
--- a/net/checksum.c
+++ b/net/checksum.c
@@ -16,6 +16,7 @@
*/
#include "qemu/osdep.h"
+#include "qemu/ldst_unaligned.h"
#include "net/checksum.h"
#include "net/eth.h"
diff --git a/ui/vnc-enc-tight.c b/ui/vnc-enc-tight.c
index 9dfe6ae5a24..78ac7a2eacc 100644
--- a/ui/vnc-enc-tight.c
+++ b/ui/vnc-enc-tight.c
@@ -42,6 +42,7 @@
#include <jpeglib.h>
#endif
+#include "qemu/ldst_unaligned.h"
#include "qemu/bswap.h"
#include "vnc.h"
#include "vnc-enc-tight.h"
diff --git a/util/bufferiszero.c b/util/bufferiszero.c
index 522146dab97..ca38606032d 100644
--- a/util/bufferiszero.c
+++ b/util/bufferiszero.c
@@ -23,7 +23,7 @@
*/
#include "qemu/osdep.h"
#include "qemu/cutils.h"
-#include "qemu/bswap.h"
+#include "qemu/ldst_unaligned.h"
#include "host/cpuinfo.h"
typedef bool (*biz_accel_fn)(const void *, size_t);
--
2.52.0
On Fri, 9 Jan 2026 at 06:35, Philippe Mathieu-Daudé <philmd@linaro.org> wrote: > > Unaligned memcpy API is buried within 'qemu/bswap.h', > supposed to be related to endianness swapping. Extract > to a new header to clarify. > > Suggested-by: Paolo Bonzini <pbonzini@redhat.com> > Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> I'm not very enthusiastic about this one, because it takes one set of functions out of a family of ones with related names, and moves it into a different header and drops it from the documentation comment: > -/* unaligned/endian-independent pointer access */ > - > /* > * the generic syntax is: > * > @@ -201,7 +200,6 @@ CPU_CONVERT(le, 64, uint64_t) > * q: 64 bits > * > * endian is: > - * he : host endian > * be : big endian > * le : little endian > * te : target endian These are all doing variants on the same thing, which is why they're all in one place and have the "same pattern" names they do. thanks -- PMM
On 1/9/26 17:35, Philippe Mathieu-Daudé wrote: > Unaligned memcpy API is buried within 'qemu/bswap.h', > supposed to be related to endianness swapping. Extract > to a new header to clarify. > > Suggested-by: Paolo Bonzini<pbonzini@redhat.com> > Signed-off-by: Philippe Mathieu-Daudé<philmd@linaro.org> > --- > MAINTAINERS | 1 + > include/qemu/bswap.h | 62 +------------------------------- > include/qemu/ldst_unaligned.h | 67 +++++++++++++++++++++++++++++++++++ > accel/tcg/translator.c | 1 + > hw/display/ati_2d.c | 1 + > hw/display/sm501.c | 2 +- > hw/remote/vfio-user-obj.c | 1 + > hw/vmapple/virtio-blk.c | 1 + > net/checksum.c | 1 + > ui/vnc-enc-tight.c | 1 + > util/bufferiszero.c | 2 +- > 11 files changed, 77 insertions(+), 63 deletions(-) > create mode 100644 include/qemu/ldst_unaligned.h Reviewed-by: Richard Henderson <richard.henderson@linaro.org> r~
On Fri, 9 Jan 2026, Philippe Mathieu-Daudé wrote: > Unaligned memcpy API is buried within 'qemu/bswap.h', > supposed to be related to endianness swapping. Extract > to a new header to clarify. > > Suggested-by: Paolo Bonzini <pbonzini@redhat.com> > Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> > --- > MAINTAINERS | 1 + > include/qemu/bswap.h | 62 +------------------------------- > include/qemu/ldst_unaligned.h | 67 +++++++++++++++++++++++++++++++++++ > accel/tcg/translator.c | 1 + > hw/display/ati_2d.c | 1 + > hw/display/sm501.c | 2 +- > hw/remote/vfio-user-obj.c | 1 + > hw/vmapple/virtio-blk.c | 1 + > net/checksum.c | 1 + > ui/vnc-enc-tight.c | 1 + > util/bufferiszero.c | 2 +- > 11 files changed, 77 insertions(+), 63 deletions(-) > create mode 100644 include/qemu/ldst_unaligned.h Acked-by: BALATON Zoltan <balaton@eik.bme.hu>
© 2016 - 2026 Red Hat, Inc.