[libcxx-commits] [PATCH] D125329: Replace modulus operations in std::seed_seq::generate with conditional checks.

Laramie Leavitt via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue May 10 11:38:16 PDT 2022


laramiel created this revision.
Herald added a project: All.
laramiel updated this revision to Diff 428441.
laramiel added a comment.
laramiel added reviewers: ldionne, STL_MSFT.
laramiel published this revision for review.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Updating D125329 <https://reviews.llvm.org/D125329>: Replace modulus operations in std::seed_seq::generate with conditional checks.


Abseil benchmarks suggest that the conditional checks result in faster code (4-5x)
as they can be compiled into conditional move instructions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125329

Files:
  libcxx/include/__random/seed_seq.h


Index: libcxx/include/__random/seed_seq.h
===================================================================
--- libcxx/include/__random/seed_seq.h
+++ libcxx/include/__random/seed_seq.h
@@ -109,39 +109,61 @@
             __first[__q] += __r;
             __first[0] = __r;
         }
+        size_t __kmodn = 0;          // __k % __n
+        size_t __kpmodn = __p % __n; // (__k + __p) % __n
+        size_t __kqmodn = __q % __n; // (__k + __q) % __n
+        size_t __km1modn = _n - 1;   // (__k-1) % __n
+
         for (size_t __k = 1; __k <= __s; ++__k)
         {
-            const size_t __kmodn = __k % __n;
-            const size_t __kpmodn = (__k + __p) % __n;
-            result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
-                                           ^ __first[(__k - 1) % __n]);
-            __first[__kpmodn] += __r;
-            __r +=  __kmodn + __v_[__k-1];
-            __first[(__k + __q) % __n] += __r;
-            __first[__kmodn] = __r;
+          if (++__kmodn == __n)
+            __kmodn = 0;
+          if (++__km1modn == __n)
+            __km1modn = 0;
+          if (++__kpmodn == __n)
+            __kpmodn = 0;
+          if (++__kqmodn == __n)
+            __kqmodn = 0;
+
+          result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] ^ __first[__km1modn]);
+          __first[__kpmodn] += __r;
+          __r += __kmodn + __v_[__k - 1];
+          __first[__kqmodn] += __r;
+          __first[__kmodn] = __r;
         }
         for (size_t __k = __s + 1; __k < __m; ++__k)
         {
-            const size_t __kmodn = __k % __n;
-            const size_t __kpmodn = (__k + __p) % __n;
-            result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
-                                           ^ __first[(__k - 1) % __n]);
-            __first[__kpmodn] += __r;
-            __r +=  __kmodn;
-            __first[(__k + __q) % __n] += __r;
-            __first[__kmodn] = __r;
+          if (++__kmodn == __n)
+            __kmodn = 0;
+          if (++__km1modn == __n)
+            __km1modn = 0;
+          if (++__kpmodn == __n)
+            __kpmodn = 0;
+          if (++__kqmodn == __n)
+            __kqmodn = 0;
+
+          result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] ^ __first[__km1modn]);
+          __first[__kpmodn] += __r;
+          __r += __kmodn;
+          __first[__kqmodn] += __r;
+          __first[__kmodn] = __r;
         }
         for (size_t __k = __m; __k < __m + __n; ++__k)
         {
-            const size_t __kmodn = __k % __n;
-            const size_t __kpmodn = (__k + __p) % __n;
-            result_type __r = 1566083941 * _Tp(__first[__kmodn] +
-                                              __first[__kpmodn] +
-                                              __first[(__k - 1) % __n]);
-            __first[__kpmodn] ^= __r;
-            __r -= __kmodn;
-            __first[(__k + __q) % __n] ^= __r;
-            __first[__kmodn] = __r;
+          if (++__kmodn == __n)
+            __kmodn = 0;
+          if (++__km1modn == __n)
+            __km1modn = 0;
+          if (++__kpmodn == __n)
+            __kpmodn = 0;
+          if (++__kqmodn == __n)
+            __kqmodn = 0;
+
+          result_type __r = 1566083941 * _Tp(__first[__kmodn] + __first[__kpmodn] + __first[__km1modn]);
+          __first[__kpmodn] ^= __r;
+          __r -= __kmodn;
+          __first[__kqmodn] ^= __r;
+          __first[__kmodn] = __r;
         }
     }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125329.428441.patch
Type: text/x-patch
Size: 3523 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220510/69897bda/attachment.bin>


More information about the libcxx-commits mailing list