Compare commits

...

9 Commits

Author SHA1 Message Date
Arvinder Bhathal b67422bf4a Use git FT for compilation include path 2017-07-08 20:42:31 -04:00
Arvinder Bhathal dd11684f96 Dynamially allocate and grow array of entries 2017-07-08 19:48:15 -04:00
Arvinder Bhathal 7799fec100 Store PNGs in 'images' folder 2017-07-08 02:43:37 -04:00
Arvinder Bhathal 78e74cf349 Add HTML generating code 2017-07-08 02:26:59 -04:00
Arvinder Bhathal 31d84500e8 Begin to consolidate files 2017-07-08 02:14:02 -04:00
Arvinder Bhathal 78bb1b3f48 Updated readme 2017-07-08 02:11:02 -04:00
Arvinder Bhathal ccd52daaa2 Generate test BMPs for glyphs with differing hashes 2017-07-08 02:10:04 -04:00
Arvinder Bhathal 35ca70a141 Add basic HTML table with zoomed glyphs w/o anti-aliasing 2017-07-08 02:08:11 -04:00
Arvinder Bhathal 96766b7a37 Initial commit - only C 2017-07-08 02:04:17 -04:00
6 changed files with 960 additions and 0 deletions

View File

@ -0,0 +1,5 @@
Compile with current ft folder:
gcc main.c bitmap.c murmur3.c -Wall -I ../../include/ -ldl -lpng -lm -o main
Run
./main <base libfreetype.so> <test libfreetype.so> <size> <font file>

View File

@ -0,0 +1,282 @@
#include "bitmap.h"
HASH_128 * Generate_Hash_x64_128( FT_Bitmap * bitmap,
HASH_128 * murmur)
{
int seed = 99; // Dont change
MurmurHash3_x64_128(bitmap->buffer,
(bitmap->pitch * bitmap->rows),
seed,
murmur->hash);
return murmur;
}
HASH_128 * Generate_Hash_x86_128( FT_Bitmap * bitmap,
HASH_128 * murmur)
{
int seed = 99; // Dont change
MurmurHash3_x86_128(bitmap->buffer,
(bitmap->pitch * bitmap->rows),
seed,
murmur->hash);
return murmur;
}
HASH_32 * Generate_Hash_x86_32( FT_Bitmap * bitmap,
HASH_32 * murmur)
{
int seed = 99; // Dont change
MurmurHash3_x86_32( bitmap->buffer,
(bitmap->pitch * bitmap->rows),
seed,
murmur->hash);
return murmur;
}
PIXEL * Pixel_At (IMAGE * bitmap, int x, int y)
{
return bitmap->pixels + bitmap->width * y + x;
}
int Generate_PNG (IMAGE *bitmap,
const char *path,
int render_mode)
{
FILE * fp;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
size_t x, y;
png_byte ** row_pointers = NULL;
int status = -1;
int pixel_size = 4;
int depth = 8;
fp = fopen (path, "wb");
if (! fp) {
goto fopen_failed;
}
png_ptr = png_create_write_struct ( PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
if (png_ptr == NULL) {
goto png_create_write_struct_failed;
}
info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == NULL) {
goto png_create_info_struct_failed;
}
if (setjmp (png_jmpbuf (png_ptr))) {
goto png_failure;
}
png_set_IHDR (png_ptr,
info_ptr,
bitmap->width,
bitmap->height,
depth,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
row_pointers = png_malloc ( png_ptr,
bitmap->height * sizeof (png_byte *));
for (y = 0; y < bitmap->height; y++) {
png_byte *row = png_malloc (png_ptr,
sizeof (uint8_t) * bitmap->width * pixel_size);
row_pointers[y] = row;
for (x = 0; x < bitmap->width; x++) {
PIXEL * pixel = Pixel_At (bitmap, x, y);
if (render_mode == 3 || render_mode == 5)
{
*row++ = pixel->blue;
*row++ = pixel->green;
*row++ = pixel->red;
*row++ = pixel->alpha;
continue;
}
*row++ = pixel->red;
*row++ = pixel->green;
*row++ = pixel->blue;
*row++ = pixel->alpha;
}
}
png_init_io ( png_ptr,
fp);
png_set_rows (png_ptr,
info_ptr,
row_pointers);
png_write_png ( png_ptr,
info_ptr,
PNG_TRANSFORM_IDENTITY,
NULL);
status = 0;
for (y = 0; y < bitmap->height; y++) {
png_free (png_ptr, row_pointers[y]);
}
png_free (png_ptr, row_pointers);
png_failure:
png_create_info_struct_failed:
png_destroy_write_struct (&png_ptr, &info_ptr);
png_create_write_struct_failed:
fclose (fp);
fopen_failed:
return status;
}
void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode){
IMAGE fruit;
int x;
int y;
unsigned char value;
int p;
switch(render_mode){
case 0 : fruit.width = bitmap->width; // MONO and GRAY
fruit.height = bitmap->rows;
fruit.pixels = calloc ( fruit.width * fruit.height,
sizeof (PIXEL));
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
PIXEL * pixel = Pixel_At (& fruit, x, y);
p = (y * bitmap->pitch ) + x;
value = bitmap->buffer[p];
if ( value != 0x00 ){
value = 0xff;
}else{
value = 0x00;
}
pixel->red = 255- value;
pixel->green = 255- value;
pixel->blue = 255- value;
pixel->alpha = 255;
}
}
break;
case 1 : fruit.width = bitmap->width; // MONO and GRAY
fruit.height = bitmap->rows;
fruit.pixels = calloc ( fruit.width * fruit.height,
sizeof (PIXEL));
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
PIXEL * pixel = Pixel_At (& fruit, x, y);
p = (y * bitmap->pitch ) + x;
value = bitmap->buffer[p];
pixel->red = 255- value;
pixel->green = 255- value;
pixel->blue = 255- value;
pixel->alpha = 255;
}
}
break;
case 2 :
case 3 : fruit.width = bitmap->width / 3; // LCD
fruit.height = bitmap->rows;
fruit.pixels = calloc ( fruit.width * fruit.height,
sizeof (PIXEL));
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
PIXEL * pixel = Pixel_At (& fruit, x, y);
p = (y * bitmap->pitch ) + (x)*3;
value = bitmap->buffer[p];
pixel->red = 255- value;
p++;
value = bitmap->buffer[p];
pixel->green = 255- value;
p++;
value = bitmap->buffer[p];
pixel->blue = 255- value;
pixel->alpha = 255;
}
}
break;
case 4 :
case 5 : fruit.width = bitmap->width; // LCD_V
fruit.height = bitmap->rows / 3;
fruit.pixels = calloc ( fruit.width * fruit.height,
sizeof (PIXEL));
for (y = 0; y < fruit.height; y++) {
for (x = 0; x < fruit.width; x++) {
PIXEL * pixel = Pixel_At (& fruit, x, y);
p = ((y*3) * bitmap->pitch ) + x;
value = bitmap->buffer[p];
pixel->red = 255- value;
p += bitmap->pitch;
value = bitmap->buffer[p];
pixel->green = 255- value;
p += bitmap->pitch;
value = bitmap->buffer[p];
pixel->blue = 255- value;
pixel->alpha = 255;
}
}
break;
default : fruit.width = bitmap->width;
fruit.height = bitmap->rows;
break;
}
char * file_name = ( char * )calloc(150,sizeof(char));
sprintf(file_name, "%s_%d.png", name, i);
Generate_PNG (& fruit, file_name, render_mode);
free (fruit.pixels);
}

View File

@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ft2build.h>
#include "murmur3.h" // MurmurHash3_x64_128 header file
#include <png.h>
#include FT_FREETYPE_H
#include FT_MODULE_H
#include FT_LCD_FILTER_H
#include FT_BITMAP_H
#define BITS_PER_PIXEL_RGBA 32
typedef struct { // To store 32bit Hash
FT_UInt32 hash[1];
}HASH_32;
typedef struct { // To store 128bit Hash
FT_UInt32 hash[4];
}HASH_128;
typedef struct {
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
} PIXEL;
typedef struct {
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
} PIXEL_BGRA;
/* A picture. */
typedef struct {
PIXEL *pixels;
size_t width;
size_t height;
} IMAGE;
//------------------------------------------------------------------------------
HASH_32 * Generate_Hash_x86_32(FT_Bitmap * bitmap, HASH_32 * murmur);
HASH_128 * Generate_Hash_x86_128(FT_Bitmap * bitmap, HASH_128 * murmur);
HASH_128 * Generate_Hash_x64_128(FT_Bitmap * bitmap, HASH_128 * murmur);
//------------------------------------------------------------------------------
PIXEL * Pixel_At (IMAGE * bitmap, int x, int y); // Returns a pointer to pixel
// at (x,y) co-ordinate
// buffer to image
void Make_PNG(FT_Bitmap* bitmap,char* name,int i,int render_mode);
// Image to file
int Generate_PNG (IMAGE *bitmap, const char *path,int render_mode);

270
tests/web_interface/main.c Normal file
View File

@ -0,0 +1,270 @@
// 'main' takes in .so files for two versions of FT, the character size, and
// a font directory. It uses 'bitmap' and 'murmur3' to compare hashes for all
// glyphs in the font, using the base and test FT libraries, then renders
// differing glyphs as PNGs. A webpage is generated with a table of these
// glyphs.
#include "bitmap.h"
#include <dlfcn.h> // for dynamic linking
#include <math.h> // for glyph comparison
// a single 'entry' is related to a single charcode for a glyph, as well as
// a row in the generated html table
struct entry
{
char base_img[256]; // filename of base png
char test_img[256]; // filename of test png
char base_hash[33]; // base mm3 hash
char test_hash[33]; // test mm3 hash
double base_value; // comparison metric for base glyph
double test_value; // comparison metric for test glyph
double difference; // difference holding the distance between glyphs
};
void render(const char*, const char*, FT_UInt32, int, struct entry**, int*);
int compare(const void* e1, const void* e2);
void make_html(struct entry**, int* , const char*);
int main(int argc, char const *argv[])
{
const char* base_ft; // path of base ft
const char* test_ft; // path of test ft
FT_UInt32 size; // char size
const char* font; // path of font
int mode;
// create array of structs with size 1
struct entry* entries = malloc(1 * sizeof(struct entry));
int entries_len = 1;
if(argc != 5)
{
printf("Usage: %s <base ft.so> <test ft.so> <char size> <font>\n", argv[0]);
return 0;
}
base_ft = argv[1];
test_ft = argv[2];
size = atoi(argv[3]);
font = argv[4];
mode = 0; // base hashes
render(base_ft, font, size, mode, &entries, &entries_len);
mode = 1; // test hashes
render(test_ft, font, size, mode, &entries, &entries_len);
mode = 2; // base images for differing glyphs
render(test_ft, font, size, mode, &entries, &entries_len);
mode = 3; // test images for differing glyphs
render(test_ft, font, size, mode, &entries, &entries_len);
// quicksort array by decreasing difference
qsort(entries, entries_len, sizeof(struct entry), compare);
// generate HTML output
make_html(&entries, &entries_len, font);
// free array
free(entries);
}
// generate HTML output
void make_html(struct entry** entries, int* num, const char* font)
{
FILE *fp = fopen("index.html", "w");
if (fp == NULL)
{
printf("Error opening file.\n");
exit(1);
}
// TODO: refactor
fprintf(fp, "<!DOCTYPE html>\n<html>\n<head>\n<style>\nimg{image-rendering: optimizeSpeed;image-rendering: -moz-crisp-edges;image-rendering: -o-crisp-edges;image-rendering: -webkit-optimize-contrast;image-rendering: pixelated;image-rendering: optimize-contrast;-ms-interpolation-mode: nearest-neighbor;min-width:10%%}\ntable, th, td{\nborder: 1px solid black;\n}\n</style>\n</head>\n\n<body>\n");
fprintf(fp, "<p>%s</p>\n<table style=\"width:100%%\">", font);
fprintf(fp, "<tr><th>ID</th><th>Difference</th><th>Base glyph | Test glyph</th></tr>\n");
for (int i = 0; i < *num; ++i)
{
if ((*entries)[i].difference > 0){
fprintf(fp, "<tr><td>%d</td><td>%.2f</td><td><img src=\"%s\"</img> <img src=\"%s\"</img></td></tr>\n", i, (*entries)[i].difference, (*entries)[i].base_img, (*entries)[i].test_img);
}
}
fprintf(fp, "</table>\n</body>\n</html>");
fclose(fp);
}
// comparison function for quick sort
int compare (const void* e1, const void* e2)
{
struct entry *s1 = (struct entry *)e1;
struct entry *s2 = (struct entry *)e2;
int comp = (int)(s2->difference) - (int)(s1->difference);
return comp;
}
// multifunctional render function
void render(const char* ft_dir, const char* font, FT_UInt32 size, int mode, struct entry** entries, int* entries_len)
{
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
FT_Bitmap *bitmap;
FT_Error error;
int i;
// declare dynamically loaded functions
FT_Error (*ft_init_fun)(FT_Library*);
FT_Error (*ft_newface_fun)(FT_Library, const char*, FT_Long, FT_Face*);
FT_Error (*ft_setcharsize_fun)(FT_Face, FT_F26Dot6, FT_F26Dot6, FT_UInt,
FT_UInt);
FT_Error (*ft_loadglyph_fun)(FT_Face, FT_UInt, FT_Int32);
FT_Error (*ft_renderglyph_fun)(FT_GlyphSlot, FT_Render_Mode);
FT_Error (*ft_doneface_fun)(FT_Face);
FT_Error (*ft_donefreetype_fun)(FT_Library);
void (*ft_bitmapinit_fun)(FT_Bitmap*);
FT_Error (*ft_bitmapconvert_fun)(FT_Library, const FT_Bitmap*, FT_Bitmap,
FT_Int);
// dynamically load ft version specified by ft_dir
void* handle = dlopen(ft_dir, RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
if (!handle) {
fputs(dlerror(), stderr);
exit(1);
}
// error checking
dlerror();
// initialize dynamically loaded functions
*(void**)(&ft_init_fun) = dlsym(handle,"FT_Init_FreeType");
*(void**)(&ft_newface_fun) = dlsym(handle,"FT_New_Face");
*(void**)(&ft_setcharsize_fun) = dlsym(handle,"FT_Set_Char_Size");
*(void**)(&ft_loadglyph_fun) = dlsym(handle,"FT_Load_Glyph");
*(void**)(&ft_renderglyph_fun) = dlsym(handle,"FT_Render_Glyph");
*(void**)(&ft_doneface_fun) = dlsym(handle,"FT_Done_Face");
*(void**)(&ft_donefreetype_fun) = dlsym(handle,"FT_Done_FreeType");
*(void**)(&ft_bitmapinit_fun) = dlsym(handle,"FT_Bitmap_Init");
*(void**)(&ft_bitmapconvert_fun) = dlsym(handle,"FT_Bitmap_Convert");
// error checking
dlerror();
error = ft_init_fun(&library);
if (error)
{
printf("Error: library init");
}
error = ft_newface_fun(library, font, 0, &face);
if (error)
{
printf("Error: loading face");
}
error = ft_setcharsize_fun(face, size * 64, 0, 96, 0);
if (error)
{
printf("Error: setting char size");
}
slot = face->glyph;
// if 'images' folder doesn't exist, create it
struct stat st = {0};
if (stat("./images/", &st) == -1) {
mkdir("./images/", 0777);
}
// iterate over all glyphs in the font
for (i = 0; i < face->num_glyphs; ++i)
{
// if we've reached capacity of array, double array size
if (i == *entries_len)
{
*entries = realloc(*entries, (*entries_len * 2) * sizeof (struct entry));
entries = &(*entries);
*entries_len = *entries_len * 2;
}
// initially assume no difference between glyphs
(*entries)[i].difference = 0.0;
// if hashes are the same, move to the next glyph
if (((mode == 2) || (mode == 3)) &&
(strcmp((*entries)[i].base_hash, (*entries)[i].test_hash) == 0))
{
continue;
}
error = ft_loadglyph_fun(face, i, FT_LOAD_DEFAULT);
if (error)
{
printf("Error: loading glyph");
}
error = ft_renderglyph_fun(slot, FT_RENDER_MODE_NORMAL);
if (error)
{
printf("Error: rendering glyph");
}
bitmap = &slot->bitmap;
// calculates and stores hash for a glyph
if ((mode == 0) || (mode == 1))
{
HASH_128 * murmur = (HASH_128 *)malloc(sizeof(HASH_128));
murmur = Generate_Hash_x64_128(bitmap, murmur);
if (mode == 0)
{
sprintf((*entries)[i].base_hash, "%08x%08x%08x%08x",
murmur->hash[0], murmur->hash[1], murmur->hash[2], murmur->hash[3]);
}
else
{
sprintf((*entries)[i].test_hash, "%08x%08x%08x%08x",
murmur->hash[0], murmur->hash[1], murmur->hash[2], murmur->hash[3]);
}
} else { // if we're done hashing
// if glyph is empty, move to the next glyph
if (bitmap->width == 0 || bitmap->rows == 0)
{
continue;
}
// the glyphs have a different hash and are not empty so we generate a
// PNG for the glyph and calculate the difference between them
if (mode == 2)
{
Make_PNG(bitmap, "./images/base", i, 1);
sprintf((*entries)[i].base_img, "images/base_%d.png", i);
(*entries)[i].base_value = (double)(rand() % 1000);
} else if (mode == 3){
Make_PNG(bitmap, "./images/test", i, 1);
sprintf((*entries)[i].test_img, "images/test_%d.png", i);
(*entries)[i].test_value = (double)(rand() % 1000);
(*entries)[i].difference = fabs((*entries)[i].base_value - (*entries)[i].test_value);
}
}
}
error = ft_doneface_fun(face);
if (error)
{
printf("Error: freeing face");
}
error = ft_donefreetype_fun(library);
if (error)
{
printf("Error: freeing library");
}
dlclose(handle);
}

View File

@ -0,0 +1,312 @@
//-----------------------------------------------------------------------------
// Note - The x86 and x64 versions do _not_ produce the same results, as the
// algorithms are optimized for their respective platforms. You can still
// compile and run any of them on any platform, but your performance with the
// non-native version will be less than optimal.
#include "murmur3.h"
//-----------------------------------------------------------------------------
// Platform-specific functions and macros
#ifdef __GNUC__
#define FORCE_INLINE __attribute__((always_inline)) inline
#else
#define FORCE_INLINE inline
#endif
static FORCE_INLINE uint32_t rotl32 ( uint32_t x, int8_t r )
{
return (x << r) | (x >> (32 - r));
}
static FORCE_INLINE uint64_t rotl64 ( uint64_t x, int8_t r )
{
return (x << r) | (x >> (64 - r));
}
#define ROTL32(x,y) rotl32(x,y)
#define ROTL64(x,y) rotl64(x,y)
#define BIG_CONSTANT(x) (x##LLU)
//-----------------------------------------------------------------------------
// Block read - if your platform needs to do endian-swapping or can only
// handle aligned reads, do the conversion here
#define getblock(p, i) (p[i])
//-----------------------------------------------------------------------------
// Finalization mix - force all bits of a hash block to avalanche
static FORCE_INLINE uint32_t fmix32 ( uint32_t h )
{
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
//----------
static FORCE_INLINE uint64_t fmix64 ( uint64_t k )
{
k ^= k >> 33;
k *= BIG_CONSTANT(0xff51afd7ed558ccd);
k ^= k >> 33;
k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
k ^= k >> 33;
return k;
}
//-----------------------------------------------------------------------------
void MurmurHash3_x86_32 ( const void * key, int len,
uint32_t seed, void * out )
{
const uint8_t * data = (const uint8_t*)key;
const int nblocks = len / 4;
int i;
uint32_t h1 = seed;
uint32_t c1 = 0xcc9e2d51;
uint32_t c2 = 0x1b873593;
//----------
// body
const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
for(i = -nblocks; i; i++)
{
uint32_t k1 = getblock(blocks,i);
k1 *= c1;
k1 = ROTL32(k1,15);
k1 *= c2;
h1 ^= k1;
h1 = ROTL32(h1,13);
h1 = h1*5+0xe6546b64;
}
//----------
// tail
const uint8_t * tail = (const uint8_t*)(data + nblocks*4);
uint32_t k1 = 0;
switch(len & 3)
{
case 3: k1 ^= tail[2] << 16;
case 2: k1 ^= tail[1] << 8;
case 1: k1 ^= tail[0];
k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
};
//----------
// finalization
h1 ^= len;
h1 = fmix32(h1);
*(uint32_t*)out = h1;
}
//-----------------------------------------------------------------------------
void MurmurHash3_x86_128 ( const void * key, const int len,
uint32_t seed, void * out )
{
const uint8_t * data = (const uint8_t*)key;
const int nblocks = len / 16;
int i;
uint32_t h1 = seed;
uint32_t h2 = seed;
uint32_t h3 = seed;
uint32_t h4 = seed;
uint32_t c1 = 0x239b961b;
uint32_t c2 = 0xab0e9789;
uint32_t c3 = 0x38b34ae5;
uint32_t c4 = 0xa1e38b93;
//----------
// body
const uint32_t * blocks = (const uint32_t *)(data + nblocks*16);
for(i = -nblocks; i; i++)
{
uint32_t k1 = getblock(blocks,i*4+0);
uint32_t k2 = getblock(blocks,i*4+1);
uint32_t k3 = getblock(blocks,i*4+2);
uint32_t k4 = getblock(blocks,i*4+3);
k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
h1 = ROTL32(h1,19); h1 += h2; h1 = h1*5+0x561ccd1b;
k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
h2 = ROTL32(h2,17); h2 += h3; h2 = h2*5+0x0bcaa747;
k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
h3 = ROTL32(h3,15); h3 += h4; h3 = h3*5+0x96cd1c35;
k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
h4 = ROTL32(h4,13); h4 += h1; h4 = h4*5+0x32ac3b17;
}
//----------
// tail
const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
uint32_t k1 = 0;
uint32_t k2 = 0;
uint32_t k3 = 0;
uint32_t k4 = 0;
switch(len & 15)
{
case 15: k4 ^= tail[14] << 16;
case 14: k4 ^= tail[13] << 8;
case 13: k4 ^= tail[12] << 0;
k4 *= c4; k4 = ROTL32(k4,18); k4 *= c1; h4 ^= k4;
case 12: k3 ^= tail[11] << 24;
case 11: k3 ^= tail[10] << 16;
case 10: k3 ^= tail[ 9] << 8;
case 9: k3 ^= tail[ 8] << 0;
k3 *= c3; k3 = ROTL32(k3,17); k3 *= c4; h3 ^= k3;
case 8: k2 ^= tail[ 7] << 24;
case 7: k2 ^= tail[ 6] << 16;
case 6: k2 ^= tail[ 5] << 8;
case 5: k2 ^= tail[ 4] << 0;
k2 *= c2; k2 = ROTL32(k2,16); k2 *= c3; h2 ^= k2;
case 4: k1 ^= tail[ 3] << 24;
case 3: k1 ^= tail[ 2] << 16;
case 2: k1 ^= tail[ 1] << 8;
case 1: k1 ^= tail[ 0] << 0;
k1 *= c1; k1 = ROTL32(k1,15); k1 *= c2; h1 ^= k1;
};
//----------
// finalization
h1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;
h1 += h2; h1 += h3; h1 += h4;
h2 += h1; h3 += h1; h4 += h1;
h1 = fmix32(h1);
h2 = fmix32(h2);
h3 = fmix32(h3);
h4 = fmix32(h4);
h1 += h2; h1 += h3; h1 += h4;
h2 += h1; h3 += h1; h4 += h1;
((uint32_t*)out)[0] = h1;
((uint32_t*)out)[1] = h2;
((uint32_t*)out)[2] = h3;
((uint32_t*)out)[3] = h4;
}
//-----------------------------------------------------------------------------
void MurmurHash3_x64_128 ( const void * key, const int len,
const uint32_t seed, void * out )
{
const uint8_t * data = (const uint8_t*)key;
const int nblocks = len / 16;
int i;
uint64_t h1 = seed;
uint64_t h2 = seed;
uint64_t c1 = BIG_CONSTANT(0x87c37b91114253d5);
uint64_t c2 = BIG_CONSTANT(0x4cf5ad432745937f);
//----------
// body
const uint64_t * blocks = (const uint64_t *)(data);
for(i = 0; i < nblocks; i++)
{
uint64_t k1 = getblock(blocks,i*2+0);
uint64_t k2 = getblock(blocks,i*2+1);
k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
h1 = ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;
k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
h2 = ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
}
//----------
// tail
const uint8_t * tail = (const uint8_t*)(data + nblocks*16);
uint64_t k1 = 0;
uint64_t k2 = 0;
switch(len & 15)
{
case 15: k2 ^= (uint64_t)(tail[14]) << 48;
case 14: k2 ^= (uint64_t)(tail[13]) << 40;
case 13: k2 ^= (uint64_t)(tail[12]) << 32;
case 12: k2 ^= (uint64_t)(tail[11]) << 24;
case 11: k2 ^= (uint64_t)(tail[10]) << 16;
case 10: k2 ^= (uint64_t)(tail[ 9]) << 8;
case 9: k2 ^= (uint64_t)(tail[ 8]) << 0;
k2 *= c2; k2 = ROTL64(k2,33); k2 *= c1; h2 ^= k2;
case 8: k1 ^= (uint64_t)(tail[ 7]) << 56;
case 7: k1 ^= (uint64_t)(tail[ 6]) << 48;
case 6: k1 ^= (uint64_t)(tail[ 5]) << 40;
case 5: k1 ^= (uint64_t)(tail[ 4]) << 32;
case 4: k1 ^= (uint64_t)(tail[ 3]) << 24;
case 3: k1 ^= (uint64_t)(tail[ 2]) << 16;
case 2: k1 ^= (uint64_t)(tail[ 1]) << 8;
case 1: k1 ^= (uint64_t)(tail[ 0]) << 0;
k1 *= c1; k1 = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
};
//----------
// finalization
h1 ^= len; h2 ^= len;
h1 += h2;
h2 += h1;
h1 = fmix64(h1);
h2 = fmix64(h2);
h1 += h2;
h2 += h1;
((uint64_t*)out)[0] = h1;
((uint64_t*)out)[1] = h2;
}
//-----------------------------------------------------------------------------

View File

@ -0,0 +1,24 @@
#ifndef _MURMURHASH3_H_
#define _MURMURHASH3_H_
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
//-----------------------------------------------------------------------------
void MurmurHash3_x86_32 (const void *key, int len, uint32_t seed, void *out);
void MurmurHash3_x86_128(const void *key, int len, uint32_t seed, void *out);
void MurmurHash3_x64_128(const void *key, int len, uint32_t seed, void *out);
//-----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
#endif // _MURMURHASH3_H_