Hi Mark, On Thu, Nov 26, 2026 at 8:56 PM Mark Wielaard <[email protected]> wrote: > > Replace the ALLOW_UNALIGNED and regular read unaligned functions with > a simple memcpy variant. It generates the same code but doesn't rely > on undefined behavior or packaged unions which are a GCC extension. > > Drop the read_4ubyte_unaligned_noncvt variant, which wasn't used. > > * libcpu/memory-access.h (read_[248][us]byte_unaligned): > Rewrite using memcpy. > (read_4ubyte_unaligned_noncvt): Removed. > > Signed-off-by: Mark Wielaard <[email protected]>
LGTM. Aaron > --- > > V2 no changes from V1. > > libcpu/memory-access.h | 96 ++++++++---------------------------------- > 1 file changed, 18 insertions(+), 78 deletions(-) > > diff --git a/libcpu/memory-access.h b/libcpu/memory-access.h > index 6e92fc5b7a65..71eff6043eef 100644 > --- a/libcpu/memory-access.h > +++ b/libcpu/memory-access.h > @@ -1,5 +1,6 @@ > /* Unaligned memory access functionality. > Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2008 Red Hat, Inc. > + Copyright (C) 2026 Mark J. Wielaard <[email protected]> > Written by Ulrich Drepper <[email protected]>, 2001. > > This file is free software; you can redistribute it and/or modify > @@ -45,112 +46,51 @@ > #endif > > > -/* We use simple memory access functions in case the hardware allows it. > - The caller has to make sure we don't have alias problems. */ > -#if ALLOW_UNALIGNED > - > -# define read_2ubyte_unaligned(Addr) \ > - (unlikely (MACHINE_ENCODING != BYTE_ORDER) > \ > - ? bswap_16 (*((const uint16_t *) (Addr))) > \ > - : *((const uint16_t *) (Addr))) > -# define read_2sbyte_unaligned(Addr) \ > - (unlikely (MACHINE_ENCODING != BYTE_ORDER) > \ > - ? (int16_t) bswap_16 (*((const int16_t *) (Addr))) > \ > - : *((const int16_t *) (Addr))) > - > -# define read_4ubyte_unaligned_noncvt(Addr) \ > - *((const uint32_t *) (Addr)) > -# define read_4ubyte_unaligned(Addr) \ > - (unlikely (MACHINE_ENCODING != BYTE_ORDER) > \ > - ? bswap_32 (*((const uint32_t *) (Addr))) > \ > - : *((const uint32_t *) (Addr))) > -# define read_4sbyte_unaligned(Addr) \ > - (unlikely (MACHINE_ENCODING != BYTE_ORDER) > \ > - ? (int32_t) bswap_32 (*((const int32_t *) (Addr))) > \ > - : *((const int32_t *) (Addr))) > - > -# define read_8ubyte_unaligned(Addr) \ > - (unlikely (MACHINE_ENCODING != BYTE_ORDER) > \ > - ? bswap_64 (*((const uint64_t *) (Addr))) > \ > - : *((const uint64_t *) (Addr))) > -# define read_8sbyte_unaligned(Addr) \ > - (unlikely (MACHINE_ENCODING != BYTE_ORDER) > \ > - ? (int64_t) bswap_64 (*((const int64_t *) (Addr))) > \ > - : *((const int64_t *) (Addr))) > - > -#else > - > -union unaligned > - { > - void *p; > - uint16_t u2; > - uint32_t u4; > - uint64_t u8; > - int16_t s2; > - int32_t s4; > - int64_t s8; > - } attribute_packed; > +/* We use memcpy to read the values so that the compiler takes care of > + alignment. The caller has to make sure we don't have alias problems. */ > > static inline uint16_t > read_2ubyte_unaligned (const void *p) > { > - const union unaligned *up = p; > - if (MACHINE_ENCODING != BYTE_ORDER) > - return bswap_16 (up->u2); > - return up->u2; > + uint16_t v; > + memcpy (&v, p, sizeof v); > + return unlikely (MACHINE_ENCODING != BYTE_ORDER) ? bswap_16 (v) : v; > } > + > static inline int16_t > read_2sbyte_unaligned (const void *p) > { > - const union unaligned *up = p; > - if (MACHINE_ENCODING != BYTE_ORDER) > - return (int16_t) bswap_16 (up->u2); > - return up->s2; > + return (int16_t) read_2ubyte_unaligned (p); > } > > -static inline uint32_t > -read_4ubyte_unaligned_noncvt (const void *p) > -{ > - const union unaligned *up = p; > - return up->u4; > -} > static inline uint32_t > read_4ubyte_unaligned (const void *p) > { > - const union unaligned *up = p; > - if (MACHINE_ENCODING != BYTE_ORDER) > - return bswap_32 (up->u4); > - return up->u4; > + uint32_t v; > + memcpy (&v, p, sizeof v); > + return unlikely (MACHINE_ENCODING != BYTE_ORDER) ? bswap_32 (v) : v; > } > + > static inline int32_t > read_4sbyte_unaligned (const void *p) > { > - const union unaligned *up = p; > - if (MACHINE_ENCODING != BYTE_ORDER) > - return (int32_t) bswap_32 (up->u4); > - return up->s4; > + return (int32_t) read_4ubyte_unaligned (p); > } > > static inline uint64_t > read_8ubyte_unaligned (const void *p) > { > - const union unaligned *up = p; > - if (MACHINE_ENCODING != BYTE_ORDER) > - return bswap_64 (up->u8); > - return up->u8; > + uint64_t v; > + memcpy (&v, p, sizeof v); > + return unlikely (MACHINE_ENCODING != BYTE_ORDER) ? bswap_64 (v) : v; > } > + > static inline int64_t > read_8sbyte_unaligned (const void *p) > { > - const union unaligned *up = p; > - if (MACHINE_ENCODING != BYTE_ORDER) > - return (int64_t) bswap_64 (up->u8); > - return up->s8; > + return (int64_t) read_8ubyte_unaligned (p); > } > > -#endif /* allow unaligned */ > - > - > #define read_2ubyte_unaligned_inc(Addr) \ > ({ uint16_t t_ = read_2ubyte_unaligned (Addr); > \ > Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 2); > \ > -- > 2.56.1 >
