hash.c 4.6 KB

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