Skip to content

Commit f17bd49

Browse files
committed
Migrate all simdhash fixes and changes from simdhash-2 into simdhash PR #1
1 parent 338b23c commit f17bd49

13 files changed

+725
-293
lines changed

src/native/containers/containers.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ list(APPEND SHARED_CONTAINER_SOURCES
1212
# dn-simdhash.c
1313
# dn-simdhash-string-ptr.c
1414
# dn-simdhash-u32-ptr.c
15+
# dn-simdhash-ptr-ptr.c
1516
)
1617

1718
list(APPEND SHARED_CONTAINER_HEADERS
@@ -34,4 +35,5 @@ list(APPEND SHARED_CONTAINER_HEADERS
3435
dn-simdhash-specializations.h
3536
dn-simdhash-arch.h
3637
dn-simdhash-string-ptr.h
38+
dn-simdhash-utils.h
3739
)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#include <config.h>
5+
#include "dn-simdhash.h"
6+
7+
#include "dn-simdhash-utils.h"
8+
9+
typedef unsigned int guint;
10+
typedef int32_t gboolean;
11+
typedef void * gpointer;
12+
typedef const void * gconstpointer;
13+
14+
typedef void (*GDestroyNotify) (gpointer data);
15+
typedef guint (*GHashFunc) (gconstpointer key);
16+
typedef gboolean (*GEqualFunc) (gconstpointer a, gconstpointer b);
17+
18+
typedef struct dn_simdhash_ght_data {
19+
GHashFunc hash_func;
20+
GEqualFunc key_equal_func;
21+
GDestroyNotify key_destroy_func;
22+
GDestroyNotify value_destroy_func;
23+
} dn_simdhash_ght_data;
24+
25+
static inline uint32_t
26+
dn_simdhash_ght_hash (dn_simdhash_ght_data data, gconstpointer key)
27+
{
28+
GHashFunc hash_func = data.hash_func;
29+
if (hash_func)
30+
return (uint32_t)hash_func(key);
31+
else
32+
// FIXME: Seed
33+
return MurmurHash3_32_ptr(key, 0);
34+
}
35+
36+
static inline gboolean
37+
dn_simdhash_ght_equals (dn_simdhash_ght_data data, gconstpointer lhs, gconstpointer rhs)
38+
{
39+
GEqualFunc equal_func = data.key_equal_func;
40+
if (equal_func)
41+
return equal_func(lhs, rhs);
42+
else
43+
return lhs == rhs;
44+
}
45+
46+
static inline void
47+
dn_simdhash_ght_removed (dn_simdhash_ght_data data, gconstpointer key, gpointer value)
48+
{
49+
GDestroyNotify key_destroy_func = data.key_destroy_func,
50+
value_destroy_func = data.value_destroy_func;
51+
if (key_destroy_func)
52+
key_destroy_func((gpointer)key);
53+
if (value_destroy_func)
54+
value_destroy_func((gpointer)value);
55+
}
56+
57+
static inline void
58+
dn_simdhash_ght_replaced (dn_simdhash_ght_data data, gconstpointer key, gpointer old_value, gpointer new_value)
59+
{
60+
if (old_value == new_value)
61+
return;
62+
63+
GDestroyNotify value_destroy_func = data.value_destroy_func;
64+
if (value_destroy_func)
65+
value_destroy_func((gpointer)old_value);
66+
}
67+
68+
#define DN_SIMDHASH_T dn_simdhash_ght
69+
#define DN_SIMDHASH_KEY_T gconstpointer
70+
#define DN_SIMDHASH_VALUE_T gpointer
71+
#define DN_SIMDHASH_INSTANCE_DATA_T dn_simdhash_ght_data
72+
#define DN_SIMDHASH_KEY_HASHER dn_simdhash_ght_hash
73+
#define DN_SIMDHASH_KEY_EQUALS dn_simdhash_ght_equals
74+
#define DN_SIMDHASH_ON_REMOVE dn_simdhash_ght_removed
75+
#define DN_SIMDHASH_ON_REPLACE dn_simdhash_ght_replaced
76+
#if SIZEOF_VOID_P == 8
77+
#define DN_SIMDHASH_BUCKET_CAPACITY 11
78+
#else
79+
#define DN_SIMDHASH_BUCKET_CAPACITY 12
80+
#endif
81+
#define DN_SIMDHASH_NO_DEFAULT_NEW 1
82+
83+
#include "dn-simdhash-specialization.h"
84+
#include "dn-simdhash-ght-compatible.h"
85+
86+
dn_simdhash_ght_t *
87+
dn_simdhash_ght_new (
88+
GHashFunc hash_func, GEqualFunc key_equal_func,
89+
uint32_t capacity, dn_allocator_t *allocator
90+
)
91+
{
92+
dn_simdhash_ght_t *hash = dn_simdhash_new_internal(&DN_SIMDHASH_T_META, DN_SIMDHASH_T_VTABLE, capacity, allocator);
93+
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).hash_func = hash_func;
94+
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).key_equal_func = key_equal_func;
95+
return hash;
96+
}
97+
98+
dn_simdhash_ght_t *
99+
dn_simdhash_ght_new_full (
100+
GHashFunc hash_func, GEqualFunc key_equal_func,
101+
GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func,
102+
uint32_t capacity, dn_allocator_t *allocator
103+
)
104+
{
105+
dn_simdhash_ght_t *hash = dn_simdhash_new_internal(&DN_SIMDHASH_T_META, DN_SIMDHASH_T_VTABLE, capacity, allocator);
106+
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).hash_func = hash_func;
107+
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).key_equal_func = key_equal_func;
108+
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).key_destroy_func = key_destroy_func;
109+
dn_simdhash_instance_data(dn_simdhash_ght_data, hash).value_destroy_func = value_destroy_func;
110+
return hash;
111+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
dn_simdhash_ght_t *
2+
dn_simdhash_ght_new (
3+
GHashFunc hash_func, GEqualFunc key_equal_func,
4+
uint32_t capacity, dn_allocator_t *allocator
5+
);
6+
7+
dn_simdhash_ght_t *
8+
dn_simdhash_ght_new_full (
9+
GHashFunc hash_func, GEqualFunc key_equal_func,
10+
GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func,
11+
uint32_t capacity, dn_allocator_t *allocator
12+
);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#include <config.h>
5+
#include "dn-simdhash.h"
6+
7+
#include "dn-simdhash-utils.h"
8+
9+
#define DN_SIMDHASH_T dn_simdhash_ptr_ptr
10+
#define DN_SIMDHASH_KEY_T void *
11+
#define DN_SIMDHASH_VALUE_T void *
12+
#define DN_SIMDHASH_KEY_HASHER(hash, key) (MurmurHash3_32_ptr(key, 0))
13+
#define DN_SIMDHASH_KEY_EQUALS(hash, lhs, rhs) (lhs == rhs)
14+
#if SIZEOF_VOID_P == 8
15+
#define DN_SIMDHASH_BUCKET_CAPACITY 11
16+
#else
17+
#define DN_SIMDHASH_BUCKET_CAPACITY 12
18+
#endif
19+
20+
#include "dn-simdhash-specialization.h"

src/native/containers/dn-simdhash-specialization-declarations.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,21 @@
3232
#define DN_SIMDHASH_TRY_GET_VALUE_WITH_HASH DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_try_get_value_with_hash,DN_SIMDHASH_ACCESSOR_SUFFIX)
3333
#define DN_SIMDHASH_TRY_REMOVE DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_try_remove,DN_SIMDHASH_ACCESSOR_SUFFIX)
3434
#define DN_SIMDHASH_TRY_REMOVE_WITH_HASH DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_try_remove_with_hash,DN_SIMDHASH_ACCESSOR_SUFFIX)
35+
#define DN_SIMDHASH_TRY_REPLACE DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_try_replace,DN_SIMDHASH_ACCESSOR_SUFFIX)
36+
#define DN_SIMDHASH_TRY_REPLACE_WITH_HASH DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_try_replace_with_hash,DN_SIMDHASH_ACCESSOR_SUFFIX)
3537
#define DN_SIMDHASH_FOREACH DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_foreach,DN_SIMDHASH_ACCESSOR_SUFFIX)
3638
#define DN_SIMDHASH_FOREACH_FUNC DN_SIMDHASH_GLUE_3(DN_SIMDHASH_T,_foreach_func,DN_SIMDHASH_ACCESSOR_SUFFIX)
39+
#define DN_SIMDHASH_DESTROY_ALL DN_SIMDHASH_GLUE(DN_SIMDHASH_T,_destroy_all)
3740

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

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

46+
#ifndef DN_SIMDHASH_NO_DEFAULT_NEW
4347
DN_SIMDHASH_T_PTR
4448
DN_SIMDHASH_NEW (uint32_t capacity, dn_allocator_t *allocator);
49+
#endif
4550

4651
uint8_t
4752
DN_SIMDHASH_TRY_ADD (DN_SIMDHASH_T_PTR hash, DN_SIMDHASH_KEY_T key, DN_SIMDHASH_VALUE_T value);
@@ -61,5 +66,11 @@ DN_SIMDHASH_TRY_REMOVE (DN_SIMDHASH_T_PTR hash, DN_SIMDHASH_KEY_T key);
6166
uint8_t
6267
DN_SIMDHASH_TRY_REMOVE_WITH_HASH (DN_SIMDHASH_T_PTR hash, DN_SIMDHASH_KEY_T key, uint32_t key_hash);
6368

69+
uint8_t
70+
DN_SIMDHASH_TRY_REPLACE (DN_SIMDHASH_T_PTR hash, DN_SIMDHASH_KEY_T key, DN_SIMDHASH_VALUE_T new_value);
71+
72+
uint8_t
73+
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);
74+
6475
void
6576
DN_SIMDHASH_FOREACH (DN_SIMDHASH_T_PTR hash, DN_SIMDHASH_FOREACH_FUNC func, void *user_data);

0 commit comments

Comments
 (0)