add Relocation helper functions
authorShankar Easwaran <shankare@codeaurora.org>
Wed, 30 Jan 2013 04:49:54 +0000 (04:49 +0000)
committerShankar Easwaran <shankare@codeaurora.org>
Wed, 30 Jan 2013 04:49:54 +0000 (04:49 +0000)
llvm-svn: 173895

lld/include/lld/ReaderWriter/RelocationHelperFunctions.h [new file with mode: 0644]

diff --git a/lld/include/lld/ReaderWriter/RelocationHelperFunctions.h b/lld/include/lld/ReaderWriter/RelocationHelperFunctions.h
new file mode 100644 (file)
index 0000000..cfdbd14
--- /dev/null
@@ -0,0 +1,52 @@
+//===- lld/ReaderWriter/RelocationHelperFunctions.h------------------------===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_READER_WRITER_RELOCATION_HELPER_FUNCTIONS_H
+#define LLD_READER_WRITER_RELOCATION_HELPER_FUNCTIONS_H
+
+namespace lld {
+
+/// \brief Return the bits that are described by the mask
+template < typename T > 
+T BitsGather(T val, T mask)
+{
+  T result = 0;
+  size_t off = 0;
+  
+  for (size_t bit = 0; bit != sizeof (T) * 8; ++bit) {
+    const bool valBit = (val >> bit) & 1;
+    const bool maskBit = (mask >> bit) & 1;
+    if (maskBit) { 
+      result |= static_cast <T> (valBit) << off;
+      ++off;
+    }
+  }
+  return result;
+}
+
+template <typename T> 
+T BitsScatter(T val, T mask)
+{
+  T result = 0;
+  size_t off = 0;
+
+  for (size_t bit = 0; bit != sizeof (T) * 8; ++bit) {
+    const bool valBit = (val >> off) & 1;
+    const bool maskBit = (mask >> bit) & 1;
+    if (maskBit) {
+      result |= static_cast<T>(valBit) << bit;
+      ++off;
+    }
+  }
+  return result;
+}
+
+} // namespace lld 
+
+#endif // LLD_READER_WRITER_RELOCATION_HELPER_FUNCTIONS_H