From c7547dbd5eeed5fd24ec96dfc64d5ae98cdcb7c2 Mon Sep 17 00:00:00 2001 From: Helmut Date: Mon, 28 Jan 2019 11:25:33 -0800 Subject: [PATCH] Fix compiler error in swapBytes64 for rare architectures (#16418) Summary: swapBytes64 used to use SwapByteOrder_32 and value, both of which dont exist. This commit rewrites that part from scratch. This happened on Debugbuild on Microsoft compiler. For that case " && !defined(_DEBUG)" is also removed, because _byteswap_uint64 works fine in debug mode (if it is necessary it should me commented why). Pull Request resolved: https://github.com/pytorch/pytorch/pull/16418 Differential Revision: D13843306 Pulled By: ezyang fbshipit-source-id: dde1c7baeccec3aaa750d4b7200b3f4ccb4a00cb --- torch/csrc/byte_order.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/torch/csrc/byte_order.cpp b/torch/csrc/byte_order.cpp index 263e627..01f671f 100644 --- a/torch/csrc/byte_order.cpp +++ b/torch/csrc/byte_order.cpp @@ -44,14 +44,21 @@ static inline void swapBytes64(void *ptr) { uint64_t output; memcpy(&output, ptr, sizeof(uint64_t)); -#if defined(_MSC_VER) && !defined(_DEBUG) +#if defined(_MSC_VER) output = _byteswap_uint64(output); #elif defined(__llvm__) || defined(__GNUC__) && !defined(__ICC) output = __builtin_bswap64(output); #else - uint64_t Hi = SwapByteOrder_32(uint32_t(value)); - uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32)); - return (Hi << 32) | Lo; + uint64_t Byte0 = output & 0x00000000000000FF; + uint64_t Byte1 = output & 0x000000000000FF00; + uint64_t Byte2 = output & 0x0000000000FF0000; + uint64_t Byte3 = output & 0x00000000FF000000; + uint64_t Byte4 = output & 0x000000FF00000000; + uint64_t Byte5 = output & 0x0000FF0000000000; + uint64_t Byte6 = output & 0x00FF000000000000; + uint64_t Byte7 = output & 0xFF00000000000000; + output = (Byte0 << (7*8)) | (Byte1 << (5*8)) | (Byte2 << (3*8)) | (Byte3 << (1*8)) | + (Byte7 >> (7*8)) | (Byte6 >> (5*8)) | (Byte5 >> (3*8)) | (Byte4 >> (1*8)); #endif memcpy(ptr, &output, sizeof(uint64_t)); } -- 2.7.4