add hash functions (fnv_32a and Jenkins lookup2 hash)
This commit is contained in:
parent
97a44aae9a
commit
054360be40
12
clock/main.c
12
clock/main.c
@ -1,6 +1,8 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <Windows.h>
|
||||
#include <stdlib.h>
|
||||
#include "../progressbar test/progressbar.h"
|
||||
|
||||
// \033[ = CSI, CSI ?25l hides the cursor, CSI ?25h shows the cursor
|
||||
@ -24,7 +26,7 @@ int main() {
|
||||
#endif
|
||||
|
||||
struct progressbar pb;
|
||||
int n = 10, sl = 1;
|
||||
int n = 1, sl = 1;
|
||||
printf(HIDE_CURSOR);
|
||||
printf("\033[385;31m");
|
||||
progressbar_start(&pb, n * sl);
|
||||
@ -57,12 +59,12 @@ int main() {
|
||||
printf("\033[385;%dm", i);
|
||||
for (int j = 30; j <= 37; j++) {
|
||||
printf("\033[385;%dm", j);
|
||||
printf("%2X ", count);
|
||||
printf("%02X ", count);
|
||||
count++;
|
||||
}
|
||||
for (int j = 90; j <= 97; j++) {
|
||||
printf("\033[385;%dm", j);
|
||||
printf("%2X ", count);
|
||||
printf("%02X ", count);
|
||||
count++;
|
||||
}
|
||||
printf("\n");
|
||||
@ -71,12 +73,12 @@ int main() {
|
||||
printf("\033[385;%dm", i);
|
||||
for (int j = 30; j <= 37; j++) {
|
||||
printf("\033[385;%dm", j);
|
||||
printf("%2X ", count);
|
||||
printf("%02X ", count);
|
||||
count++;
|
||||
}
|
||||
for (int j = 90; j <= 97; j++) {
|
||||
printf("\033[385;%dm", j);
|
||||
printf("%2X ", count);
|
||||
printf("%02X ", count);
|
||||
count++;
|
||||
}
|
||||
printf("\n");
|
||||
|
249
hash functions/fnv.h
Normal file
249
hash functions/fnv.h
Normal file
@ -0,0 +1,249 @@
|
||||
/*
|
||||
* fnv - Fowler/Noll/Vo- hash code
|
||||
*
|
||||
* @(#) $Revision: 5.4 $
|
||||
* @(#) $Id: fnv.h,v 5.4 2009/07/30 22:49:13 chongo Exp $
|
||||
* @(#) $Source: /usr/local/src/cmd/fnv/RCS/fnv.h,v $
|
||||
*
|
||||
***
|
||||
*
|
||||
* Fowler/Noll/Vo- hash
|
||||
*
|
||||
* The basis of this hash algorithm was taken from an idea sent
|
||||
* as reviewer comments to the IEEE POSIX P1003.2 committee by:
|
||||
*
|
||||
* Phong Vo (http://www.research.att.com/info/kpv/)
|
||||
* Glenn Fowler (http://www.research.att.com/~gsf/)
|
||||
*
|
||||
* In a subsequent ballot round:
|
||||
*
|
||||
* Landon Curt Noll (http://www.isthe.com/chongo/)
|
||||
*
|
||||
* improved on their algorithm. Some people tried this hash
|
||||
* and found that it worked rather well. In an EMail message
|
||||
* to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
|
||||
*
|
||||
* FNV hashes are designed to be fast while maintaining a low
|
||||
* collision rate. The FNV speed allows one to quickly hash lots
|
||||
* of data while maintaining a reasonable collision rate. See:
|
||||
*
|
||||
* http://www.isthe.com/chongo/tech/comp/fnv/index.html
|
||||
*
|
||||
* for more details as well as other forms of the FNV hash.
|
||||
*
|
||||
***
|
||||
*
|
||||
* NOTE: The FNV-0 historic hash is not recommended. One should use
|
||||
* the FNV-1 hash instead.
|
||||
*
|
||||
* To use the 32 bit FNV-0 historic hash, pass FNV0_32_INIT as the
|
||||
* Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str().
|
||||
*
|
||||
* To use the 64 bit FNV-0 historic hash, pass FNV0_64_INIT as the
|
||||
* Fnv64_t hashval argument to fnv_64_buf() or fnv_64_str().
|
||||
*
|
||||
* To use the recommended 32 bit FNV-1 hash, pass FNV1_32_INIT as the
|
||||
* Fnv32_t hashval argument to fnv_32_buf() or fnv_32_str().
|
||||
*
|
||||
* To use the recommended 64 bit FNV-1 hash, pass FNV1_64_INIT as the
|
||||
* Fnv64_t hashval argument to fnv_64_buf() or fnv_64_str().
|
||||
*
|
||||
* To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
|
||||
* Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
|
||||
*
|
||||
* To use the recommended 64 bit FNV-1a hash, pass FNV1A_64_INIT as the
|
||||
* Fnv64_t hashval argument to fnv_64a_buf() or fnv_64a_str().
|
||||
*
|
||||
***
|
||||
*
|
||||
* Please do not copyright this code. This code is in the public domain.
|
||||
*
|
||||
* LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
||||
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
|
||||
* EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
||||
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
|
||||
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* By:
|
||||
* chongo <Landon Curt Noll> /\oo/\
|
||||
* http://www.isthe.com/chongo/
|
||||
*
|
||||
* Share and Enjoy! :-)
|
||||
*/
|
||||
|
||||
#if !defined(__FNV_H__)
|
||||
#define __FNV_H__
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#define FNV_VERSION "5.0.2" /* @(#) FNV Version */
|
||||
|
||||
|
||||
/*
|
||||
* 32 bit FNV-0 hash type
|
||||
*/
|
||||
typedef u_int32_t Fnv32_t;
|
||||
|
||||
|
||||
/*
|
||||
* 32 bit FNV-0 zero initial basis
|
||||
*
|
||||
* This historic hash is not recommended. One should use
|
||||
* the FNV-1 hash and initial basis instead.
|
||||
*/
|
||||
#define FNV0_32_INIT ((Fnv32_t)0)
|
||||
|
||||
|
||||
/*
|
||||
* 32 bit FNV-1 and FNV-1a non-zero initial basis
|
||||
*
|
||||
* The FNV-1 initial basis is the FNV-0 hash of the following 32 octets:
|
||||
*
|
||||
* chongo <Landon Curt Noll> /\../\
|
||||
*
|
||||
* NOTE: The \'s above are not back-slashing escape characters.
|
||||
* They are literal ASCII backslash 0x5c characters.
|
||||
*
|
||||
* NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition.
|
||||
*/
|
||||
#define FNV1_32_INIT ((Fnv32_t)0x811c9dc5)
|
||||
#define FNV1_32A_INIT FNV1_32_INIT
|
||||
|
||||
|
||||
/*
|
||||
* determine how 64 bit unsigned values are represented
|
||||
*/
|
||||
#include "longlong.h"
|
||||
|
||||
|
||||
/*
|
||||
* 64 bit FNV-0 hash
|
||||
*/
|
||||
#if defined(HAVE_64BIT_LONG_LONG)
|
||||
typedef u_int64_t Fnv64_t;
|
||||
#else /* HAVE_64BIT_LONG_LONG */
|
||||
typedef struct {
|
||||
u_int32_t w32[2]; /* w32[0] is low order, w32[1] is high order word */
|
||||
} Fnv64_t;
|
||||
#endif /* HAVE_64BIT_LONG_LONG */
|
||||
|
||||
|
||||
/*
|
||||
* 64 bit FNV-0 zero initial basis
|
||||
*
|
||||
* This historic hash is not recommended. One should use
|
||||
* the FNV-1 hash and initial basis instead.
|
||||
*/
|
||||
#if defined(HAVE_64BIT_LONG_LONG)
|
||||
#define FNV0_64_INIT ((Fnv64_t)0)
|
||||
#else /* HAVE_64BIT_LONG_LONG */
|
||||
extern const Fnv64_t fnv0_64_init;
|
||||
#define FNV0_64_INIT (fnv0_64_init)
|
||||
#endif /* HAVE_64BIT_LONG_LONG */
|
||||
|
||||
|
||||
/*
|
||||
* 64 bit FNV-1 non-zero initial basis
|
||||
*
|
||||
* The FNV-1 initial basis is the FNV-0 hash of the following 32 octets:
|
||||
*
|
||||
* chongo <Landon Curt Noll> /\../\
|
||||
*
|
||||
* NOTE: The \'s above are not back-slashing escape characters.
|
||||
* They are literal ASCII backslash 0x5c characters.
|
||||
*
|
||||
* NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition.
|
||||
*/
|
||||
#if defined(HAVE_64BIT_LONG_LONG)
|
||||
#define FNV1_64_INIT ((Fnv64_t)0xcbf29ce484222325ULL)
|
||||
#define FNV1A_64_INIT FNV1_64_INIT
|
||||
#else /* HAVE_64BIT_LONG_LONG */
|
||||
extern const fnv1_64_init;
|
||||
extern const Fnv64_t fnv1a_64_init;
|
||||
#define FNV1_64_INIT (fnv1_64_init)
|
||||
#define FNV1A_64_INIT (fnv1a_64_init)
|
||||
#endif /* HAVE_64BIT_LONG_LONG */
|
||||
|
||||
|
||||
/*
|
||||
* hash types
|
||||
*/
|
||||
enum fnv_type {
|
||||
FNV_NONE = 0, /* invalid FNV hash type */
|
||||
FNV0_32 = 1, /* FNV-0 32 bit hash */
|
||||
FNV1_32 = 2, /* FNV-1 32 bit hash */
|
||||
FNV1a_32 = 3, /* FNV-1a 32 bit hash */
|
||||
FNV0_64 = 4, /* FNV-0 64 bit hash */
|
||||
FNV1_64 = 5, /* FNV-1 64 bit hash */
|
||||
FNV1a_64 = 6, /* FNV-1a 64 bit hash */
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* these test vectors are used as part o the FNV test suite
|
||||
*/
|
||||
struct test_vector {
|
||||
void *buf; /* start of test vector buffer */
|
||||
int len; /* length of test vector */
|
||||
};
|
||||
struct fnv0_32_test_vector {
|
||||
struct test_vector *test; /* test vector buffer to hash */
|
||||
Fnv32_t fnv0_32; /* expected FNV-0 32 bit hash value */
|
||||
};
|
||||
struct fnv1_32_test_vector {
|
||||
struct test_vector *test; /* test vector buffer to hash */
|
||||
Fnv32_t fnv1_32; /* expected FNV-1 32 bit hash value */
|
||||
};
|
||||
struct fnv1a_32_test_vector {
|
||||
struct test_vector *test; /* test vector buffer to hash */
|
||||
Fnv32_t fnv1a_32; /* expected FNV-1a 32 bit hash value */
|
||||
};
|
||||
struct fnv0_64_test_vector {
|
||||
struct test_vector *test; /* test vector buffer to hash */
|
||||
Fnv64_t fnv0_64; /* expected FNV-0 64 bit hash value */
|
||||
};
|
||||
struct fnv1_64_test_vector {
|
||||
struct test_vector *test; /* test vector buffer to hash */
|
||||
Fnv64_t fnv1_64; /* expected FNV-1 64 bit hash value */
|
||||
};
|
||||
struct fnv1a_64_test_vector {
|
||||
struct test_vector *test; /* test vector buffer to hash */
|
||||
Fnv64_t fnv1a_64; /* expected FNV-1a 64 bit hash value */
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* external functions
|
||||
*/
|
||||
/* hash_32.c */
|
||||
extern Fnv32_t fnv_32_buf(void *buf, size_t len, Fnv32_t hashval);
|
||||
extern Fnv32_t fnv_32_str(char *buf, Fnv32_t hashval);
|
||||
|
||||
/* hash_32a.c */
|
||||
extern Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hashval);
|
||||
extern Fnv32_t fnv_32a_str(char *buf, Fnv32_t hashval);
|
||||
|
||||
/* hash_64.c */
|
||||
extern Fnv64_t fnv_64_buf(void *buf, size_t len, Fnv64_t hashval);
|
||||
extern Fnv64_t fnv_64_str(char *buf, Fnv64_t hashval);
|
||||
|
||||
/* hash_64a.c */
|
||||
extern Fnv64_t fnv_64a_buf(void *buf, size_t len, Fnv64_t hashval);
|
||||
extern Fnv64_t fnv_64a_str(char *buf, Fnv64_t hashval);
|
||||
|
||||
/* test_fnv.c */
|
||||
extern struct test_vector fnv_test_str[];
|
||||
extern struct fnv0_32_test_vector fnv0_32_vector[];
|
||||
extern struct fnv1_32_test_vector fnv1_32_vector[];
|
||||
extern struct fnv1a_32_test_vector fnv1a_32_vector[];
|
||||
extern struct fnv0_64_test_vector fnv0_64_vector[];
|
||||
extern struct fnv1_64_test_vector fnv1_64_vector[];
|
||||
extern struct fnv1a_64_test_vector fnv1a_64_vector[];
|
||||
extern void unknown_hash_type(char *prog, enum fnv_type type, int code);
|
||||
extern void print_fnv32(Fnv32_t hval, Fnv32_t mask, int verbose, char *arg);
|
||||
extern void print_fnv64(Fnv64_t hval, Fnv64_t mask, int verbose, char *arg);
|
||||
|
||||
|
||||
#endif /* __FNV_H__ */
|
129
hash functions/fnvhash_32a.c
Normal file
129
hash functions/fnvhash_32a.c
Normal file
@ -0,0 +1,129 @@
|
||||
/*
|
||||
* hash_32 - 32 bit Fowler/Noll/Vo FNV-1a hash code
|
||||
*
|
||||
* @(#) $Revision: 5.1 $
|
||||
* @(#) $Id: hash_32a.c,v 5.1 2009/06/30 09:13:32 chongo Exp $
|
||||
* @(#) $Source: /usr/local/src/cmd/fnv/RCS/hash_32a.c,v $
|
||||
*
|
||||
***
|
||||
*
|
||||
* Fowler/Noll/Vo hash
|
||||
*
|
||||
* The basis of this hash algorithm was taken from an idea sent
|
||||
* as reviewer comments to the IEEE POSIX P1003.2 committee by:
|
||||
*
|
||||
* Phong Vo (http://www.research.att.com/info/kpv/)
|
||||
* Glenn Fowler (http://www.research.att.com/~gsf/)
|
||||
*
|
||||
* In a subsequent ballot round:
|
||||
*
|
||||
* Landon Curt Noll (http://www.isthe.com/chongo/)
|
||||
*
|
||||
* improved on their algorithm. Some people tried this hash
|
||||
* and found that it worked rather well. In an EMail message
|
||||
* to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
|
||||
*
|
||||
* FNV hashes are designed to be fast while maintaining a low
|
||||
* collision rate. The FNV speed allows one to quickly hash lots
|
||||
* of data while maintaining a reasonable collision rate. See:
|
||||
*
|
||||
* http://www.isthe.com/chongo/tech/comp/fnv/index.html
|
||||
*
|
||||
* for more details as well as other forms of the FNV hash.
|
||||
***
|
||||
*
|
||||
* To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
|
||||
* Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
|
||||
*
|
||||
***
|
||||
*
|
||||
* Please do not copyright this code. This code is in the public domain.
|
||||
*
|
||||
* LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
||||
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
|
||||
* EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
||||
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
|
||||
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*
|
||||
* By:
|
||||
* chongo <Landon Curt Noll> /\oo/\
|
||||
* http://www.isthe.com/chongo/
|
||||
*
|
||||
* Share and Enjoy! :-)
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include "fnvhash_32a.h"
|
||||
|
||||
/*
|
||||
* fnv_32a_buf - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a buffer
|
||||
*
|
||||
* input:
|
||||
* buf - start of buffer to hash
|
||||
* len - length of buffer in octets
|
||||
* hval - previous hash value or FNV1_32A_INIT if first call
|
||||
*
|
||||
* returns:
|
||||
* 32 bit hash as a static hash type
|
||||
*
|
||||
* NOTE: To use the recommended 32 bit FNV-1a hash, use FNV1_32A_INIT as the
|
||||
* hval arg on the first call to either fnv_32a_buf() or fnv_32a_str().
|
||||
*/
|
||||
Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hval)
|
||||
{
|
||||
unsigned char *bp = (unsigned char *)buf; /* start of buffer */
|
||||
unsigned char *be = bp + len; /* beyond end of buffer */
|
||||
|
||||
/*
|
||||
* FNV-1a hash each octet in the buffer
|
||||
*/
|
||||
while (bp < be) {
|
||||
|
||||
/* xor the bottom with the current octet */
|
||||
hval ^= (Fnv32_t)*bp++;
|
||||
|
||||
/* multiply by the 32 bit FNV magic prime mod 2^32 */
|
||||
hval *= FNV_32_PRIME;
|
||||
}
|
||||
|
||||
/* return our new hash value */
|
||||
return hval;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* fnv_32a_str - perform a 32 bit Fowler/Noll/Vo FNV-1a hash on a string
|
||||
*
|
||||
* input:
|
||||
* str - string to hash
|
||||
* hval- previous hash value or FNV1_32A_INIT if first call
|
||||
*
|
||||
* returns:
|
||||
* 32 bit hash as a static hash type
|
||||
*
|
||||
* NOTE: To use the recommended 32 bit FNV-1a hash, use FNV1_32A_INIT as the
|
||||
* hval arg on the first call to either fnv_32a_buf() or fnv_32a_str().
|
||||
*/
|
||||
Fnv32_t fnv_32a_str(char *str, Fnv32_t hval)
|
||||
{
|
||||
unsigned char *s = (unsigned char *)str; /* unsigned string */
|
||||
|
||||
/*
|
||||
* FNV-1a hash each octet in the buffer
|
||||
*/
|
||||
while (*s) {
|
||||
|
||||
/* xor the bottom with the current octet */
|
||||
hval ^= (Fnv32_t)*s++;
|
||||
|
||||
/* multiply by the 32 bit FNV magic prime mod 2^32 */
|
||||
hval *= FNV_32_PRIME;
|
||||
|
||||
}
|
||||
|
||||
/* return our new hash value */
|
||||
return hval;
|
||||
}
|
86
hash functions/fnvhash_32a.h
Normal file
86
hash functions/fnvhash_32a.h
Normal file
@ -0,0 +1,86 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
* https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function
|
||||
* http://www.isthe.com/chongo/tech/comp/fnv/index.html
|
||||
*/
|
||||
|
||||
/*
|
||||
* To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
|
||||
* Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
|
||||
*/
|
||||
|
||||
typedef uint32_t Fnv32_t;
|
||||
|
||||
/*
|
||||
* 32 bit magic FNV-1a prime
|
||||
*/
|
||||
#define FNV_32_PRIME ((Fnv32_t)0x01000193)
|
||||
|
||||
/*
|
||||
* 32 bit FNV-1 and FNV-1a non-zero initial basis
|
||||
*
|
||||
* The FNV-1 initial basis is the FNV-0 hash of the following 32 octets:
|
||||
*
|
||||
* chongo <Landon Curt Noll> /\../\
|
||||
*
|
||||
* NOTE: The \'s above are not back-slashing escape characters.
|
||||
* They are literal ASCII backslash 0x5c characters.
|
||||
*
|
||||
* NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition.
|
||||
*/
|
||||
#define FNV1_32_INIT ((Fnv32_t)0x811c9dc5)
|
||||
#define FNV1_32A_INIT FNV1_32_INIT
|
||||
|
||||
/*
|
||||
* If you need an x-bit hash where x is not a power of 2,
|
||||
* then we recommend that you compute the FNV hash that is just larger than x-bits
|
||||
* and xor-fold the result down to x-bits.By xor-folding we mean shift the excess
|
||||
* high order bits down and xor them with the lower x - bits.
|
||||
*/
|
||||
|
||||
//For example to produce a 24 bit FNV-1 hash in C we xor-fold fold a 32 bit FNV-1 hash:
|
||||
|
||||
#define MASK_24 (((uint32_t)1<<24)-1) /* i.e., (uint32_t)0xffffff */
|
||||
/*
|
||||
uint32_t hash;
|
||||
void* data;
|
||||
size_t data_len;
|
||||
|
||||
hash = fnv_32_buf(data, data_len, FNV1_32_INIT);
|
||||
hash = (hash >> 24) ^ (hash & MASK_24);
|
||||
*/
|
||||
|
||||
#define FNV_24(hash) ((hash >> 24) ^ (hash & MASK_24))
|
||||
|
||||
//To produce a 16 bit FNV-1 hash in C we xor-fold fold a 32 bit FNV-1 hash:
|
||||
|
||||
#define MASK_16 (((uint32_t)1<<16)-1) /* i.e., (uint32_t)0xffff */
|
||||
/*
|
||||
uint32_t hash;
|
||||
void* data;
|
||||
size_t data_len;
|
||||
|
||||
hash = fnv_32_buf(data, data_len, FNV1_32_INIT);
|
||||
hash = (hash >> 16) ^ (hash & MASK_16);
|
||||
*/
|
||||
|
||||
#define FNV_16(hash) ((hash >> 16) ^ (hash & MASK_16))
|
||||
|
||||
//For tiny x < 16 bit values, we recommend using a 32 bit FNV - 1 hash as follows:
|
||||
|
||||
/* NOTE: for 0 < x < 16 ONLY!!! */
|
||||
#define TINY_MASK(x) (((uint32_t)1<<(x))-1)
|
||||
/*
|
||||
uint32_t hash;
|
||||
void* data;
|
||||
size_t data_len;
|
||||
|
||||
hash = fnv_32_buf(data, data_len, FNV1_32_INIT);
|
||||
hash = (((hash >> x) ^ hash) & TINY_MASK(x));
|
||||
*/
|
||||
|
||||
#define TINY_FNV(x, hash) (((hash >> x) ^ hash) & TINY_MASK(x))
|
||||
|
||||
Fnv32_t fnv_32a_buf(void* buf, size_t len, Fnv32_t hval);
|
||||
Fnv32_t fnv_32a_str(char* str, Fnv32_t hval);
|
28
hash functions/hash functions.sln
Normal file
28
hash functions/hash functions.sln
Normal file
@ -0,0 +1,28 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.12.35527.113 d17.12
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hash functions", "hash functions.vcxproj", "{61817318-7F41-4087-850B-FEE499E3CE31}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{61817318-7F41-4087-850B-FEE499E3CE31}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{61817318-7F41-4087-850B-FEE499E3CE31}.Debug|x64.Build.0 = Debug|x64
|
||||
{61817318-7F41-4087-850B-FEE499E3CE31}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{61817318-7F41-4087-850B-FEE499E3CE31}.Debug|x86.Build.0 = Debug|Win32
|
||||
{61817318-7F41-4087-850B-FEE499E3CE31}.Release|x64.ActiveCfg = Release|x64
|
||||
{61817318-7F41-4087-850B-FEE499E3CE31}.Release|x64.Build.0 = Release|x64
|
||||
{61817318-7F41-4087-850B-FEE499E3CE31}.Release|x86.ActiveCfg = Release|Win32
|
||||
{61817318-7F41-4087-850B-FEE499E3CE31}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
142
hash functions/hash functions.vcxproj
Normal file
142
hash functions/hash functions.vcxproj
Normal file
@ -0,0 +1,142 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{61817318-7f41-4087-850b-fee499e3ce31}</ProjectGuid>
|
||||
<RootNamespace>hashfunctions</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="fnvhash_32a.c" />
|
||||
<ClCompile Include="jhash.c" />
|
||||
<ClCompile Include="main.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="fnv.h" />
|
||||
<ClInclude Include="fnvhash_32a.h" />
|
||||
<ClInclude Include="jhash.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
39
hash functions/hash functions.vcxproj.filters
Normal file
39
hash functions/hash functions.vcxproj.filters
Normal file
@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Исходные файлы">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Файлы заголовков">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Файлы ресурсов">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.c">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="fnvhash_32a.c">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="jhash.c">
|
||||
<Filter>Исходные файлы</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="fnv.h">
|
||||
<Filter>Файлы заголовков</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="fnvhash_32a.h">
|
||||
<Filter>Файлы заголовков</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="jhash.h">
|
||||
<Filter>Файлы заголовков</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
75
hash functions/jhash.c
Normal file
75
hash functions/jhash.c
Normal file
@ -0,0 +1,75 @@
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include "jhash.h"
|
||||
|
||||
/*
|
||||
from https://www.burtleburtle.net/bob/c/lookup2.c
|
||||
--------------------------------------------------------------------
|
||||
jhash() -- hash a variable-length key into a 32-bit value
|
||||
k : the key (the unaligned variable-length array of bytes)
|
||||
len : the length of the key, counting by bytes
|
||||
initval : the previous hash, or an arbitrary value
|
||||
Returns a 32-bit value. Every bit of the key affects every bit of
|
||||
the return value. Every 1-bit and 2-bit delta achieves avalanche.
|
||||
About 36+6len instructions.
|
||||
|
||||
The best hash table sizes are powers of 2. There is no need to do
|
||||
mod a prime (mod is sooo slow!). If you need less than 32 bits,
|
||||
use a bitmask. For example, if you need only 10 bits, do
|
||||
h = (h & hashmask(10));
|
||||
In which case, the hash table should have hashsize(10) elements.
|
||||
|
||||
If you are hashing n strings (ub1 **)k, do it like this:
|
||||
for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
|
||||
|
||||
By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
|
||||
code any way you wish, private, educational, or commercial. It's free.
|
||||
|
||||
See http://burtleburtle.net/bob/hash/evahash.html
|
||||
Use for hash table lookup, or anything where one collision in 2^32 is
|
||||
acceptable. Do NOT use for cryptographic purposes.
|
||||
--------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
ub4 jhash(register ub1* k, register ub4 length, register ub4 initval)
|
||||
{
|
||||
register ub4 a, b, c, len;
|
||||
|
||||
/* Set up the internal state */
|
||||
len = length;
|
||||
a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
|
||||
c = initval; /* the previous hash value */
|
||||
|
||||
/*---------------------------------------- handle most of the key */
|
||||
while (len >= 12)
|
||||
{
|
||||
a += (k[0] + ((ub4)k[1] << 8) + ((ub4)k[2] << 16) + ((ub4)k[3] << 24));
|
||||
b += (k[4] + ((ub4)k[5] << 8) + ((ub4)k[6] << 16) + ((ub4)k[7] << 24));
|
||||
c += (k[8] + ((ub4)k[9] << 8) + ((ub4)k[10] << 16) + ((ub4)k[11] << 24));
|
||||
mix(a, b, c);
|
||||
k += 12; len -= 12;
|
||||
}
|
||||
|
||||
/*------------------------------------- handle the last 11 bytes */
|
||||
c += length;
|
||||
switch (len) /* all the case statements fall through */
|
||||
{
|
||||
case 11: c += ((ub4)k[10] << 24);
|
||||
case 10: c += ((ub4)k[9] << 16);
|
||||
case 9: c += ((ub4)k[8] << 8);
|
||||
/* the first byte of c is reserved for the length */
|
||||
case 8: b += ((ub4)k[7] << 24);
|
||||
case 7: b += ((ub4)k[6] << 16);
|
||||
case 6: b += ((ub4)k[5] << 8);
|
||||
case 5: b += k[4];
|
||||
case 4: a += ((ub4)k[3] << 24);
|
||||
case 3: a += ((ub4)k[2] << 16);
|
||||
case 2: a += ((ub4)k[1] << 8);
|
||||
case 1: a += k[0];
|
||||
/* case 0: nothing left to add */
|
||||
}
|
||||
mix(a, b, c);
|
||||
/*-------------------------------------------- report the result */
|
||||
return c;
|
||||
}
|
48
hash functions/jhash.h
Normal file
48
hash functions/jhash.h
Normal file
@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
typedef unsigned long int ub4; /* unsigned 4-byte quantities */
|
||||
typedef unsigned char ub1;
|
||||
|
||||
#define hashsize(n) ((ub4)1<<(n))
|
||||
#define hashmask(n) (hashsize(n)-1)
|
||||
|
||||
/*
|
||||
--------------------------------------------------------------------
|
||||
mix -- mix 3 32-bit values reversibly.
|
||||
For every delta with one or two bit set, and the deltas of all three
|
||||
high bits or all three low bits, whether the original value of a,b,c
|
||||
is almost all zero or is uniformly distributed,
|
||||
* If mix() is run forward or backward, at least 32 bits in a,b,c
|
||||
have at least 1/4 probability of changing.
|
||||
* If mix() is run forward, every bit of c will change between 1/3 and
|
||||
2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
|
||||
mix() was built out of 36 single-cycle latency instructions in a
|
||||
structure that could supported 2x parallelism, like so:
|
||||
a -= b;
|
||||
a -= c; x = (c>>13);
|
||||
b -= c; a ^= x;
|
||||
b -= a; x = (a<<8);
|
||||
c -= a; b ^= x;
|
||||
c -= b; x = (b>>13);
|
||||
...
|
||||
Unfortunately, superscalar Pentiums and Sparcs can't take advantage
|
||||
of that parallelism. They've also turned some of those single-cycle
|
||||
latency instructions into multi-cycle latency instructions. Still,
|
||||
this is the fastest good hash I could find. There were about 2^^68
|
||||
to choose from. I only looked at a billion or so.
|
||||
--------------------------------------------------------------------
|
||||
*/
|
||||
#define mix(a,b,c) \
|
||||
{ \
|
||||
a -= b; a -= c; a ^= (c>>13); \
|
||||
b -= c; b -= a; b ^= (a<<8); \
|
||||
c -= a; c -= b; c ^= (b>>13); \
|
||||
a -= b; a -= c; a ^= (c>>12); \
|
||||
b -= c; b -= a; b ^= (a<<16); \
|
||||
c -= a; c -= b; c ^= (b>>5); \
|
||||
a -= b; a -= c; a ^= (c>>3); \
|
||||
b -= c; b -= a; b ^= (a<<10); \
|
||||
c -= a; c -= b; c ^= (b>>15); \
|
||||
}
|
||||
|
||||
ub4 jhash(register ub1* k, register ub4 length, register ub4 initval);
|
40
hash functions/main.c
Normal file
40
hash functions/main.c
Normal file
@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "jhash.h"
|
||||
#include "fnvhash_32a.h"
|
||||
|
||||
// Assumes little endian
|
||||
void printBits(size_t const size, void const* const ptr) {
|
||||
unsigned char* b = (unsigned char*)ptr;
|
||||
unsigned char byte;
|
||||
int i, j;
|
||||
|
||||
for (i = (int)size - 1; i >= 0; i--) {
|
||||
for (j = 7; j >= 0; j--) {
|
||||
byte = (b[i] >> j) & 1;
|
||||
printf("%u", byte);
|
||||
}
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
|
||||
int main() {
|
||||
char s1[] = "cbbc";
|
||||
char s2[] = "cddc";
|
||||
Fnv32_t h1 = fnv_32a_str(s1, FNV1_32A_INIT);
|
||||
Fnv32_t h2 = fnv_32a_str(s2, FNV1_32A_INIT);
|
||||
Fnv32_t h11 = h1;
|
||||
|
||||
|
||||
printBits(4, &h1);
|
||||
h1 = FNV_16(h1);
|
||||
h11 = TINY_FNV(15, h1);
|
||||
printBits(4, &h1);
|
||||
printBits(4, &h11);
|
||||
|
||||
return 0;
|
||||
}
|
@ -9,7 +9,7 @@ static unsigned get_to_print(unsigned current, unsigned max) {
|
||||
return progressbar_width * current / max;
|
||||
}
|
||||
|
||||
unsigned get_percentage(unsigned current, unsigned max) {
|
||||
static unsigned get_percentage(unsigned current, unsigned max) {
|
||||
return 100 * current / max;
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,4 @@ void progressbar_inc(struct progressbar *pb);
|
||||
// clear line of progress bar
|
||||
void progressbar_clear();
|
||||
|
||||
unsigned get_percentage(unsigned current, unsigned max);
|
||||
|
||||
#endif
|
||||
|
BIN
Лекции/FIST2024_OsnProgram_lek_14_ATD_DvoichnoeDerevo_Khesh.pptx
Normal file
BIN
Лекции/FIST2024_OsnProgram_lek_14_ATD_DvoichnoeDerevo_Khesh.pptx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user