Commit c03e671e authored by Shai Szulanski's avatar Shai Szulanski Committed by Facebook GitHub Bot

Make folly::FixedString::hash constexpr

Reviewed By: Mizuchi

Differential Revision: D29329759

fbshipit-source-id: 588aa9ce11db2c1aea51dcf96e0de3b50633fa29
parent f434460f
......@@ -393,7 +393,8 @@ struct ReverseIterator {
} // namespace detail
// Defined in folly/hash/Hash.h
std::uint32_t hsieh_hash32_buf(const void* buf, std::size_t len);
std::uint32_t hsieh_hash32_buf_constexpr(
const unsigned char* buf, std::size_t len);
/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *
* \class BasicFixedString
......@@ -1026,10 +1027,8 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase {
*/
static constexpr std::size_t max_size() noexcept { return N; }
// We would need to reimplement folly::Hash to make this
// constexpr. :-(
std::uint32_t hash() const noexcept {
return folly::hsieh_hash32_buf(data_, size_);
constexpr std::uint32_t hash() const noexcept {
return folly::hsieh_hash32_buf_constexpr(data_, size_);
}
/**
......
......@@ -305,12 +305,13 @@ inline uint64_t fnva64(
#define get16bits(d) folly::loadUnaligned<uint16_t>(d)
inline uint32_t hsieh_hash32_buf(const void* buf, size_t len) noexcept {
inline constexpr uint32_t hsieh_hash32_buf_constexpr(
const unsigned char* buf, size_t len) noexcept {
// forcing signed char, since other platforms can use unsigned
const unsigned char* s = reinterpret_cast<const unsigned char*>(buf);
const unsigned char* s = buf;
uint32_t hash = static_cast<uint32_t>(len);
uint32_t tmp;
size_t rem;
uint32_t tmp = 0;
size_t rem = 0;
if (len <= 0 || buf == nullptr) {
return 0;
......@@ -360,6 +361,11 @@ inline uint32_t hsieh_hash32_buf(const void* buf, size_t len) noexcept {
#undef get16bits
inline uint32_t hsieh_hash32_buf(const void* buf, size_t len) noexcept {
return hsieh_hash32_buf_constexpr(
reinterpret_cast<const unsigned char*>(buf), len);
}
inline uint32_t hsieh_hash32(const char* s) noexcept {
return hsieh_hash32_buf(s, std::strlen(s));
}
......
......@@ -344,7 +344,7 @@ FOLLY_POP_WARNING
* Read an unaligned value of type T and return it.
*/
template <class T>
inline T loadUnaligned(const void* p) {
inline constexpr T loadUnaligned(const void* p) {
static_assert(sizeof(Unaligned<T>) == sizeof(T), "Invalid unaligned size");
static_assert(alignof(Unaligned<T>) == 1, "Invalid alignment");
if (kHasUnalignedAccess) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment