<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2985: std::reverse should be permitted to be vectorized</title>
<meta property="og:title" content="Issue 2985: std::reverse should be permitted to be vectorized">
<meta property="og:description" content="C++ library issue. Status: LEWG">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2985.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#LEWG">LEWG</a> status.</em></p>
<h3 id="2985"><a href="lwg-active.html#2985">2985</a>. <code>std::reverse</code> should be permitted to be vectorized</h3>
<p><b>Section:</b> 26.7.10 <a href="https://wg21.link/alg.reverse">[alg.reverse]</a> <b>Status:</b> <a href="lwg-active.html#LEWG">LEWG</a>
 <b>Submitter:</b> Billy O'Neal III <b>Opened:</b> 2017-06-24 <b>Last modified:</b> 2018-04-03</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#alg.reverse">issues</a> in [alg.reverse].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#LEWG">LEWG</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The fine folks on our backend team suggested that we special case <code>std::reverse</code> of 1/2/4/8 to take 
advantage of vector units. Unfortunately, at present <code>std::reverse</code> says it does <code>N/2 iter_swap</code>s, 
which doesn't permit our vector implementation even if the iterator inputs are pointers to trivially copyable <code>T</code>s.
<p/>
The vectorized version for pointers to <code>short</code>s is 
<a href="https://twitter.com/MalwareMinigun/status/878150939512717312">~8x faster on Skylake</a> than the serial version, 
and about 7x faster for <code>unsigned long long</code>s; and users don't actually care whether or not we call <code>swap</code> here.
</p>

<p><i>[2017-07 Toronto Monday issue prioritization]</i></p>

<p>Status to LEWG; this is similar to <a href="lwg-active.html#2973" title="inplace_merge exact comparison count complexity prohibits useful real-world optimizations (Status: LEWG)">2973</a><sup><a href="https://cplusplus.github.io/LWG/issue2973" title="Latest snapshot">(i)</a></sup></p>

<p><i>[2018-04-02, Billy comments]</i></p>

<p>
This issue should be resolved by <a href="https://wg21.link/p0551">P0551</a>, because it prohibits user specialization of  
<code>std::swap</code> and <code>std::iter_swap</code>, which means the proposed vectorization optimization for 
pointers-to-trivially-copyable is now implementable without changes to <code>reverse</code>'s specification (We can detect 
if the user has provided an alternate <code>swap</code> in their own namespace, but not if they explicitly specialized 
<code>swap</code> or <code>iter_swap</code>).
</p>


<p id="res-2985"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4659">N4659</a>.
</p>

<ol>
<li><p>Edit 26.7.10 <a href="https://wg21.link/alg.reverse">[alg.reverse]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class BidirectionalIterator&gt;
  void reverse(BidirectionalIterator first, BidirectionalIterator last);
template&lt;class ExecutionPolicy, class BidirectionalIterator>
  void reverse(ExecutionPolicy&amp;&amp; exec,
               BidirectionalIterator first, BidirectionalIterator last);
</pre>
<blockquote>
<p>
-1- <i>Requires:</i> <code>*first</code> shall be swappable (16.4.4.3 <a href="https://wg21.link/swappable.requirements">[swappable.requirements]</a>).
<p/>
-2- <i>Effects:</i> For each non-negative integer <code>i &lt; (last - first) / 2</code>, applies <code>iter_swap</code> 
to all pairs of iterators <code>first + i, (last - i) - 1</code>. <ins>If 
<code>is_trivially_copyable_v&lt;typename iterator_traits&lt;BidirectionalIterator&gt;::value_type&gt;</code> is 
<code>true</code>, an implementation may permute the elements by making temporary copies, rather than by calling 
<code>iter_swap</code>. [<i>Note:</i> this allows the implementation to be vectorized. &mdash; <i>end note</i>]</ins>
</p>
</blockquote>
</blockquote>
</li>
</ol>




</body>
</html>
