util_murmurhash.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2018 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* This is taken from alShaders/Cryptomatte/MurmurHash3.h:
  17. *
  18. * MurmurHash3 was written by Austin Appleby, and is placed in the public
  19. * domain. The author hereby disclaims copyright to this source code.
  20. */
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "util/util_algorithm.h"
  24. #include "util/util_murmurhash.h"
  25. #if defined(_MSC_VER)
  26. # define ROTL32(x, y) _rotl(x, y)
  27. # define ROTL64(x, y) _rotl64(x, y)
  28. # define BIG_CONSTANT(x) (x)
  29. #else
  30. ccl_device_inline uint32_t rotl32(uint32_t x, int8_t r)
  31. {
  32. return (x << r) | (x >> (32 - r));
  33. }
  34. # define ROTL32(x, y) rotl32(x, y)
  35. # define BIG_CONSTANT(x) (x##LLU)
  36. #endif
  37. CCL_NAMESPACE_BEGIN
  38. /* Block read - if your platform needs to do endian-swapping or can only
  39. * handle aligned reads, do the conversion here. */
  40. ccl_device_inline uint32_t mm_hash_getblock32(const uint32_t *p, int i)
  41. {
  42. return p[i];
  43. }
  44. /* Finalization mix - force all bits of a hash block to avalanche */
  45. ccl_device_inline uint32_t mm_hash_fmix32(uint32_t h)
  46. {
  47. h ^= h >> 16;
  48. h *= 0x85ebca6b;
  49. h ^= h >> 13;
  50. h *= 0xc2b2ae35;
  51. h ^= h >> 16;
  52. return h;
  53. }
  54. uint32_t util_murmur_hash3(const void *key, int len, uint32_t seed)
  55. {
  56. const uint8_t *data = (const uint8_t *)key;
  57. const int nblocks = len / 4;
  58. uint32_t h1 = seed;
  59. const uint32_t c1 = 0xcc9e2d51;
  60. const uint32_t c2 = 0x1b873593;
  61. const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
  62. for (int i = -nblocks; i; i++) {
  63. uint32_t k1 = mm_hash_getblock32(blocks, i);
  64. k1 *= c1;
  65. k1 = ROTL32(k1, 15);
  66. k1 *= c2;
  67. h1 ^= k1;
  68. h1 = ROTL32(h1, 13);
  69. h1 = h1 * 5 + 0xe6546b64;
  70. }
  71. const uint8_t *tail = (const uint8_t *)(data + nblocks * 4);
  72. uint32_t k1 = 0;
  73. switch (len & 3) {
  74. case 3:
  75. k1 ^= tail[2] << 16;
  76. ATTR_FALLTHROUGH;
  77. case 2:
  78. k1 ^= tail[1] << 8;
  79. ATTR_FALLTHROUGH;
  80. case 1:
  81. k1 ^= tail[0];
  82. k1 *= c1;
  83. k1 = ROTL32(k1, 15);
  84. k1 *= c2;
  85. h1 ^= k1;
  86. }
  87. h1 ^= len;
  88. h1 = mm_hash_fmix32(h1);
  89. return h1;
  90. }
  91. /* This is taken from the cryptomatte specification 1.0 */
  92. float util_hash_to_float(uint32_t hash)
  93. {
  94. uint32_t mantissa = hash & ((1 << 23) - 1);
  95. uint32_t exponent = (hash >> 23) & ((1 << 8) - 1);
  96. exponent = max(exponent, (uint32_t)1);
  97. exponent = min(exponent, (uint32_t)254);
  98. exponent = exponent << 23;
  99. uint32_t sign = (hash >> 31);
  100. sign = sign << 31;
  101. uint32_t float_bits = sign | exponent | mantissa;
  102. float f;
  103. memcpy(&f, &float_bits, sizeof(uint32_t));
  104. return f;
  105. }
  106. CCL_NAMESPACE_END