Upstream: https://github.com/llvm/llvm-project/issues/94726 diff --git a/lib/Target/M68k/MCTargetDesc/M68kBaseInfo.h b/lib/Target/M68k/MCTargetDesc/M68kBaseInfo.h index 1376b06bef6f..62ea03cc9760 100644 --- a/lib/Target/M68k/MCTargetDesc/M68kBaseInfo.h +++ b/lib/Target/M68k/MCTargetDesc/M68kBaseInfo.h @@ -85,9 +85,16 @@ template value_t swapWord(value_t Val) { Val = support::endian::byte_swap(Val, llvm::endianness::big); value_t NewVal = 0; for (unsigned i = 0U; i != NumWords; ++i) { - uint16_t Part = (Val >> (i * 16)) & 0xFFFF; - Part = support::endian::byte_swap(Part, llvm::endianness::big); - NewVal |= (Part << (i * 16)); + if (llvm::endianness::native == llvm::endianness::big) { + uint16_t Lo = (Val >> (i * 16)) & 0xFFFF; + NewVal |= (Lo << (++i * 16)); + uint16_t Hi = (Val >> (++i * 16)) & 0xFFFF; + NewVal |= (Hi << ((i - 1) * 16)); + } else { + uint16_t Part = (Val >> (i * 16)) & 0xFFFF; + Part = support::endian::byte_swap(Part, llvm::endianness::big); + NewVal |= (Part << (i * 16)); + } } return NewVal; }