blob: 977d058fd550aa891029c6f3753b4ec32502a6ce [file] [log] [blame]
Jason Evanse6b7aa42013-12-16 22:55:41 -08001/*
2 * This file is based on code that is part of SMHasher
3 * (https://code.google.com/p/smhasher/), and is subject to the MIT license
4 * (http://www.opensource.org/licenses/mit-license.php). Both email addresses
5 * associated with the source code's revision history belong to Austin Appleby,
6 * and the revision history ranges from 2010 to 2012. Therefore the copyright
7 * and license are here taken to be:
8 *
9 * Copyright (c) 2010-2012 Austin Appleby
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 */
29
30#include "test/jemalloc_test.h"
31
32typedef enum {
33 hash_variant_x86_32,
34 hash_variant_x86_128,
35 hash_variant_x64_128
36} hash_variant_t;
37
Jason Evans9e1810c2016-02-24 12:42:23 -080038static int
Jason Evansc4c25922017-01-15 16:56:30 -080039hash_variant_bits(hash_variant_t variant) {
Jason Evanse6b7aa42013-12-16 22:55:41 -080040 switch (variant) {
41 case hash_variant_x86_32: return (32);
42 case hash_variant_x86_128: return (128);
43 case hash_variant_x64_128: return (128);
44 default: not_reached();
45 }
46}
47
48static const char *
Jason Evansc4c25922017-01-15 16:56:30 -080049hash_variant_string(hash_variant_t variant) {
Jason Evanse6b7aa42013-12-16 22:55:41 -080050 switch (variant) {
51 case hash_variant_x86_32: return ("hash_x86_32");
52 case hash_variant_x86_128: return ("hash_x86_128");
Jason Evanseca367b2013-12-17 09:14:39 -080053 case hash_variant_x64_128: return ("hash_x64_128");
Jason Evanse6b7aa42013-12-16 22:55:41 -080054 default: not_reached();
55 }
56}
57
Jason Evansa0aaad12016-02-20 10:23:48 -080058#define KEY_SIZE 256
Jason Evanse6b7aa42013-12-16 22:55:41 -080059static void
Jason Evansc4c25922017-01-15 16:56:30 -080060hash_variant_verify_key(hash_variant_t variant, uint8_t *key) {
Jason Evans9e1810c2016-02-24 12:42:23 -080061 const int hashbytes = hash_variant_bits(variant) / 8;
Dmitri Smirnovc3ab9042016-02-29 14:30:19 -080062 const int hashes_size = hashbytes * 256;
63 VARIABLE_ARRAY(uint8_t, hashes, hashes_size);
Mike Hommeyf41f1432014-05-21 17:24:08 +090064 VARIABLE_ARRAY(uint8_t, final, hashbytes);
Jason Evanse6b7aa42013-12-16 22:55:41 -080065 unsigned i;
66 uint32_t computed, expected;
67
Jason Evansa0aaad12016-02-20 10:23:48 -080068 memset(key, 0, KEY_SIZE);
Dmitri Smirnovc3ab9042016-02-29 14:30:19 -080069 memset(hashes, 0, hashes_size);
70 memset(final, 0, hashbytes);
Jason Evanse6b7aa42013-12-16 22:55:41 -080071
72 /*
73 * Hash keys of the form {0}, {0,1}, {0,1,2}, ..., {0,1,...,255} as the
74 * seed.
75 */
76 for (i = 0; i < 256; i++) {
77 key[i] = (uint8_t)i;
78 switch (variant) {
79 case hash_variant_x86_32: {
80 uint32_t out;
81 out = hash_x86_32(key, i, 256-i);
82 memcpy(&hashes[i*hashbytes], &out, hashbytes);
83 break;
84 } case hash_variant_x86_128: {
85 uint64_t out[2];
86 hash_x86_128(key, i, 256-i, out);
87 memcpy(&hashes[i*hashbytes], out, hashbytes);
88 break;
Jason Evans5aeeda62014-01-02 13:38:23 -080089 } case hash_variant_x64_128: {
Jason Evanse6b7aa42013-12-16 22:55:41 -080090 uint64_t out[2];
91 hash_x64_128(key, i, 256-i, out);
92 memcpy(&hashes[i*hashbytes], out, hashbytes);
93 break;
Jason Evans5aeeda62014-01-02 13:38:23 -080094 } default: not_reached();
Jason Evanse6b7aa42013-12-16 22:55:41 -080095 }
96 }
97
98 /* Hash the result array. */
99 switch (variant) {
100 case hash_variant_x86_32: {
Dmitri Smirnovc3ab9042016-02-29 14:30:19 -0800101 uint32_t out = hash_x86_32(hashes, hashes_size, 0);
Jason Evanse6b7aa42013-12-16 22:55:41 -0800102 memcpy(final, &out, sizeof(out));
103 break;
104 } case hash_variant_x86_128: {
105 uint64_t out[2];
Dmitri Smirnovc3ab9042016-02-29 14:30:19 -0800106 hash_x86_128(hashes, hashes_size, 0, out);
Jason Evanse6b7aa42013-12-16 22:55:41 -0800107 memcpy(final, out, sizeof(out));
108 break;
109 } case hash_variant_x64_128: {
110 uint64_t out[2];
Dmitri Smirnovc3ab9042016-02-29 14:30:19 -0800111 hash_x64_128(hashes, hashes_size, 0, out);
Jason Evanse6b7aa42013-12-16 22:55:41 -0800112 memcpy(final, out, sizeof(out));
113 break;
114 } default: not_reached();
115 }
116
117 computed = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) |
118 (final[3] << 24);
119
120 switch (variant) {
Jason Evansdf3f2702014-03-30 16:27:08 -0700121#ifdef JEMALLOC_BIG_ENDIAN
122 case hash_variant_x86_32: expected = 0x6213303eU; break;
123 case hash_variant_x86_128: expected = 0x266820caU; break;
124 case hash_variant_x64_128: expected = 0xcc622b6fU; break;
125#else
Jason Evanse6b7aa42013-12-16 22:55:41 -0800126 case hash_variant_x86_32: expected = 0xb0f57ee3U; break;
127 case hash_variant_x86_128: expected = 0xb3ece62aU; break;
128 case hash_variant_x64_128: expected = 0x6384ba69U; break;
Jason Evansdf3f2702014-03-30 16:27:08 -0700129#endif
Jason Evanse6b7aa42013-12-16 22:55:41 -0800130 default: not_reached();
131 }
132
133 assert_u32_eq(computed, expected,
134 "Hash mismatch for %s(): expected %#x but got %#x",
135 hash_variant_string(variant), expected, computed);
136}
137
Jason Evansa0aaad12016-02-20 10:23:48 -0800138static void
Jason Evansc4c25922017-01-15 16:56:30 -0800139hash_variant_verify(hash_variant_t variant) {
Jason Evansa0aaad12016-02-20 10:23:48 -0800140#define MAX_ALIGN 16
141 uint8_t key[KEY_SIZE + (MAX_ALIGN - 1)];
142 unsigned i;
143
Jason Evansc4c25922017-01-15 16:56:30 -0800144 for (i = 0; i < MAX_ALIGN; i++) {
Jason Evansa0aaad12016-02-20 10:23:48 -0800145 hash_variant_verify_key(variant, &key[i]);
Jason Evansc4c25922017-01-15 16:56:30 -0800146 }
Jason Evansa0aaad12016-02-20 10:23:48 -0800147#undef MAX_ALIGN
148}
149#undef KEY_SIZE
150
Jason Evansc4c25922017-01-15 16:56:30 -0800151TEST_BEGIN(test_hash_x86_32) {
Jason Evanse6b7aa42013-12-16 22:55:41 -0800152 hash_variant_verify(hash_variant_x86_32);
153}
154TEST_END
155
Jason Evansc4c25922017-01-15 16:56:30 -0800156TEST_BEGIN(test_hash_x86_128) {
Jason Evanse6b7aa42013-12-16 22:55:41 -0800157 hash_variant_verify(hash_variant_x86_128);
158}
159TEST_END
160
Jason Evansc4c25922017-01-15 16:56:30 -0800161TEST_BEGIN(test_hash_x64_128) {
Jason Evanse6b7aa42013-12-16 22:55:41 -0800162 hash_variant_verify(hash_variant_x64_128);
163}
164TEST_END
165
166int
Jason Evansc4c25922017-01-15 16:56:30 -0800167main(void) {
Jason Evanse6b7aa42013-12-16 22:55:41 -0800168 return (test(
169 test_hash_x86_32,
170 test_hash_x86_128,
171 test_hash_x64_128));
172}