[llvm] 30dae38 - Fix LLP64 detection in SwapByteOrder.h

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 26 10:59:52 PST 2020


Author: Reid Kleckner
Date: 2020-02-26T10:59:45-08:00
New Revision: 30dae38349cbe293bee0705b313dc747e53afd49

URL: https://github.com/llvm/llvm-project/commit/30dae38349cbe293bee0705b313dc747e53afd49
DIFF: https://github.com/llvm/llvm-project/commit/30dae38349cbe293bee0705b313dc747e53afd49.diff

LOG: Fix LLP64 detection in SwapByteOrder.h

MSVC does not define __LONG_MAX__, so we were just getting lucky in this
conditional:
  #if __LONG_MAX__ == __INT_MAX__

Undefined identifiers evaluate to zero in preprocessor conditionals, so
this became true, which happens to work for MSVC platforms.

Instead, use this pattern and let the compiler constant fold:
  return sizeof(long) == sizeof(int) ? SwapByteOrder_32((uint32_t)C)
                                     : SwapByteOrder_64((uint64_t)C);

Added: 
    

Modified: 
    llvm/include/llvm/Support/SwapByteOrder.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/SwapByteOrder.h b/llvm/include/llvm/Support/SwapByteOrder.h
index 94ba1aa0a266..a8bfc39c7049 100644
--- a/llvm/include/llvm/Support/SwapByteOrder.h
+++ b/llvm/include/llvm/Support/SwapByteOrder.h
@@ -104,15 +104,16 @@ inline   signed short getSwappedBytes(  signed short C) { return SwapByteOrder_1
 inline unsigned int   getSwappedBytes(unsigned int   C) { return SwapByteOrder_32(C); }
 inline   signed int   getSwappedBytes(  signed int   C) { return SwapByteOrder_32(C); }
 
-#if __LONG_MAX__ == __INT_MAX__
-inline unsigned long  getSwappedBytes(unsigned long  C) { return SwapByteOrder_32(C); }
-inline   signed long  getSwappedBytes(  signed long  C) { return SwapByteOrder_32(C); }
-#elif __LONG_MAX__ == __LONG_LONG_MAX__
-inline unsigned long  getSwappedBytes(unsigned long  C) { return SwapByteOrder_64(C); }
-inline   signed long  getSwappedBytes(  signed long  C) { return SwapByteOrder_64(C); }
-#else
-#error "Unknown long size!"
-#endif
+inline unsigned long getSwappedBytes(unsigned long C) {
+  // Handle LLP64 and LP64 platforms.
+  return sizeof(long) == sizeof(int) ? SwapByteOrder_32((uint32_t)C)
+                                     : SwapByteOrder_64((uint64_t)C);
+}
+inline signed long getSwappedBytes(signed long C) {
+  // Handle LLP64 and LP64 platforms.
+  return sizeof(long) == sizeof(int) ? SwapByteOrder_32((uint32_t)C)
+                                     : SwapByteOrder_64((uint64_t)C);
+}
 
 inline unsigned long long getSwappedBytes(unsigned long long C) {
   return SwapByteOrder_64(C);


        


More information about the llvm-commits mailing list