[PATCH] Add getSwappedBytes implementations for float / double

Matt Arsenault Matthew.Arsenault at amd.com
Thu Dec 18 13:53:22 PST 2014


Alternatively these could be special cased in endian::Writer

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D6730

Files:
  include/llvm/Support/SwapByteOrder.h

Index: include/llvm/Support/SwapByteOrder.h
===================================================================
--- include/llvm/Support/SwapByteOrder.h
+++ include/llvm/Support/SwapByteOrder.h
@@ -19,6 +19,7 @@
 #include "llvm/Support/DataTypes.h"
 #include <cstddef>
 #include <limits>
+#include <string>
 
 namespace llvm {
 namespace sys {
@@ -77,6 +78,26 @@
 inline unsigned int   getSwappedBytes(unsigned int   C) { return SwapByteOrder_32(C); }
 inline   signed int   getSwappedBytes(  signed int   C) { return SwapByteOrder_32(C); }
 
+inline uint32_t getSwappedBytes(float C) {
+  union {
+    uint32_t I;
+    float F;
+  } Tmp;
+
+  std::memcpy(&Tmp, &C, sizeof(float));
+  return SwapByteOrder_32(Tmp.I);
+}
+
+inline uint64_t getSwappedBytes(double C) {
+  union {
+    uint64_t I;
+    double F;
+  } Tmp;
+
+  std::memcpy(&Tmp, &C, sizeof(double));
+  return SwapByteOrder_64(Tmp.I);
+}
+
 #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); }

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D6730.17471.patch
Type: text/x-patch
Size: 1115 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141218/f2736c07/attachment.bin>


More information about the llvm-commits mailing list