diff --git a/c/include/pjwhash-inline.h b/c/include/pjwhash-inline.h new file mode 100644 index 0000000..ed5e8f5 --- /dev/null +++ b/c/include/pjwhash-inline.h @@ -0,0 +1,53 @@ +/* pjwhash-inline.h - PJW hash function, inline version. + * + * Copyright (C) 2021-2022 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifndef _PJWHASH_INLINE_H +#define _PJWHASH_INLINE_H + +#include "bits.h" + +#define THREE_QUARTERS ((int) ((BITS_PER_INT * 3) / 4)) +#define ONE_EIGHTH ((int) (BITS_PER_INT / 8)) +#define HIGH_BITS ( ~((uint)(~0) >> ONE_EIGHTH )) + +#ifndef _pjw_inline +#define _pjw_inline static inline +#endif + +/** + * unsigned int pjwhash - PJW hash function + * @key: the key address. + * @length: the length of key. + * + * This hash was created by Peter Jay Weinberger (AT&T Bell Labs): + * https://en.wikipedia.org/wiki/PJW_hash_function + * + * Return: the PJW hash. + */ +_pjw_inline uint pjwhash(const void* key, uint length) +{ + uint hash = 0, high; + const u8 *k = key; + + for (uint i = 0; i < length; ++k, ++i) { + hash = (hash << ONE_EIGHTH) + *k; + high = hash & HIGH_BITS; + if (high != 0) { + hash ^= high >> THREE_QUARTERS; + hash &= ~high; + } + } + return hash; +} + +#endif /* _PJWHASH_INLINE_H */ diff --git a/c/include/pjwhash.h b/c/include/pjwhash.h new file mode 100644 index 0000000..19b0d76 --- /dev/null +++ b/c/include/pjwhash.h @@ -0,0 +1,30 @@ +/* pjwhash.h - PJW hash function, extern version. + * + * Copyright (C) 2021-2022 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#ifndef _PJWHASH_H +#define _PJWHASH_H + +#include "bits.h" + +/** + * unsigned int pjwhash - PJW hash function + * @key: the key address. + * @length: the length of key. + * + * This hash was created by Peter Jay Weinberger (AT&T Bell Labs): + * https://en.wikipedia.org/wiki/PJW_hash_function + * + * Return: the PJW hash. + */ +extern uint pjwhash (const void* key, uint length); + +#endif /* _PJWHASH_H */ diff --git a/c/pjwhash.c b/c/pjwhash.c new file mode 100644 index 0000000..554f9e3 --- /dev/null +++ b/c/pjwhash.c @@ -0,0 +1,20 @@ +/* pjwhash.c - PJW hash function. + * + * Copyright (C) 2021-2022 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#define _pjw_inline extern + +//#include "bits.h" +//extern unsigned int pjwhash (const void* key, uint length); + +#include "pjwhash.h" +#include "pjwhash-inline.h"