hash.h 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: hash.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. #ifndef ABSL_HASH_INTERNAL_HASH_H_
  20. #define ABSL_HASH_INTERNAL_HASH_H_
  21. #include <algorithm>
  22. #include <array>
  23. #include <bitset>
  24. #include <cmath>
  25. #include <cstddef>
  26. #include <cstring>
  27. #include <deque>
  28. #include <forward_list>
  29. #include <functional>
  30. #include <iterator>
  31. #include <limits>
  32. #include <list>
  33. #include <map>
  34. #include <memory>
  35. #include <set>
  36. #include <string>
  37. #include <tuple>
  38. #include <type_traits>
  39. #include <unordered_map>
  40. #include <unordered_set>
  41. #include <utility>
  42. #include <vector>
  43. #include "absl/base/config.h"
  44. #include "absl/base/internal/unaligned_access.h"
  45. #include "absl/base/port.h"
  46. #include "absl/container/fixed_array.h"
  47. #include "absl/hash/internal/city.h"
  48. #include "absl/hash/internal/low_level_hash.h"
  49. #include "absl/meta/type_traits.h"
  50. #include "absl/numeric/bits.h"
  51. #include "absl/numeric/int128.h"
  52. #include "absl/strings/string_view.h"
  53. #include "absl/types/optional.h"
  54. #include "absl/types/variant.h"
  55. #include "absl/utility/utility.h"
  56. namespace absl {
  57. ABSL_NAMESPACE_BEGIN
  58. class HashState;
  59. namespace hash_internal {
  60. // Internal detail: Large buffers are hashed in smaller chunks. This function
  61. // returns the size of these chunks.
  62. constexpr size_t PiecewiseChunkSize() { return 1024; }
  63. // PiecewiseCombiner
  64. //
  65. // PiecewiseCombiner is an internal-only helper class for hashing a piecewise
  66. // buffer of `char` or `unsigned char` as though it were contiguous. This class
  67. // provides two methods:
  68. //
  69. // H add_buffer(state, data, size)
  70. // H finalize(state)
  71. //
  72. // `add_buffer` can be called zero or more times, followed by a single call to
  73. // `finalize`. This will produce the same hash expansion as concatenating each
  74. // buffer piece into a single contiguous buffer, and passing this to
  75. // `H::combine_contiguous`.
  76. //
  77. // Example usage:
  78. // PiecewiseCombiner combiner;
  79. // for (const auto& piece : pieces) {
  80. // state = combiner.add_buffer(std::move(state), piece.data, piece.size);
  81. // }
  82. // return combiner.finalize(std::move(state));
  83. class PiecewiseCombiner {
  84. public:
  85. PiecewiseCombiner() : position_(0) {}
  86. PiecewiseCombiner(const PiecewiseCombiner&) = delete;
  87. PiecewiseCombiner& operator=(const PiecewiseCombiner&) = delete;
  88. // PiecewiseCombiner::add_buffer()
  89. //
  90. // Appends the given range of bytes to the sequence to be hashed, which may
  91. // modify the provided hash state.
  92. template <typename H>
  93. H add_buffer(H state, const unsigned char* data, size_t size);
  94. template <typename H>
  95. H add_buffer(H state, const char* data, size_t size) {
  96. return add_buffer(std::move(state),
  97. reinterpret_cast<const unsigned char*>(data), size);
  98. }
  99. // PiecewiseCombiner::finalize()
  100. //
  101. // Finishes combining the hash sequence, which may may modify the provided
  102. // hash state.
  103. //
  104. // Once finalize() is called, add_buffer() may no longer be called. The
  105. // resulting hash state will be the same as if the pieces passed to
  106. // add_buffer() were concatenated into a single flat buffer, and then provided
  107. // to H::combine_contiguous().
  108. template <typename H>
  109. H finalize(H state);
  110. private:
  111. unsigned char buf_[PiecewiseChunkSize()];
  112. size_t position_;
  113. };
  114. // is_hashable()
  115. //
  116. // Trait class which returns true if T is hashable by the absl::Hash framework.
  117. // Used for the AbslHashValue implementations for composite types below.
  118. template <typename T>
  119. struct is_hashable;
  120. // HashStateBase
  121. //
  122. // An internal implementation detail that contains common implementation details
  123. // for all of the "hash state objects" objects generated by Abseil. This is not
  124. // a public API; users should not create classes that inherit from this.
  125. //
  126. // A hash state object is the template argument `H` passed to `AbslHashValue`.
  127. // It represents an intermediate state in the computation of an unspecified hash
  128. // algorithm. `HashStateBase` provides a CRTP style base class for hash state
  129. // implementations. Developers adding type support for `absl::Hash` should not
  130. // rely on any parts of the state object other than the following member
  131. // functions:
  132. //
  133. // * HashStateBase::combine()
  134. // * HashStateBase::combine_contiguous()
  135. // * HashStateBase::combine_unordered()
  136. //
  137. // A derived hash state class of type `H` must provide a public member function
  138. // with a signature similar to the following:
  139. //
  140. // `static H combine_contiguous(H state, const unsigned char*, size_t)`.
  141. //
  142. // It must also provide a private template method named RunCombineUnordered.
  143. //
  144. // A "consumer" is a 1-arg functor returning void. Its argument is a reference
  145. // to an inner hash state object, and it may be called multiple times. When
  146. // called, the functor consumes the entropy from the provided state object,
  147. // and resets that object to its empty state.
  148. //
  149. // A "combiner" is a stateless 2-arg functor returning void. Its arguments are
  150. // an inner hash state object and an ElementStateConsumer functor. A combiner
  151. // uses the provided inner hash state object to hash each element of the
  152. // container, passing the inner hash state object to the consumer after hashing
  153. // each element.
  154. //
  155. // Given these definitions, a derived hash state class of type H
  156. // must provide a private template method with a signature similar to the
  157. // following:
  158. //
  159. // `template <typename CombinerT>`
  160. // `static H RunCombineUnordered(H outer_state, CombinerT combiner)`
  161. //
  162. // This function is responsible for constructing the inner state object and
  163. // providing a consumer to the combiner. It uses side effects of the consumer
  164. // and combiner to mix the state of each element in an order-independent manner,
  165. // and uses this to return an updated value of `outer_state`.
  166. //
  167. // This inside-out approach generates efficient object code in the normal case,
  168. // but allows us to use stack storage to implement the absl::HashState type
  169. // erasure mechanism (avoiding heap allocations while hashing).
  170. //
  171. // `HashStateBase` will provide a complete implementation for a hash state
  172. // object in terms of these two methods.
  173. //
  174. // Example:
  175. //
  176. // // Use CRTP to define your derived class.
  177. // struct MyHashState : HashStateBase<MyHashState> {
  178. // static H combine_contiguous(H state, const unsigned char*, size_t);
  179. // using MyHashState::HashStateBase::combine;
  180. // using MyHashState::HashStateBase::combine_contiguous;
  181. // using MyHashState::HashStateBase::combine_unordered;
  182. // private:
  183. // template <typename CombinerT>
  184. // static H RunCombineUnordered(H state, CombinerT combiner);
  185. // };
  186. template <typename H>
  187. class HashStateBase {
  188. public:
  189. // HashStateBase::combine()
  190. //
  191. // Combines an arbitrary number of values into a hash state, returning the
  192. // updated state.
  193. //
  194. // Each of the value types `T` must be separately hashable by the Abseil
  195. // hashing framework.
  196. //
  197. // NOTE:
  198. //
  199. // state = H::combine(std::move(state), value1, value2, value3);
  200. //
  201. // is guaranteed to produce the same hash expansion as:
  202. //
  203. // state = H::combine(std::move(state), value1);
  204. // state = H::combine(std::move(state), value2);
  205. // state = H::combine(std::move(state), value3);
  206. template <typename T, typename... Ts>
  207. static H combine(H state, const T& value, const Ts&... values);
  208. static H combine(H state) { return state; }
  209. // HashStateBase::combine_contiguous()
  210. //
  211. // Combines a contiguous array of `size` elements into a hash state, returning
  212. // the updated state.
  213. //
  214. // NOTE:
  215. //
  216. // state = H::combine_contiguous(std::move(state), data, size);
  217. //
  218. // is NOT guaranteed to produce the same hash expansion as a for-loop (it may
  219. // perform internal optimizations). If you need this guarantee, use the
  220. // for-loop instead.
  221. template <typename T>
  222. static H combine_contiguous(H state, const T* data, size_t size);
  223. template <typename I>
  224. static H combine_unordered(H state, I begin, I end);
  225. using AbslInternalPiecewiseCombiner = PiecewiseCombiner;
  226. template <typename T>
  227. using is_hashable = absl::hash_internal::is_hashable<T>;
  228. private:
  229. // Common implementation of the iteration step of a "combiner", as described
  230. // above.
  231. template <typename I>
  232. struct CombineUnorderedCallback {
  233. I begin;
  234. I end;
  235. template <typename InnerH, typename ElementStateConsumer>
  236. void operator()(InnerH inner_state, ElementStateConsumer cb) {
  237. for (; begin != end; ++begin) {
  238. inner_state = H::combine(std::move(inner_state), *begin);
  239. cb(inner_state);
  240. }
  241. }
  242. };
  243. };
  244. // is_uniquely_represented
  245. //
  246. // `is_uniquely_represented<T>` is a trait class that indicates whether `T`
  247. // is uniquely represented.
  248. //
  249. // A type is "uniquely represented" if two equal values of that type are
  250. // guaranteed to have the same bytes in their underlying storage. In other
  251. // words, if `a == b`, then `memcmp(&a, &b, sizeof(T))` is guaranteed to be
  252. // zero. This property cannot be detected automatically, so this trait is false
  253. // by default, but can be specialized by types that wish to assert that they are
  254. // uniquely represented. This makes them eligible for certain optimizations.
  255. //
  256. // If you have any doubt whatsoever, do not specialize this template.
  257. // The default is completely safe, and merely disables some optimizations
  258. // that will not matter for most types. Specializing this template,
  259. // on the other hand, can be very hazardous.
  260. //
  261. // To be uniquely represented, a type must not have multiple ways of
  262. // representing the same value; for example, float and double are not
  263. // uniquely represented, because they have distinct representations for
  264. // +0 and -0. Furthermore, the type's byte representation must consist
  265. // solely of user-controlled data, with no padding bits and no compiler-
  266. // controlled data such as vptrs or sanitizer metadata. This is usually
  267. // very difficult to guarantee, because in most cases the compiler can
  268. // insert data and padding bits at its own discretion.
  269. //
  270. // If you specialize this template for a type `T`, you must do so in the file
  271. // that defines that type (or in this file). If you define that specialization
  272. // anywhere else, `is_uniquely_represented<T>` could have different meanings
  273. // in different places.
  274. //
  275. // The Enable parameter is meaningless; it is provided as a convenience,
  276. // to support certain SFINAE techniques when defining specializations.
  277. template <typename T, typename Enable = void>
  278. struct is_uniquely_represented : std::false_type {};
  279. // is_uniquely_represented<unsigned char>
  280. //
  281. // unsigned char is a synonym for "byte", so it is guaranteed to be
  282. // uniquely represented.
  283. template <>
  284. struct is_uniquely_represented<unsigned char> : std::true_type {};
  285. // is_uniquely_represented for non-standard integral types
  286. //
  287. // Integral types other than bool should be uniquely represented on any
  288. // platform that this will plausibly be ported to.
  289. template <typename Integral>
  290. struct is_uniquely_represented<
  291. Integral, typename std::enable_if<std::is_integral<Integral>::value>::type>
  292. : std::true_type {};
  293. // is_uniquely_represented<bool>
  294. //
  295. //
  296. template <>
  297. struct is_uniquely_represented<bool> : std::false_type {};
  298. // hash_bytes()
  299. //
  300. // Convenience function that combines `hash_state` with the byte representation
  301. // of `value`.
  302. template <typename H, typename T>
  303. H hash_bytes(H hash_state, const T& value) {
  304. const unsigned char* start = reinterpret_cast<const unsigned char*>(&value);
  305. return H::combine_contiguous(std::move(hash_state), start, sizeof(value));
  306. }
  307. // -----------------------------------------------------------------------------
  308. // AbslHashValue for Basic Types
  309. // -----------------------------------------------------------------------------
  310. // Note: Default `AbslHashValue` implementations live in `hash_internal`. This
  311. // allows us to block lexical scope lookup when doing an unqualified call to
  312. // `AbslHashValue` below. User-defined implementations of `AbslHashValue` can
  313. // only be found via ADL.
  314. // AbslHashValue() for hashing bool values
  315. //
  316. // We use SFINAE to ensure that this overload only accepts bool, not types that
  317. // are convertible to bool.
  318. template <typename H, typename B>
  319. typename std::enable_if<std::is_same<B, bool>::value, H>::type AbslHashValue(
  320. H hash_state, B value) {
  321. return H::combine(std::move(hash_state),
  322. static_cast<unsigned char>(value ? 1 : 0));
  323. }
  324. // AbslHashValue() for hashing enum values
  325. template <typename H, typename Enum>
  326. typename std::enable_if<std::is_enum<Enum>::value, H>::type AbslHashValue(
  327. H hash_state, Enum e) {
  328. // In practice, we could almost certainly just invoke hash_bytes directly,
  329. // but it's possible that a sanitizer might one day want to
  330. // store data in the unused bits of an enum. To avoid that risk, we
  331. // convert to the underlying type before hashing. Hopefully this will get
  332. // optimized away; if not, we can reopen discussion with c-toolchain-team.
  333. return H::combine(std::move(hash_state),
  334. static_cast<typename std::underlying_type<Enum>::type>(e));
  335. }
  336. // AbslHashValue() for hashing floating-point values
  337. template <typename H, typename Float>
  338. typename std::enable_if<std::is_same<Float, float>::value ||
  339. std::is_same<Float, double>::value,
  340. H>::type
  341. AbslHashValue(H hash_state, Float value) {
  342. return hash_internal::hash_bytes(std::move(hash_state),
  343. value == 0 ? 0 : value);
  344. }
  345. // Long double has the property that it might have extra unused bytes in it.
  346. // For example, in x86 sizeof(long double)==16 but it only really uses 80-bits
  347. // of it. This means we can't use hash_bytes on a long double and have to
  348. // convert it to something else first.
  349. template <typename H, typename LongDouble>
  350. typename std::enable_if<std::is_same<LongDouble, long double>::value, H>::type
  351. AbslHashValue(H hash_state, LongDouble value) {
  352. const int category = std::fpclassify(value);
  353. switch (category) {
  354. case FP_INFINITE:
  355. // Add the sign bit to differentiate between +Inf and -Inf
  356. hash_state = H::combine(std::move(hash_state), std::signbit(value));
  357. break;
  358. case FP_NAN:
  359. case FP_ZERO:
  360. default:
  361. // Category is enough for these.
  362. break;
  363. case FP_NORMAL:
  364. case FP_SUBNORMAL:
  365. // We can't convert `value` directly to double because this would have
  366. // undefined behavior if the value is out of range.
  367. // std::frexp gives us a value in the range (-1, -.5] or [.5, 1) that is
  368. // guaranteed to be in range for `double`. The truncation is
  369. // implementation defined, but that works as long as it is deterministic.
  370. int exp;
  371. auto mantissa = static_cast<double>(std::frexp(value, &exp));
  372. hash_state = H::combine(std::move(hash_state), mantissa, exp);
  373. }
  374. return H::combine(std::move(hash_state), category);
  375. }
  376. // AbslHashValue() for hashing pointers
  377. template <typename H, typename T>
  378. H AbslHashValue(H hash_state, T* ptr) {
  379. auto v = reinterpret_cast<uintptr_t>(ptr);
  380. // Due to alignment, pointers tend to have low bits as zero, and the next few
  381. // bits follow a pattern since they are also multiples of some base value.
  382. // Mixing the pointer twice helps prevent stuck low bits for certain alignment
  383. // values.
  384. return H::combine(std::move(hash_state), v, v);
  385. }
  386. // AbslHashValue() for hashing nullptr_t
  387. template <typename H>
  388. H AbslHashValue(H hash_state, std::nullptr_t) {
  389. return H::combine(std::move(hash_state), static_cast<void*>(nullptr));
  390. }
  391. // AbslHashValue() for hashing pointers-to-member
  392. template <typename H, typename T, typename C>
  393. H AbslHashValue(H hash_state, T C::* ptr) {
  394. auto salient_ptm_size = [](std::size_t n) -> std::size_t {
  395. #if defined(_MSC_VER)
  396. // Pointers-to-member-function on MSVC consist of one pointer plus 0, 1, 2,
  397. // or 3 ints. In 64-bit mode, they are 8-byte aligned and thus can contain
  398. // padding (namely when they have 1 or 3 ints). The value below is a lower
  399. // bound on the number of salient, non-padding bytes that we use for
  400. // hashing.
  401. if (alignof(T C::*) == alignof(int)) {
  402. // No padding when all subobjects have the same size as the total
  403. // alignment. This happens in 32-bit mode.
  404. return n;
  405. } else {
  406. // Padding for 1 int (size 16) or 3 ints (size 24).
  407. // With 2 ints, the size is 16 with no padding, which we pessimize.
  408. return n == 24 ? 20 : n == 16 ? 12 : n;
  409. }
  410. #else
  411. // On other platforms, we assume that pointers-to-members do not have
  412. // padding.
  413. #ifdef __cpp_lib_has_unique_object_representations
  414. static_assert(std::has_unique_object_representations<T C::*>::value);
  415. #endif // __cpp_lib_has_unique_object_representations
  416. return n;
  417. #endif
  418. };
  419. return H::combine_contiguous(std::move(hash_state),
  420. reinterpret_cast<unsigned char*>(&ptr),
  421. salient_ptm_size(sizeof ptr));
  422. }
  423. // -----------------------------------------------------------------------------
  424. // AbslHashValue for Composite Types
  425. // -----------------------------------------------------------------------------
  426. // AbslHashValue() for hashing pairs
  427. template <typename H, typename T1, typename T2>
  428. typename std::enable_if<is_hashable<T1>::value && is_hashable<T2>::value,
  429. H>::type
  430. AbslHashValue(H hash_state, const std::pair<T1, T2>& p) {
  431. return H::combine(std::move(hash_state), p.first, p.second);
  432. }
  433. // hash_tuple()
  434. //
  435. // Helper function for hashing a tuple. The third argument should
  436. // be an index_sequence running from 0 to tuple_size<Tuple> - 1.
  437. template <typename H, typename Tuple, size_t... Is>
  438. H hash_tuple(H hash_state, const Tuple& t, absl::index_sequence<Is...>) {
  439. return H::combine(std::move(hash_state), std::get<Is>(t)...);
  440. }
  441. // AbslHashValue for hashing tuples
  442. template <typename H, typename... Ts>
  443. #if defined(_MSC_VER)
  444. // This SFINAE gets MSVC confused under some conditions. Let's just disable it
  445. // for now.
  446. H
  447. #else // _MSC_VER
  448. typename std::enable_if<absl::conjunction<is_hashable<Ts>...>::value, H>::type
  449. #endif // _MSC_VER
  450. AbslHashValue(H hash_state, const std::tuple<Ts...>& t) {
  451. return hash_internal::hash_tuple(std::move(hash_state), t,
  452. absl::make_index_sequence<sizeof...(Ts)>());
  453. }
  454. // -----------------------------------------------------------------------------
  455. // AbslHashValue for Pointers
  456. // -----------------------------------------------------------------------------
  457. // AbslHashValue for hashing unique_ptr
  458. template <typename H, typename T, typename D>
  459. H AbslHashValue(H hash_state, const std::unique_ptr<T, D>& ptr) {
  460. return H::combine(std::move(hash_state), ptr.get());
  461. }
  462. // AbslHashValue for hashing shared_ptr
  463. template <typename H, typename T>
  464. H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
  465. return H::combine(std::move(hash_state), ptr.get());
  466. }
  467. // -----------------------------------------------------------------------------
  468. // AbslHashValue for String-Like Types
  469. // -----------------------------------------------------------------------------
  470. // AbslHashValue for hashing strings
  471. //
  472. // All the string-like types supported here provide the same hash expansion for
  473. // the same character sequence. These types are:
  474. //
  475. // - `absl::Cord`
  476. // - `std::string` (and std::basic_string<char, std::char_traits<char>, A> for
  477. // any allocator A)
  478. // - `absl::string_view` and `std::string_view`
  479. //
  480. // For simplicity, we currently support only `char` strings. This support may
  481. // be broadened, if necessary, but with some caution - this overload would
  482. // misbehave in cases where the traits' `eq()` member isn't equivalent to `==`
  483. // on the underlying character type.
  484. template <typename H>
  485. H AbslHashValue(H hash_state, absl::string_view str) {
  486. return H::combine(
  487. H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
  488. str.size());
  489. }
  490. // Support std::wstring, std::u16string and std::u32string.
  491. template <typename Char, typename Alloc, typename H,
  492. typename = absl::enable_if_t<std::is_same<Char, wchar_t>::value ||
  493. std::is_same<Char, char16_t>::value ||
  494. std::is_same<Char, char32_t>::value>>
  495. H AbslHashValue(
  496. H hash_state,
  497. const std::basic_string<Char, std::char_traits<Char>, Alloc>& str) {
  498. return H::combine(
  499. H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
  500. str.size());
  501. }
  502. // -----------------------------------------------------------------------------
  503. // AbslHashValue for Sequence Containers
  504. // -----------------------------------------------------------------------------
  505. // AbslHashValue for hashing std::array
  506. template <typename H, typename T, size_t N>
  507. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  508. H hash_state, const std::array<T, N>& array) {
  509. return H::combine_contiguous(std::move(hash_state), array.data(),
  510. array.size());
  511. }
  512. // AbslHashValue for hashing std::deque
  513. template <typename H, typename T, typename Allocator>
  514. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  515. H hash_state, const std::deque<T, Allocator>& deque) {
  516. // TODO(gromer): investigate a more efficient implementation taking
  517. // advantage of the chunk structure.
  518. for (const auto& t : deque) {
  519. hash_state = H::combine(std::move(hash_state), t);
  520. }
  521. return H::combine(std::move(hash_state), deque.size());
  522. }
  523. // AbslHashValue for hashing std::forward_list
  524. template <typename H, typename T, typename Allocator>
  525. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  526. H hash_state, const std::forward_list<T, Allocator>& list) {
  527. size_t size = 0;
  528. for (const T& t : list) {
  529. hash_state = H::combine(std::move(hash_state), t);
  530. ++size;
  531. }
  532. return H::combine(std::move(hash_state), size);
  533. }
  534. // AbslHashValue for hashing std::list
  535. template <typename H, typename T, typename Allocator>
  536. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  537. H hash_state, const std::list<T, Allocator>& list) {
  538. for (const auto& t : list) {
  539. hash_state = H::combine(std::move(hash_state), t);
  540. }
  541. return H::combine(std::move(hash_state), list.size());
  542. }
  543. // AbslHashValue for hashing std::vector
  544. //
  545. // Do not use this for vector<bool> on platforms that have a working
  546. // implementation of std::hash. It does not have a .data(), and a fallback for
  547. // std::hash<> is most likely faster.
  548. template <typename H, typename T, typename Allocator>
  549. typename std::enable_if<is_hashable<T>::value && !std::is_same<T, bool>::value,
  550. H>::type
  551. AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
  552. return H::combine(H::combine_contiguous(std::move(hash_state), vector.data(),
  553. vector.size()),
  554. vector.size());
  555. }
  556. // AbslHashValue special cases for hashing std::vector<bool>
  557. #if defined(ABSL_IS_BIG_ENDIAN) && \
  558. (defined(__GLIBCXX__) || defined(__GLIBCPP__))
  559. // std::hash in libstdc++ does not work correctly with vector<bool> on Big
  560. // Endian platforms therefore we need to implement a custom AbslHashValue for
  561. // it. More details on the bug:
  562. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102531
  563. template <typename H, typename T, typename Allocator>
  564. typename std::enable_if<is_hashable<T>::value && std::is_same<T, bool>::value,
  565. H>::type
  566. AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
  567. typename H::AbslInternalPiecewiseCombiner combiner;
  568. for (const auto& i : vector) {
  569. unsigned char c = static_cast<unsigned char>(i);
  570. hash_state = combiner.add_buffer(std::move(hash_state), &c, sizeof(c));
  571. }
  572. return H::combine(combiner.finalize(std::move(hash_state)), vector.size());
  573. }
  574. #else
  575. // When not working around the libstdc++ bug above, we still have to contend
  576. // with the fact that std::hash<vector<bool>> is often poor quality, hashing
  577. // directly on the internal words and on no other state. On these platforms,
  578. // vector<bool>{1, 1} and vector<bool>{1, 1, 0} hash to the same value.
  579. //
  580. // Mixing in the size (as we do in our other vector<> implementations) on top
  581. // of the library-provided hash implementation avoids this QOI issue.
  582. template <typename H, typename T, typename Allocator>
  583. typename std::enable_if<is_hashable<T>::value && std::is_same<T, bool>::value,
  584. H>::type
  585. AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
  586. return H::combine(std::move(hash_state),
  587. std::hash<std::vector<T, Allocator>>{}(vector),
  588. vector.size());
  589. }
  590. #endif
  591. // -----------------------------------------------------------------------------
  592. // AbslHashValue for Ordered Associative Containers
  593. // -----------------------------------------------------------------------------
  594. // AbslHashValue for hashing std::map
  595. template <typename H, typename Key, typename T, typename Compare,
  596. typename Allocator>
  597. typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
  598. H>::type
  599. AbslHashValue(H hash_state, const std::map<Key, T, Compare, Allocator>& map) {
  600. for (const auto& t : map) {
  601. hash_state = H::combine(std::move(hash_state), t);
  602. }
  603. return H::combine(std::move(hash_state), map.size());
  604. }
  605. // AbslHashValue for hashing std::multimap
  606. template <typename H, typename Key, typename T, typename Compare,
  607. typename Allocator>
  608. typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
  609. H>::type
  610. AbslHashValue(H hash_state,
  611. const std::multimap<Key, T, Compare, Allocator>& map) {
  612. for (const auto& t : map) {
  613. hash_state = H::combine(std::move(hash_state), t);
  614. }
  615. return H::combine(std::move(hash_state), map.size());
  616. }
  617. // AbslHashValue for hashing std::set
  618. template <typename H, typename Key, typename Compare, typename Allocator>
  619. typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
  620. H hash_state, const std::set<Key, Compare, Allocator>& set) {
  621. for (const auto& t : set) {
  622. hash_state = H::combine(std::move(hash_state), t);
  623. }
  624. return H::combine(std::move(hash_state), set.size());
  625. }
  626. // AbslHashValue for hashing std::multiset
  627. template <typename H, typename Key, typename Compare, typename Allocator>
  628. typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
  629. H hash_state, const std::multiset<Key, Compare, Allocator>& set) {
  630. for (const auto& t : set) {
  631. hash_state = H::combine(std::move(hash_state), t);
  632. }
  633. return H::combine(std::move(hash_state), set.size());
  634. }
  635. // -----------------------------------------------------------------------------
  636. // AbslHashValue for Unordered Associative Containers
  637. // -----------------------------------------------------------------------------
  638. // AbslHashValue for hashing std::unordered_set
  639. template <typename H, typename Key, typename Hash, typename KeyEqual,
  640. typename Alloc>
  641. typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
  642. H hash_state, const std::unordered_set<Key, Hash, KeyEqual, Alloc>& s) {
  643. return H::combine(
  644. H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
  645. s.size());
  646. }
  647. // AbslHashValue for hashing std::unordered_multiset
  648. template <typename H, typename Key, typename Hash, typename KeyEqual,
  649. typename Alloc>
  650. typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
  651. H hash_state,
  652. const std::unordered_multiset<Key, Hash, KeyEqual, Alloc>& s) {
  653. return H::combine(
  654. H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
  655. s.size());
  656. }
  657. // AbslHashValue for hashing std::unordered_set
  658. template <typename H, typename Key, typename T, typename Hash,
  659. typename KeyEqual, typename Alloc>
  660. typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
  661. H>::type
  662. AbslHashValue(H hash_state,
  663. const std::unordered_map<Key, T, Hash, KeyEqual, Alloc>& s) {
  664. return H::combine(
  665. H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
  666. s.size());
  667. }
  668. // AbslHashValue for hashing std::unordered_multiset
  669. template <typename H, typename Key, typename T, typename Hash,
  670. typename KeyEqual, typename Alloc>
  671. typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
  672. H>::type
  673. AbslHashValue(H hash_state,
  674. const std::unordered_multimap<Key, T, Hash, KeyEqual, Alloc>& s) {
  675. return H::combine(
  676. H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
  677. s.size());
  678. }
  679. // -----------------------------------------------------------------------------
  680. // AbslHashValue for Wrapper Types
  681. // -----------------------------------------------------------------------------
  682. // AbslHashValue for hashing std::reference_wrapper
  683. template <typename H, typename T>
  684. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  685. H hash_state, std::reference_wrapper<T> opt) {
  686. return H::combine(std::move(hash_state), opt.get());
  687. }
  688. // AbslHashValue for hashing absl::optional
  689. template <typename H, typename T>
  690. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  691. H hash_state, const absl::optional<T>& opt) {
  692. if (opt) hash_state = H::combine(std::move(hash_state), *opt);
  693. return H::combine(std::move(hash_state), opt.has_value());
  694. }
  695. // VariantVisitor
  696. template <typename H>
  697. struct VariantVisitor {
  698. H&& hash_state;
  699. template <typename T>
  700. H operator()(const T& t) const {
  701. return H::combine(std::move(hash_state), t);
  702. }
  703. };
  704. // AbslHashValue for hashing absl::variant
  705. template <typename H, typename... T>
  706. typename std::enable_if<conjunction<is_hashable<T>...>::value, H>::type
  707. AbslHashValue(H hash_state, const absl::variant<T...>& v) {
  708. if (!v.valueless_by_exception()) {
  709. hash_state = absl::visit(VariantVisitor<H>{std::move(hash_state)}, v);
  710. }
  711. return H::combine(std::move(hash_state), v.index());
  712. }
  713. // -----------------------------------------------------------------------------
  714. // AbslHashValue for Other Types
  715. // -----------------------------------------------------------------------------
  716. // AbslHashValue for hashing std::bitset is not defined on Little Endian
  717. // platforms, for the same reason as for vector<bool> (see std::vector above):
  718. // It does not expose the raw bytes, and a fallback to std::hash<> is most
  719. // likely faster.
  720. #if defined(ABSL_IS_BIG_ENDIAN) && \
  721. (defined(__GLIBCXX__) || defined(__GLIBCPP__))
  722. // AbslHashValue for hashing std::bitset
  723. //
  724. // std::hash in libstdc++ does not work correctly with std::bitset on Big Endian
  725. // platforms therefore we need to implement a custom AbslHashValue for it. More
  726. // details on the bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102531
  727. template <typename H, size_t N>
  728. H AbslHashValue(H hash_state, const std::bitset<N>& set) {
  729. typename H::AbslInternalPiecewiseCombiner combiner;
  730. for (int i = 0; i < N; i++) {
  731. unsigned char c = static_cast<unsigned char>(set[i]);
  732. hash_state = combiner.add_buffer(std::move(hash_state), &c, sizeof(c));
  733. }
  734. return H::combine(combiner.finalize(std::move(hash_state)), N);
  735. }
  736. #endif
  737. // -----------------------------------------------------------------------------
  738. // hash_range_or_bytes()
  739. //
  740. // Mixes all values in the range [data, data+size) into the hash state.
  741. // This overload accepts only uniquely-represented types, and hashes them by
  742. // hashing the entire range of bytes.
  743. template <typename H, typename T>
  744. typename std::enable_if<is_uniquely_represented<T>::value, H>::type
  745. hash_range_or_bytes(H hash_state, const T* data, size_t size) {
  746. const auto* bytes = reinterpret_cast<const unsigned char*>(data);
  747. return H::combine_contiguous(std::move(hash_state), bytes, sizeof(T) * size);
  748. }
  749. // hash_range_or_bytes()
  750. template <typename H, typename T>
  751. typename std::enable_if<!is_uniquely_represented<T>::value, H>::type
  752. hash_range_or_bytes(H hash_state, const T* data, size_t size) {
  753. for (const auto end = data + size; data < end; ++data) {
  754. hash_state = H::combine(std::move(hash_state), *data);
  755. }
  756. return hash_state;
  757. }
  758. #if defined(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE) && \
  759. ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
  760. #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 1
  761. #else
  762. #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 0
  763. #endif
  764. // HashSelect
  765. //
  766. // Type trait to select the appropriate hash implementation to use.
  767. // HashSelect::type<T> will give the proper hash implementation, to be invoked
  768. // as:
  769. // HashSelect::type<T>::Invoke(state, value)
  770. // Also, HashSelect::type<T>::value is a boolean equal to `true` if there is a
  771. // valid `Invoke` function. Types that are not hashable will have a ::value of
  772. // `false`.
  773. struct HashSelect {
  774. private:
  775. struct State : HashStateBase<State> {
  776. static State combine_contiguous(State hash_state, const unsigned char*,
  777. size_t);
  778. using State::HashStateBase::combine_contiguous;
  779. };
  780. struct UniquelyRepresentedProbe {
  781. template <typename H, typename T>
  782. static auto Invoke(H state, const T& value)
  783. -> absl::enable_if_t<is_uniquely_represented<T>::value, H> {
  784. return hash_internal::hash_bytes(std::move(state), value);
  785. }
  786. };
  787. struct HashValueProbe {
  788. template <typename H, typename T>
  789. static auto Invoke(H state, const T& value) -> absl::enable_if_t<
  790. std::is_same<H,
  791. decltype(AbslHashValue(std::move(state), value))>::value,
  792. H> {
  793. return AbslHashValue(std::move(state), value);
  794. }
  795. };
  796. struct LegacyHashProbe {
  797. #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
  798. template <typename H, typename T>
  799. static auto Invoke(H state, const T& value) -> absl::enable_if_t<
  800. std::is_convertible<
  801. decltype(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>()(value)),
  802. size_t>::value,
  803. H> {
  804. return hash_internal::hash_bytes(
  805. std::move(state),
  806. ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>{}(value));
  807. }
  808. #endif // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
  809. };
  810. struct StdHashProbe {
  811. template <typename H, typename T>
  812. static auto Invoke(H state, const T& value)
  813. -> absl::enable_if_t<type_traits_internal::IsHashable<T>::value, H> {
  814. return hash_internal::hash_bytes(std::move(state), std::hash<T>{}(value));
  815. }
  816. };
  817. template <typename Hash, typename T>
  818. struct Probe : Hash {
  819. private:
  820. template <typename H, typename = decltype(H::Invoke(
  821. std::declval<State>(), std::declval<const T&>()))>
  822. static std::true_type Test(int);
  823. template <typename U>
  824. static std::false_type Test(char);
  825. public:
  826. static constexpr bool value = decltype(Test<Hash>(0))::value;
  827. };
  828. public:
  829. // Probe each implementation in order.
  830. // disjunction provides short circuiting wrt instantiation.
  831. template <typename T>
  832. using Apply = absl::disjunction< //
  833. Probe<UniquelyRepresentedProbe, T>, //
  834. Probe<HashValueProbe, T>, //
  835. Probe<LegacyHashProbe, T>, //
  836. Probe<StdHashProbe, T>, //
  837. std::false_type>;
  838. };
  839. template <typename T>
  840. struct is_hashable
  841. : std::integral_constant<bool, HashSelect::template Apply<T>::value> {};
  842. // MixingHashState
  843. class ABSL_DLL MixingHashState : public HashStateBase<MixingHashState> {
  844. // absl::uint128 is not an alias or a thin wrapper around the intrinsic.
  845. // We use the intrinsic when available to improve performance.
  846. #ifdef ABSL_HAVE_INTRINSIC_INT128
  847. using uint128 = __uint128_t;
  848. #else // ABSL_HAVE_INTRINSIC_INT128
  849. using uint128 = absl::uint128;
  850. #endif // ABSL_HAVE_INTRINSIC_INT128
  851. static constexpr uint64_t kMul =
  852. sizeof(size_t) == 4 ? uint64_t{0xcc9e2d51}
  853. : uint64_t{0x9ddfea08eb382d69};
  854. template <typename T>
  855. using IntegralFastPath =
  856. conjunction<std::is_integral<T>, is_uniquely_represented<T>>;
  857. public:
  858. // Move only
  859. MixingHashState(MixingHashState&&) = default;
  860. MixingHashState& operator=(MixingHashState&&) = default;
  861. // MixingHashState::combine_contiguous()
  862. //
  863. // Fundamental base case for hash recursion: mixes the given range of bytes
  864. // into the hash state.
  865. static MixingHashState combine_contiguous(MixingHashState hash_state,
  866. const unsigned char* first,
  867. size_t size) {
  868. return MixingHashState(
  869. CombineContiguousImpl(hash_state.state_, first, size,
  870. std::integral_constant<int, sizeof(size_t)>{}));
  871. }
  872. using MixingHashState::HashStateBase::combine_contiguous;
  873. // MixingHashState::hash()
  874. //
  875. // For performance reasons in non-opt mode, we specialize this for
  876. // integral types.
  877. // Otherwise we would be instantiating and calling dozens of functions for
  878. // something that is just one multiplication and a couple xor's.
  879. // The result should be the same as running the whole algorithm, but faster.
  880. template <typename T, absl::enable_if_t<IntegralFastPath<T>::value, int> = 0>
  881. static size_t hash(T value) {
  882. return static_cast<size_t>(Mix(Seed(), static_cast<uint64_t>(value)));
  883. }
  884. // Overload of MixingHashState::hash()
  885. template <typename T, absl::enable_if_t<!IntegralFastPath<T>::value, int> = 0>
  886. static size_t hash(const T& value) {
  887. return static_cast<size_t>(combine(MixingHashState{}, value).state_);
  888. }
  889. private:
  890. // Invoked only once for a given argument; that plus the fact that this is
  891. // move-only ensures that there is only one non-moved-from object.
  892. MixingHashState() : state_(Seed()) {}
  893. friend class MixingHashState::HashStateBase;
  894. template <typename CombinerT>
  895. static MixingHashState RunCombineUnordered(MixingHashState state,
  896. CombinerT combiner) {
  897. uint64_t unordered_state = 0;
  898. combiner(MixingHashState{}, [&](MixingHashState& inner_state) {
  899. // Add the hash state of the element to the running total, but mix the
  900. // carry bit back into the low bit. This in intended to avoid losing
  901. // entropy to overflow, especially when unordered_multisets contain
  902. // multiple copies of the same value.
  903. auto element_state = inner_state.state_;
  904. unordered_state += element_state;
  905. if (unordered_state < element_state) {
  906. ++unordered_state;
  907. }
  908. inner_state = MixingHashState{};
  909. });
  910. return MixingHashState::combine(std::move(state), unordered_state);
  911. }
  912. // Allow the HashState type-erasure implementation to invoke
  913. // RunCombinedUnordered() directly.
  914. friend class absl::HashState;
  915. // Workaround for MSVC bug.
  916. // We make the type copyable to fix the calling convention, even though we
  917. // never actually copy it. Keep it private to not affect the public API of the
  918. // type.
  919. MixingHashState(const MixingHashState&) = default;
  920. explicit MixingHashState(uint64_t state) : state_(state) {}
  921. // Implementation of the base case for combine_contiguous where we actually
  922. // mix the bytes into the state.
  923. // Dispatch to different implementations of the combine_contiguous depending
  924. // on the value of `sizeof(size_t)`.
  925. static uint64_t CombineContiguousImpl(uint64_t state,
  926. const unsigned char* first, size_t len,
  927. std::integral_constant<int, 4>
  928. /* sizeof_size_t */);
  929. static uint64_t CombineContiguousImpl(uint64_t state,
  930. const unsigned char* first, size_t len,
  931. std::integral_constant<int, 8>
  932. /* sizeof_size_t */);
  933. // Slow dispatch path for calls to CombineContiguousImpl with a size argument
  934. // larger than PiecewiseChunkSize(). Has the same effect as calling
  935. // CombineContiguousImpl() repeatedly with the chunk stride size.
  936. static uint64_t CombineLargeContiguousImpl32(uint64_t state,
  937. const unsigned char* first,
  938. size_t len);
  939. static uint64_t CombineLargeContiguousImpl64(uint64_t state,
  940. const unsigned char* first,
  941. size_t len);
  942. // Reads 9 to 16 bytes from p.
  943. // The least significant 8 bytes are in .first, the rest (zero padded) bytes
  944. // are in .second.
  945. static std::pair<uint64_t, uint64_t> Read9To16(const unsigned char* p,
  946. size_t len) {
  947. uint64_t low_mem = absl::base_internal::UnalignedLoad64(p);
  948. uint64_t high_mem = absl::base_internal::UnalignedLoad64(p + len - 8);
  949. #ifdef ABSL_IS_LITTLE_ENDIAN
  950. uint64_t most_significant = high_mem;
  951. uint64_t least_significant = low_mem;
  952. #else
  953. uint64_t most_significant = low_mem;
  954. uint64_t least_significant = high_mem;
  955. #endif
  956. return {least_significant, most_significant};
  957. }
  958. // Reads 4 to 8 bytes from p. Zero pads to fill uint64_t.
  959. static uint64_t Read4To8(const unsigned char* p, size_t len) {
  960. uint32_t low_mem = absl::base_internal::UnalignedLoad32(p);
  961. uint32_t high_mem = absl::base_internal::UnalignedLoad32(p + len - 4);
  962. #ifdef ABSL_IS_LITTLE_ENDIAN
  963. uint32_t most_significant = high_mem;
  964. uint32_t least_significant = low_mem;
  965. #else
  966. uint32_t most_significant = low_mem;
  967. uint32_t least_significant = high_mem;
  968. #endif
  969. return (static_cast<uint64_t>(most_significant) << (len - 4) * 8) |
  970. least_significant;
  971. }
  972. // Reads 1 to 3 bytes from p. Zero pads to fill uint32_t.
  973. static uint32_t Read1To3(const unsigned char* p, size_t len) {
  974. unsigned char mem0 = p[0];
  975. unsigned char mem1 = p[len / 2];
  976. unsigned char mem2 = p[len - 1];
  977. #ifdef ABSL_IS_LITTLE_ENDIAN
  978. unsigned char significant2 = mem2;
  979. unsigned char significant1 = mem1;
  980. unsigned char significant0 = mem0;
  981. #else
  982. unsigned char significant2 = mem0;
  983. unsigned char significant1 = mem1;
  984. unsigned char significant0 = mem2;
  985. #endif
  986. return static_cast<uint32_t>(significant0 | //
  987. (significant1 << (len / 2 * 8)) | //
  988. (significant2 << ((len - 1) * 8)));
  989. }
  990. ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Mix(uint64_t state, uint64_t v) {
  991. // Though the 128-bit product on AArch64 needs two instructions, it is
  992. // still a good balance between speed and hash quality.
  993. using MultType =
  994. absl::conditional_t<sizeof(size_t) == 4, uint64_t, uint128>;
  995. // We do the addition in 64-bit space to make sure the 128-bit
  996. // multiplication is fast. If we were to do it as MultType the compiler has
  997. // to assume that the high word is non-zero and needs to perform 2
  998. // multiplications instead of one.
  999. MultType m = state + v;
  1000. m *= kMul;
  1001. return static_cast<uint64_t>(m ^ (m >> (sizeof(m) * 8 / 2)));
  1002. }
  1003. // An extern to avoid bloat on a direct call to LowLevelHash() with fixed
  1004. // values for both the seed and salt parameters.
  1005. static uint64_t LowLevelHashImpl(const unsigned char* data, size_t len);
  1006. ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Hash64(const unsigned char* data,
  1007. size_t len) {
  1008. #ifdef ABSL_HAVE_INTRINSIC_INT128
  1009. return LowLevelHashImpl(data, len);
  1010. #else
  1011. return hash_internal::CityHash64(reinterpret_cast<const char*>(data), len);
  1012. #endif
  1013. }
  1014. // Seed()
  1015. //
  1016. // A non-deterministic seed.
  1017. //
  1018. // The current purpose of this seed is to generate non-deterministic results
  1019. // and prevent having users depend on the particular hash values.
  1020. // It is not meant as a security feature right now, but it leaves the door
  1021. // open to upgrade it to a true per-process random seed. A true random seed
  1022. // costs more and we don't need to pay for that right now.
  1023. //
  1024. // On platforms with ASLR, we take advantage of it to make a per-process
  1025. // random value.
  1026. // See https://en.wikipedia.org/wiki/Address_space_layout_randomization
  1027. //
  1028. // On other platforms this is still going to be non-deterministic but most
  1029. // probably per-build and not per-process.
  1030. ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Seed() {
  1031. #if (!defined(__clang__) || __clang_major__ > 11) && \
  1032. !defined(__apple_build_version__)
  1033. return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(&kSeed));
  1034. #else
  1035. // Workaround the absence of
  1036. // https://github.com/llvm/llvm-project/commit/bc15bf66dcca76cc06fe71fca35b74dc4d521021.
  1037. return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(kSeed));
  1038. #endif
  1039. }
  1040. static const void* const kSeed;
  1041. uint64_t state_;
  1042. };
  1043. // MixingHashState::CombineContiguousImpl()
  1044. inline uint64_t MixingHashState::CombineContiguousImpl(
  1045. uint64_t state, const unsigned char* first, size_t len,
  1046. std::integral_constant<int, 4> /* sizeof_size_t */) {
  1047. // For large values we use CityHash, for small ones we just use a
  1048. // multiplicative hash.
  1049. uint64_t v;
  1050. if (len > 8) {
  1051. if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
  1052. return CombineLargeContiguousImpl32(state, first, len);
  1053. }
  1054. v = hash_internal::CityHash32(reinterpret_cast<const char*>(first), len);
  1055. } else if (len >= 4) {
  1056. v = Read4To8(first, len);
  1057. } else if (len > 0) {
  1058. v = Read1To3(first, len);
  1059. } else {
  1060. // Empty ranges have no effect.
  1061. return state;
  1062. }
  1063. return Mix(state, v);
  1064. }
  1065. // Overload of MixingHashState::CombineContiguousImpl()
  1066. inline uint64_t MixingHashState::CombineContiguousImpl(
  1067. uint64_t state, const unsigned char* first, size_t len,
  1068. std::integral_constant<int, 8> /* sizeof_size_t */) {
  1069. // For large values we use LowLevelHash or CityHash depending on the platform,
  1070. // for small ones we just use a multiplicative hash.
  1071. uint64_t v;
  1072. if (len > 16) {
  1073. if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
  1074. return CombineLargeContiguousImpl64(state, first, len);
  1075. }
  1076. v = Hash64(first, len);
  1077. } else if (len > 8) {
  1078. // This hash function was constructed by the ML-driven algorithm discovery
  1079. // using reinforcement learning. We fed the agent lots of inputs from
  1080. // microbenchmarks, SMHasher, low hamming distance from generated inputs and
  1081. // picked up the one that was good on micro and macrobenchmarks.
  1082. auto p = Read9To16(first, len);
  1083. uint64_t lo = p.first;
  1084. uint64_t hi = p.second;
  1085. // Rotation by 53 was found to be most often useful when discovering these
  1086. // hashing algorithms with ML techniques.
  1087. lo = absl::rotr(lo, 53);
  1088. state += kMul;
  1089. lo += state;
  1090. state ^= hi;
  1091. uint128 m = state;
  1092. m *= lo;
  1093. return static_cast<uint64_t>(m ^ (m >> 64));
  1094. } else if (len >= 4) {
  1095. v = Read4To8(first, len);
  1096. } else if (len > 0) {
  1097. v = Read1To3(first, len);
  1098. } else {
  1099. // Empty ranges have no effect.
  1100. return state;
  1101. }
  1102. return Mix(state, v);
  1103. }
  1104. struct AggregateBarrier {};
  1105. // HashImpl
  1106. // Add a private base class to make sure this type is not an aggregate.
  1107. // Aggregates can be aggregate initialized even if the default constructor is
  1108. // deleted.
  1109. struct PoisonedHash : private AggregateBarrier {
  1110. PoisonedHash() = delete;
  1111. PoisonedHash(const PoisonedHash&) = delete;
  1112. PoisonedHash& operator=(const PoisonedHash&) = delete;
  1113. };
  1114. template <typename T>
  1115. struct HashImpl {
  1116. size_t operator()(const T& value) const {
  1117. return MixingHashState::hash(value);
  1118. }
  1119. };
  1120. template <typename T>
  1121. struct Hash
  1122. : absl::conditional_t<is_hashable<T>::value, HashImpl<T>, PoisonedHash> {};
  1123. template <typename H>
  1124. template <typename T, typename... Ts>
  1125. H HashStateBase<H>::combine(H state, const T& value, const Ts&... values) {
  1126. return H::combine(hash_internal::HashSelect::template Apply<T>::Invoke(
  1127. std::move(state), value),
  1128. values...);
  1129. }
  1130. // HashStateBase::combine_contiguous()
  1131. template <typename H>
  1132. template <typename T>
  1133. H HashStateBase<H>::combine_contiguous(H state, const T* data, size_t size) {
  1134. return hash_internal::hash_range_or_bytes(std::move(state), data, size);
  1135. }
  1136. // HashStateBase::combine_unordered()
  1137. template <typename H>
  1138. template <typename I>
  1139. H HashStateBase<H>::combine_unordered(H state, I begin, I end) {
  1140. return H::RunCombineUnordered(std::move(state),
  1141. CombineUnorderedCallback<I>{begin, end});
  1142. }
  1143. // HashStateBase::PiecewiseCombiner::add_buffer()
  1144. template <typename H>
  1145. H PiecewiseCombiner::add_buffer(H state, const unsigned char* data,
  1146. size_t size) {
  1147. if (position_ + size < PiecewiseChunkSize()) {
  1148. // This partial chunk does not fill our existing buffer
  1149. memcpy(buf_ + position_, data, size);
  1150. position_ += size;
  1151. return state;
  1152. }
  1153. // If the buffer is partially filled we need to complete the buffer
  1154. // and hash it.
  1155. if (position_ != 0) {
  1156. const size_t bytes_needed = PiecewiseChunkSize() - position_;
  1157. memcpy(buf_ + position_, data, bytes_needed);
  1158. state = H::combine_contiguous(std::move(state), buf_, PiecewiseChunkSize());
  1159. data += bytes_needed;
  1160. size -= bytes_needed;
  1161. }
  1162. // Hash whatever chunks we can without copying
  1163. while (size >= PiecewiseChunkSize()) {
  1164. state = H::combine_contiguous(std::move(state), data, PiecewiseChunkSize());
  1165. data += PiecewiseChunkSize();
  1166. size -= PiecewiseChunkSize();
  1167. }
  1168. // Fill the buffer with the remainder
  1169. memcpy(buf_, data, size);
  1170. position_ = size;
  1171. return state;
  1172. }
  1173. // HashStateBase::PiecewiseCombiner::finalize()
  1174. template <typename H>
  1175. H PiecewiseCombiner::finalize(H state) {
  1176. // Hash the remainder left in the buffer, which may be empty
  1177. return H::combine_contiguous(std::move(state), buf_, position_);
  1178. }
  1179. } // namespace hash_internal
  1180. ABSL_NAMESPACE_END
  1181. } // namespace absl
  1182. #endif // ABSL_HASH_INTERNAL_HASH_H_