Skip to content

Commit 53d234e

Browse files
committed
[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
1 parent ac93b61 commit 53d234e

File tree

4 files changed

+31
-48
lines changed

4 files changed

+31
-48
lines changed

llvm/include/llvm/Support/OnDiskHashTable.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "llvm/Support/Allocator.h"
1818
#include "llvm/Support/DataTypes.h"
1919
#include "llvm/Support/EndianStream.h"
20-
#include "llvm/Support/Host.h"
2120
#include "llvm/Support/MathExtras.h"
2221
#include "llvm/Support/raw_ostream.h"
2322
#include <cassert>

llvm/lib/Support/FoldingSet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "llvm/ADT/StringRef.h"
1616
#include "llvm/Support/Allocator.h"
1717
#include "llvm/Support/ErrorHandling.h"
18-
#include "llvm/Support/Host.h"
1918
#include "llvm/Support/MathExtras.h"
19+
#include "llvm/Support/SwapByteOrder.h"
2020
#include <cassert>
2121
#include <cstring>
2222
using namespace llvm;

llvm/lib/Support/SHA1.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
1818
#include "llvm/ADT/ArrayRef.h"
1919
#include "llvm/ADT/StringRef.h"
2020
#include "llvm/Support/Endian.h"
21-
#include "llvm/Support/Host.h"
21+
#include "llvm/Support/SwapByteOrder.h"
2222
#include <string.h>
2323

2424
using namespace llvm;
2525

26-
#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
27-
#define SHA_BIG_ENDIAN
28-
#endif
29-
3026
static inline uint32_t rol(uint32_t Number, int Bits) {
3127
return (Number << Bits) | (Number >> (32 - Bits));
3228
}
@@ -192,11 +188,10 @@ void SHA1::hashBlock() {
192188
}
193189

194190
void SHA1::addUncounted(uint8_t Data) {
195-
#ifdef SHA_BIG_ENDIAN
196-
InternalState.Buffer.C[InternalState.BufferOffset] = Data;
197-
#else
198-
InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
199-
#endif
191+
if constexpr (sys::IsBigEndianHost)
192+
InternalState.Buffer.C[InternalState.BufferOffset] = Data;
193+
else
194+
InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
200195

201196
InternalState.BufferOffset++;
202197
if (InternalState.BufferOffset == BLOCK_LENGTH) {
@@ -267,20 +262,17 @@ void SHA1::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) {
267262
// Pad to complete the last block
268263
pad();
269264

270-
#ifdef SHA_BIG_ENDIAN
271-
// Just copy the current state
272-
for (int i = 0; i < 5; i++) {
273-
HashResult[i] = InternalState.State[i];
274-
}
275-
#else
276-
// Swap byte order back
277-
for (int i = 0; i < 5; i++) {
278-
HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
279-
(((InternalState.State[i]) << 8) & 0x00ff0000) |
280-
(((InternalState.State[i]) >> 8) & 0x0000ff00) |
281-
(((InternalState.State[i]) >> 24) & 0x000000ff);
265+
if constexpr (sys::IsBigEndianHost) {
266+
// Just copy the current state
267+
for (int i = 0; i < 5; i++) {
268+
HashResult[i] = InternalState.State[i];
269+
}
270+
} else {
271+
// Swap byte order back
272+
for (int i = 0; i < 5; i++) {
273+
HashResult[i] = sys::getSwappedBytes(InternalState.State[i]);
274+
}
282275
}
283-
#endif
284276
}
285277

286278
std::array<uint8_t, 20> SHA1::final() {

llvm/lib/Support/SHA256.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@
2323
#include "llvm/ADT/ArrayRef.h"
2424
#include "llvm/ADT/StringRef.h"
2525
#include "llvm/Support/Endian.h"
26-
#include "llvm/Support/Host.h"
26+
#include "llvm/Support/SwapByteOrder.h"
2727
#include <string.h>
2828

2929
namespace llvm {
3030

31-
#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
32-
#define SHA_BIG_ENDIAN
33-
#endif
34-
3531
#define SHR(x, c) ((x) >> (c))
3632
#define ROTR(x, n) (((x) >> n) | ((x) << (32 - (n))))
3733

@@ -171,11 +167,10 @@ void SHA256::hashBlock() {
171167
}
172168

173169
void SHA256::addUncounted(uint8_t Data) {
174-
#ifdef SHA_BIG_ENDIAN
175-
InternalState.Buffer.C[InternalState.BufferOffset] = Data;
176-
#else
177-
InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
178-
#endif
170+
if constexpr (sys::IsBigEndianHost)
171+
InternalState.Buffer.C[InternalState.BufferOffset] = Data;
172+
else
173+
InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
179174

180175
InternalState.BufferOffset++;
181176
if (InternalState.BufferOffset == BLOCK_LENGTH) {
@@ -247,20 +242,17 @@ void SHA256::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) {
247242
// Pad to complete the last block
248243
pad();
249244

250-
#ifdef SHA_BIG_ENDIAN
251-
// Just copy the current state
252-
for (int i = 0; i < 8; i++) {
253-
HashResult[i] = InternalState.State[i];
254-
}
255-
#else
256-
// Swap byte order back
257-
for (int i = 0; i < 8; i++) {
258-
HashResult[i] = (((InternalState.State[i]) << 24) & 0xff000000) |
259-
(((InternalState.State[i]) << 8) & 0x00ff0000) |
260-
(((InternalState.State[i]) >> 8) & 0x0000ff00) |
261-
(((InternalState.State[i]) >> 24) & 0x000000ff);
245+
if constexpr (sys::IsBigEndianHost) {
246+
// Just copy the current state
247+
for (int i = 0; i < 8; i++) {
248+
HashResult[i] = InternalState.State[i];
249+
}
250+
} else {
251+
// Swap byte order back
252+
for (int i = 0; i < 8; i++) {
253+
HashResult[i] = sys::getSwappedBytes(InternalState.State[i]);
254+
}
262255
}
263-
#endif
264256
}
265257

266258
std::array<uint8_t, 32> SHA256::final() {

0 commit comments

Comments
 (0)