t1ha.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2016-2017 Positive Technologies, https://www.ptsecurity.com,
  3. * Fast Positive Hash.
  4. *
  5. * Portions Copyright (c) 2010-2017 Leonid Yuriev <leo@yuriev.ru>,
  6. * The 1Hippeus project (t1h).
  7. *
  8. * This software is provided 'as-is', without any express or implied
  9. * warranty. In no event will the authors be held liable for any damages
  10. * arising from the use of this software.
  11. *
  12. * Permission is granted to anyone to use this software for any purpose,
  13. * including commercial applications, and to alter it and redistribute it
  14. * freely, subject to the following restrictions:
  15. *
  16. * 1. The origin of this software must not be misrepresented; you must not
  17. * claim that you wrote the original software. If you use this software
  18. * in a product, an acknowledgement in the product documentation would be
  19. * appreciated but is not required.
  20. * 2. Altered source versions must be plainly marked as such, and must not be
  21. * misrepresented as being the original software.
  22. * 3. This notice may not be removed or altered from any source distribution.
  23. */
  24. /*
  25. * t1ha = { Fast Positive Hash, aka "Позитивный Хэш" }
  26. * by [Positive Technologies](https://www.ptsecurity.ru)
  27. *
  28. * Briefly, it is a 64-bit Hash Function:
  29. * 1. Created for 64-bit little-endian platforms, in predominantly for x86_64,
  30. * but without penalties could runs on any 64-bit CPU.
  31. * 2. In most cases up to 15% faster than City64, xxHash, mum-hash, metro-hash
  32. * and all others which are not use specific hardware tricks.
  33. * 3. Not suitable for cryptography.
  34. *
  35. * The Future will Positive. Всё будет хорошо.
  36. *
  37. * ACKNOWLEDGEMENT:
  38. * The t1ha was originally developed by Leonid Yuriev (Леонид Юрьев)
  39. * for The 1Hippeus project - zerocopy messaging in the spirit of Sparta!
  40. */
  41. #pragma once
  42. #include <stddef.h>
  43. #include <stdint.h>
  44. #ifndef __has_attribute
  45. #define __has_attribute(x) (0)
  46. #endif
  47. #ifndef __GNUC_PREREQ
  48. #if defined(__GNUC__) && defined(__GNUC_MINOR__)
  49. #define __GNUC_PREREQ(maj, min) \
  50. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  51. #else
  52. #define __GNUC_PREREQ(maj, min) 0
  53. #endif
  54. #endif
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. /* The main generic version of "Fast Positive Hash".
  59. * - returns same result on all architectures and CPUs.
  60. * - created for 64-bit little-endian platforms,
  61. * in other cases may runs slowly. */
  62. uint64_t t1ha(const void *data, size_t len, uint64_t seed);
  63. /* The big-endian version.
  64. * - runs faster on 64-bit big-endian platforms,
  65. * in other cases may runs slowly.
  66. * - returns same result on all architectures and CPUs,
  67. * but it is differs from t1ha(). */
  68. uint64_t t1ha_64be(const void *data, size_t len, uint64_t seed);
  69. /* Just a nickname for the generic t1ha.
  70. * 't1ha_64le' mean that is for 64-bit little-endian platforms. */
  71. static __inline uint64_t t1ha_64le(const void *data, size_t len,
  72. uint64_t seed) {
  73. return t1ha(data, len, seed);
  74. }
  75. /* The alternative little/big-endian versions, which were
  76. * designed for a 32-bit CPUs. In comparison to the main t1ha:
  77. * - much faster on 32-bit architectures.
  78. * - half of speed on 64-bit architectures.
  79. * - useful in case primary target platform is 32-bit. */
  80. uint64_t t1ha_32le(const void *data, size_t len, uint64_t seed);
  81. uint64_t t1ha_32be(const void *data, size_t len, uint64_t seed);
  82. #if (defined(__x86_64__) && (defined(__SSE4_2__) || __GNUC_PREREQ(4, 4) || \
  83. __has_attribute(target))) || \
  84. defined(_M_X64) || defined(_X86_64_)
  85. /* Machine specific hash, which uses CRC32c hardware acceleration.
  86. * Available only on modern x86 CPUs with support for SSE 4.2. */
  87. uint64_t t1ha_ia32crc(const void *data, size_t len, uint64_t seed);
  88. #endif
  89. #if ((defined(__AES__) || __GNUC_PREREQ(4, 4) || __has_attribute(target)) && \
  90. (defined(__x86_64__) || defined(__i386__))) || \
  91. defined(_M_X64) || defined(_M_IX86)
  92. /* Machine specific hash, which uses AES hardware acceleration.
  93. * Available only on modern x86 CPUs with AES-NI extension. */
  94. uint64_t t1ha_ia32aes(const void *data, size_t len, uint64_t seed);
  95. #endif /* __AES__ */
  96. /* Machine-specific facade that selects the fastest hash for
  97. * the current processor.
  98. *
  99. * BE CAREFUL!!! This is mean that hash result could be differ,
  100. * when someone (CPU, BIOS, OS, compiler, source code) was changed.
  101. * Briefly, such hash-results and their derivatives, should
  102. * not be persist or transferred over a network. */
  103. #if defined(__ELF__) && (__GNUC_PREREQ(4, 6) || __has_attribute(ifunc))
  104. uint64_t t1ha_local(const void *data, size_t len, uint64_t seed);
  105. #else
  106. extern uint64_t (*t1ha_local_ptr)(const void *data, size_t len, uint64_t seed);
  107. static __inline uint64_t t1ha_local(const void *data, size_t len,
  108. uint64_t seed) {
  109. return t1ha_local_ptr(data, len, seed);
  110. }
  111. #endif
  112. #ifdef __cplusplus
  113. }
  114. #endif