util_hash.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2011-2013 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. #ifndef __UTIL_HASH_H__
  17. #define __UTIL_HASH_H__
  18. #include "util/util_types.h"
  19. CCL_NAMESPACE_BEGIN
  20. ccl_device_inline uint hash_int_2d(uint kx, uint ky)
  21. {
  22. #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
  23. uint a, b, c;
  24. a = b = c = 0xdeadbeef + (2 << 2) + 13;
  25. a += kx;
  26. b += ky;
  27. c ^= b;
  28. c -= rot(b, 14);
  29. a ^= c;
  30. a -= rot(c, 11);
  31. b ^= a;
  32. b -= rot(a, 25);
  33. c ^= b;
  34. c -= rot(b, 16);
  35. a ^= c;
  36. a -= rot(c, 4);
  37. b ^= a;
  38. b -= rot(a, 14);
  39. c ^= b;
  40. c -= rot(b, 24);
  41. return c;
  42. #undef rot
  43. }
  44. ccl_device_inline uint hash_int(uint k)
  45. {
  46. return hash_int_2d(k, 0);
  47. }
  48. #ifndef __KERNEL_GPU__
  49. static inline uint hash_string(const char *str)
  50. {
  51. uint i = 0, c;
  52. while ((c = *str++))
  53. i = i * 37 + c;
  54. return i;
  55. }
  56. #endif
  57. ccl_device_inline float hash_int_01(uint k)
  58. {
  59. return (float)hash_int(k) * (1.0f / (float)0xFFFFFFFF);
  60. }
  61. CCL_NAMESPACE_END
  62. #endif /* __UTIL_HASH_H__ */