Skip to content

Commit

Permalink
Merge branch 'hash_functions_c' of https://github.com/perazz/stdlib i…
Browse files Browse the repository at this point in the history
…nto hash_functions_c
  • Loading branch information
perazz committed Jan 29, 2025
2 parents 74046fa + e0977db commit d396933
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 56 deletions.
11 changes: 10 additions & 1 deletion test/hash_functions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@ target_sources(
waterhash.c
generate_hash_arrays.cpp
)

if(CMAKE_Fortran_COMPILER_ID MATCHES "^Intel")

# Set the C++ standard to prevent icpc breakage
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set_target_properties(test_hash_functions PROPERTIES LINKER_LANGUAGE Fortran)
endif()

if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU AND CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 10.0)
target_compile_options(
test_hash_functions
PRIVATE
$<$<COMPILE_LANGUAGE:Fortran>:-fno-range-check>
)
)
endif()

125 changes: 70 additions & 55 deletions test/hash_functions/generate_hash_arrays.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <iostream>
#include <fstream>
#include <stdio.h>

extern "C" {
#include "nmhash.h"
Expand Down Expand Up @@ -52,114 +51,130 @@ void SpookyHash_seed_state_test(int in_bits, const void *seed, void *state) {
}
}

using namespace std;

static const int SIZE = 2048;
char * key_array = new char[SIZE];
static const uint32_t NM_SEED = 0xdeadbeef;
static const uint64_t WATER_SEED = 0xdeadbeef1eadbeef;
static const uint32_t PENGY_SEED = 0xdeadbeef;
static const uint64_t SPOOKY_SEED[2] = { WATER_SEED, WATER_SEED };

int read_keys(){
string inFileName = "key_array.bin";
std::ifstream fin( inFileName, ios::in | ios::binary );
if (!fin){
cout << "Cannot open key_array.bin!" << endl;
int read_keys() {
const char *inFileName = "key_array.bin";
FILE *fin = fopen(inFileName, "rb");

if (!fin) {
fprintf(stderr, "Cannot open key_array.bin!\n");
return 1;
}

size_t bytesRead = fread(key_array, 1, SIZE, fin);
if (bytesRead != SIZE) {
fprintf(stderr, "Error reading key_array.bin! Only %zu bytes read.\n", bytesRead);
fclose(fin);
return 1;
}
fin.read(key_array, SIZE);
fin.close();

fclose(fin);
return 0;
}

int write_nmhash32(){
int write_nmhash32() {
size_t i;
uint32_t hash;
string outFileName = "c_nmhash32_array.bin";
std::ofstream fout( outFileName, ios::out | ios::binary );
const char *outFileName = "c_nmhash32_array.bin";
FILE *fout = fopen(outFileName, "wb");

if (!fout){
cout << "Cannot open c_nmhash32_array.bin!" << endl;
if (!fout) {
fprintf(stderr, "Cannot open c_nmhash32_array.bin!\n");
return 1;
}
for( i=0; i<=SIZE; i+=1 ){
hash = NMHASH32((void *) key_array, i, NM_SEED);
fout.write((char *) &hash, 4);

for (i = 0; i <= SIZE; i++) {
hash = NMHASH32((const void *)key_array, i, NM_SEED);
fwrite(&hash, sizeof(uint32_t), 1, fout); // Write 4 bytes (1 uint32_t) to the file
}
fout.close();

fclose(fout);
return 0;
}

int write_nmhash32x(){
int write_nmhash32x() {
size_t i;
uint32_t hash;
string outFileName = "c_nmhash32x_array.bin";
std::ofstream fout( outFileName, ios::out | ios::binary );
const char *outFileName = "c_nmhash32x_array.bin";
FILE *fout = fopen(outFileName, "wb");

if (!fout){
cout << "Cannot open c_nmhash32x_array.bin!" << endl;
if (!fout) {
fprintf(stderr, "Cannot open c_nmhash32x_array.bin!\n");
return 1;
}
for( i=0; i<=SIZE; i+=1 ){
hash = NMHASH32X((void *) key_array, i, NM_SEED);
fout.write((char *) &hash, 4);

for (i = 0; i <= SIZE; i++) {
hash = NMHASH32X((const void *)key_array, i, NM_SEED);
fwrite(&hash, sizeof(uint32_t), 1, fout); // Write 4 bytes (1 uint32_t) to the file
}
fout.close();

fclose(fout);
return 0;
}

int write_water(){
int write_water() {
uint32_t i;
uint32_t hash;
string outFileName = "c_water_hash_array.bin";
std::ofstream fout( outFileName, ios::out | ios::binary );
const char *outFileName = "c_water_hash_array.bin";
FILE *fout = fopen(outFileName, "wb");

if (!fout){
cout << "Cannot open c_water_hash_array.bin!" << endl;
if (!fout) {
fprintf(stderr, "Cannot open c_water_hash_array.bin!\n");
return 1;
}
for( i=0; i<=SIZE; i+=1 ){
hash = waterhash((void *) key_array, i, WATER_SEED);
fout.write((char *) &hash, 4);

for (i = 0; i <= SIZE; i++) {
hash = waterhash((const void *)key_array, i, WATER_SEED);
fwrite(&hash, sizeof(uint32_t), 1, fout); // Write 4 bytes (1 uint32_t) to the file
}
fout.close();

fclose(fout);
return 0;
}

int write_pengy(){
size_t i;
uint64_t hash;
string outFileName = "c_pengy_hash_array.bin";
std::ofstream fout( outFileName, ios::out | ios::binary );
const char *outFileName = "c_pengy_hash_array.bin";
FILE *fout = fopen(outFileName, "wb");

if (!fout){
cout << "Cannot open c_pengy_hash_array.bin!" << endl;
if (!fout) {
fprintf(stderr, "Cannot open c_pengy_hash_array.bin!\n");
return 1;
}
for( i=0; i<=SIZE; i+=1 ){
hash = pengyhash((void *) key_array, i, PENGY_SEED);
fout.write((char *) &hash, 8);

for (i = 0; i <= SIZE; i++) {
hash = pengyhash((const void *)key_array, i, PENGY_SEED);
fwrite(&hash, sizeof(uint64_t), 1, fout); // Write 8 bytes (1 uint64_t) to the file
}
fout.close();

fclose(fout);
return 0;
}

int write_spooky(){
int write_spooky() {
size_t i;
uint64_t hash[2];
string outFileName = "c_spooky_hash_array.bin";
std::ofstream fout( outFileName, ios::out | ios::binary );
const char *outFileName = "c_spooky_hash_array.bin";
FILE *fout = fopen(outFileName, "wb");

if (!fout){
cout << "Cannot open c_spooky_hash_array.bin!" << endl;
if (!fout) {
fprintf(stderr, "Cannot open c_spooky_hash_array.bin!\n");
return 1;
}
for( i=0; i<=SIZE; i+=1 ){
SpookyHash128_with_state_test((void *) key_array, i, (void *) SPOOKY_SEED, (void *) hash);
fout.write((char *) hash, 16);

for (i = 0; i <= SIZE; i++) {
SpookyHash128_with_state_test((const void *)key_array, i, (const void *)SPOOKY_SEED, (void *)hash);
fwrite(hash, sizeof(uint64_t), 2, fout); // Write 16 bytes (2 * 8 bytes) to the file
}
fout.close();

fclose(fout);
return 0;
}

Expand Down

0 comments on commit d396933

Please sign in to comment.