murmur3.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 inline uint64_t 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 inline uint64_t 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 inline uint64_t 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. #if FF_API_CRYPTO_SIZE_T
  82. void av_murmur3_update(AVMurMur3 *c, const uint8_t *src, int len)
  83. #else
  84. void av_murmur3_update(AVMurMur3 *c, const uint8_t *src, size_t len)
  85. #endif
  86. {
  87. const uint8_t *end;
  88. uint64_t h1 = c->h1, h2 = c->h2;
  89. uint64_t k1, k2;
  90. if (len <= 0) return;
  91. c->len += len;
  92. if (c->state_pos > 0) {
  93. while (c->state_pos < 16) {
  94. c->state[c->state_pos++] = *src++;
  95. if (--len <= 0) return;
  96. }
  97. c->state_pos = 0;
  98. k1 = get_k1(c->state);
  99. k2 = get_k2(c->state);
  100. h1 = update_h1(k1, h1, h2);
  101. h2 = update_h2(k2, h1, h2);
  102. }
  103. end = src + (len & ~15);
  104. while (src < end) {
  105. // These could be done sequentially instead
  106. // of interleaved, but like this is over 10% faster
  107. k1 = get_k1(src);
  108. k2 = get_k2(src);
  109. h1 = update_h1(k1, h1, h2);
  110. h2 = update_h2(k2, h1, h2);
  111. src += 16;
  112. }
  113. c->h1 = h1;
  114. c->h2 = h2;
  115. len &= 15;
  116. if (len > 0) {
  117. memcpy(c->state, src, len);
  118. c->state_pos = len;
  119. }
  120. }
  121. static inline uint64_t fmix(uint64_t k)
  122. {
  123. k ^= k >> 33;
  124. k *= UINT64_C(0xff51afd7ed558ccd);
  125. k ^= k >> 33;
  126. k *= UINT64_C(0xc4ceb9fe1a85ec53);
  127. k ^= k >> 33;
  128. return k;
  129. }
  130. void av_murmur3_final(AVMurMur3 *c, uint8_t dst[16])
  131. {
  132. uint64_t h1 = c->h1, h2 = c->h2;
  133. memset(c->state + c->state_pos, 0, sizeof(c->state) - c->state_pos);
  134. h1 ^= get_k1(c->state) ^ c->len;
  135. h2 ^= get_k2(c->state) ^ c->len;
  136. h1 += h2;
  137. h2 += h1;
  138. h1 = fmix(h1);
  139. h2 = fmix(h2);
  140. h1 += h2;
  141. h2 += h1;
  142. AV_WL64(dst, h1);
  143. AV_WL64(dst + 8, h2);
  144. }