about summary refs log tree commit diff
path: root/absl/random
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2019-07-18T22·07-0700
committerDerek Mauro <dmauro@google.com>2019-07-19T17·04-0400
commitf3840bc5e33ce4932e35986cf3718450c6f02af2 (patch)
tree43dd14bfbd542b155040546c4fab6e592c504988 /absl/random
parent278b26058c036833a4f7f3047d3f4d9296527f87 (diff)
Export of internal Abseil changes.
--
dcff7cc4eb3837d39d1e083cbf1d4f082054cbf6 by Laramie Leavitt <lar@google.com>:

Add default and fallback modes for SaltedSeedSeq::generate().

SeedSeq::generate will often be called with contiguous ranges of uint32_t.
Detect this case and use the currently direct code paths to handle it.
Otherwise use a fallback mechanism which does an additional copy.
PiperOrigin-RevId: 258853656

--
59ec88845ac2ee6567c493021b54385940d66762 by Gennadiy Rozental <rogeeff@google.com>:

Internal change

PiperOrigin-RevId: 258822137

--
eb2d1ab21f6f94c24c51c1cd3719846be259e8e7 by Abseil Team <absl-team@google.com>:

Add iOS version 11 to the mix of Apple OS versions that have various C++17 header files
but do not support them.

PiperOrigin-RevId: 258820405

--
452cea3e13b29797b2c385d7c6da7613d36bdc45 by Gennadiy Rozental <rogeeff@google.com>:

Internal change

PiperOrigin-RevId: 258802436
GitOrigin-RevId: dcff7cc4eb3837d39d1e083cbf1d4f082054cbf6
Change-Id: I2261cb58e142eb15017ef646a56710dd64f06496
Diffstat (limited to 'absl/random')
-rw-r--r--absl/random/internal/salted_seed_seq.h63
1 files changed, 38 insertions, 25 deletions
diff --git a/absl/random/internal/salted_seed_seq.h b/absl/random/internal/salted_seed_seq.h
index 3d16cf97d3d7..e160f79e1f5b 100644
--- a/absl/random/internal/salted_seed_seq.h
+++ b/absl/random/internal/salted_seed_seq.h
@@ -64,10 +64,15 @@ class SaltedSeedSeq {
 
   template <typename RandomAccessIterator>
   void generate(RandomAccessIterator begin, RandomAccessIterator end) {
+    // The common case is that generate is called with ContiguousIterators
+    // to uint arrays. Such contiguous memory regions may be optimized,
+    // which we detect here.
+    using tag = absl::conditional_t<
+        (std::is_pointer<RandomAccessIterator>::value &&
+         std::is_same<absl::decay_t<decltype(*begin)>, uint32_t>::value),
+        ContiguousAndUint32Tag, DefaultTag>;
     if (begin != end) {
-      generate_impl(
-          std::integral_constant<bool, sizeof(*begin) == sizeof(uint32_t)>{},
-          begin, end);
+      generate_impl(begin, end, tag{});
     }
   }
 
@@ -79,19 +84,12 @@ class SaltedSeedSeq {
   size_t size() const { return seq_->size(); }
 
  private:
-  // The common case for generate is that it is called with iterators over a
-  // 32-bit value buffer. These can be reinterpreted to a uint32_t and we can
-  // operate on them as such.
-  template <typename RandomAccessIterator>
-  void generate_impl(std::integral_constant<bool, true> /*is_32bit*/,
-                     RandomAccessIterator begin, RandomAccessIterator end) {
-    seq_->generate(begin, end);
-    const uint32_t salt = absl::random_internal::GetSaltMaterial().value_or(0);
-    auto buffer = absl::MakeSpan(begin, end);
-    MixIntoSeedMaterial(
-        absl::MakeConstSpan(&salt, 1),
-        absl::MakeSpan(reinterpret_cast<uint32_t*>(buffer.data()),
-                       buffer.size()));
+  struct ContiguousAndUint32Tag {};
+  struct DefaultTag {};
+
+  // Generate which requires the iterators are contiguous pointers to uint32_t.
+  void generate_impl(uint32_t* begin, uint32_t* end, ContiguousAndUint32Tag) {
+    generate_contiguous(absl::MakeSpan(begin, end));
   }
 
   // The uncommon case for generate is that it is called with iterators over
@@ -99,17 +97,32 @@ class SaltedSeedSeq {
   // case we allocate a temporary 32-bit buffer and then copy-assign back
   // to the initial inputs.
   template <typename RandomAccessIterator>
-  void generate_impl(std::integral_constant<bool, false> /*is_32bit*/,
-                     RandomAccessIterator begin, RandomAccessIterator end) {
-    // Allocate a temporary buffer, seed, and then copy.
-    absl::InlinedVector<uint32_t, 8> data(std::distance(begin, end), 0);
-    generate_impl(std::integral_constant<bool, true>{}, data.begin(),
-                  data.end());
-    std::copy(data.begin(), data.end(), begin);
+  void generate_impl(RandomAccessIterator begin, RandomAccessIterator end,
+                     DefaultTag) {
+    return generate_and_copy(std::distance(begin, end), begin);
+  }
+
+  // Fills the initial seed buffer the underlying SSeq::generate() call,
+  // mixing in the salt material.
+  void generate_contiguous(absl::Span<uint32_t> buffer) {
+    seq_->generate(buffer.begin(), buffer.end());
+    const uint32_t salt = absl::random_internal::GetSaltMaterial().value_or(0);
+    MixIntoSeedMaterial(absl::MakeConstSpan(&salt, 1), buffer);
+  }
+
+  // Allocates a seed buffer of `n` elements, generates the seed, then
+  // copies the result into the `out` iterator.
+  template <typename Iterator>
+  void generate_and_copy(size_t n, Iterator out) {
+    // Allocate a temporary buffer, generate, and then copy.
+    absl::InlinedVector<uint32_t, 8> data(n, 0);
+    generate_contiguous(absl::MakeSpan(data.data(), data.size()));
+    std::copy(data.begin(), data.end(), out);
   }
 
-  // Because [rand.req.seedseq] is not copy-constructible, copy-assignable nor
-  // movable so we wrap it with unique pointer to be able to move SaltedSeedSeq.
+  // Because [rand.req.seedseq] is not required to be copy-constructible,
+  // copy-assignable nor movable, we wrap it with unique pointer to be able
+  // to move SaltedSeedSeq.
   std::unique_ptr<SSeq> seq_;
 };