- if (!L || !M || !N) {
- SCEV *CNC = new SCEVCouldNotCompute();
- return std::make_pair(CNC, CNC);
- }
-
- Constant *C = L->getValue();
- Constant *Two = ConstantInt::get(C->getType(), 2);
-
- // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
- // The B coefficient is M-N/2
- Constant *B = ConstantExpr::getSub(M->getValue(),
- ConstantExpr::getSDiv(N->getValue(),
- Two));
- // The A coefficient is N/2
- Constant *A = ConstantExpr::getSDiv(N->getValue(), Two);
-
- // Compute the B^2-4ac term.
- Constant *SqrtTerm =
- ConstantExpr::getMul(ConstantInt::get(C->getType(), 4),
- ConstantExpr::getMul(A, C));
- SqrtTerm = ConstantExpr::getSub(ConstantExpr::getMul(B, B), SqrtTerm);
-
- // Compute floor(sqrt(B^2-4ac))
- ConstantInt *SqrtVal =
- cast<ConstantInt>(ConstantExpr::getBitCast(SqrtTerm,
- SqrtTerm->getType()->getUnsignedVersion()));
- uint64_t SqrtValV = SqrtVal->getZExtValue();
- uint64_t SqrtValV2 = (uint64_t)sqrt((double)SqrtValV);
- // The square root might not be precise for arbitrary 64-bit integer
- // values. Do some sanity checks to ensure it's correct.
- if (SqrtValV2*SqrtValV2 > SqrtValV ||
- (SqrtValV2+1)*(SqrtValV2+1) <= SqrtValV) {