Sha256: 12d26f7a22f1095df05e823613fb10af1d50df51c59d4c782e074c94dbe42c7e

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

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


/* murmur hash 64A implementation */
VALUE hash_64_2A(VALUE self, VALUE value) {
  int64_t buf_len = RSTRING_LEN(value);
  char* buf = RSTRING_PTR(value);

  const int32_t seed = 0x1234ABCDL;
  const int64_t m = 0xc6a4a7935bd1e995LL;
  const int r = 47;

  int64_t h = seed ^ (buf_len * m);

  int64_t * data = (int64_t *)buf;
  int64_t * end = data + (buf_len / 8);

  while(data != end)
  {
    int64_t k = *data++;

    k *= m;
    uint64_t k1 = k;
    k ^= k1 >> r;
    k *= m;

    h ^= k;
    h *= m;
  }

  char * data2 = (char*)data;

  switch(buf_len & 7)
  {
    case 7: h ^= (int64_t)data2[6] << 48;
    case 6: h ^= (int64_t)data2[5] << 40;
    case 5: h ^= (int64_t)data2[4] << 32;
    case 4: h ^= (int64_t)data2[3] << 24;
    case 3: h ^= (int64_t)data2[2] << 16;
    case 2: h ^= (int64_t)data2[1] << 8;
    case 1: h ^= (int64_t)data2[0];
    h *= m;
  };

  uint64_t h1 = h;
  h ^= h1 >> r;
  h *= m;
  uint64_t h2 = h;
  h ^= h2 >> r;

  return LL2NUM(h);
}

void Init_murmurhash() {
  VALUE Murmurhash = rb_define_module("Murmurhash");
  rb_define_module_function(Murmurhash, "hash2A", hash_64_2A, 1);
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
murmurhash-0.0.1 ext/murmurhash/murmurhash.c