Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 32 | typedef enum { |
| 33 | hash_variant_x86_32, |
| 34 | hash_variant_x86_128, |
| 35 | hash_variant_x64_128 |
| 36 | } hash_variant_t; |
| 37 | |
Jason Evans | 9e1810c | 2016-02-24 12:42:23 -0800 | [diff] [blame] | 38 | static int |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 39 | hash_variant_bits(hash_variant_t variant) |
| 40 | { |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 41 | switch (variant) { |
| 42 | case hash_variant_x86_32: return (32); |
| 43 | case hash_variant_x86_128: return (128); |
| 44 | case hash_variant_x64_128: return (128); |
| 45 | default: not_reached(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static const char * |
| 50 | hash_variant_string(hash_variant_t variant) |
| 51 | { |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 52 | switch (variant) { |
| 53 | case hash_variant_x86_32: return ("hash_x86_32"); |
| 54 | case hash_variant_x86_128: return ("hash_x86_128"); |
Jason Evans | eca367b | 2013-12-17 09:14:39 -0800 | [diff] [blame] | 55 | case hash_variant_x64_128: return ("hash_x64_128"); |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 56 | default: not_reached(); |
| 57 | } |
| 58 | } |
| 59 | |
Jason Evans | a0aaad1 | 2016-02-20 10:23:48 -0800 | [diff] [blame] | 60 | #define KEY_SIZE 256 |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 61 | static void |
Jason Evans | a0aaad1 | 2016-02-20 10:23:48 -0800 | [diff] [blame] | 62 | hash_variant_verify_key(hash_variant_t variant, uint8_t *key) |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 63 | { |
Jason Evans | 9e1810c | 2016-02-24 12:42:23 -0800 | [diff] [blame] | 64 | const int hashbytes = hash_variant_bits(variant) / 8; |
Dmitri Smirnov | c3ab904 | 2016-02-29 14:30:19 -0800 | [diff] [blame] | 65 | const int hashes_size = hashbytes * 256; |
| 66 | VARIABLE_ARRAY(uint8_t, hashes, hashes_size); |
Mike Hommey | f41f143 | 2014-05-21 17:24:08 +0900 | [diff] [blame] | 67 | VARIABLE_ARRAY(uint8_t, final, hashbytes); |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 68 | unsigned i; |
| 69 | uint32_t computed, expected; |
| 70 | |
Jason Evans | a0aaad1 | 2016-02-20 10:23:48 -0800 | [diff] [blame] | 71 | memset(key, 0, KEY_SIZE); |
Dmitri Smirnov | c3ab904 | 2016-02-29 14:30:19 -0800 | [diff] [blame] | 72 | memset(hashes, 0, hashes_size); |
| 73 | memset(final, 0, hashbytes); |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Hash keys of the form {0}, {0,1}, {0,1,2}, ..., {0,1,...,255} as the |
| 77 | * seed. |
| 78 | */ |
| 79 | for (i = 0; i < 256; i++) { |
| 80 | key[i] = (uint8_t)i; |
| 81 | switch (variant) { |
| 82 | case hash_variant_x86_32: { |
| 83 | uint32_t out; |
| 84 | out = hash_x86_32(key, i, 256-i); |
| 85 | memcpy(&hashes[i*hashbytes], &out, hashbytes); |
| 86 | break; |
| 87 | } case hash_variant_x86_128: { |
| 88 | uint64_t out[2]; |
| 89 | hash_x86_128(key, i, 256-i, out); |
| 90 | memcpy(&hashes[i*hashbytes], out, hashbytes); |
| 91 | break; |
Jason Evans | 5aeeda6 | 2014-01-02 13:38:23 -0800 | [diff] [blame] | 92 | } case hash_variant_x64_128: { |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 93 | uint64_t out[2]; |
| 94 | hash_x64_128(key, i, 256-i, out); |
| 95 | memcpy(&hashes[i*hashbytes], out, hashbytes); |
| 96 | break; |
Jason Evans | 5aeeda6 | 2014-01-02 13:38:23 -0800 | [diff] [blame] | 97 | } default: not_reached(); |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | /* Hash the result array. */ |
| 102 | switch (variant) { |
| 103 | case hash_variant_x86_32: { |
Dmitri Smirnov | c3ab904 | 2016-02-29 14:30:19 -0800 | [diff] [blame] | 104 | uint32_t out = hash_x86_32(hashes, hashes_size, 0); |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 105 | memcpy(final, &out, sizeof(out)); |
| 106 | break; |
| 107 | } case hash_variant_x86_128: { |
| 108 | uint64_t out[2]; |
Dmitri Smirnov | c3ab904 | 2016-02-29 14:30:19 -0800 | [diff] [blame] | 109 | hash_x86_128(hashes, hashes_size, 0, out); |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 110 | memcpy(final, out, sizeof(out)); |
| 111 | break; |
| 112 | } case hash_variant_x64_128: { |
| 113 | uint64_t out[2]; |
Dmitri Smirnov | c3ab904 | 2016-02-29 14:30:19 -0800 | [diff] [blame] | 114 | hash_x64_128(hashes, hashes_size, 0, out); |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 115 | memcpy(final, out, sizeof(out)); |
| 116 | break; |
| 117 | } default: not_reached(); |
| 118 | } |
| 119 | |
| 120 | computed = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) | |
| 121 | (final[3] << 24); |
| 122 | |
| 123 | switch (variant) { |
Jason Evans | df3f270 | 2014-03-30 16:27:08 -0700 | [diff] [blame] | 124 | #ifdef JEMALLOC_BIG_ENDIAN |
| 125 | case hash_variant_x86_32: expected = 0x6213303eU; break; |
| 126 | case hash_variant_x86_128: expected = 0x266820caU; break; |
| 127 | case hash_variant_x64_128: expected = 0xcc622b6fU; break; |
| 128 | #else |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 129 | case hash_variant_x86_32: expected = 0xb0f57ee3U; break; |
| 130 | case hash_variant_x86_128: expected = 0xb3ece62aU; break; |
| 131 | case hash_variant_x64_128: expected = 0x6384ba69U; break; |
Jason Evans | df3f270 | 2014-03-30 16:27:08 -0700 | [diff] [blame] | 132 | #endif |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 133 | default: not_reached(); |
| 134 | } |
| 135 | |
| 136 | assert_u32_eq(computed, expected, |
| 137 | "Hash mismatch for %s(): expected %#x but got %#x", |
| 138 | hash_variant_string(variant), expected, computed); |
| 139 | } |
| 140 | |
Jason Evans | a0aaad1 | 2016-02-20 10:23:48 -0800 | [diff] [blame] | 141 | static void |
| 142 | hash_variant_verify(hash_variant_t variant) |
| 143 | { |
| 144 | #define MAX_ALIGN 16 |
| 145 | uint8_t key[KEY_SIZE + (MAX_ALIGN - 1)]; |
| 146 | unsigned i; |
| 147 | |
| 148 | for (i = 0; i < MAX_ALIGN; i++) |
| 149 | hash_variant_verify_key(variant, &key[i]); |
| 150 | #undef MAX_ALIGN |
| 151 | } |
| 152 | #undef KEY_SIZE |
| 153 | |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 154 | TEST_BEGIN(test_hash_x86_32) |
| 155 | { |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 156 | hash_variant_verify(hash_variant_x86_32); |
| 157 | } |
| 158 | TEST_END |
| 159 | |
| 160 | TEST_BEGIN(test_hash_x86_128) |
| 161 | { |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 162 | hash_variant_verify(hash_variant_x86_128); |
| 163 | } |
| 164 | TEST_END |
| 165 | |
| 166 | TEST_BEGIN(test_hash_x64_128) |
| 167 | { |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 168 | hash_variant_verify(hash_variant_x64_128); |
| 169 | } |
| 170 | TEST_END |
| 171 | |
| 172 | int |
| 173 | main(void) |
| 174 | { |
Jason Evans | e6b7aa4 | 2013-12-16 22:55:41 -0800 | [diff] [blame] | 175 | return (test( |
| 176 | test_hash_x86_32, |
| 177 | test_hash_x86_128, |
| 178 | test_hash_x64_128)); |
| 179 | } |