Received: from eggs.gnu.org ([2001:4830:134:3::10]:44167) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bfqXD-0002pd-KB for qemu-devel@nongnu.org; Fri, 02 Sep 2016 11:37:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bfqXA-0006fG-BV for qemu-devel@nongnu.org; Fri, 02 Sep 2016 11:37:39 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:32934) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bfqXA-0006f3-4u for qemu-devel@nongnu.org; Fri, 02 Sep 2016 11:37:36 -0400 Received: by mail-wm0-x243.google.com with SMTP id w207so3343655wmw.0 for ; Fri, 02 Sep 2016 08:37:35 -0700 (PDT) Received: from 640k.lan (dynamic-adsl-78-12-240-23.clienti.tiscali.it. [78.12.240.23]) by smtp.gmail.com with ESMTPSA id f10sm10996493wje.14.2016.09.02.08.37.32 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Sep 2016 08:37:32 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Fri, 2 Sep 2016 17:37:30 +0200 Message-Id: <1472830650-8283-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH] optionrom: do not rely on compiler's bswap optimization List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Recent compilers can detect and inline manually-written bswap code, but GCC 4.2.1 (the last GPLv2 version) cannot and generates really awful code. Depending on how the compiler is configured, it might also not want to generate bswap because it was not in i386. Using asm is fine because TCG knows about bswap and all processors with virtualization extensions also do. Reported-by: Peter Maydell Signed-off-by: Paolo Bonzini --- pc-bios/optionrom/linuxboot_dma.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/pc-bios/optionrom/linuxboot_dma.c b/pc-bios/optionrom/linuxboot_dma.c index 7549797..4754282 100644 --- a/pc-bios/optionrom/linuxboot_dma.c +++ b/pc-bios/optionrom/linuxboot_dma.c @@ -122,24 +122,14 @@ static inline void writel_es(uint16_t offset, uint32_t val) static inline uint32_t bswap32(uint32_t x) { - return - ((x & 0x000000ffU) << 24) | - ((x & 0x0000ff00U) << 8) | - ((x & 0x00ff0000U) >> 8) | - ((x & 0xff000000U) >> 24); + asm("bswapl %0" : "=r" (x) : "0" (x)); + return x; } static inline uint64_t bswap64(uint64_t x) { - return - ((x & 0x00000000000000ffULL) << 56) | - ((x & 0x000000000000ff00ULL) << 40) | - ((x & 0x0000000000ff0000ULL) << 24) | - ((x & 0x00000000ff000000ULL) << 8) | - ((x & 0x000000ff00000000ULL) >> 8) | - ((x & 0x0000ff0000000000ULL) >> 24) | - ((x & 0x00ff000000000000ULL) >> 40) | - ((x & 0xff00000000000000ULL) >> 56); + asm("bswapl %%eax; bswapl %%edx; xchg %%eax, %%edx" : "=A" (x) : "0" (x)); + return x; } static inline uint64_t cpu_to_be64(uint64_t x) -- 1.8.3.1