[optimization] Replace unnecessary while loops with single equations

Replacing some do loops with single equations. I left in iCnt since du is calculated with iCnt after aUx is calculated, but this probably could be further reduced. I say keep it in this form to keep it as close to the original code as possible though.