1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 03:33:20 +01:00
llvm-mirror/include/llvm/Support/SHA1.h

86 lines
2.4 KiB
C
Raw Normal View History

//==- SHA1.h - SHA1 implementation for LLVM --*- C++ -*-==//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// This code is taken from public domain
// (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c)
// and modified by wrapping it in a C++ interface for LLVM,
// and removing unnecessary code.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_SHA1_H
#define LLVM_SUPPORT_SHA1_H
#include "llvm/ADT/ArrayRef.h"
#include <cstdint>
namespace llvm {
template <typename T> class ArrayRef;
class StringRef;
/// A class that wrap the SHA1 algorithm.
class SHA1 {
public:
SHA1() { init(); }
/// Reinitialize the internal state
void init();
/// Digest more data.
void update(ArrayRef<uint8_t> Data);
/// Digest more data.
void update(StringRef Str) {
update(ArrayRef<uint8_t>((uint8_t *)const_cast<char *>(Str.data()),
Str.size()));
}
/// Return a reference to the current raw 160-bits SHA1 for the digested data
/// since the last call to init(). This call will add data to the internal
/// state and as such is not suited for getting an intermediate result
/// (see result()).
StringRef final();
/// Return a reference to the current raw 160-bits SHA1 for the digested data
/// since the last call to init(). This is suitable for getting the SHA1 at
/// any time without invalidating the internal state so that more calls can be
/// made into update.
StringRef result();
private:
/// Define some constants.
/// "static constexpr" would be cleaner but MSVC does not support it yet.
enum { BLOCK_LENGTH = 64 };
enum { HASH_LENGTH = 20 };
// Internal State
struct {
SHA1: unroll loop in hashBlock. This code is taken from public domain. https://github.com/jsonn/src/blob/trunk/common/lib/libc/hash/sha1/sha1.c I wrote a sha1 command and ran it on my Xeon E5-2680 v2 2.80GHz machine. Here is a result. The new hash function is 37% faster than before. Performance counter stats for './llvm-sha1-old /ssd/build/bin/lld' (10 runs): 6640.503687 task-clock (msec) # 1.001 CPUs utilized ( +- 0.03% ) 54 context-switches # 0.008 K/sec ( +- 5.03% ) 5 cpu-migrations # 0.001 K/sec ( +- 31.73% ) 183,803 page-faults # 0.028 M/sec ( +- 0.00% ) 18,527,954,113 cycles # 2.790 GHz ( +- 0.03% ) 4,993,237,485 stalled-cycles-frontend # 26.95% frontend cycles idle ( +- 0.11% ) <not supported> stalled-cycles-backend 50,217,149,423 instructions # 2.71 insns per cycle # 0.10 stalled cycles per insn ( +- 0.00% ) 6,094,322,337 branches # 917.750 M/sec ( +- 0.00% ) 11,778,239 branch-misses # 0.19% of all branches ( +- 0.01% ) 6.634017401 seconds time elapsed ( +- 0.03% ) Performance counter stats for './llvm-sha1-new /ssd/build/bin/lld' (10 runs): 4167.062720 task-clock (msec) # 1.001 CPUs utilized ( +- 0.02% ) 52 context-switches # 0.012 K/sec ( +- 16.45% ) 7 cpu-migrations # 0.002 K/sec ( +- 32.20% ) 183,804 page-faults # 0.044 M/sec ( +- 0.00% ) 11,626,611,958 cycles # 2.790 GHz ( +- 0.02% ) 4,491,897,976 stalled-cycles-frontend # 38.63% frontend cycles idle ( +- 0.05% ) <not supported> stalled-cycles-backend 24,320,180,617 instructions # 2.09 insns per cycle # 0.18 stalled cycles per insn ( +- 0.00% ) 1,574,674,576 branches # 377.886 M/sec ( +- 0.00% ) 11,769,693 branch-misses # 0.75% of all branches ( +- 0.00% ) 4.163251552 seconds time elapsed ( +- 0.02% ) Differential Revision: https://reviews.llvm.org/D26890 llvm-svn: 287473
2016-11-20 02:03:22 +01:00
union {
uint8_t C[BLOCK_LENGTH];
uint32_t L[BLOCK_LENGTH / 4];
} Buffer;
uint32_t State[HASH_LENGTH / 4];
uint32_t ByteCount;
uint8_t BufferOffset;
} InternalState;
// Internal copy of the hash, populated and accessed on calls to result()
uint32_t HashResult[HASH_LENGTH / 4];
// Helper
void writebyte(uint8_t data);
void hashBlock();
void addUncounted(uint8_t data);
void pad();
};
} // end llvm namespace
#endif