summaryrefslogtreecommitdiff
path: root/debian/patches/translators-list0003-libihash-add-general-purpose-hash-functions.patch
blob: 2d0957776aea642dea1c38486a6fb6fa92fd7a2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
From 217f0c33da62cb96e12873a8f80a673caefac4c3 Mon Sep 17 00:00:00 2001
From: Justus Winter <4winter@informatik.uni-hamburg.de>
Date: Tue, 24 Nov 2015 01:07:32 +0100
Subject: [PATCH hurd 3/5] libihash: add general purpose hash functions

* libihash/Makefile (SRCS): Add new file.
* libihash/fasthash.c: New file.
* libihash/ihash.h (hurd_ihash_fasthash{32,64}): New prototypes.
---
 libihash/Makefile   |  2 +-
 libihash/fasthash.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 libihash/ihash.h    | 14 ++++++++++
 3 files changed, 91 insertions(+), 1 deletion(-)
 create mode 100644 libihash/fasthash.c

diff --git a/libihash/Makefile b/libihash/Makefile
index 09bb136..eb5d87e 100644
--- a/libihash/Makefile
+++ b/libihash/Makefile
@@ -20,7 +20,7 @@ dir := libihash
 makemode := library
 
 libname := libihash
-SRCS = ihash.c
+SRCS = ihash.c fasthash.c
 installhdrs = ihash.h
 
 OBJS = $(SRCS:.c=.o)
diff --git a/libihash/fasthash.c b/libihash/fasthash.c
new file mode 100644
index 0000000..9295cf1
--- /dev/null
+++ b/libihash/fasthash.c
@@ -0,0 +1,76 @@
+/* The MIT License
+
+   Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com)
+
+   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.
+*/
+
+#include <stdint.h>
+#include <stdio.h>
+
+// Compression function for Merkle-Damgard construction.
+// This function is generated using the framework provided.
+#define mix(h) ({					\
+			(h) ^= (h) >> 23;		\
+			(h) *= 0x2127599bf4325c37ULL;	\
+			(h) ^= (h) >> 47; })
+
+uint64_t hurd_ihash_fasthash64(const void *buf, size_t len, uint64_t seed)
+{
+	const uint64_t    m = 0x880355f21e6d1965ULL;
+	const uint64_t *pos = (const uint64_t *)buf;
+	const uint64_t *end = pos + (len / 8);
+	const unsigned char *pos2;
+	uint64_t h = seed ^ (len * m);
+	uint64_t v;
+
+	while (pos != end) {
+		v  = *pos++;
+		h ^= mix(v);
+		h *= m;
+	}
+
+	pos2 = (const unsigned char*)pos;
+	v = 0;
+
+	switch (len & 7) {
+	case 7: v ^= (uint64_t)pos2[6] << 48;
+	case 6: v ^= (uint64_t)pos2[5] << 40;
+	case 5: v ^= (uint64_t)pos2[4] << 32;
+	case 4: v ^= (uint64_t)pos2[3] << 24;
+	case 3: v ^= (uint64_t)pos2[2] << 16;
+	case 2: v ^= (uint64_t)pos2[1] << 8;
+	case 1: v ^= (uint64_t)pos2[0];
+		h ^= mix(v);
+		h *= m;
+	}
+
+	return mix(h);
+}
+
+uint32_t hurd_ihash_fasthash32(const void *buf, size_t len, uint32_t seed)
+{
+	// the following trick converts the 64-bit hashcode to Fermat
+	// residue, which shall retain information from both the higher
+	// and lower parts of hashcode.
+        uint64_t h = hurd_ihash_fasthash64(buf, len, seed);
+	return h - (h >> 32);
+}
diff --git a/libihash/ihash.h b/libihash/ihash.h
index 986291b..50676a6 100644
--- a/libihash/ihash.h
+++ b/libihash/ihash.h
@@ -136,6 +136,13 @@ typedef struct hurd_ihash *hurd_ihash_t;
     .max_load = HURD_IHASH_MAX_LOAD_DEFAULT,				\
     .locp_offset = (locp_offs)}
 
+#define HURD_IHASH_INITIALIZER_GKI(locp_offs, cleanup, hash, compare)   \
+  { .nr_items = 0, .size = 0, .cleanup = (cleanup),			\
+    .max_load = HURD_IHASH_MAX_LOAD_DEFAULT,				\
+    .locp_offset = (locp_offs),						\
+    .fct_hash = (hash),							\
+    .fct_cmp = (compare)}						\
+
 /* Initialize the hash table at address HT.  If LOCP_OFFSET is not
    HURD_IHASH_NO_LOCP, then this is an offset (in bytes) from the
    address of a hash value where a location pointer can be found.  The
@@ -339,5 +346,12 @@ int hurd_ihash_remove (hurd_ihash_t ht, hurd_ihash_key_t key);
    was provided to hurd_ihash_add().  This call is faster than
    hurd_ihash_remove().  */
 void hurd_ihash_locp_remove (hurd_ihash_t ht, hurd_ihash_locp_t locp);
+
+/* General purpose hash functions.  */
+
+/* Zilong Tans fast-hash.  Based on 'Marsaglia, George. "Xorshift
+   rngs."  Journal of Statistical Software 8.14 (2003): 1-6.'  */
+uint32_t hurd_ihash_fasthash32 (const void *buf, size_t len, uint32_t seed);
+uint64_t hurd_ihash_fasthash64 (const void *buf, size_t len, uint64_t seed);
 
 #endif	/* _HURD_IHASH_H */
-- 
2.1.4