[LLVMdev] APInt::sdivrem error?

Preston Briggs preston.briggs at gmail.com
Sun May 20 18:21:48 PDT 2012


I wrote the following bit of code

static APInt FloorOfQuotient(APInt a, APInt b) {
 unsigned bits = a.getBitWidth();
 APInt q(bits, 1), r(bits, 1);
 APInt::sdivrem(a, b, q, r);
*  errs() << "sdivrem(" << a << ", " << b << ") = (" << q << ", " << r <<
")\n";
*  if (r == 0)
   return q;
 else {
   if ((a.sgt(0) && b.sgt(0)) ||
       (a.slt(0) && b.slt(0)))
     return q;
   else
     return q - 1;
 }
}


but sometimes see surprising results.  Here are examples:

sdivrem(9, -2) = (-4, 1)
sdivrem(9, -3) = (-3, 0)
sdivrem(10, -3) = (-3, 1)
sdivrem(38, 2) = (19, 0)
sdivrem(29, 1) = (29, 0)
*sdivrem(-8, -1) = (-8, 0)*
*sdivrem(-9, -1) = (-9, 0)*


What's up with the last two?

Am I doing something wrong (again)?

Thanks,
Preston
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120520/426080c7/attachment.html>


More information about the llvm-dev mailing list