[llvm] 53d234e - [Support] Reduce Dependence on Host.h

Archibald Elliott via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 21 10:28:53 PST 2022


Author: Archibald Elliott
Date: 2022-11-21T18:28:07Z
New Revision: 53d234eab637307955f562974f76742b906024f5

URL: https://github.com/llvm/llvm-project/commit/53d234eab637307955f562974f76742b906024f5
DIFF: https://github.com/llvm/llvm-project/commit/53d234eab637307955f562974f76742b906024f5.diff

LOG: [Support] Reduce Dependence on Host.h

The intention behind this commit is to reduce the use of Host.h/Host.cpp
in Support, to where it is only necessary.

In this case, the endian-detection and support functionality needed by
these implementations can be provided by `Support/SwapByteOrder.h` in a
cleaner manner.

This patch also changes the byte swap in SHA256.cpp to use the byte swap
function from that header, rather than an inlined implementation.

Differential Revision: https://reviews.llvm.org/D137834

Added: 
    

Modified: 
    llvm/include/llvm/Support/OnDiskHashTable.h
    llvm/lib/Support/FoldingSet.cpp
    llvm/lib/Support/SHA1.cpp
    llvm/lib/Support/SHA256.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/OnDiskHashTable.h b/llvm/include/llvm/Support/OnDiskHashTable.h
index 11dc0de0f354..07ee8e79423b 100644
--- a/llvm/include/llvm/Support/OnDiskHashTable.h
+++ b/llvm/include/llvm/Support/OnDiskHashTable.h
@@ -17,7 +17,6 @@
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/DataTypes.h"
 #include "llvm/Support/EndianStream.h"
-#include "llvm/Support/Host.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cassert>

diff  --git a/llvm/lib/Support/FoldingSet.cpp b/llvm/lib/Support/FoldingSet.cpp
index 443a06ef7da8..ece31b971c1c 100644
--- a/llvm/lib/Support/FoldingSet.cpp
+++ b/llvm/lib/Support/FoldingSet.cpp
@@ -15,8 +15,8 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/Host.h"
 #include "llvm/Support/MathExtras.h"
+#include "llvm/Support/SwapByteOrder.h"
 #include <cassert>
 #include <cstring>
 using namespace llvm;

diff  --git a/llvm/lib/Support/SHA1.cpp b/llvm/lib/Support/SHA1.cpp
index df3658c076a8..7e66063b0760 100644
--- a/llvm/lib/Support/SHA1.cpp
+++ b/llvm/lib/Support/SHA1.cpp
@@ -18,15 +18,11 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Endian.h"
-#include "llvm/Support/Host.h"
+#include "llvm/Support/SwapByteOrder.h"
 #include <string.h>
 
 using namespace llvm;
 
-#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
-#define SHA_BIG_ENDIAN
-#endif
-
 static inline uint32_t rol(uint32_t Number, int Bits) {
   return (Number << Bits) | (Number >> (32 - Bits));
 }
@@ -192,11 +188,10 @@ void SHA1::hashBlock() {
 }
 
 void SHA1::addUncounted(uint8_t Data) {
-#ifdef SHA_BIG_ENDIAN
-  InternalState.Buffer.C[InternalState.BufferOffset] = Data;
-#else
-  InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
-#endif
+  if constexpr (sys::IsBigEndianHost)
+    InternalState.Buffer.C[InternalState.BufferOffset] = Data;
+  else
+    InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
 
   InternalState.BufferOffset++;
   if (InternalState.BufferOffset == BLOCK_LENGTH) {
@@ -267,20 +262,17 @@ void SHA1::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) {
   // Pad to complete the last block
   pad();
 
-#ifdef SHA_BIG_ENDIAN
-  // Just copy the current state
-  for (int i = 0; i < 5; i++) {
-    HashResult[i] = InternalState.State[i];
-  }
-#else
-  // Swap byte order back
-  for (int i = 0; i < 5; i++) {
-    HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
-                    (((InternalState.State[i]) << 8) & 0x00ff0000) |
-                    (((InternalState.State[i]) >> 8) & 0x0000ff00) |
-                    (((InternalState.State[i]) >> 24) & 0x000000ff);
+  if constexpr (sys::IsBigEndianHost) {
+    // Just copy the current state
+    for (int i = 0; i < 5; i++) {
+      HashResult[i] = InternalState.State[i];
+    }
+  } else {
+    // Swap byte order back
+    for (int i = 0; i < 5; i++) {
+      HashResult[i] = sys::getSwappedBytes(InternalState.State[i]);
+    }
   }
-#endif
 }
 
 std::array<uint8_t, 20> SHA1::final() {

diff  --git a/llvm/lib/Support/SHA256.cpp b/llvm/lib/Support/SHA256.cpp
index 46cbb67e86bc..b1c07f83b68f 100644
--- a/llvm/lib/Support/SHA256.cpp
+++ b/llvm/lib/Support/SHA256.cpp
@@ -23,15 +23,11 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Endian.h"
-#include "llvm/Support/Host.h"
+#include "llvm/Support/SwapByteOrder.h"
 #include <string.h>
 
 namespace llvm {
 
-#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
-#define SHA_BIG_ENDIAN
-#endif
-
 #define SHR(x, c) ((x) >> (c))
 #define ROTR(x, n) (((x) >> n) | ((x) << (32 - (n))))
 
@@ -171,11 +167,10 @@ void SHA256::hashBlock() {
 }
 
 void SHA256::addUncounted(uint8_t Data) {
-#ifdef SHA_BIG_ENDIAN
-  InternalState.Buffer.C[InternalState.BufferOffset] = Data;
-#else
-  InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
-#endif
+  if constexpr (sys::IsBigEndianHost)
+    InternalState.Buffer.C[InternalState.BufferOffset] = Data;
+  else
+    InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
 
   InternalState.BufferOffset++;
   if (InternalState.BufferOffset == BLOCK_LENGTH) {
@@ -247,20 +242,17 @@ void SHA256::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) {
   // Pad to complete the last block
   pad();
 
-#ifdef SHA_BIG_ENDIAN
-  // Just copy the current state
-  for (int i = 0; i < 8; i++) {
-    HashResult[i] = InternalState.State[i];
-  }
-#else
-  // Swap byte order back
-  for (int i = 0; i < 8; i++) {
-    HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
-                    (((InternalState.State[i]) << 8) & 0x00ff0000) |
-                    (((InternalState.State[i]) >> 8) & 0x0000ff00) |
-                    (((InternalState.State[i]) >> 24) & 0x000000ff);
+  if constexpr (sys::IsBigEndianHost) {
+    // Just copy the current state
+    for (int i = 0; i < 8; i++) {
+      HashResult[i] = InternalState.State[i];
+    }
+  } else {
+    // Swap byte order back
+    for (int i = 0; i < 8; i++) {
+      HashResult[i] = sys::getSwappedBytes(InternalState.State[i]);
+    }
   }
-#endif
 }
 
 std::array<uint8_t, 32> SHA256::final() {


        


More information about the llvm-commits mailing list