common implementation of iterative div/mod