<div dir="ltr">> Maybe I would add explanation why the term  (__a & __b) + ((__a ^ __b) >> 1) calculates the floored average.<br><div><br></div><div>FWIW, it's mentioned in the second edition of Hacker's Delight.</div><div><br></div><div>= - = - =</div><div><br></div><div>I'm a bit wary of a change that produces more instructions but happens to execute more quickly on Intel CPUs... though of course most CPUs running this code are likely to be x86.</div><div><br></div><div>Note that if the integer in question isn't as wide as int64, then a much simpler algorithm can be used:</div><div><br></div><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254);font-family:"Droid Sans Mono",monospace,monospace,"Droid Sans Fallback";font-size:14px;line-height:19px;white-space:pre"><div>    <span style="color:rgb(0,0,255)">return</span> __a + (__b - int64_t{__a}) / <span style="color:rgb(9,136,90)">2</span>;</div><div></div></div></div><div><br></div><div>This is indeed faster, as seen here => <a href="http://quick-bench.com/BP71mRtaL1f3lCAR6n8mdhAN7MM">http://quick-bench.com/BP71mRtaL1f3lCAR6n8mdhAN7MM</a><br></div><div><br></div><div>-- Jorg</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Nov 24, 2019 at 4:21 AM Andrej Korman via Phabricator <<a href="mailto:reviews@reviews.llvm.org">reviews@reviews.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Aj0SK added a comment.<br>
<br>
Maybe I would add explanation why the term<br>
<br>
  (__a & __b) + ((__a ^ __b) >> 1)<br>
<br>
calculates the floored average. Adding a and b can be rewritten as a + b = (a ^ b) + ((a & b) << 1) . After this we have to divide a+b by 2. This can be done also using shifting to right by 1. After this we get (a + b)/2 = ((a ^ b) >>1) + (a & b).<br>
<br>
<br>
Repository:<br>
  rG LLVM Github Monorepo<br>
<br>
CHANGES SINCE LAST ACTION<br>
  <a href="https://reviews.llvm.org/D70640/new/" rel="noreferrer" target="_blank">https://reviews.llvm.org/D70640/new/</a><br>
<br>
<a href="https://reviews.llvm.org/D70640" rel="noreferrer" target="_blank">https://reviews.llvm.org/D70640</a><br>
<br>
<br>
<br>
</blockquote></div>