From e535e894196387b12d9fcbc271a98b21572a630b Mon Sep 17 00:00:00 2001 From: "Timothy B. Terriberry" Date: Sat, 2 Jul 2022 12:14:06 -0700 Subject: Work around UBSan unaligned access errors. The underlying objects are all 8-bit integers. Verified that the generated assembly still just uses MOVD. Signed-off-by: Jean-Marc Valin --- celt/x86/x86cpu.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/celt/x86/x86cpu.h b/celt/x86/x86cpu.h index 0de8df35..04e80489 100644 --- a/celt/x86/x86cpu.h +++ b/celt/x86/x86cpu.h @@ -56,8 +56,16 @@ int opus_select_arch(void); # endif +/*MOVD should not impose any alignment restrictions, but the C standard does, + and UBSan will report errors if we actually make unaligned accesses. + Use this to work around those restrictions (which should hopefully all get + optimized to a single MOVD instruction).*/ +#define OP_LOADU_EPI32(x) \ + (int)((*(unsigned char *)(x) | *((unsigned char *)(x) + 1) << 8U |\ + *((unsigned char *)(x) + 2) << 16U | (opus_uint32)*((unsigned char *)(x) + 3) << 24U)) + #define OP_CVTEPI8_EPI32_M32(x) \ - (_mm_cvtepi8_epi32(_mm_cvtsi32_si128(*(int *)(x)))) + (_mm_cvtepi8_epi32(_mm_cvtsi32_si128(OP_LOADU_EPI32(x)))) #define OP_CVTEPI16_EPI32_M64(x) \ (_mm_cvtepi16_epi32(_mm_loadl_epi64((__m128i *)(x)))) -- cgit v1.2.3