murmur3.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2013 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdint.h>
  21. #include "mem.h"
  22. #include "intreadwrite.h"
  23. #include "murmur3.h"
  24. typedef struct AVMurMur3 {
  25. uint64_t h1, h2;
  26. uint8_t state[16];
  27. int state_pos;
  28. uint64_t len;
  29. } AVMurMur3;
  30. AVMurMur3 *av_murmur3_alloc(void)
  31. {
  32. return av_mallocz(sizeof(AVMurMur3));
  33. }
  34. void av_murmur3_init_seeded(AVMurMur3 *c, uint64_t seed)
  35. {
  36. memset(c, 0, sizeof(*c));
  37. c->h1 = c->h2 = seed;
  38. }
  39. void av_murmur3_init(AVMurMur3 *c)
  40. {
  41. // arbitrary random number as seed
  42. av_murmur3_init_seeded(c, 0x725acc55daddca55);
  43. }
  44. static const uint64_t c1 = UINT64_C(0x87c37b91114253d5);
  45. static const uint64_t c2 = UINT64_C(0x4cf5ad432745937f);
  46. #define ROT(a, b) ((a << b) | (a >> (64 - b)))
  47. static uint64_t inline get_k1(const uint8_t *src)
  48. {
  49. uint64_t k = AV_RL64(src);
  50. k *= c1;
  51. k = ROT(k, 31);
  52. k *= c2;
  53. return k;
  54. }
  55. static uint64_t inline get_k2(const uint8_t *src)
  56. {
  57. uint64_t k = AV_RL64(src + 8);
  58. k *= c2;
  59. k = ROT(k, 33);
  60. k *= c1;
  61. return k;
  62. }
  63. static uint64_t inline update_h1(uint64_t k, uint64_t h1, uint64_t h2)
  64. {
  65. k ^= h1;
  66. k = ROT(k, 27);
  67. k += h2;
  68. k *= 5;
  69. k += 0x52dce729;
  70. return k;
  71. }
  72. static uint64_t inline update_h2(uint64_t k, uint64_t h1, uint64_t h2)
  73. {
  74. k ^= h2;
  75. k = ROT(k, 31);
  76. k += h1;
  77. k *= 5;
  78. k += 0x38495ab5;
  79. return k;
  80. }
  81. void av_murmur3_update(AVMurMur3 *c, const uint8_t *src, int len)
  82. {
  83. const uint8_t *end;
  84. uint64_t h1 = c->h1, h2 = c->h2;
  85. uint64_t k1, k2;
  86. if (len <= 0) return;
  87. c->len += len;
  88. if (c->state_pos > 0) {
  89. while (c->state_pos < 16) {
  90. c->state[c->state_pos++] = *src++;
  91. if (--len <= 0) return;
  92. }
  93. c->state_pos = 0;
  94. k1 = get_k1(c->state);
  95. k2 = get_k2(c->state);
  96. h1 = update_h1(k1, h1, h2);
  97. h2 = update_h2(k2, h1, h2);
  98. }
  99. end = src + (len & ~15);
  100. while (src < end) {
  101. // These could be done sequentially instead
  102. // of interleaved, but like this is over 10% faster
  103. k1 = get_k1(src);
  104. k2 = get_k2(src);
  105. h1 = update_h1(k1, h1, h2);
  106. h2 = update_h2(k2, h1, h2);
  107. src += 16;
  108. }
  109. c->h1 = h1;
  110. c->h2 = h2;
  111. len &= 15;
  112. if (len > 0) {
  113. memcpy(c->state, src, len);
  114. c->state_pos = len;
  115. }
  116. }
  117. static inline uint64_t fmix(uint64_t k)
  118. {
  119. k ^= k >> 33;
  120. k *= UINT64_C(0xff51afd7ed558ccd);
  121. k ^= k >> 33;
  122. k *= UINT64_C(0xc4ceb9fe1a85ec53);
  123. k ^= k >> 33;
  124. return k;
  125. }
  126. void av_murmur3_final(AVMurMur3 *c, uint8_t dst[16])
  127. {
  128. uint64_t h1 = c->h1, h2 = c->h2;
  129. memset(c->state + c->state_pos, 0, sizeof(c->state) - c->state_pos);
  130. h1 ^= get_k1(c->state) ^ c->len;
  131. h2 ^= get_k2(c->state) ^ c->len;
  132. h1 += h2;
  133. h2 += h1;
  134. h1 = fmix(h1);
  135. h2 = fmix(h2);
  136. h1 += h2;
  137. h2 += h1;
  138. AV_WL64(dst, h1);
  139. AV_WL64(dst + 8, h2);
  140. }
  141. #ifdef TEST
  142. int main(void)
  143. {
  144. int i;
  145. uint8_t hash_result[16] = {0};
  146. AVMurMur3 *ctx = av_murmur3_alloc();
  147. #if 1
  148. uint8_t in[256] = {0};
  149. uint8_t *hashes = av_mallocz(256 * 16);
  150. for (i = 0; i < 256; i++)
  151. {
  152. in[i] = i;
  153. av_murmur3_init_seeded(ctx, 256 - i);
  154. // Note: this actually tests hashing 0 bytes
  155. av_murmur3_update(ctx, in, i);
  156. av_murmur3_final(ctx, hashes + 16 * i);
  157. }
  158. av_murmur3_init_seeded(ctx, 0);
  159. av_murmur3_update(ctx, hashes, 256 * 16);
  160. av_murmur3_final(ctx, hash_result);
  161. av_free(hashes);
  162. av_freep(&ctx);
  163. printf("result: 0x%"PRIx64" 0x%"PRIx64"\n", AV_RL64(hash_result), AV_RL64(hash_result + 8));
  164. // official reference value is 32 bit
  165. return AV_RL32(hash_result) != 0x6384ba69;
  166. #else
  167. uint8_t *in = av_mallocz(512*1024);
  168. av_murmur3_init(ctx);
  169. for (i = 0; i < 40*1024; i++)
  170. av_murmur3_update(ctx, in, 512*1024);
  171. av_murmur3_final(ctx, hash_result);
  172. av_free(in);
  173. return hash_result[0];
  174. #endif
  175. }
  176. #endif