aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Hanspeter Portner <dev@open-music-kontrollers.ch>2018-12-31 23:24:58 +0100
committerGravatar Hanspeter Portner <dev@open-music-kontrollers.ch>2018-12-31 23:24:58 +0100
commit56234f740d654b08cd7e03fa3d85550b33d1ff18 (patch)
tree85bb0659c449647b6e913987c71ca7a0e619ba65
parentacf6ac757f0b8f624b7dbb5b8de9ada7c3f4eebe (diff)
downloadmapper.lv2-56234f740d654b08cd7e03fa3d85550b33d1ff18.zip
mapper.lv2-56234f740d654b08cd7e03fa3d85550b33d1ff18.tar.gz
mapper.lv2-56234f740d654b08cd7e03fa3d85550b33d1ff18.tar.bz2
mapper.lv2-56234f740d654b08cd7e03fa3d85550b33d1ff18.tar.xz
use mum hasher, add support for static URIDs.
-rw-r--r--VERSION2
-rw-r--r--mapper.lv2/mapper.h144
-rw-r--r--mapper.lv2/mum.h416
-rw-r--r--test/mapper_test.c53
4 files changed, 513 insertions, 102 deletions
diff --git a/VERSION b/VERSION
index c892edd..a4c528c 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.1.51
+0.1.53
diff --git a/mapper.lv2/mapper.h b/mapper.lv2/mapper.h
index dd4fa1e..4d1da12 100644
--- a/mapper.lv2/mapper.h
+++ b/mapper.lv2/mapper.h
@@ -34,6 +34,10 @@ extern "C" {
# define MAPPER_API static
#endif
+#if !defined(MAPPER_SEED)
+# define MAPPER_SEED 12345
+#endif
+
typedef struct _mapper_t mapper_t;
typedef char *(*mapper_alloc_t)(void *data, size_t size);
@@ -43,7 +47,7 @@ MAPPER_API bool
mapper_is_lock_free(void);
MAPPER_API mapper_t *
-mapper_new(uint32_t nitems,
+mapper_new(uint32_t nitems, uint32_t nstats, const char **stats,
mapper_alloc_t mapper_alloc_cb, mapper_free_t mapper_free_cb, void *data);
MAPPER_API void
@@ -60,6 +64,8 @@ mapper_get_unmap(mapper_t *mapper);
#ifdef MAPPER_IMPLEMENTATION
+#include <mapper.lv2/mum.h>
+
#if !defined(_WIN32)
# include <sys/mman.h> // mlock
#endif
@@ -68,6 +74,7 @@ typedef struct _mapper_item_t mapper_item_t;
struct _mapper_item_t {
atomic_uintptr_t val;
+ uint32_t stat;
};
struct _mapper_t {
@@ -82,98 +89,12 @@ struct _mapper_t {
LV2_URID_Map map;
LV2_URID_Unmap unmap;
+ uint32_t nstats;
+ const char **stats;
+
mapper_item_t items [];
};
-/*
- * MurmurHash3 was created by Austin Appleby in 2008. The initial
- * implementation was published in C++ and placed in the public.
- * https://sites.google.com/site/murmurhash/
- * Seungyoung Kim has ported its implementation into C language
- * in 2012 and published it as a part of qLibc component.
- *
- * Copyright (c) 2010-2015 Seungyoung Kim.
- * 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.
- *
- * 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 OWNER 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.
- *****************************************************************************/
-static inline uint32_t
-_mapper_murmur3_32(const void *data, size_t nbytes)
-{
- const uint32_t c1 = 0xcc9e2d51;
- const uint32_t c2 = 0x1b873593;
-
- const int nblocks = nbytes / 4;
- const uint32_t *blocks = (const uint32_t *)(data);
- const uint8_t *tail = (const uint8_t *)data + (nblocks * 4);
-
- uint32_t h = 0;
-
- uint32_t k;
- for(int i = 0; i < nblocks; i++)
- {
- k = blocks[i];
-
- k *= c1;
- k = (k << 15) | (k >> (32 - 15));
- k *= c2;
-
- h ^= k;
- h = (h << 13) | (h >> (32 - 13));
- h = (h * 5) + 0xe6546b64;
- }
-
- k = 0;
- switch(nbytes & 3)
- {
- case 3:
- k ^= tail[2] << 16;
-#if __GNUC__ >= 7
- __attribute__((fallthrough));
-#endif
- case 2:
- k ^= tail[1] << 8;
-#if __GNUC__ >= 7
- __attribute__((fallthrough));
-#endif
- case 1:
- k ^= tail[0];
- k *= c1;
- k = (k << 15) | (k >> (32 - 15));
- k *= c2;
- h ^= k;
- };
-
- h ^= nbytes;
-
- h ^= h >> 16;
- h *= 0x85ebca6b;
- h ^= h >> 13;
- h *= 0xc2b2ae35;
- h ^= h >> 16;
-
- return h;
-}
-
static uint32_t
_mapper_map(void *data, const char *uri)
{
@@ -185,7 +106,7 @@ _mapper_map(void *data, const char *uri)
char *uri_clone = NULL;
const size_t uri_len = strlen(uri) + 1;
mapper_t *mapper = data;
- const uint32_t hash = _mapper_murmur3_32(uri, uri_len - 1); // ignore zero terminator
+ const uint32_t hash = mum_hash(uri, uri_len - 1, MAPPER_SEED); // ignore zero terminator
for(uint32_t i = 0, idx = (hash + i*i) & mapper->nitems_mask;
i < mapper->nitems;
@@ -204,7 +125,7 @@ _mapper_map(void *data, const char *uri)
mapper->free(mapper->data, uri_clone); // free superfluous URI
}
- return idx + 1;
+ return item->stat ? item->stat : idx + mapper->nstats;
}
// slot is already taken by another URI, try next slot
@@ -233,13 +154,13 @@ _mapper_map(void *data, const char *uri)
{
atomic_fetch_add_explicit(&mapper->usage, 1, memory_order_relaxed);
- return idx + 1;
+ return item->stat ? item->stat : idx + mapper->nstats;
}
else if(memcmp((const char *)expected, uri, uri_len) == 0) // other thread stole it
{
mapper->free(mapper->data, uri_clone); // free superfluous URI
- return idx + 1;
+ return item->stat ? item->stat : idx + mapper->nstats;
}
// slot is already taken by another URI, try next slot
@@ -256,16 +177,28 @@ _mapper_map(void *data, const char *uri)
}
static const char *
-_mapper_unmap(void *data, uint32_t idx)
+_mapper_unmap(void *data, uint32_t urid)
{
mapper_t *mapper = data;
- if( (idx == 0) || (idx > mapper->nitems) ) // invalid URID
+ if(urid == 0) // invalid URID
+ {
+ return NULL;
+ }
+
+ if(urid < mapper->nstats)
+ {
+ return mapper->stats[urid];
+ }
+
+ urid -= mapper->nstats;
+
+ if(urid > mapper->nitems) // invalid URID
{
return NULL;
}
- mapper_item_t *item = &mapper->items[idx - 1];
+ mapper_item_t *item = &mapper->items[urid];
const uintptr_t val = atomic_load_explicit(&item->val, memory_order_relaxed);
@@ -297,7 +230,7 @@ mapper_is_lock_free(void)
}
MAPPER_API mapper_t *
-mapper_new(uint32_t nitems,
+mapper_new(uint32_t nitems, uint32_t nstats, const char **stats,
mapper_alloc_t mapper_alloc_cb, mapper_free_t mapper_free_cb, void *data)
{
// item number needs to be a power of two
@@ -318,6 +251,9 @@ mapper_new(uint32_t nitems,
mapper->nitems = power_of_two;
mapper->nitems_mask = power_of_two - 1;
+ mapper->nstats = nstats;
+ mapper->stats = stats;
+
mapper->alloc = mapper_alloc_cb
? mapper_alloc_cb
: _mapper_alloc_fallback;
@@ -341,6 +277,7 @@ mapper_new(uint32_t nitems,
mapper_item_t *item = &mapper->items[idx];
atomic_init(&item->val, 0);
+ item->stat = 0;
}
#if !defined(_WIN32)
@@ -348,6 +285,17 @@ mapper_new(uint32_t nitems,
mlock(mapper, sizeof(mapper_t) + mapper->nitems*sizeof(mapper_item_t));
#endif
+ // populate static URIDs
+ for(uint32_t i = 1; i < mapper->nstats; i++)
+ {
+ const char *uri = mapper->stats[i];
+
+ const uint32_t urid = _mapper_map(mapper, uri);
+ mapper_item_t *item = &mapper->items[urid - mapper->nstats];
+
+ item->stat = i;
+ }
+
return mapper;
}
diff --git a/mapper.lv2/mum.h b/mapper.lv2/mum.h
new file mode 100644
index 0000000..325ed99
--- /dev/null
+++ b/mapper.lv2/mum.h
@@ -0,0 +1,416 @@
+/* Copyright (c) 2016 Vladimir Makarov <vmakarov@gcc.gnu.org>
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the "Software"), to deal in the Software without
+ restriction, including without limitation the rights to use, copy,
+ modify, merge, publish, distribute, sublicense, and/or sell copies
+ of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ SOFTWARE.
+*/
+
+/* This file implements MUM (MUltiply and Mix) hashing. We randomize
+ input data by 64x64-bit multiplication and mixing hi- and low-parts
+ of the multiplication result by using an addition and then mix it
+ into the current state. We use prime numbers randomly generated
+ with the equal probability of their bit values for the
+ multiplication. When all primes are used once, the state is
+ randomized and the same prime numbers are used again for data
+ randomization.
+
+ The MUM hashing passes all SMHasher tests. Pseudo Random Number
+ Generator based on MUM also passes NIST Statistical Test Suite for
+ Random and Pseudorandom Number Generators for Cryptographic
+ Applications (version 2.2.1) with 1000 bitstreams each containing
+ 1M bits. MUM hashing is also faster Spooky64 and City64 on small
+ strings (at least upto 512-bit) on Haswell and Power7. The MUM bulk
+ speed (speed on very long data) is bigger than Spooky and City on
+ Power7. On Haswell the bulk speed is bigger than Spooky one and
+ close to City speed. */
+
+#ifndef __MUM_HASH__
+#define __MUM_HASH__
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+#ifdef _MSC_VER
+typedef unsigned __int16 uint16_t;
+typedef unsigned __int32 uint32_t;
+typedef unsigned __int64 uint64_t;
+#else
+#include <stdint.h>
+#endif
+
+#ifdef __GNUC__
+#define _MUM_ATTRIBUTE_UNUSED __attribute__((unused))
+#define _MUM_OPTIMIZE(opts) __attribute__((__optimize__ (opts)))
+#define _MUM_TARGET(opts) __attribute__((__target__ (opts)))
+#else
+#define _MUM_ATTRIBUTE_UNUSED
+#define _MUM_OPTIMIZE(opts)
+#define _MUM_TARGET(opts)
+#endif
+
+/* Macro saying to use 128-bit integers implemented by GCC for some
+ targets. */
+#ifndef _MUM_USE_INT128
+/* In GCC uint128_t is defined if HOST_BITS_PER_WIDE_INT >= 64.
+ HOST_WIDE_INT is long if HOST_BITS_PER_LONG > HOST_BITS_PER_INT,
+ otherwise int. */
+#if defined(__GNUC__) && UINT_MAX != ULONG_MAX
+#define _MUM_USE_INT128 1
+#else
+#define _MUM_USE_INT128 0
+#endif
+#endif
+
+#if defined(__GNUC__) && ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 9) || (__GNUC__ > 4))
+#define _MUM_FRESH_GCC
+#endif
+
+/* Here are different primes randomly generated with the equal
+ probability of their bit values. They are used to randomize input
+ values. */
+static uint64_t _mum_hash_step_prime = 0x2e0bb864e9ea7df5ULL;
+static uint64_t _mum_key_step_prime = 0xcdb32970830fcaa1ULL;
+static uint64_t _mum_block_start_prime = 0xc42b5e2e6480b23bULL;
+static uint64_t _mum_unroll_prime = 0x7b51ec3d22f7096fULL;
+static uint64_t _mum_tail_prime = 0xaf47d47c99b1461bULL;
+static uint64_t _mum_finish_prime1 = 0xa9a7ae7ceff79f3fULL;
+static uint64_t _mum_finish_prime2 = 0xaf47d47c99b1461bULL;
+
+static uint64_t _mum_primes [] = {
+ 0X9ebdcae10d981691, 0X32b9b9b97a27ac7d, 0X29b5584d83d35bbd, 0X4b04e0e61401255f,
+ 0X25e8f7b1f1c9d027, 0X80d4c8c000f3e881, 0Xbd1255431904b9dd, 0X8a3bd4485eee6d81,
+ 0X3bc721b2aad05197, 0X71b1a19b907d6e33, 0X525e6c1084a8534b, 0X9e4c2cd340c1299f,
+ 0Xde3add92e94caa37, 0X7e14eadb1f65311d, 0X3f5aa40f89812853, 0X33b15a3b587d15c9,
+};
+
+/* Multiply 64-bit V and P and return sum of high and low parts of the
+ result. */
+static inline uint64_t
+_mum (uint64_t v, uint64_t p) {
+ uint64_t hi, lo;
+#if _MUM_USE_INT128
+#if defined(__aarch64__)
+ /* AARCH64 needs 2 insns to calculate 128-bit result of the
+ multiplication. If we use a generic code we actually call a
+ function doing 128x128->128 bit multiplication. The function is
+ very slow. */
+ lo = v * p, hi;
+ asm ("umulh %0, %1, %2" : "=r" (hi) : "r" (v), "r" (p));
+#else
+ __uint128_t r = (__uint128_t) v * (__uint128_t) p;
+ hi = (uint64_t) (r >> 64);
+ lo = (uint64_t) r;
+#endif
+#else
+ /* Implementation of 64x64->128-bit multiplication by four 32x32->64
+ bit multiplication. */
+ uint64_t hv = v >> 32, hp = p >> 32;
+ uint64_t lv = (uint32_t) v, lp = (uint32_t) p;
+ uint64_t rh = hv * hp;
+ uint64_t rm_0 = hv * lp;
+ uint64_t rm_1 = hp * lv;
+ uint64_t rl = lv * lp;
+ uint64_t t, carry = 0;
+
+ /* We could ignore a carry bit here if we did not care about the
+ same hash for 32-bit and 64-bit targets. */
+ t = rl + (rm_0 << 32);
+#ifdef MUM_TARGET_INDEPENDENT_HASH
+ carry = t < rl;
+#endif
+ lo = t + (rm_1 << 32);
+#ifdef MUM_TARGET_INDEPENDENT_HASH
+ carry += lo < t;
+#endif
+ hi = rh + (rm_0 >> 32) + (rm_1 >> 32) + carry;
+#endif
+ /* We could use XOR here too but, for some reasons, on Haswell and
+ Power7 using an addition improves hashing performance by 10% for
+ small strings. */
+ return hi + lo;
+}
+
+#if defined(_MSC_VER)
+#define _mum_bswap_32(x) _byteswap_uint32_t (x)
+#define _mum_bswap_64(x) _byteswap_uint64_t (x)
+#elif defined(__APPLE__)
+#include <libkern/OSByteOrder.h>
+#define _mum_bswap_32(x) OSSwapInt32 (x)
+#define _mum_bswap_64(x) OSSwapInt64 (x)
+#elif defined(__GNUC__)
+#define _mum_bswap32(x) __builtin_bswap32 (x)
+#define _mum_bswap64(x) __builtin_bswap64 (x)
+#else
+#include <byteswap.h>
+#define _mum_bswap32(x) bswap32 (x)
+#define _mum_bswap64(x) bswap64 (x)
+#endif
+
+static inline uint64_t
+_mum_le (uint64_t v) {
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || !defined(MUM_TARGET_INDEPENDENT_HASH)
+ return v;
+#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+ return _mum_bswap64 (v);
+#else
+#error "Unknown endianess"
+#endif
+}
+
+static inline uint32_t
+_mum_le32 (uint32_t v) {
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || !defined(MUM_TARGET_INDEPENDENT_HASH)
+ return v;
+#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+ return _mum_bswap32 (v);
+#else
+#error "Unknown endianess"
+#endif
+}
+
+/* Macro defining how many times the most nested loop in
+ _mum_hash_aligned will be unrolled by the compiler (although it can
+ make an own decision:). Use only a constant here to help a
+ compiler to unroll a major loop.
+
+ The macro value affects the result hash for strings > 128 bit. The
+ unroll factor greatly affects the hashing speed. We prefer the
+ speed. */
+#ifndef _MUM_UNROLL_FACTOR_POWER
+#if defined(__PPC64__) && !defined(MUM_TARGET_INDEPENDENT_HASH)
+#define _MUM_UNROLL_FACTOR_POWER 3
+#elif defined(__aarch64__) && !defined(MUM_TARGET_INDEPENDENT_HASH)
+#define _MUM_UNROLL_FACTOR_POWER 4
+#else
+#define _MUM_UNROLL_FACTOR_POWER 2
+#endif
+#endif
+
+#if _MUM_UNROLL_FACTOR_POWER < 1
+#error "too small unroll factor"
+#elif _MUM_UNROLL_FACTOR_POWER > 4
+#error "We have not enough primes for such unroll factor"
+#endif
+
+#define _MUM_UNROLL_FACTOR (1 << _MUM_UNROLL_FACTOR_POWER)
+
+static inline uint64_t _MUM_OPTIMIZE("unroll-loops")
+_mum_hash_aligned (uint64_t start, const void *key, size_t len) {
+ uint64_t result = start;
+ const unsigned char *str = (const unsigned char *) key;
+ uint64_t u64;
+ size_t i;
+ size_t n;
+
+ result = _mum (result, _mum_block_start_prime);
+ while (len > _MUM_UNROLL_FACTOR * sizeof (uint64_t)) {
+ /* This loop could be vectorized when we have vector insns for
+ 64x64->128-bit multiplication. AVX2 currently only have a
+ vector insn for 4 32x32->64-bit multiplication. */
+ for (i = 0; i < _MUM_UNROLL_FACTOR; i++)
+ result ^= _mum (_mum_le (((uint64_t *) str)[i]), _mum_primes[i]);
+ len -= _MUM_UNROLL_FACTOR * sizeof (uint64_t);
+ str += _MUM_UNROLL_FACTOR * sizeof (uint64_t);
+ /* We will use the same prime numbers on the next iterations --
+ randomize the state. */
+ result = _mum (result, _mum_unroll_prime);
+ }
+ n = len / sizeof (uint64_t);
+ for (i = 0; i < n; i++)
+ result ^= _mum (_mum_le (((uint64_t *) str)[i]), _mum_primes[i]);
+ len -= n * sizeof (uint64_t); str += n * sizeof (uint64_t);
+ switch (len) {
+ case 7:
+ u64 = _mum_le32 (*(uint32_t *) str);
+ u64 |= (uint64_t) str[4] << 32;
+ u64 |= (uint64_t) str[5] << 40;
+ u64 |= (uint64_t) str[6] << 48;
+ return result ^ _mum (u64, _mum_tail_prime);
+ case 6:
+ u64 = _mum_le32 (*(uint32_t *) str);
+ u64 |= (uint64_t) str[4] << 32;
+ u64 |= (uint64_t) str[5] << 40;
+ return result ^ _mum (u64, _mum_tail_prime);
+ case 5:
+ u64 = _mum_le32 (*(uint32_t *) str);
+ u64 |= (uint64_t) str[4] << 32;
+ return result ^ _mum (u64, _mum_tail_prime);
+ case 4:
+ u64 = _mum_le32 (*(uint32_t *) str);
+ return result ^ _mum (u64, _mum_tail_prime);
+ case 3:
+ u64 = str[0];
+ u64 |= (uint64_t) str[1] << 8;
+ u64 |= (uint64_t) str[2] << 16;
+ return result ^ _mum (u64, _mum_tail_prime);
+ case 2:
+ u64 = str[0];
+ u64 |= (uint64_t) str[1] << 8;
+ return result ^ _mum (u64, _mum_tail_prime);
+ case 1:
+ u64 = str[0];
+ return result ^ _mum (u64, _mum_tail_prime);
+ }
+ return result;
+}
+
+/* Final randomization of H. */
+static inline uint64_t
+_mum_final (uint64_t h) {
+ h ^= _mum (h, _mum_finish_prime1);
+ h ^= _mum (h, _mum_finish_prime2);
+ return h;
+}
+
+#if defined(__x86_64__) && defined(_MUM_FRESH_GCC)
+
+/* We want to use AVX2 insn MULX instead of generic x86-64 MULQ where
+ it is possible. Although on modern Intel processors MULQ takes
+ 3-cycles vs. 4 for MULX, MULX permits more freedom in insn
+ scheduling as it uses less fixed registers. */
+static inline uint64_t _MUM_TARGET("arch=haswell")
+_mum_hash_avx2 (const void * key, size_t len, uint64_t seed) {
+ return _mum_final (_mum_hash_aligned (seed + len, key, len));
+}
+#endif
+
+#ifndef _MUM_UNALIGNED_ACCESS
+#if defined(__x86_64__) || defined(__i386__) || defined(__PPC64__) \
+ || defined(__s390__) || defined(__m32c__) || defined(cris) \
+ || defined(__CR16__) || defined(__vax__) || defined(__m68k__) \
+ || defined(__aarch64__) || defined(_M_AMD64) || defined(_M_IX86)
+#define _MUM_UNALIGNED_ACCESS 1
+#else
+#define _MUM_UNALIGNED_ACCESS 0
+#endif
+#endif
+
+/* When we need an aligned access to data being hashed we move part of
+ the unaligned data to an aligned block of given size and then
+ process it, repeating processing the data by the block. */
+#ifndef _MUM_BLOCK_LEN
+#define _MUM_BLOCK_LEN 1024
+#endif
+
+#if _MUM_BLOCK_LEN < 8
+#error "too small block length"
+#endif
+
+static inline uint64_t
+#if defined(__x86_64__)
+_MUM_TARGET("inline-all-stringops")
+#endif
+_mum_hash_default (const void *key, size_t len, uint64_t seed) {
+ uint64_t result;
+ const unsigned char *str = (const unsigned char *) key;
+ size_t block_len;
+ uint64_t buf[_MUM_BLOCK_LEN / sizeof (uint64_t)];
+
+ result = seed + len;
+ if (_MUM_UNALIGNED_ACCESS || ((size_t) str & 0x7) == 0)
+ result = _mum_hash_aligned (result, key, len);
+ else {
+ while (len != 0) {
+ block_len = len < _MUM_BLOCK_LEN ? len : _MUM_BLOCK_LEN;
+ memmove (buf, str, block_len);
+ result = _mum_hash_aligned (result, buf, block_len);
+ len -= block_len;
+ str += block_len;
+ }
+ }
+ return _mum_final (result);
+}
+
+static inline uint64_t
+_mum_next_factor (void) {
+ uint64_t start = 0;
+ int i;
+
+ for (i = 0; i < 8; i++)
+ start = (start << 8) | rand() % 256;
+ return start;
+}
+
+/* ++++++++++++++++++++++++++ Interface functions: +++++++++++++++++++ */
+
+/* Set random multiplicators depending on SEED. */
+static inline void
+mum_hash_randomize (uint64_t seed) {
+ size_t i;
+
+ srand (seed);
+ _mum_hash_step_prime = _mum_next_factor ();
+ _mum_key_step_prime = _mum_next_factor ();
+ _mum_finish_prime1 = _mum_next_factor ();
+ _mum_finish_prime2 = _mum_next_factor ();
+ _mum_block_start_prime = _mum_next_factor ();
+ _mum_unroll_prime = _mum_next_factor ();
+ _mum_tail_prime = _mum_next_factor ();
+ for (i = 0; i < sizeof (_mum_primes) / sizeof (uint64_t); i++)
+ _mum_primes[i] = _mum_next_factor ();
+}
+
+/* Start hashing data with SEED. Return the state. */
+static inline uint64_t
+mum_hash_init (uint64_t seed) {
+ return seed;
+}
+
+/* Process data KEY with the state H and return the updated state. */
+static inline uint64_t
+mum_hash_step (uint64_t h, uint64_t key)
+{
+ return _mum (h, _mum_hash_step_prime) ^ _mum (key, _mum_key_step_prime);
+}
+
+/* Return the result of hashing using the current state H. */
+static inline uint64_t
+mum_hash_finish (uint64_t h) {
+ return _mum_final (h);
+}
+
+/* Fast hashing of KEY with SEED. The hash is always the same for the
+ same key on any target. */
+static inline size_t
+mum_hash64 (uint64_t key, uint64_t seed) {
+ return mum_hash_finish (mum_hash_step (mum_hash_init (seed), key));
+}
+
+/* Hash data KEY of length LEN and SEED. The hash depends on the
+ target endianess and the unroll factor. */
+static inline uint64_t
+mum_hash (const void *key, size_t len, uint64_t seed) {
+#if defined(__x86_64__) && defined(_MUM_FRESH_GCC)
+ static int avx2_support = 0;
+
+ if (avx2_support > 0)
+ return _mum_hash_avx2 (key, len, seed);
+ else if (! avx2_support) {
+ __builtin_cpu_init ();
+ avx2_support = __builtin_cpu_supports ("avx2") ? 1 : -1;
+ if (avx2_support > 0)
+ return _mum_hash_avx2 (key, len, seed);
+ }
+#endif
+ return _mum_hash_default (key, len, seed);
+}
+
+#endif
diff --git a/test/mapper_test.c b/test/mapper_test.c
index 0c9c3c1..88cf007 100644
--- a/test/mapper_test.c
+++ b/test/mapper_test.c
@@ -30,6 +30,11 @@
#define MAX_URI_LEN 46
#define MAX_ITEMS 0x100000 // 1M
+#define USE_STATS
+
+#if defined(USE_STATS)
+# include <lv2/lv2plug.in/ns/ext/atom/atom.h>
+#endif
typedef struct _rtmem_slot_t rtmem_slot_t;
typedef struct _rtmem_t rtmem_t;
@@ -61,6 +66,34 @@ struct _pool_t {
MT mersenne;
};
+enum {
+ lv2_invalid = 0,
+
+#if defined(USE_STATS)
+ lv2_atom_Atom,
+ lv2_atom_AtomPort,
+ lv2_atom_Blank,
+ lv2_atom_Bool,
+ lv2_atom_Chunk,
+ lv2_atom_Double,
+#endif
+
+ nstats
+};
+
+static const char *stats [nstats] = {
+ [lv2_invalid] = NULL,
+
+#if defined(USE_STATS)
+ [lv2_atom_Atom] = LV2_ATOM__Atom,
+ [lv2_atom_AtomPort] = LV2_ATOM__AtomPort,
+ [lv2_atom_Blank] = LV2_ATOM__Blank,
+ [lv2_atom_Bool] = LV2_ATOM__Bool,
+ [lv2_atom_Chunk] = LV2_ATOM__Chunk,
+ [lv2_atom_Double] = LV2_ATOM__Double
+#endif
+};
+
static rtmem_t *
rtmem_new(uint32_t rpools)
{
@@ -144,6 +177,20 @@ _thread(void *data)
while(!atomic_load_explicit(&rolling, memory_order_relaxed))
{} // wait for go signal
+ // test static URIDs
+ for(uint32_t i = 1; i < nstats; i++)
+ {
+ const char *uri = stats[i];
+
+ const uint32_t urid = map->map(map->handle, uri);
+ assert(urid);
+ assert(urid == i);
+
+ const char *dst = unmap->unmap(unmap->handle, i);
+ assert(dst);
+ assert(strcmp(dst, uri) == 0);
+ }
+
char uri [MAX_URI_LEN];
for(uint32_t i = 0; i < MAX_ITEMS/2; i++)
{
@@ -209,8 +256,8 @@ main(int argc, char **argv)
// create mapper
mapper_t *mapper = is_rt
- ? mapper_new(MAX_ITEMS, _rtmem_alloc, _rtmem_free, rtmem)
- : mapper_new(MAX_ITEMS, _nrtmem_alloc, _nrtmem_free, &nrtmem);
+ ? mapper_new(MAX_ITEMS, nstats, stats, _rtmem_alloc, _rtmem_free, rtmem)
+ : mapper_new(MAX_ITEMS, nstats, stats, _nrtmem_alloc, _nrtmem_free, &nrtmem);
assert(mapper);
// create array of threads
@@ -242,7 +289,7 @@ main(int argc, char **argv)
// query usage
const uint32_t usage = mapper_get_usage(mapper);
- assert(usage == MAX_ITEMS/2);
+ assert(usage == MAX_ITEMS/2 + (nstats - 1));
// query rt memory allocations and frees
const uint32_t rt_nalloc = atomic_load_explicit(&rtmem->nalloc, memory_order_relaxed);