1From 426384ce34cf410d892eeeeeb7f6046d52bff8e7 Mon Sep 17 00:00:00 2001
2From: Tom Kaitchuck <Tom.Kaitchuck@gmail.com>
3Date: Sat, 11 Jul 2020 17:15:56 -0700
4Subject: [PATCH] Add support for ahash
5
6---
7 CMakeLists.txt |  1 +
8 Hashes.h       |  5 +++++
9 ahash.h        | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
10 main.cpp       |  2 +-
11 4 files changed, 55 insertions(+), 1 deletion(-)
12 create mode 100644 ahash.h
13
14diff --git a/CMakeLists.txt b/CMakeLists.txt
15index 6ebab1a..9d79e98 100644
16--- a/CMakeLists.txt
17+++ b/CMakeLists.txt
18@@ -470,10 +470,11 @@ add_executable(
19 target_link_libraries(
20   SMHasher
21   SMHasherSupport
22   ${HIGHWAY_LIB}
23   ${BLAKE3_LIB}
24+  libahash_c.a
25   ${CMAKE_THREAD_LIBS_INIT}
26   )
27
28 #add_executable(
29 #  bittest
30diff --git a/Hashes.h b/Hashes.h
31index 4e111c1..fcd3e38 100644
32--- a/Hashes.h
33+++ b/Hashes.h
34@@ -19,10 +19,11 @@
35 #if defined(__SSE4_2__) && defined(__x86_64__)
36 #include "metrohash/metrohash64crc.h"
37 #include "metrohash/metrohash128crc.h"
38 #endif
39
40+#include "ahash.h"
41 #include "fasthash.h"
42 #include "jody_hash32.h"
43 #include "jody_hash64.h"
44
45 // objsize: 0-0x113 = 276
46@@ -356,10 +357,14 @@ inline void fasthash32_test ( const void * key, int len, uint32_t seed, void * o
47 }
48 #ifdef HAVE_INT64
49 inline void fasthash64_test ( const void * key, int len, uint32_t seed, void * out ) {
50   *(uint64_t*)out = fasthash64(key, (size_t) len, (uint64_t)seed);
51 }
52+inline void ahash64_test ( const void * key, int len, uint32_t seed, void * out ) {
53+  *(uint64_t*)out = ahash64(key, (size_t) len, (uint64_t)seed);
54+}
55+
56 #endif
57
58 // objsize 0-778: 1912
59 void mum_hash_test(const void * key, int len, uint32_t seed, void * out);
60
61diff --git a/ahash.h b/ahash.h
62new file mode 100644
63index 0000000..6c59caf
64--- /dev/null
65+++ b/ahash.h
66@@ -0,0 +1,48 @@
67+/* The MIT License
68+
69+   Copyright (C) 2012 Zilong Tan (eric.zltan@gmail.com)
70+
71+   Permission is hereby granted, free of charge, to any person
72+   obtaining a copy of this software and associated documentation
73+   files (the "Software"), to deal in the Software without
74+   restriction, including without limitation the rights to use, copy,
75+   modify, merge, publish, distribute, sublicense, and/or sell copies
76+   of the Software, and to permit persons to whom the Software is
77+   furnished to do so, subject to the following conditions:
78+
79+   The above copyright notice and this permission notice shall be
80+   included in all copies or substantial portions of the Software.
81+
82+   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
83+   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
84+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
85+   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
86+   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
87+   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
88+   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
89+   SOFTWARE.
90+*/
91+
92+#ifndef _AHASH_H
93+#define _AHASH_H
94+
95+#include <stdint.h>
96+#include <stdio.h>
97+
98+#ifdef __cplusplus
99+extern "C" {
100+#endif
101+
102+/**
103+ * Ahash - 64-bit implementation of aHash
104+ * @buf:  data buffer
105+ * @len:  data size
106+ * @seed: the seed
107+ */
108+       uint64_t ahash64(const void *buf, size_t len, uint64_t seed);
109+
110+#ifdef __cplusplus
111+}
112+#endif
113+
114+#endif
115\ No newline at end of file
116diff --git a/main.cpp b/main.cpp
117index 04060f2..7489aaf 100644
118--- a/main.cpp
119+++ b/main.cpp
120@@ -263,11 +263,11 @@ HashInfo g_hashes[] =
121
122   { xxh3_test,            64, 0x39CD9E4A, "xxh3",        "xxHash v3, 64-bit", GOOD },
123   { xxh3low_test,         32, 0xFAE8467B, "xxh3low",     "xxHash v3, 64-bit, low 32-bits part", GOOD },
124   { xxh128_test,         128, 0xEB61B3A0, "xxh128",      "xxHash v3, 128-bit", GOOD },
125   { xxh128low_test,       64, 0x54D1CC70, "xxh128low",   "xxHash v3, 128-bit, low 64-bits part", GOOD },
126-
127+  { ahash64_test,         64, 0x00000000, "ahash64",  "ahash 64bit", GOOD }, //Expected value set to zero because aHash does not adhere to a fixed output.
128 #if __WORDSIZE >= 64
129 # define TIFU_VERIF       0x644236D4
130 #else
131   // broken on certain travis
132 # define TIFU_VERIF       0x0
133--
1342.25.1
135
136diff --git a/CMakeLists.txt b/CMakeLists.txt
137index e4658a7..efef724 100644
138--- a/CMakeLists.txt
139+++ b/CMakeLists.txt
140@@ -630,20 +630,21 @@ if(ipo_supported)
141   set_property(TARGET SMHasherSupport PROPERTY INTERPROCEDURAL_OPTIMIZATION
142                                                True)
143   set_property(TARGET SMHasher PROPERTY INTERPROCEDURAL_OPTIMIZATION True)
144   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLTO")
145   # set_source_files_properties(main.cpp PROPERTIES COMPILE_FLAGS "-DLTO")
146 else()
147   message(STATUS "IPO / LTO not supported: <${error}>")
148 endif()
149
150 target_link_libraries(SMHasher SMHasherSupport ${HIGHWAY_LIB} ${BLAKE3_LIB}
151+                      libahash_c.a
152                       ${CMAKE_THREAD_LIBS_INIT})
153
154 # add_executable( bittest bittest.cpp )
155 #
156 # target_link_libraries( bittest SMHasherSupport ${CMAKE_THREAD_LIBS_INIT} )
157
158 if(NOT (CMAKE_CROSSCOMPILING))
159   enable_testing()
160   add_test(VerifyAll SMHasher --test=VerifyAll)
161   add_test(Sanity SMHasher --test=Sanity)
162diff --git a/Hashes.h b/Hashes.h
163index f795403..036b49b 100644
164--- a/Hashes.h
165+++ b/Hashes.h
166@@ -14,20 +14,21 @@
167 #include "metrohash/metrohash64.h"
168 #include "metrohash/metrohash128.h"
169 #include "cmetrohash.h"
170 #include "opt_cmetrohash.h"
171
172 #if defined(__SSE4_2__) && defined(__x86_64__)
173 #include "metrohash/metrohash64crc.h"
174 #include "metrohash/metrohash128crc.h"
175 #endif
176
177+#include "ahash.h"
178 #include "fasthash.h"
179 #include "jody_hash32.h"
180 #include "jody_hash64.h"
181
182 // objsize: 0-0x113 = 276
183 #include "tifuhash.h"
184 // objsize: 5f0-85f = 623
185 #include "floppsyhash.h"
186
187 #include "vmac.h"
188@@ -353,20 +354,24 @@ inline void cmetrohash64_2_test ( const void * key, int len, uint32_t seed, void
189 }
190 #endif
191
192 inline void fasthash32_test ( const void * key, int len, uint32_t seed, void * out ) {
193   *(uint32_t*)out = fasthash32(key, (size_t) len, seed);
194 }
195 #ifdef HAVE_INT64
196 inline void fasthash64_test ( const void * key, int len, uint32_t seed, void * out ) {
197   *(uint64_t*)out = fasthash64(key, (size_t) len, (uint64_t)seed);
198 }
199+
200+inline void ahash64_test ( const void * key, int len, uint32_t seed, void * out ) {
201+  *(uint64_t*)out = ahash64(key, (size_t) len, (uint64_t)seed);
202+}
203 #endif
204
205 // objsize 0-778: 1912
206 void mum_hash_test(const void * key, int len, uint32_t seed, void * out);
207
208 inline void mum_low_test ( const void * key, int len, uint32_t seed, void * out ) {
209   uint64_t result;
210   mum_hash_test(key, len, seed, &result);
211   *(uint32_t*)out = (uint32_t)result;
212 }
213diff --git a/ahash.h b/ahash.h
214new file mode 100644
215index 0000000..2ed416d
216--- /dev/null
217+++ b/ahash.h
218@@ -0,0 +1,24 @@
219+
220+#ifndef _AHASH_H
221+#define _AHASH_H
222+
223+#include <stdint.h>
224+#include <stdio.h>
225+
226+#ifdef __cplusplus
227+extern "C" {
228+#endif
229+
230+/**
231+ * Ahash - 64-bit implementation of aHash
232+ * @buf:  data buffer
233+ * @len:  data size
234+ * @seed: the seed
235+ */
236+       uint64_t ahash64(const void *buf, size_t len, uint64_t seed);
237+
238+#ifdef __cplusplus
239+}
240+#endif
241+
242+#endif
243\ No newline at end of file
244diff --git a/main.cpp b/main.cpp
245index f742fbf..c221f7d 100644
246--- a/main.cpp
247+++ b/main.cpp
248@@ -434,20 +434,21 @@ HashInfo g_hashes[] =
249   { t1ha0_ia32aes_avx1_test,   64, 0xF07C4DA5, "t1ha0_aes_avx1",  "Fast Positive Hash (machine-specific, requires AES-NI & AVX)", GOOD },
250 #  endif /* __AVX__ */
251 #  if defined(__AVX2__)
252   { t1ha0_ia32aes_avx2_test,   64, 0x8B38C599, "t1ha0_aes_avx2",  "Fast Positive Hash (machine-specific, requires AES-NI & AVX2)", GOOD },
253 #  endif /* __AVX2__ */
254 #endif /* T1HA0_AESNI_AVAILABLE */
255   { xxh3_test,            64, 0x39CD9E4A, "xxh3",        "xxHash v3, 64-bit", GOOD },
256   { xxh3low_test,         32, 0xFAE8467B, "xxh3low",     "xxHash v3, 64-bit, low 32-bits part", GOOD },
257   { xxh128_test,         128, 0xEB61B3A0, "xxh128",      "xxHash v3, 128-bit", GOOD },
258   { xxh128low_test,       64, 0x54D1CC70, "xxh128low",   "xxHash v3, 128-bit, low 64-bits part", GOOD },
259+  { ahash64_test,         64, 0x00000000, "ahash64",  "ahash 64bit", GOOD }, //Expected value set to zero because aHash does not adhere to a fixed output.
260 #ifdef HAVE_BIT32
261   { wyhash32_test,        32, 0x09DE8066, "wyhash32",       "wyhash (32-bit)", GOOD },
262 #else
263   { wyhash32low,          32, 0x9241B8A3, "wyhash32low",    "wyhash lower 32bit", GOOD },
264 #endif
265 #ifdef HAVE_INT64
266   { wyhash_test,          64, 0x7C62138D, "wyhash",         "wyhash (64-bit)", GOOD },
267 #endif
268
269 };
270