Skip to content

Commit

Permalink
Migrate all simdhash fixes and changes from simdhash-2 into simdhash PR
Browse files Browse the repository at this point in the history
  • Loading branch information
kg committed Apr 10, 2024
1 parent f6833df commit ae7dcf1
Show file tree
Hide file tree
Showing 13 changed files with 725 additions and 293 deletions.
2 changes: 2 additions & 0 deletions src/native/containers/containers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ list(APPEND SHARED_CONTAINER_SOURCES
# dn-simdhash.c
# dn-simdhash-string-ptr.c
# dn-simdhash-u32-ptr.c
# dn-simdhash-ptr-ptr.c
)

list(APPEND SHARED_CONTAINER_HEADERS
Expand All @@ -34,4 +35,5 @@ list(APPEND SHARED_CONTAINER_HEADERS
dn-simdhash-specializations.h
dn-simdhash-arch.h
dn-simdhash-string-ptr.h
dn-simdhash-utils.h
)
111 changes: 111 additions & 0 deletions src/native/containers/dn-simdhash-ght-compatible.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <config.h>
#include "dn-simdhash.h"

#include "dn-simdhash-utils.h"

typedef unsigned int guint;
typedef int32_t gboolean;
typedef void * gpointer;
typedef const void * gconstpointer;

typedef void (*GDestroyNotify) (gpointer data);
typedef guint (*GHashFunc) (gconstpointer key);
typedef gboolean (*GEqualFunc) (gconstpointer a, gconstpointer b);

typedef struct dn_simdhash_ght_data {
GHashFunc hash_func;
GEqualFunc key_equal_func;
GDestroyNotify key_destroy_func;
GDestroyNotify value_destroy_func;
} dn_simdhash_ght_data;

static inline uint32_t
dn_simdhash_ght_hash (dn_simdhash_ght_data data, gconstpointer key)
{
GHashFunc hash_func = data.hash_func;
if (hash_func)
return (uint32_t)hash_func(key);
else
// FIXME: Seed
return MurmurHash3_32_ptr(key, 0);
}

static inline gboolean
dn_simdhash_ght_equals (dn_simdhash_ght_data data, gconstpointer lhs, gconstpointer rhs)
{
GEqualFunc equal_func = data.key_equal_func;
if (equal_func)
return equal_func(lhs, rhs);
else
return lhs == rhs;
}

static inline void
dn_simdhash_ght_removed (dn_simdhash_ght_data data, gconstpointer key, gpointer value)
{
GDestroyNotify key_destroy_func = data.key_destroy_func,
value_destroy_func = data.value_destroy_func;
if (key_destroy_func)
key_destroy_func((gpointer)key);
if (value_destroy_func)
value_destroy_func((gpointer)value);
}

static inline void
dn_simdhash_ght_replaced (dn_simdhash_ght_data data, gconstpointer key, gpointer old_value, gpointer new_value)
{
if (old_value == new_value)
return;

GDestroyNotify value_destroy_func = data.value_destroy_func;
if (value_destroy_func)
value_destroy_func((gpointer)old_value);
}

#define DN_SIMDHASH_T dn_simdhash_ght
#define DN_SIMDHASH_KEY_T gconstpointer
#define DN_SIMDHASH_VALUE_T gpointer
#define DN_SIMDHASH_INSTANCE_DATA_T dn_simdhash_ght_data
#define DN_SIMDHASH_KEY_HASHER dn_simdhash_ght_hash
#define DN_SIMDHASH_KEY_EQUALS dn_simdhash_ght_equals
#define DN_SIMDHASH_ON_REMOVE dn_simdhash_ght_removed
#define DN_SIMDHASH_ON_REPLACE dn_simdhash_ght_replaced
#if SIZEOF_VOID_P == 8
#define DN_SIMDHASH_BUCKET_CAPACITY 11
#else
#define DN_SIMDHASH_BUCKET_CAPACITY 12
#endif
#define DN_SIMDHASH_NO_DEFAULT_NEW 1

#include "dn-simdhash-specialization.h"
#include "dn-simdhash-ght-compatible.h"

dn_simdhash_ght_t *
dn_simdhash_ght_new (
GHashFunc hash_func, GEqualFunc key_equal_func,
uint32_t capacity, dn_allocator_t *allocator
)
{
dn_simdhash_ght_t *hash = dn_simdhash_new_internal(&DN_SIMDHASH_T_META, DN_SIMDHASH_T_VTABLE, capacity, allocator);
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).hash_func = hash_func;
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).key_equal_func = key_equal_func;
return hash;
}

dn_simdhash_ght_t *
dn_simdhash_ght_new_full (
GHashFunc hash_func, GEqualFunc key_equal_func,
GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func,
uint32_t capacity, dn_allocator_t *allocator
)
{
dn_simdhash_ght_t *hash = dn_simdhash_new_internal(&DN_SIMDHASH_T_META, DN_SIMDHASH_T_VTABLE, capacity, allocator);
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).hash_func = hash_func;
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).key_equal_func = key_equal_func;
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).key_destroy_func = key_destroy_func;
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).value_destroy_func = value_destroy_func;
return hash;
}
12 changes: 12 additions & 0 deletions src/native/containers/dn-simdhash-ght-compatible.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
dn_simdhash_ght_t *
dn_simdhash_ght_new (
GHashFunc hash_func, GEqualFunc key_equal_func,
uint32_t capacity, dn_allocator_t *allocator
);

dn_simdhash_ght_t *
dn_simdhash_ght_new_full (
GHashFunc hash_func, GEqualFunc key_equal_func,
GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func,
uint32_t capacity, dn_allocator_t *allocator
);
20 changes: 20 additions & 0 deletions src/native/containers/dn-simdhash-ptr-ptr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <config.h>
#include "dn-simdhash.h"

#include "dn-simdhash-utils.h"

#define DN_SIMDHASH_T dn_simdhash_ptr_ptr
#define DN_SIMDHASH_KEY_T void *
#define DN_SIMDHASH_VALUE_T void *
#define DN_SIMDHASH_KEY_HASHER(hash, key) (MurmurHash3_32_ptr(key, 0))
#define DN_SIMDHASH_KEY_EQUALS(hash, lhs, rhs) (lhs == rhs)
#if SIZEOF_VOID_P == 8
#define DN_SIMDHASH_BUCKET_CAPACITY 11
#else
#define DN_SIMDHASH_BUCKET_CAPACITY 12
#endif

#include "dn-simdhash-specialization.h"
11 changes: 11 additions & 0 deletions src/native/containers/dn-simdhash-specialization-declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,21 @@
#define DN_SIMDHASH_TRY_GET_VALUE_WITH_HASH DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_try_get_value_with_hash,DN_SIMDHASH_ACCESSOR_SUFFIX)
#define DN_SIMDHASH_TRY_REMOVE DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_try_remove,DN_SIMDHASH_ACCESSOR_SUFFIX)
#define DN_SIMDHASH_TRY_REMOVE_WITH_HASH DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_try_remove_with_hash,DN_SIMDHASH_ACCESSOR_SUFFIX)
#define DN_SIMDHASH_TRY_REPLACE DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_try_replace,DN_SIMDHASH_ACCESSOR_SUFFIX)
#define DN_SIMDHASH_TRY_REPLACE_WITH_HASH DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_try_replace_with_hash,DN_SIMDHASH_ACCESSOR_SUFFIX)
#define DN_SIMDHASH_FOREACH DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_foreach,DN_SIMDHASH_ACCESSOR_SUFFIX)
#define DN_SIMDHASH_FOREACH_FUNC DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_foreach_func,DN_SIMDHASH_ACCESSOR_SUFFIX)
#define DN_SIMDHASH_DESTROY_ALL DN_SIMDHASH_GLUE(DN_SIMDHASH_T,_destroy_all)

typedef void (*DN_SIMDHASH_FOREACH_FUNC) (DN_SIMDHASH_KEY_T key, DN_SIMDHASH_VALUE_T value, void *user_data);

// Declare a specific alias so intellisense gives more helpful info
typedef dn_simdhash_t DN_SIMDHASH_T_NAME;

#ifndef DN_SIMDHASH_NO_DEFAULT_NEW
DN_SIMDHASH_T_PTR
DN_SIMDHASH_NEW (uint32_t capacity, dn_allocator_t *allocator);
#endif

uint8_t
DN_SIMDHASH_TRY_ADD (DN_SIMDHASH_T_PTR hash, DN_SIMDHASH_KEY_T key, DN_SIMDHASH_VALUE_T value);
Expand All @@ -61,5 +66,11 @@ DN_SIMDHASH_TRY_REMOVE (DN_SIMDHASH_T_PTR hash, DN_SIMDHASH_KEY_T key);
uint8_t
DN_SIMDHASH_TRY_REMOVE_WITH_HASH (DN_SIMDHASH_T_PTR hash, DN_SIMDHASH_KEY_T key, uint32_t key_hash);

uint8_t
DN_SIMDHASH_TRY_REPLACE (DN_SIMDHASH_T_PTR hash, DN_SIMDHASH_KEY_T key, DN_SIMDHASH_VALUE_T new_value);

uint8_t
DN_SIMDHASH_TRY_REPLACE_WITH_HASH (DN_SIMDHASH_T_PTR hash, DN_SIMDHASH_KEY_T key, uint32_t key_hash, DN_SIMDHASH_VALUE_T new_value);

void
DN_SIMDHASH_FOREACH (DN_SIMDHASH_T_PTR hash, DN_SIMDHASH_FOREACH_FUNC func, void *user_data);
Loading

0 comments on commit ae7dcf1

Please sign in to comment.