hash.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include "test/jemalloc_test.h"
  30. #include "jemalloc/internal/hash.h"
  31. typedef enum {
  32. hash_variant_x86_32,
  33. hash_variant_x86_128,
  34. hash_variant_x64_128
  35. } hash_variant_t;
  36. static int
  37. hash_variant_bits(hash_variant_t variant) {
  38. switch (variant) {
  39. case hash_variant_x86_32: return 32;
  40. case hash_variant_x86_128: return 128;
  41. case hash_variant_x64_128: return 128;
  42. default: not_reached();
  43. }
  44. }
  45. static const char *
  46. hash_variant_string(hash_variant_t variant) {
  47. switch (variant) {
  48. case hash_variant_x86_32: return "hash_x86_32";
  49. case hash_variant_x86_128: return "hash_x86_128";
  50. case hash_variant_x64_128: return "hash_x64_128";
  51. default: not_reached();
  52. }
  53. }
  54. #define KEY_SIZE 256
  55. static void
  56. hash_variant_verify_key(hash_variant_t variant, uint8_t *key) {
  57. const int hashbytes = hash_variant_bits(variant) / 8;
  58. const int hashes_size = hashbytes * 256;
  59. VARIABLE_ARRAY(uint8_t, hashes, hashes_size);
  60. VARIABLE_ARRAY(uint8_t, final, hashbytes);
  61. unsigned i;
  62. uint32_t computed, expected;
  63. memset(key, 0, KEY_SIZE);
  64. memset(hashes, 0, hashes_size);
  65. memset(final, 0, hashbytes);
  66. /*
  67. * Hash keys of the form {0}, {0,1}, {0,1,2}, ..., {0,1,...,255} as the
  68. * seed.
  69. */
  70. for (i = 0; i < 256; i++) {
  71. key[i] = (uint8_t)i;
  72. switch (variant) {
  73. case hash_variant_x86_32: {
  74. uint32_t out;
  75. out = hash_x86_32(key, i, 256-i);
  76. memcpy(&hashes[i*hashbytes], &out, hashbytes);
  77. break;
  78. } case hash_variant_x86_128: {
  79. uint64_t out[2];
  80. hash_x86_128(key, i, 256-i, out);
  81. memcpy(&hashes[i*hashbytes], out, hashbytes);
  82. break;
  83. } case hash_variant_x64_128: {
  84. uint64_t out[2];
  85. hash_x64_128(key, i, 256-i, out);
  86. memcpy(&hashes[i*hashbytes], out, hashbytes);
  87. break;
  88. } default: not_reached();
  89. }
  90. }
  91. /* Hash the result array. */
  92. switch (variant) {
  93. case hash_variant_x86_32: {
  94. uint32_t out = hash_x86_32(hashes, hashes_size, 0);
  95. memcpy(final, &out, sizeof(out));
  96. break;
  97. } case hash_variant_x86_128: {
  98. uint64_t out[2];
  99. hash_x86_128(hashes, hashes_size, 0, out);
  100. memcpy(final, out, sizeof(out));
  101. break;
  102. } case hash_variant_x64_128: {
  103. uint64_t out[2];
  104. hash_x64_128(hashes, hashes_size, 0, out);
  105. memcpy(final, out, sizeof(out));
  106. break;
  107. } default: not_reached();
  108. }
  109. computed = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) |
  110. (final[3] << 24);
  111. switch (variant) {
  112. #ifdef JEMALLOC_BIG_ENDIAN
  113. case hash_variant_x86_32: expected = 0x6213303eU; break;
  114. case hash_variant_x86_128: expected = 0x266820caU; break;
  115. case hash_variant_x64_128: expected = 0xcc622b6fU; break;
  116. #else
  117. case hash_variant_x86_32: expected = 0xb0f57ee3U; break;
  118. case hash_variant_x86_128: expected = 0xb3ece62aU; break;
  119. case hash_variant_x64_128: expected = 0x6384ba69U; break;
  120. #endif
  121. default: not_reached();
  122. }
  123. expect_u32_eq(computed, expected,
  124. "Hash mismatch for %s(): expected %#x but got %#x",
  125. hash_variant_string(variant), expected, computed);
  126. }
  127. static void
  128. hash_variant_verify(hash_variant_t variant) {
  129. #define MAX_ALIGN 16
  130. uint8_t key[KEY_SIZE + (MAX_ALIGN - 1)];
  131. unsigned i;
  132. for (i = 0; i < MAX_ALIGN; i++) {
  133. hash_variant_verify_key(variant, &key[i]);
  134. }
  135. #undef MAX_ALIGN
  136. }
  137. #undef KEY_SIZE
  138. TEST_BEGIN(test_hash_x86_32) {
  139. hash_variant_verify(hash_variant_x86_32);
  140. }
  141. TEST_END
  142. TEST_BEGIN(test_hash_x86_128) {
  143. hash_variant_verify(hash_variant_x86_128);
  144. }
  145. TEST_END
  146. TEST_BEGIN(test_hash_x64_128) {
  147. hash_variant_verify(hash_variant_x64_128);
  148. }
  149. TEST_END
  150. int
  151. main(void) {
  152. return test(
  153. test_hash_x86_32,
  154. test_hash_x86_128,
  155. test_hash_x64_128);
  156. }