Skip to content

Commit

Permalink
switch to clang c++ format
Browse files Browse the repository at this point in the history
  • Loading branch information
oertl committed Jan 26, 2025
1 parent 50bd047 commit 97431d1
Show file tree
Hide file tree
Showing 17 changed files with 184 additions and 294 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ static def readPythonLicense(licenseName) {

spotless {
def googleJavaFormatVersion = '1.25.0'
def eclipseCdtVersion = '11.6'
def clangFormatVersion = '15.0.6'
def blackVersion = '24.10.0'
def greclipseVersion = '4.32'
def specialLicenseHeaders = [
Expand Down Expand Up @@ -215,7 +215,7 @@ spotless {
}
cpp {
target 'reference-implementations/*/*.cpp', 'reference-implementations/*/*.hpp', 'reference-implementations/*.cpp', 'reference-implementations/*.hpp'
eclipseCdt(eclipseCdtVersion)
clangFormat(clangFormatVersion)
licenseHeader readJavaLicense('APACHE_2_0_DYNATRACE')
}
java {
Expand Down
178 changes: 88 additions & 90 deletions reference-implementations/calculate_checksums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,139 +13,137 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fstream>
#include <iomanip>
#include <random>
#include <fstream>

#include <openssl/evp.h>

#include "farmhash_na/farmhash_na_checksum_config.hpp"
#include "farmhash_uo/farmhash_uo_checksum_config.hpp"
#include "komihash_4_3/komihash_4_3_checksum_config.hpp"
#include "komihash_4_5/komihash_4_5_checksum_config.hpp"
#include "komihash_4_7/komihash_4_7_checksum_config.hpp"
#include "komihash_5_0/komihash_5_0_checksum_config.hpp"
#include "komihash_5_10/komihash_5_10_checksum_config.hpp"
#include "murmur3_128/murmur3_128_checksum_config.hpp"
#include "murmur3_32/murmur3_32_checksum_config.hpp"
#include "polymur-hash_2_0/polymur-hash_2_0_checksum_config.hpp"
#include "wyhash_final_3/wyhash_final_3_checksum_config.hpp"
#include "wyhash_final_4/wyhash_final_4_checksum_config.hpp"
#include "murmur3_128/murmur3_128_checksum_config.hpp"
#include "murmur3_32/murmur3_32_checksum_config.hpp"
#include "farmhash_na/farmhash_na_checksum_config.hpp"
#include "farmhash_uo/farmhash_uo_checksum_config.hpp"
#include "xxh3/xxh3_checksum_config.hpp"
#include "xxh3_128/xxh3_128_checksum_config.hpp"

using namespace std;

uint64_t splitmix_v1_update(uint64_t &state) {
state += UINT64_C(0x9e3779b97f4a7c15);
uint64_t z = state;
z = (z ^ (z >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
z = (z ^ (z >> 27)) * UINT64_C(0x94d049bb133111eb);
return z ^ (z >> 31);
state += UINT64_C(0x9e3779b97f4a7c15);
uint64_t z = state;
z = (z ^ (z >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
z = (z ^ (z >> 27)) * UINT64_C(0x94d049bb133111eb);
return z ^ (z >> 31);
}

template<typename T>
template <typename T>
void computeAndPrintChecksum(
const uint64_t maxSupportedLength = std::numeric_limits < uint64_t
> ::max(), const T &hashFunctionConfig = T()) {

mt19937_64 rng(0);
const uint64_t maxSupportedLength = std::numeric_limits<uint64_t>::max(),
const T &hashFunctionConfig = T()) {

ofstream outputFile(
"../src/test/resources/" + hashFunctionConfig.getName() + ".txt");
mt19937_64 rng(0);

std::vector<std::pair<uint64_t, uint64_t>> lengthAndCycles;
lengthAndCycles.emplace_back(0, 1);
for (uint64_t dataLength = 1; dataLength <= 1024; ++dataLength) {
lengthAndCycles.emplace_back(dataLength, 100);
}
for (uint64_t dataLength = 1025; dataLength <= 4096; ++dataLength) {
lengthAndCycles.emplace_back(dataLength, 10);
}
lengthAndCycles.emplace_back((UINT64_C(1) << 31) - 1, 1);
lengthAndCycles.emplace_back((UINT64_C(1) << 31) + 0, 1);
lengthAndCycles.emplace_back((UINT64_C(1) << 31) + 1, 1);
lengthAndCycles.emplace_back((UINT64_C(1) << 32) - 1, 1);
lengthAndCycles.emplace_back((UINT64_C(1) << 32) + 0, 1);
lengthAndCycles.emplace_back((UINT64_C(1) << 32) + 1, 1);
ofstream outputFile("../src/test/resources/" + hashFunctionConfig.getName() +
".txt");

for (const auto &lengthAndCycle : lengthAndCycles) {
std::vector<std::pair<uint64_t, uint64_t>> lengthAndCycles;
lengthAndCycles.emplace_back(0, 1);
for (uint64_t dataLength = 1; dataLength <= 1024; ++dataLength) {
lengthAndCycles.emplace_back(dataLength, 100);
}
for (uint64_t dataLength = 1025; dataLength <= 4096; ++dataLength) {
lengthAndCycles.emplace_back(dataLength, 10);
}
lengthAndCycles.emplace_back((UINT64_C(1) << 31) - 1, 1);
lengthAndCycles.emplace_back((UINT64_C(1) << 31) + 0, 1);
lengthAndCycles.emplace_back((UINT64_C(1) << 31) + 1, 1);
lengthAndCycles.emplace_back((UINT64_C(1) << 32) - 1, 1);
lengthAndCycles.emplace_back((UINT64_C(1) << 32) + 0, 1);
lengthAndCycles.emplace_back((UINT64_C(1) << 32) + 1, 1);

uint64_t dataLength = lengthAndCycle.first;
uint64_t numCycles = lengthAndCycle.second;
for (const auto &lengthAndCycle : lengthAndCycles) {

if (dataLength > maxSupportedLength)
continue;
uint64_t dataLength = lengthAndCycle.first;
uint64_t numCycles = lengthAndCycle.second;

uint8_t checkSum[EVP_MAX_MD_SIZE];
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
if (dataLength > maxSupportedLength)
continue;

EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL);
uint8_t checkSum[EVP_MAX_MD_SIZE];
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();

uint64_t seed = rng();
uint64_t rngState = seed;
uint64_t effectiveSeedLength = (hashFunctionConfig.getSeedSize() + 7)
>> 3;
EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL);

std::vector < uint64_t > seedBytesTemp(effectiveSeedLength);
std::vector < uint8_t > hashBytes(hashFunctionConfig.getHashSize());
uint64_t seed = rng();
uint64_t rngState = seed;
uint64_t effectiveSeedLength = (hashFunctionConfig.getSeedSize() + 7) >> 3;

uint64_t effectiveDataLength = (dataLength + 7) >> 3;
std::vector<uint64_t> seedBytesTemp(effectiveSeedLength);
std::vector<uint8_t> hashBytes(hashFunctionConfig.getHashSize());

std::vector < uint64_t > dataBytesTemp(effectiveDataLength);
uint64_t effectiveDataLength = (dataLength + 7) >> 3;

for (uint64_t cycle = 0; cycle < numCycles; ++cycle) {
for (uint64_t i = 0; i < effectiveSeedLength; ++i) {
seedBytesTemp[i] = splitmix_v1_update(rngState);
}
for (uint64_t i = 0; i < effectiveDataLength; ++i) {
dataBytesTemp[i] = splitmix_v1_update(rngState);
}
std::vector<uint64_t> dataBytesTemp(effectiveDataLength);

uint8_t *dataBytes = reinterpret_cast<uint8_t*>(&dataBytesTemp[0]);
uint8_t *seedBytes = reinterpret_cast<uint8_t*>(&seedBytesTemp[0]);
for (uint64_t cycle = 0; cycle < numCycles; ++cycle) {
for (uint64_t i = 0; i < effectiveSeedLength; ++i) {
seedBytesTemp[i] = splitmix_v1_update(rngState);
}
for (uint64_t i = 0; i < effectiveDataLength; ++i) {
dataBytesTemp[i] = splitmix_v1_update(rngState);
}

hashFunctionConfig.calculateHash(seedBytes, &hashBytes[0],
dataBytes, dataLength);
uint8_t *dataBytes = reinterpret_cast<uint8_t *>(&dataBytesTemp[0]);
uint8_t *seedBytes = reinterpret_cast<uint8_t *>(&seedBytesTemp[0]);

EVP_DigestUpdate(mdctx, &hashBytes[0], hashBytes.size());
hashFunctionConfig.calculateHash(seedBytes, &hashBytes[0], dataBytes,
dataLength);

}
EVP_DigestUpdate(mdctx, &hashBytes[0], hashBytes.size());
}

unsigned int lengthOfHash = 0;
EVP_DigestFinal_ex(mdctx, checkSum, &lengthOfHash);
EVP_MD_CTX_free(mdctx);
unsigned int lengthOfHash = 0;
EVP_DigestFinal_ex(mdctx, checkSum, &lengthOfHash);
EVP_MD_CTX_free(mdctx);

outputFile << dec << dataLength << ",";
outputFile << dec << numCycles << ",";
outputFile << hex << setfill('0') << setw(16) << seed << ",";
for (uint64_t k = 0; k < lengthOfHash; ++k)
outputFile << hex << setfill('0') << setw(2)
<< static_cast<uint64_t>(checkSum[k]);
outputFile << dec << dataLength << ",";
outputFile << dec << numCycles << ",";
outputFile << hex << setfill('0') << setw(16) << seed << ",";
for (uint64_t k = 0; k < lengthOfHash; ++k)
outputFile << hex << setfill('0') << setw(2)
<< static_cast<uint64_t>(checkSum[k]);

outputFile << endl;
}
outputFile.close();
outputFile << endl;
}
outputFile.close();
}

int main(int argc, char *argv[]) {

computeAndPrintChecksum<Komihash4_3ChecksumConfig>();
computeAndPrintChecksum<Komihash4_5ChecksumConfig>();
computeAndPrintChecksum<Komihash4_7ChecksumConfig>();
computeAndPrintChecksum<Komihash5_0ChecksumConfig>();
computeAndPrintChecksum<Komihash5_10ChecksumConfig>();
computeAndPrintChecksum<WyhashFinal3ChecksumConfig>();
computeAndPrintChecksum<WyhashFinal4ChecksumConfig>();
computeAndPrintChecksum<Murmur3_128_ChecksumConfig>(
std::numeric_limits<int>::max());
computeAndPrintChecksum<Murmur3_32_ChecksumConfig>(
std::numeric_limits<int>::max());
computeAndPrintChecksum<PolymurHash_2_0_ChecksumConfig>();
computeAndPrintChecksum<FarmHashNaChecksumConfig>();
computeAndPrintChecksum<FarmHashUoChecksumConfig>();
computeAndPrintChecksum<XXH3ChecksumConfig>();
computeAndPrintChecksum<XXH3_128_ChecksumConfig>();

return 0;
computeAndPrintChecksum<Komihash4_3ChecksumConfig>();
computeAndPrintChecksum<Komihash4_5ChecksumConfig>();
computeAndPrintChecksum<Komihash4_7ChecksumConfig>();
computeAndPrintChecksum<Komihash5_0ChecksumConfig>();
computeAndPrintChecksum<Komihash5_10ChecksumConfig>();
computeAndPrintChecksum<WyhashFinal3ChecksumConfig>();
computeAndPrintChecksum<WyhashFinal4ChecksumConfig>();
computeAndPrintChecksum<Murmur3_128_ChecksumConfig>(
std::numeric_limits<int>::max());
computeAndPrintChecksum<Murmur3_32_ChecksumConfig>(
std::numeric_limits<int>::max());
computeAndPrintChecksum<PolymurHash_2_0_ChecksumConfig>();
computeAndPrintChecksum<FarmHashNaChecksumConfig>();
computeAndPrintChecksum<FarmHashUoChecksumConfig>();
computeAndPrintChecksum<XXH3ChecksumConfig>();
computeAndPrintChecksum<XXH3_128_ChecksumConfig>();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,20 @@
#ifndef FARMHASH_NA_CHECKSUM_CONFIG_HPP
#define FARMHASH_NA_CHECKSUM_CONFIG_HPP

#include <string>
#include <cstdint>
#include <string>

class FarmHashNaChecksumConfig {

public:
uint64_t getSeedSize() const { return 24; }

uint64_t getSeedSize() const {
return 24;
}

uint64_t getHashSize() const {
return 24;
}

std::string getName() const {
return "FarmHash NA";
}
uint64_t getHashSize() const { return 24; }

void calculateHash(const uint8_t *seedBytes, uint8_t *hashBytes,
const uint8_t *dataBytes, uint64_t size) const;
std::string getName() const { return "FarmHash NA"; }

void calculateHash(const uint8_t *seedBytes, uint8_t *hashBytes,
const uint8_t *dataBytes, uint64_t size) const;
};

#endif // FARMHASH_NA_CHECKSUM_CONFIG_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,20 @@
#ifndef FARMHASH_UO_CHECKSUM_CONFIG_HPP
#define FARMHASH_UO_CHECKSUM_CONFIG_HPP

#include <string>
#include <cstdint>
#include <string>

class FarmHashUoChecksumConfig {

public:
uint64_t getSeedSize() const { return 24; }

uint64_t getSeedSize() const {
return 24;
}

uint64_t getHashSize() const {
return 24;
}

std::string getName() const {
return "FarmHash UO";
}
uint64_t getHashSize() const { return 24; }

void calculateHash(const uint8_t *seedBytes, uint8_t *hashBytes,
const uint8_t *dataBytes, uint64_t size) const;
std::string getName() const { return "FarmHash UO"; }

void calculateHash(const uint8_t *seedBytes, uint8_t *hashBytes,
const uint8_t *dataBytes, uint64_t size) const;
};

#endif // FARMHASH_UO_CHECKSUM_CONFIG_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,20 @@
#ifndef KOMIHASH_4_3_CHECKSUM_CONFIG_HPP
#define KOMIHASH_4_3_CHECKSUM_CONFIG_HPP

#include <string>
#include <cstdint>
#include <string>

class Komihash4_3ChecksumConfig {

public:
uint64_t getSeedSize() const { return 8; }

uint64_t getSeedSize() const {
return 8;
}

uint64_t getHashSize() const {
return 16;
}
uint64_t getHashSize() const { return 16; }

std::string getName() const {
return "Komihash 4.3";
}
std::string getName() const { return "Komihash 4.3"; }

void calculateHash(const uint8_t *seedBytes, uint8_t *hashBytes,
const uint8_t *dataBytes, uint64_t size) const;
void calculateHash(const uint8_t *seedBytes, uint8_t *hashBytes,
const uint8_t *dataBytes, uint64_t size) const;
};

#endif // KOMIHASH_4_3_CHECKSUM_CONFIG_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,20 @@
#ifndef KOMIHASH_4_5_CHECKSUM_CONFIG_HPP
#define KOMIHASH_4_5_CHECKSUM_CONFIG_HPP

#include <string>
#include <cstdint>
#include <string>

class Komihash4_5ChecksumConfig {

public:
uint64_t getSeedSize() const { return 8; }

uint64_t getSeedSize() const {
return 8;
}

uint64_t getHashSize() const {
return 16;
}

std::string getName() const {
return "Komihash 4.5";
}
uint64_t getHashSize() const { return 16; }

void calculateHash(const uint8_t *seedBytes, uint8_t *hashBytes,
const uint8_t *dataBytes, uint64_t size) const;
std::string getName() const { return "Komihash 4.5"; }

void calculateHash(const uint8_t *seedBytes, uint8_t *hashBytes,
const uint8_t *dataBytes, uint64_t size) const;
};

#endif // KOMIHASH_4_5_CHECKSUM_CONFIG_HPP
Loading

0 comments on commit 97431d1

Please sign in to comment.