hash.h 57 KB

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