OK, the code for sdivrem in APInt.h is wrong.<br>Here's what's written:<br><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><font face="courier new, monospace"> static void sdivrem(const APInt &LHS, const APInt &RHS,<br>
APInt &Quotient, APInt &Remainder) {<br> if (LHS.isNegative()) {<br> if (RHS.isNegative())<br> APInt::udivrem(-LHS, -RHS, Quotient, Remainder);<br> else<br> APInt::udivrem(-LHS, RHS, Quotient, Remainder);<br>
Quotient = -Quotient;<br> Remainder = -Remainder;<br> } else if (RHS.isNegative()) {<br> APInt::udivrem(LHS, -RHS, Quotient, Remainder);<br> Quotient = -Quotient;<br> } else {<br> APInt::udivrem(LHS, RHS, Quotient, Remainder);<br>
}<br> }</font></blockquote><br>Here's what it should be:<br><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><font face="courier new, monospace"> static void sdivrem(const APInt &LHS, const APInt &RHS,<br>
</font><font face="courier new, monospace"> APInt &Quotient, APInt &Remainder) {<br></font><font face="courier new, monospace"> if (LHS.isNegative()) {<br></font><font face="courier new, monospace"> if (RHS.isNegative())<br>
</font><font face="courier new, monospace"> APInt::udivrem(-LHS, -RHS, Quotient, Remainder);<br></font><font face="courier new, monospace"> else <b>{<br></b></font><b><font face="courier new, monospace"> APInt::udivrem(-LHS, RHS, Quotient, Remainder);<br>
</font><font face="courier new, monospace"> Quotient = -Quotient;<br></font></b><font face="courier new, monospace"><b> Remainder = -Remainder;<br> }</b><br></font><font face="courier new, monospace"> } else if (RHS.isNegative()) {<br>
</font><font face="courier new, monospace"> APInt::udivrem(LHS, -RHS, Quotient, Remainder);<br></font><font face="courier new, monospace"> Quotient = -Quotient;<br></font><font face="courier new, monospace"> } else {<br>
</font><font face="courier new, monospace"> APInt::udivrem(LHS, RHS, Quotient, Remainder);<br></font><font face="courier new, monospace"> }<br></font><font face="courier new, monospace"> }</font></blockquote><br>
What should I do to submit the correction?<br><br>Thanks,<br>Preston<br><br>On Sun, May 20, 2012 at 6:21 PM, Preston Briggs <<a href="mailto:preston.briggs@gmail.com">preston.briggs@gmail.com</a>> wrote:<br>> I wrote the following bit of code<br>
><br>> 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>> errs() << "sdivrem(" << a << ", " << b << ") = (" << q << ", " << r <<<br>
> ")\n";<br>> 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>> }<br>><br>><br>> but sometimes see surprising results. Here are examples:<br>><br>> sdivrem(9, -2) = (-4, 1)<br>> sdivrem(9, -3) = (-3, 0)<br>> sdivrem(10, -3) = (-3, 1)<br>> sdivrem(38, 2) = (19, 0)<br>
> sdivrem(29, 1) = (29, 0)<br>> sdivrem(-8, -1) = (-8, 0)<br>> sdivrem(-9, -1) = (-9, 0)<br>><br>><br>> What's up with the last two?<br>><br>> Am I doing something wrong (again)?<br>><br>> Thanks,<br>
> Preston<br>><br>