Matt Johnson
2011-Mar-10 19:31 UTC
[LLVMdev] compiler-rt: Infinite loop/stack overflow in __modsi3()
Hi All,
The default implementation of __modsi3() (signed integer modulus)
in compiler-rt/lib/modsi3.c is defined recursively. Thankfully, LLVM is
smart enough to do tail call elimination on the recursion, so I got an
infinite loop rather than a stack overflow :)
Here's the patch, patterned after the correct implementation in
umodsi3.c:
diff --git a/lib/compiler-rt/lib/modsi3.c b/lib/compiler-rt/lib/modsi3.c
index 388418a..3759ce0 100644
--- a/lib/compiler-rt/lib/modsi3.c
+++ b/lib/compiler-rt/lib/modsi3.c
@@ -16,8 +16,10 @@
/* Returns: a % b */
+su_int __divsi3(si_int a, si_int b);
+
si_int
__modsi3(si_int a, si_int b)
{
- return a - (a / b) * b;
+ return a - __divsi3(a, b) * b;
}
Best,
Matt
Chris Lattner
2011-Mar-10 22:16 UTC
[LLVMdev] compiler-rt: Infinite loop/stack overflow in __modsi3()
On Mar 10, 2011, at 11:31 AM, Matt Johnson wrote:> Hi All, > The default implementation of __modsi3() (signed integer modulus) > in compiler-rt/lib/modsi3.c is defined recursively. Thankfully, LLVM is > smart enough to do tail call elimination on the recursion, so I got an > infinite loop rather than a stack overflow :)Looks good, applied in r127429, thanks! -Chris> > Here's the patch, patterned after the correct implementation in > umodsi3.c: > > diff --git a/lib/compiler-rt/lib/modsi3.c b/lib/compiler-rt/lib/modsi3.c > index 388418a..3759ce0 100644 > --- a/lib/compiler-rt/lib/modsi3.c > +++ b/lib/compiler-rt/lib/modsi3.c > @@ -16,8 +16,10 @@ > > /* Returns: a % b */ > > +su_int __divsi3(si_int a, si_int b); > + > si_int > __modsi3(si_int a, si_int b) > { > - return a - (a / b) * b; > + return a - __divsi3(a, b) * b; > } > > > Best, > Matt > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev