Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
Merge pull request #11 from centaurean/dev
Browse files Browse the repository at this point in the history
Dev merge
  • Loading branch information
Guillaume Voirin committed Jun 25, 2015
2 parents 17529c7 + 052fd04 commit f82ce50
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 61 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
1.0.6
-----
*June 26, 2015*

* Switched to premake 5
* Updated API definitions
* Added version information access in the API

1.0.5
-----
*May 4, 2015*
Expand Down
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ Build
-----
To build a static and dynamic library, as well as a test binary of SpookyHash on Windows, Linux or Mac OSX,

1) Download [premake](http://premake.github.io/) and make it available in your path
1) Download [premake 5](http://premake.github.io/) and make it available in your path

2) Run the following from the command line

cd build
premake4 gmake
premake5 gmake
make

or alternatively, on windows for example :

premake4.exe vs2010
premake5.exe vs2010

Quick start
-----------
```C
#include "spookyhash.h"
#include "spookyhash_api.h"

void hash(void* data, size_t data_length) {
uint64_t c, d, seed1 = 1, seed2 = 2;
Expand All @@ -49,9 +49,8 @@ void hash(void* data, size_t data_length) {
uint32_t hash32 = spookyhash_32(data, data_length, seed32); // Produce 32-bit hash

// Stream use example
spookyhash_context* context = spookyhash_context_allocate(NULL); // Create a context variable using malloc()
spookyhash_context_init(context, seed1, seed2); // Initialize the context
spookyhash_update(context, data, data_length); // Add data to hash, use this function repeatedly
spookyhash_final(context, &c, &d); // c and d now contain the resulting 128-bit hash in two uint64_t parts
spookyhash_context_free(context, NULL); // Free the context from memory using free()
spookyhash_context context; // Create a context variable
spookyhash_context_init(&context, seed1, seed2); // Initialize the context
spookyhash_update(&context, data, data_length); // Add data to hash, use this function repeatedly
spookyhash_final(&context, &c, &d); // c and d now contain the resulting 128-bit hash in two uint64_t parts
}
2 changes: 1 addition & 1 deletion build/premake4.lua → build/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ os.execute("git submodule update --init --recursive")

solution "SpookyHash"
configurations { "Release" }
flags { "OptimizeSpeed", "NoFramePointer" }
flags { "OptimizeSpeed", "NoFramePointer", "LinkTimeOptimization" }

project "spookyhash-static"
targetname ("spookyhash")
Expand Down
10 changes: 0 additions & 10 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@

#include "context.h"

SPOOKYHASH_WINDOWS_EXPORT SPOOKYHASH_FORCE_INLINE spookyhash_context* spookyhash_context_allocate(void *(*mem_alloc)(size_t)) {
void *(*memory_alloc)(size_t) = mem_alloc == NULL ? malloc : mem_alloc;
return memory_alloc(sizeof(spookyhash_context));
}

SPOOKYHASH_WINDOWS_EXPORT SPOOKYHASH_FORCE_INLINE void spookyhash_context_free(spookyhash_context* context, void (*mem_free)(void *)) {
void (*memory_free)(void *) = mem_free == NULL ? free : mem_free;
memory_free(context);
}

SPOOKYHASH_WINDOWS_EXPORT SPOOKYHASH_FORCE_INLINE void spookyhash_context_init(spookyhash_context *context, uint64_t seed1, uint64_t seed2) {
context->m_length = 0;
context->m_remainder = 0;
Expand Down
12 changes: 0 additions & 12 deletions src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,10 @@

#include "globals.h"

#define SPOOKYHASH_VARIABLES (12)
#define SPOOKYHASH_BLOCK_SIZE (SPOOKYHASH_VARIABLES * 8)
#define SPOOKYHASH_BUFFER_SIZE (2 * SPOOKYHASH_BLOCK_SIZE)
#define SPOOKYHASH_CONSTANT (0xdeadbeefdeadbeefLL)

typedef struct {
uint64_t m_data[2 * SPOOKYHASH_VARIABLES];
uint64_t m_state[SPOOKYHASH_VARIABLES];
size_t m_length;
uint8_t m_remainder;
} spookyhash_context;

SPOOKYHASH_WINDOWS_EXPORT spookyhash_context *spookyhash_context_allocate(void *(*)(size_t));

SPOOKYHASH_WINDOWS_EXPORT void spookyhash_context_free(spookyhash_context *, void (*)(void *));

SPOOKYHASH_WINDOWS_EXPORT void spookyhash_context_init(spookyhash_context *, uint64_t, uint64_t);

#endif
57 changes: 57 additions & 0 deletions src/globals.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Centaurean SpookyHash
*
* Copyright (c) 2015, Guillaume Voirin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* 26/06/15 1:08
*
* ----------
* SpookyHash
* ----------
*
* Author(s)
* Bob Jenkins (http://burtleburtle.net/bob/hash/spooky.html)
*
* Description
* Very fast non cryptographic hash
*/

#include "globals.h"

SPOOKYHASH_WINDOWS_EXPORT uint8_t spookyhash_version_major() {
return SPOOKYHASH_MAJOR_VERSION;
}

SPOOKYHASH_WINDOWS_EXPORT uint8_t spookyhash_version_minor() {
return SPOOKYHASH_MINOR_VERSION;
}

SPOOKYHASH_WINDOWS_EXPORT uint8_t spookyhash_version_revision() {
return SPOOKYHASH_REVISION;
}
16 changes: 5 additions & 11 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,17 @@
#ifndef SPOOKYHASH_GLOBALS_H
#define SPOOKYHASH_GLOBALS_H

#include <stdint.h>
#include "spookyhash_api.h"

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

#if defined(_WIN64) || defined(_WIN32)
#define SPOOKYHASH_WINDOWS_EXPORT __declspec(dllexport)
#define SPOOKYHASH_RESTRICT __restrict
#else
#define SPOOKYHASH_WINDOWS_EXPORT
#define SPOOKYHASH_RESTRICT restrict
#endif

#if defined(__GNUC__) || defined(__clang__)
#define SPOOKYHASH_FORCE_INLINE inline __attribute__((always_inline))
#define SPOOKYHASH_RESTRICT restrict
#elif defined(__INTEL_COMPILER) || defined(_MSC_VER)
#define SPOOKYHASH_FORCE_INLINE __forceinline
#define SPOOKYHASH_RESTRICT __restrict
#else
#warning Impossible to force functions inlining. Expect performance issues.
#define SPOOKYHASH_FORCE_INLINE
Expand Down Expand Up @@ -98,6 +92,6 @@

#define SPOOKYHASH_MAJOR_VERSION 1
#define SPOOKYHASH_MINOR_VERSION 0
#define SPOOKYHASH_REVISION 5
#define SPOOKYHASH_REVISION 6

#endif
10 changes: 0 additions & 10 deletions src/spookyhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,4 @@
#define SPOOKYHASH_ALLOW_UNALIGNED_READS 0
#define SPOOKYHASH_ROTATE(x, k) (((x) << (k)) | (((x) >> (64 - (k)))))

SPOOKYHASH_WINDOWS_EXPORT void spookyhash_128(const void *, size_t, uint64_t *, uint64_t *);

SPOOKYHASH_WINDOWS_EXPORT uint64_t spookyhash_64(const void *, size_t, uint64_t);

SPOOKYHASH_WINDOWS_EXPORT uint32_t spookyhash_32(const void *, size_t, uint32_t);

SPOOKYHASH_WINDOWS_EXPORT void spookyhash_update(spookyhash_context *, const void *, size_t);

SPOOKYHASH_WINDOWS_EXPORT void spookyhash_final(spookyhash_context *, uint64_t *, uint64_t *);

#endif
175 changes: 175 additions & 0 deletions src/spookyhash_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Centaurean SpookyHash
*
* Copyright (c) 2015, Guillaume Voirin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* 26/06/15 0:40
*
* ----------
* SpookyHash
* ----------
*
* Author(s)
* Bob Jenkins (http://burtleburtle.net/bob/hash/spooky.html)
*
* Description
* Very fast non cryptographic hash
*/

#ifndef SPOOKYHASH_API_H
#define SPOOKYHASH_API_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stddef.h>

#if defined(_WIN64) || defined(_WIN32)
#define SPOOKYHASH_WINDOWS_EXPORT __declspec(dllexport)
#else
#define SPOOKYHASH_WINDOWS_EXPORT
#endif

/***********************************************************************************************************************
* *
* SpookyHash data structures *
* *
***********************************************************************************************************************/

#define SPOOKYHASH_VARIABLES (12)

typedef struct {
uint64_t m_data[2 * SPOOKYHASH_VARIABLES];
uint64_t m_state[SPOOKYHASH_VARIABLES];
size_t m_length;
uint8_t m_remainder;
} spookyhash_context;


/***********************************************************************************************************************
* *
* SpookyHash version information *
* *
***********************************************************************************************************************/

/*
* Returns the major version
*/
SPOOKYHASH_WINDOWS_EXPORT uint8_t spookyhash_version_major(void);

/*
* Returns the minor version
*/
SPOOKYHASH_WINDOWS_EXPORT uint8_t spookyhash_version_minor(void);

/*
* Returns the revision
*/
SPOOKYHASH_WINDOWS_EXPORT uint8_t spookyhash_version_revision(void);

#ifdef __cplusplus
}
#endif


/***********************************************************************************************************************
* *
* SpookyHash context setup *
* *
***********************************************************************************************************************/

/*
* Initialize a context
*
* @param context a SpookyHash context structure
* @param seed_1 the first 8 bytes of the seed
* @param seed_2 the last 8 bytes of the seed
*/
SPOOKYHASH_WINDOWS_EXPORT void spookyhash_context_init(spookyhash_context *context, uint64_t seed_1, uint64_t seed_2);


/***********************************************************************************************************************
* *
* SpookyHash main API functions *
* *
***********************************************************************************************************************/

/*
* Get a direct 128 bit hash
*
* @param input a given buffer
* @param input_size the size of our buffer
* @param hash_1 used on call as a the first 8 bytes of the seed, returns the first 8 bytes of the resulting hash
* @param hash_2 used on call as a the last 8 bytes of the seed, returns the last 8 bytes of the resulting hash
*/
SPOOKYHASH_WINDOWS_EXPORT void spookyhash_128(const void *input, size_t input_size, uint64_t *hash_1, uint64_t *hash_2);

/*
* Get a direct 64 bit hash
*
* @param input a given buffer
* @param input_size the size of our buffer
* @param seed the 8 byte seed
*
* @returns an 8 byte hash
*/
SPOOKYHASH_WINDOWS_EXPORT uint64_t spookyhash_64(const void *input, size_t input_size, uint64_t seed);

/*
* Get a direct 32 bit hash
*
* @param input a given buffer
* @param input_size the size of our buffer
* @param seed the 4 byte seed
*
* @returns a 4 byte hash
*/
SPOOKYHASH_WINDOWS_EXPORT uint32_t spookyhash_32(const void *input, size_t input_size, uint32_t seed);

/*
* Update a hash calculation, using the given context
*
* @param context the calculation context (has to be initialized using @spookyhash_context_init)
* @param input a given buffer
* @param input_size the size of our buffer
*/
SPOOKYHASH_WINDOWS_EXPORT void spookyhash_update(spookyhash_context *context, const void *input, size_t input_size);

/*
* Finish a hash calculation, using the given context
*
* @param context the calculation context (has to be initialized using @spookyhash_context_init)
* @param hash_1 returns the first 8 bytes of the resulting hash
* @param hash_2 returns the last 8 bytes of the resulting hash
*/
SPOOKYHASH_WINDOWS_EXPORT void spookyhash_final(spookyhash_context *context, uint64_t *hash_1, uint64_t *hash_2);

#endif
Loading

0 comments on commit f82ce50

Please sign in to comment.