#include "math.h" /* * Integer square root. We use Newton-Raphson iteration, with a * starting point determined to be within about a factor of * times-or-divided-by 2 based on a loop that counts powers of 4 and * pwoers of 2 in parallel until the former exceeeds the argument. * * This does not test for negative arguments and does not work right * for 0. For the game's uses, these don't matter. */ int isqrt(int v) { int i; int j; i = 1; j = 1; while (j < v) { i <<= 1; j <<= 2; } while ((i < j) || (i > j+1)) { j = i; i = (i + (v/i)) / 2; } return(i); } /* * Just divide-and-round-up, done with integer arithmetic. */ unsigned int how_many(unsigned int n, unsigned int divr) { return((n+divr-1)/divr); }