I wrote the following bit of code<br><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><font face="courier new, monospace">static APInt FloorOfQuotient(APInt a, APInt b) {<br> unsigned bits = a.getBitWidth();<br>
APInt q(bits, 1), r(bits, 1);<br> APInt::sdivrem(a, b, q, r);<br><b> errs() << "sdivrem(" << a << ", " << b << ") = (" << q << ", " << r << ")\n";<br>
</b> if (r == 0)<br> return q;<br> else {<br> if ((a.sgt(0) && b.sgt(0)) ||<br> (a.slt(0) && b.slt(0)))<br> return q;<br> else<br> return q - 1;<br> }<br>}</font></blockquote><br>
but sometimes see surprising results. Here are examples:<br><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><font face="courier new, monospace">sdivrem(9, -2) = (-4, 1)</font></div></div><div>
<div><font face="courier new, monospace">sdivrem(9, -3) = (-3, 0)</font></div></div><div><div><font face="courier new, monospace">sdivrem(10, -3) = (-3, 1)</font></div></div><div><div><font face="courier new, monospace">sdivrem(38, 2) = (19, 0)</font></div>
</div><div><div><font face="courier new, monospace">sdivrem(29, 1) = (29, 0)</font></div></div><div><div><b><font face="courier new, monospace">sdivrem(-8, -1) = (-8, 0)</font></b></div></div><div><div><b><font face="courier new, monospace">sdivrem(-9, -1) = (-9, 0)</font></b></div>
</div></blockquote><div><br></div><div>What's up with the last two?</div><div><br></div><div>Am I doing something wrong (again)?</div><div><br></div><div>Thanks,</div><div>Preston</div><div><br></div>