-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tag/Pool: move code from calc_hash() to util/djb_hash.cxx
- Loading branch information
1 parent
f578b06
commit c391ada
Showing
4 changed files
with
69 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
// Copyright CM4all GmbH | ||
// author: Max Kellermann <[email protected]> | ||
|
||
#include "djb_hash.hxx" | ||
|
||
#include <cassert> | ||
|
||
[[gnu::always_inline]] [[gnu::hot]] | ||
static constexpr std::size_t | ||
djb_hash_update(std::size_t hash, std::byte b) noexcept | ||
{ | ||
return (hash * 33) ^ static_cast<std::size_t>(b); | ||
} | ||
|
||
[[gnu::hot]] | ||
std::size_t | ||
djb_hash(std::span<const std::byte> src, std::size_t init) noexcept | ||
{ | ||
std::size_t hash = init; | ||
|
||
for (const auto i : src) | ||
hash = djb_hash_update(hash, i); | ||
|
||
return hash; | ||
} | ||
|
||
[[gnu::hot]] | ||
std::size_t | ||
djb_hash_string(const char *p, std::size_t init) noexcept | ||
{ | ||
assert(p != nullptr); | ||
|
||
std::size_t hash = init; | ||
|
||
while (*p != 0) | ||
hash = djb_hash_update(hash, static_cast<std::byte>(*p++)); | ||
|
||
return hash; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
// Copyright CM4all GmbH | ||
// author: Max Kellermann <[email protected]> | ||
|
||
/* | ||
* Implementation of D. J. Bernstein's cdb hash function. | ||
* http://cr.yp.to/cdb/cdb.txt | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <span> | ||
|
||
static constexpr std::size_t DJB_HASH_INIT = 5381; | ||
|
||
[[gnu::pure]] | ||
std::size_t | ||
djb_hash(std::span<const std::byte> src, | ||
std::size_t init=DJB_HASH_INIT) noexcept; | ||
|
||
[[gnu::pure]] | ||
std::size_t | ||
djb_hash_string(const char *p, | ||
std::size_t init=DJB_HASH_INIT) noexcept; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters