xref: /netbsd/external/bsd/jemalloc/dist/test/unit/hash.c (revision d0774691)
1*d0774691Schristos /*
2*d0774691Schristos  * This file is based on code that is part of SMHasher
3*d0774691Schristos  * (https://code.google.com/p/smhasher/), and is subject to the MIT license
4*d0774691Schristos  * (http://www.opensource.org/licenses/mit-license.php).  Both email addresses
5*d0774691Schristos  * associated with the source code's revision history belong to Austin Appleby,
6*d0774691Schristos  * and the revision history ranges from 2010 to 2012.  Therefore the copyright
7*d0774691Schristos  * and license are here taken to be:
8*d0774691Schristos  *
9*d0774691Schristos  * Copyright (c) 2010-2012 Austin Appleby
10*d0774691Schristos  *
11*d0774691Schristos  * Permission is hereby granted, free of charge, to any person obtaining a copy
12*d0774691Schristos  * of this software and associated documentation files (the "Software"), to deal
13*d0774691Schristos  * in the Software without restriction, including without limitation the rights
14*d0774691Schristos  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15*d0774691Schristos  * copies of the Software, and to permit persons to whom the Software is
16*d0774691Schristos  * furnished to do so, subject to the following conditions:
17*d0774691Schristos  *
18*d0774691Schristos  * The above copyright notice and this permission notice shall be included in
19*d0774691Schristos  * all copies or substantial portions of the Software.
20*d0774691Schristos  *
21*d0774691Schristos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22*d0774691Schristos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23*d0774691Schristos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24*d0774691Schristos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25*d0774691Schristos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26*d0774691Schristos  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27*d0774691Schristos  * THE SOFTWARE.
28*d0774691Schristos  */
29*d0774691Schristos 
30*d0774691Schristos #include "test/jemalloc_test.h"
31*d0774691Schristos #include "jemalloc/internal/hash.h"
32*d0774691Schristos 
33*d0774691Schristos typedef enum {
34*d0774691Schristos 	hash_variant_x86_32,
35*d0774691Schristos 	hash_variant_x86_128,
36*d0774691Schristos 	hash_variant_x64_128
37*d0774691Schristos } hash_variant_t;
38*d0774691Schristos 
39*d0774691Schristos static int
hash_variant_bits(hash_variant_t variant)40*d0774691Schristos hash_variant_bits(hash_variant_t variant) {
41*d0774691Schristos 	switch (variant) {
42*d0774691Schristos 	case hash_variant_x86_32: return 32;
43*d0774691Schristos 	case hash_variant_x86_128: return 128;
44*d0774691Schristos 	case hash_variant_x64_128: return 128;
45*d0774691Schristos 	default: not_reached();
46*d0774691Schristos 	}
47*d0774691Schristos }
48*d0774691Schristos 
49*d0774691Schristos static const char *
hash_variant_string(hash_variant_t variant)50*d0774691Schristos hash_variant_string(hash_variant_t variant) {
51*d0774691Schristos 	switch (variant) {
52*d0774691Schristos 	case hash_variant_x86_32: return "hash_x86_32";
53*d0774691Schristos 	case hash_variant_x86_128: return "hash_x86_128";
54*d0774691Schristos 	case hash_variant_x64_128: return "hash_x64_128";
55*d0774691Schristos 	default: not_reached();
56*d0774691Schristos 	}
57*d0774691Schristos }
58*d0774691Schristos 
59*d0774691Schristos #define KEY_SIZE	256
60*d0774691Schristos static void
hash_variant_verify_key(hash_variant_t variant,uint8_t * key)61*d0774691Schristos hash_variant_verify_key(hash_variant_t variant, uint8_t *key) {
62*d0774691Schristos 	const int hashbytes = hash_variant_bits(variant) / 8;
63*d0774691Schristos 	const int hashes_size = hashbytes * 256;
64*d0774691Schristos 	VARIABLE_ARRAY(uint8_t, hashes, hashes_size);
65*d0774691Schristos 	VARIABLE_ARRAY(uint8_t, final, hashbytes);
66*d0774691Schristos 	unsigned i;
67*d0774691Schristos 	uint32_t computed, expected;
68*d0774691Schristos 
69*d0774691Schristos 	memset(key, 0, KEY_SIZE);
70*d0774691Schristos 	memset(hashes, 0, hashes_size);
71*d0774691Schristos 	memset(final, 0, hashbytes);
72*d0774691Schristos 
73*d0774691Schristos 	/*
74*d0774691Schristos 	 * Hash keys of the form {0}, {0,1}, {0,1,2}, ..., {0,1,...,255} as the
75*d0774691Schristos 	 * seed.
76*d0774691Schristos 	 */
77*d0774691Schristos 	for (i = 0; i < 256; i++) {
78*d0774691Schristos 		key[i] = (uint8_t)i;
79*d0774691Schristos 		switch (variant) {
80*d0774691Schristos 		case hash_variant_x86_32: {
81*d0774691Schristos 			uint32_t out;
82*d0774691Schristos 			out = hash_x86_32(key, i, 256-i);
83*d0774691Schristos 			memcpy(&hashes[i*hashbytes], &out, hashbytes);
84*d0774691Schristos 			break;
85*d0774691Schristos 		} case hash_variant_x86_128: {
86*d0774691Schristos 			uint64_t out[2];
87*d0774691Schristos 			hash_x86_128(key, i, 256-i, out);
88*d0774691Schristos 			memcpy(&hashes[i*hashbytes], out, hashbytes);
89*d0774691Schristos 			break;
90*d0774691Schristos 		} case hash_variant_x64_128: {
91*d0774691Schristos 			uint64_t out[2];
92*d0774691Schristos 			hash_x64_128(key, i, 256-i, out);
93*d0774691Schristos 			memcpy(&hashes[i*hashbytes], out, hashbytes);
94*d0774691Schristos 			break;
95*d0774691Schristos 		} default: not_reached();
96*d0774691Schristos 		}
97*d0774691Schristos 	}
98*d0774691Schristos 
99*d0774691Schristos 	/* Hash the result array. */
100*d0774691Schristos 	switch (variant) {
101*d0774691Schristos 	case hash_variant_x86_32: {
102*d0774691Schristos 		uint32_t out = hash_x86_32(hashes, hashes_size, 0);
103*d0774691Schristos 		memcpy(final, &out, sizeof(out));
104*d0774691Schristos 		break;
105*d0774691Schristos 	} case hash_variant_x86_128: {
106*d0774691Schristos 		uint64_t out[2];
107*d0774691Schristos 		hash_x86_128(hashes, hashes_size, 0, out);
108*d0774691Schristos 		memcpy(final, out, sizeof(out));
109*d0774691Schristos 		break;
110*d0774691Schristos 	} case hash_variant_x64_128: {
111*d0774691Schristos 		uint64_t out[2];
112*d0774691Schristos 		hash_x64_128(hashes, hashes_size, 0, out);
113*d0774691Schristos 		memcpy(final, out, sizeof(out));
114*d0774691Schristos 		break;
115*d0774691Schristos 	} default: not_reached();
116*d0774691Schristos 	}
117*d0774691Schristos 
118*d0774691Schristos 	computed = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) |
119*d0774691Schristos 	    (final[3] << 24);
120*d0774691Schristos 
121*d0774691Schristos 	switch (variant) {
122*d0774691Schristos #ifdef JEMALLOC_BIG_ENDIAN
123*d0774691Schristos 	case hash_variant_x86_32: expected = 0x6213303eU; break;
124*d0774691Schristos 	case hash_variant_x86_128: expected = 0x266820caU; break;
125*d0774691Schristos 	case hash_variant_x64_128: expected = 0xcc622b6fU; break;
126*d0774691Schristos #else
127*d0774691Schristos 	case hash_variant_x86_32: expected = 0xb0f57ee3U; break;
128*d0774691Schristos 	case hash_variant_x86_128: expected = 0xb3ece62aU; break;
129*d0774691Schristos 	case hash_variant_x64_128: expected = 0x6384ba69U; break;
130*d0774691Schristos #endif
131*d0774691Schristos 	default: not_reached();
132*d0774691Schristos 	}
133*d0774691Schristos 
134*d0774691Schristos 	assert_u32_eq(computed, expected,
135*d0774691Schristos 	    "Hash mismatch for %s(): expected %#x but got %#x",
136*d0774691Schristos 	    hash_variant_string(variant), expected, computed);
137*d0774691Schristos }
138*d0774691Schristos 
139*d0774691Schristos static void
hash_variant_verify(hash_variant_t variant)140*d0774691Schristos hash_variant_verify(hash_variant_t variant) {
141*d0774691Schristos #define MAX_ALIGN	16
142*d0774691Schristos 	uint8_t key[KEY_SIZE + (MAX_ALIGN - 1)];
143*d0774691Schristos 	unsigned i;
144*d0774691Schristos 
145*d0774691Schristos 	for (i = 0; i < MAX_ALIGN; i++) {
146*d0774691Schristos 		hash_variant_verify_key(variant, &key[i]);
147*d0774691Schristos 	}
148*d0774691Schristos #undef MAX_ALIGN
149*d0774691Schristos }
150*d0774691Schristos #undef KEY_SIZE
151*d0774691Schristos 
TEST_BEGIN(test_hash_x86_32)152*d0774691Schristos TEST_BEGIN(test_hash_x86_32) {
153*d0774691Schristos 	hash_variant_verify(hash_variant_x86_32);
154*d0774691Schristos }
155*d0774691Schristos TEST_END
156*d0774691Schristos 
TEST_BEGIN(test_hash_x86_128)157*d0774691Schristos TEST_BEGIN(test_hash_x86_128) {
158*d0774691Schristos 	hash_variant_verify(hash_variant_x86_128);
159*d0774691Schristos }
160*d0774691Schristos TEST_END
161*d0774691Schristos 
TEST_BEGIN(test_hash_x64_128)162*d0774691Schristos TEST_BEGIN(test_hash_x64_128) {
163*d0774691Schristos 	hash_variant_verify(hash_variant_x64_128);
164*d0774691Schristos }
165*d0774691Schristos TEST_END
166*d0774691Schristos 
167*d0774691Schristos int
main(void)168*d0774691Schristos main(void) {
169*d0774691Schristos 	return test(
170*d0774691Schristos 	    test_hash_x86_32,
171*d0774691Schristos 	    test_hash_x86_128,
172*d0774691Schristos 	    test_hash_x64_128);
173*d0774691Schristos }
174